1 #pragma once
2 
3 #include "types.hpp"
4 
5 constexpr const char* xyzAssociationInterface =
6     "xyz.openbmc_project.Association";
7 
8 constexpr size_t endpointsCountTimerThreshold = 100;
9 constexpr int endpointUpdateDelaySeconds = 1;
10 
11 /** @brief Remove input association
12  *
13  * @param[in] io                  - io context
14  * @param[in] sourcePath          - Path of the object that contains the
15  *                                  org.openbmc.Associations
16  * @param[in] owner               - The Dbus service having its associations
17  *                                  removed
18  * @param[in,out] server          - sdbus system object
19  * @param[in,out] assocMaps       - The association maps
20  *
21  * @return Void, server, assocMaps updated if needed
22  */
23 void removeAssociation(boost::asio::io_context& io,
24                        const std::string& sourcePath, const std::string& owner,
25                        sdbusplus::asio::object_server& server,
26                        AssociationMaps& assocMaps);
27 
28 /** @brief Remove input paths from endpoints of an association
29  *
30  * If the last endpoint was removed, then remove the whole
31  * association object, otherwise just set the property
32  *
33  * @param[in] io                  - io context
34  * @param[in] objectServer        - sdbus system object
35  * @param[in] assocPath           - Path of the object that contains the
36  *                                  org.openbmc.Associations
37  * @param[in] endpointsToRemove   - Endpoints to remove
38  * @param[in,out] assocMaps       - The association maps
39  *
40  * @return Void, objectServer and assocMaps updated if needed
41  */
42 void removeAssociationEndpoints(
43     boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
44     const std::string& assocPath,
45     const boost::container::flat_set<std::string>& endpointsToRemove,
46     AssociationMaps& assocMaps);
47 
48 /** @brief Check and remove any changed associations
49  *
50  * Based on the latest values of the org.openbmc.Associations.associations
51  * property, passed in via the newAssociations param, check if any of the
52  * paths in the xyz.openbmc_project.Association.endpoints D-Bus property
53  * for that association need to be removed.  If the last path is removed
54  * from the endpoints property, remove that whole association object from
55  * D-Bus.
56  *
57  * @param[in] io                  - io context
58  * @param[in] sourcePath         - Path of the object that contains the
59  *                                 org.openbmc.Associations
60  * @param[in] owner              - The Dbus service having it's associatons
61  *                                 changed
62  * @param[in] newAssociations    - New associations to look at for change
63  * @param[in,out] objectServer   - sdbus system object
64  * @param[in,out] assocMaps      - The association maps
65  *
66  * @return Void, objectServer and assocMaps updated if needed
67  */
68 void checkAssociationEndpointRemoves(
69     boost::asio::io_context& io, const std::string& sourcePath,
70     const std::string& owner, const AssociationPaths& newAssociations,
71     sdbusplus::asio::object_server& objectServer, AssociationMaps& assocMaps);
72 
73 /** @brief Handle new or changed association interfaces
74  *
75  * Called when either a new org.openbmc.Associations interface was
76  * created, or the associations property on that interface changed
77  *
78  * @param[in] io                  - io context
79  * @param[in,out] objectServer    - sdbus system object
80  * @param[in] associations        - New associations to look at for change
81  * @param[in] path                - Path of the object that contains the
82  *                                  org.openbmc.Associations
83  * @param[in] owner               - The Dbus service having it's associatons
84  *                                  changed
85  * @param[in] interfaceMap        - The full interface map
86  * @param[in,out] assocMaps       - The association maps
87  *
88  * @return Void, objectServer and assocMaps updated if needed
89  */
90 void associationChanged(
91     boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
92     const std::vector<Association>& associations, const std::string& path,
93     const std::string& owner, const InterfaceMapType& interfaceMap,
94     AssociationMaps& assocMaps);
95 
96 /** @brief Add a pending associations entry
97  *
98  *  Used when a client wants to create an association between
99  *  2 D-Bus endpoint paths, but one of the paths doesn't exist.
100  *  When the path does show up in D-Bus, if there is a pending
101  *  association then the real association objects can be created.
102  *
103  * @param[in] objectPath    - The D-Bus object path that should be an
104  *                            association endpoint but doesn't exist
105  *                            on D-Bus.
106  * @param[in] type          - The association type.  Gets used in the final
107  *                            association path of <objectPath>/<type>.
108  * @param[in] endpointPath  - The D-Bus path on the other side
109  *                            of the association. This path exists.
110  * @param[in] endpointType  - The endpoint association type. Gets used
111  *                            in the final association path of
112  *                            <endpointPath>/<endpointType>.
113  * @param[in] owner         - The service name that owns the association.
114  * @param[in,out] assocMaps - The association maps
115  */
116 void addPendingAssociation(
117     const std::string& objectPath, const std::string& type,
118     const std::string& endpointPath, const std::string& endpointType,
119     const std::string& owner, AssociationMaps& assocMaps);
120 
121 /** @brief Removes an endpoint from the pending associations map
122  *
123  * If the last endpoint is removed, removes the whole entry
124  *
125  * @param[in] endpointPath  - the endpoint path to remove
126  * @param[in,out] assocMaps - The association maps
127  */
128 void removeFromPendingAssociations(const std::string& endpointPath,
129                                    AssociationMaps& assocMaps);
130 
131 /** @brief Adds a single association D-Bus object (<path>/<type>)
132  *
133  * @param[in] io            - io context
134  * @param[in,out] server    - sdbus system object
135  * @param[in] assocPath     - The association D-Bus path
136  * @param[in] endpoint      - The association's D-Bus endpoint path
137  * @param[in] owner         - The owning D-Bus well known name
138  * @param[in] ownerPath     - The D-Bus path hosting the Associations property
139  * @param[in,out] assocMaps - The association maps
140  */
141 void addSingleAssociation(
142     boost::asio::io_context& io, sdbusplus::asio::object_server& server,
143     const std::string& assocPath, const std::string& endpoint,
144     const std::string& owner, const std::string& ownerPath,
145     AssociationMaps& assocMaps);
146 
147 /** @brief Create a real association out of a pending association
148  *         if there is one for this path.
149  *
150  * If objectPath is now on D-Bus, and it is also in the pending associations
151  * map, create the 2 real association objects and remove its pending
152  * associations entry.  Used when a new path shows up in D-Bus.
153  *
154  * @param[in] io            - io context
155  * @param[in] objectPath    - the path to check
156  * @param[in] interfaceMap  - The master interface map
157  * @param[in,out] assocMaps - The association maps
158  * @param[in,out] server    - sdbus system object
159  */
160 void checkIfPendingAssociation(
161     boost::asio::io_context& io, const std::string& objectPath,
162     const InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
163     sdbusplus::asio::object_server& server);
164 
165 /** @brief Find all associations in the association owners map with the
166  *         specified endpoint path.
167  *
168  * @param[in] endpointPath     - the endpoint path to look for
169  * @param[in] assocMaps        - The association maps
170  * @param[out] associationData - A vector of {owner, Association} tuples
171  *                               of all the associations with that endpoint.
172  */
173 void findAssociations(const std::string& endpointPath,
174                       AssociationMaps& assocMaps,
175                       FindAssocResults& associationData);
176 
177 /** @brief If endpointPath is in an association, move that association
178  *         to pending and remove the association objects.
179  *
180  *  Called when a path is going off of D-Bus.  If this path is an
181  *  association endpoint (the path that owns the association is still
182  *  on D-Bus), then move the association it's involved in to pending.
183  *
184  * @param[in] io            - io context
185  * @param[in] endpointPath  - the D-Bus endpoint path to check
186  * @param[in,out] assocMaps - The association maps
187  * @param[in,out] server    - sdbus system object
188  */
189 void moveAssociationToPending(
190     boost::asio::io_context& io, const std::string& endpointPath,
191     AssociationMaps& assocMaps, sdbusplus::asio::object_server& server);
192