1852db67bSMatt Spinler #pragma once
2852db67bSMatt Spinler 
3852db67bSMatt Spinler #include "config.h"
4852db67bSMatt Spinler 
5*c47ca585SMatt Spinler #include <xyz/openbmc_project/Association/Definitions/server.hpp>
6852db67bSMatt Spinler 
7852db67bSMatt Spinler namespace phosphor
8852db67bSMatt Spinler {
9852db67bSMatt Spinler namespace inventory
10852db67bSMatt Spinler {
11852db67bSMatt Spinler namespace manager
12852db67bSMatt Spinler {
13852db67bSMatt Spinler namespace associations
14852db67bSMatt Spinler {
15852db67bSMatt Spinler 
1699e66a03SMatt Spinler static constexpr auto forwardTypePos = 0;
1799e66a03SMatt Spinler static constexpr auto reverseTypePos = 1;
1899e66a03SMatt Spinler using Types = std::tuple<std::string, std::string>;
1999e66a03SMatt Spinler using Paths = std::vector<std::string>;
2099e66a03SMatt Spinler 
2199e66a03SMatt Spinler static constexpr auto typesPos = 0;
2299e66a03SMatt Spinler static constexpr auto pathsPos = 1;
2399e66a03SMatt Spinler using EndpointsEntry = std::vector<std::tuple<Types, Paths>>;
2499e66a03SMatt Spinler 
2599e66a03SMatt Spinler using AssociationMap = std::map<std::string, EndpointsEntry>;
2699e66a03SMatt Spinler 
27*c47ca585SMatt Spinler using AssociationObject = sdbusplus::server::object::object<
28*c47ca585SMatt Spinler     sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
29*c47ca585SMatt Spinler 
30*c47ca585SMatt Spinler using AssociationIfaceMap =
31*c47ca585SMatt Spinler     std::map<std::string, std::unique_ptr<AssociationObject>>;
32*c47ca585SMatt Spinler 
33852db67bSMatt Spinler /**
34852db67bSMatt Spinler  * @class Manager
35852db67bSMatt Spinler  *
36852db67bSMatt Spinler  * @brief This class provides the ability to add org.openbmc.Associations
37852db67bSMatt Spinler  *        interfaces on inventory D-Bus objects, based on a definition in a
38852db67bSMatt Spinler  *        JSON file.
39852db67bSMatt Spinler  *
40852db67bSMatt Spinler  *        The purpose for this is to be able to associate other D-Bus paths
41852db67bSMatt Spinler  *        with the inventory items they relate to.
42852db67bSMatt Spinler  *
43852db67bSMatt Spinler  *        For example, a card temperature sensor D-Bus object can be associated
44852db67bSMatt Spinler  *        with the D-Bus object for that card's inventory entry so that some
45852db67bSMatt Spinler  *        code can tie them together.
46852db67bSMatt Spinler  */
47852db67bSMatt Spinler class Manager
48852db67bSMatt Spinler {
49852db67bSMatt Spinler   public:
50852db67bSMatt Spinler     Manager() = delete;
51852db67bSMatt Spinler     ~Manager() = default;
52852db67bSMatt Spinler     Manager(const Manager&) = delete;
53852db67bSMatt Spinler     Manager& operator=(const Manager&) = delete;
54852db67bSMatt Spinler     Manager(Manager&&) = delete;
55852db67bSMatt Spinler     Manager& operator=(Manager&&) = delete;
56852db67bSMatt Spinler 
57852db67bSMatt Spinler     /**
58852db67bSMatt Spinler      * @brief Constructor
59852db67bSMatt Spinler      *
60852db67bSMatt Spinler      * @param[in] bus - sdbusplus object
61852db67bSMatt Spinler      * @param[in] jsonPath - path to the JSON File that contains associations
62852db67bSMatt Spinler      */
63852db67bSMatt Spinler     Manager(sdbusplus::bus::bus& bus, const std::string& jsonPath);
64852db67bSMatt Spinler 
65852db67bSMatt Spinler     /**
66852db67bSMatt Spinler      * @brief Constructor
67852db67bSMatt Spinler      *
68852db67bSMatt Spinler      * @param[in] bus - sdbusplus object
69852db67bSMatt Spinler      */
70852db67bSMatt Spinler     explicit Manager(sdbusplus::bus::bus& bus) :
71852db67bSMatt Spinler         Manager(bus, ASSOCIATIONS_FILE_PATH)
72852db67bSMatt Spinler     {
73852db67bSMatt Spinler     }
74852db67bSMatt Spinler 
75852db67bSMatt Spinler     /**
76852db67bSMatt Spinler      * @brief Creates any association D-Bus interfaces required based on
77852db67bSMatt Spinler      *        the JSON associations definition for the object path passed
78852db67bSMatt Spinler      *        in.
79852db67bSMatt Spinler      *
80852db67bSMatt Spinler      * Called after PIM creates a new inventory D-Bus interface on objectPath.
81852db67bSMatt Spinler      *
82852db67bSMatt Spinler      * @param[in] objectPath - the D-Bus object path to check for associations
83852db67bSMatt Spinler      */
84852db67bSMatt Spinler     void createAssociations(const std::string& objectPath);
85852db67bSMatt Spinler 
8699e66a03SMatt Spinler     /**
8799e66a03SMatt Spinler      * @brief Returned the association configuration.
8899e66a03SMatt Spinler      *        Used for testing.
8999e66a03SMatt Spinler      *
9099e66a03SMatt Spinler      * @return AssociationMap& - the association config
9199e66a03SMatt Spinler      */
9299e66a03SMatt Spinler     const AssociationMap& getAssociationsConfig()
9399e66a03SMatt Spinler     {
9499e66a03SMatt Spinler         return _associations;
9599e66a03SMatt Spinler     }
9699e66a03SMatt Spinler 
97852db67bSMatt Spinler   private:
98852db67bSMatt Spinler     /**
9999e66a03SMatt Spinler      *  @brief Loads the association YAML into the _associations data
10099e66a03SMatt Spinler      *         structure.  This file is optional, so if it doesn't exist
10199e66a03SMatt Spinler      *         it will just not load anything.
10299e66a03SMatt Spinler      */
10399e66a03SMatt Spinler     void load();
10499e66a03SMatt Spinler 
10599e66a03SMatt Spinler     /**
106*c47ca585SMatt Spinler      * @brief Creates an instance of an org.openbmc.Associations
107*c47ca585SMatt Spinler      *        interface using the passed in properties.
108*c47ca585SMatt Spinler      *
109*c47ca585SMatt Spinler      * @param[in] forwardPath - the path of the forward association
110*c47ca585SMatt Spinler      * @param[in] forwardType - the type of the forward association
111*c47ca585SMatt Spinler      * @param[in] reversePath - the path of the reverse association
112*c47ca585SMatt Spinler      * @param[in] reverseType - the type of the reverse association
113*c47ca585SMatt Spinler      */
114*c47ca585SMatt Spinler     void createAssociation(const std::string& forwardPath,
115*c47ca585SMatt Spinler                            const std::string& forwardType,
116*c47ca585SMatt Spinler                            const std::string& reversePath,
117*c47ca585SMatt Spinler                            const std::string& reverseType);
118*c47ca585SMatt Spinler 
119*c47ca585SMatt Spinler     /**
12099e66a03SMatt Spinler      * @brief The map of association data that is loaded from its
12199e66a03SMatt Spinler      *        JSON definition.  Association D-Bus objects will be
12299e66a03SMatt Spinler      *        created from this data.
12399e66a03SMatt Spinler      */
12499e66a03SMatt Spinler     AssociationMap _associations;
12599e66a03SMatt Spinler 
12699e66a03SMatt Spinler     /**
127*c47ca585SMatt Spinler      * @brief The map of org.openbmc_project.Associations D-Bus
128*c47ca585SMatt Spinler      *        interfaces objects based on their object path.
129*c47ca585SMatt Spinler      */
130*c47ca585SMatt Spinler     AssociationIfaceMap _associationIfaces;
131*c47ca585SMatt Spinler 
132*c47ca585SMatt Spinler     /**
133852db67bSMatt Spinler      * @brief The sdbusplus bus object.
134852db67bSMatt Spinler      */
135852db67bSMatt Spinler     sdbusplus::bus::bus& _bus;
136852db67bSMatt Spinler 
137852db67bSMatt Spinler     /**
138852db67bSMatt Spinler      * @brief The path to the associations JSON File.
139852db67bSMatt Spinler      */
140852db67bSMatt Spinler     const std::string _jsonFile;
141*c47ca585SMatt Spinler 
142*c47ca585SMatt Spinler     /**
143*c47ca585SMatt Spinler      * A list of the inventory association paths that have already been handled.
144*c47ca585SMatt Spinler      */
145*c47ca585SMatt Spinler     std::vector<std::string> _handled;
146852db67bSMatt Spinler };
147852db67bSMatt Spinler 
148852db67bSMatt Spinler } // namespace associations
149852db67bSMatt Spinler } // namespace manager
150852db67bSMatt Spinler } // namespace inventory
151852db67bSMatt Spinler } // namespace phosphor
152