blob: 240bbdcbce458549f919e82ebe0df272195a1203 [file] [log] [blame]
Matt Spinler852db672019-03-06 13:46:14 -06001#pragma once
2
3#include "config.h"
4
Matt Spinlerc47ca582019-03-06 14:37:42 -06005#include <xyz/openbmc_project/Association/Definitions/server.hpp>
Matt Spinler852db672019-03-06 13:46:14 -06006
7namespace phosphor
8{
9namespace inventory
10{
11namespace manager
12{
13namespace associations
14{
15
Matt Spinler99e66a02019-03-06 14:11:22 -060016static constexpr auto forwardTypePos = 0;
17static constexpr auto reverseTypePos = 1;
18using Types = std::tuple<std::string, std::string>;
19using Paths = std::vector<std::string>;
20
21static constexpr auto typesPos = 0;
22static constexpr auto pathsPos = 1;
23using EndpointsEntry = std::vector<std::tuple<Types, Paths>>;
24
25using AssociationMap = std::map<std::string, EndpointsEntry>;
26
Matt Spinlerc47ca582019-03-06 14:37:42 -060027using AssociationObject = sdbusplus::server::object::object<
28 sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
29
30using AssociationIfaceMap =
31 std::map<std::string, std::unique_ptr<AssociationObject>>;
32
Matt Spinler852db672019-03-06 13:46:14 -060033/**
34 * @class Manager
35 *
36 * @brief This class provides the ability to add org.openbmc.Associations
37 * interfaces on inventory D-Bus objects, based on a definition in a
38 * JSON file.
39 *
40 * The purpose for this is to be able to associate other D-Bus paths
41 * with the inventory items they relate to.
42 *
43 * For example, a card temperature sensor D-Bus object can be associated
44 * with the D-Bus object for that card's inventory entry so that some
45 * code can tie them together.
46 */
47class Manager
48{
49 public:
50 Manager() = delete;
51 ~Manager() = default;
52 Manager(const Manager&) = delete;
53 Manager& operator=(const Manager&) = delete;
54 Manager(Manager&&) = delete;
55 Manager& operator=(Manager&&) = delete;
56
57 /**
58 * @brief Constructor
59 *
60 * @param[in] bus - sdbusplus object
61 * @param[in] jsonPath - path to the JSON File that contains associations
62 */
63 Manager(sdbusplus::bus::bus& bus, const std::string& jsonPath);
64
65 /**
66 * @brief Constructor
67 *
68 * @param[in] bus - sdbusplus object
69 */
70 explicit Manager(sdbusplus::bus::bus& bus) :
71 Manager(bus, ASSOCIATIONS_FILE_PATH)
72 {
73 }
74
75 /**
76 * @brief Creates any association D-Bus interfaces required based on
77 * the JSON associations definition for the object path passed
78 * in.
79 *
80 * Called after PIM creates a new inventory D-Bus interface on objectPath.
81 *
82 * @param[in] objectPath - the D-Bus object path to check for associations
83 */
84 void createAssociations(const std::string& objectPath);
85
Matt Spinler99e66a02019-03-06 14:11:22 -060086 /**
87 * @brief Returned the association configuration.
88 * Used for testing.
89 *
90 * @return AssociationMap& - the association config
91 */
92 const AssociationMap& getAssociationsConfig()
93 {
94 return _associations;
95 }
96
Matt Spinler852db672019-03-06 13:46:14 -060097 private:
98 /**
Matt Spinler99e66a02019-03-06 14:11:22 -060099 * @brief Loads the association YAML into the _associations data
100 * structure. This file is optional, so if it doesn't exist
101 * it will just not load anything.
102 */
103 void load();
104
105 /**
Matt Spinlerc47ca582019-03-06 14:37:42 -0600106 * @brief Creates an instance of an org.openbmc.Associations
107 * interface using the passed in properties.
108 *
109 * @param[in] forwardPath - the path of the forward association
110 * @param[in] forwardType - the type of the forward association
111 * @param[in] reversePath - the path of the reverse association
112 * @param[in] reverseType - the type of the reverse association
113 */
114 void createAssociation(const std::string& forwardPath,
115 const std::string& forwardType,
116 const std::string& reversePath,
117 const std::string& reverseType);
118
119 /**
Matt Spinler99e66a02019-03-06 14:11:22 -0600120 * @brief The map of association data that is loaded from its
121 * JSON definition. Association D-Bus objects will be
122 * created from this data.
123 */
124 AssociationMap _associations;
125
126 /**
Matt Spinlerc47ca582019-03-06 14:37:42 -0600127 * @brief The map of org.openbmc_project.Associations D-Bus
128 * interfaces objects based on their object path.
129 */
130 AssociationIfaceMap _associationIfaces;
131
132 /**
Matt Spinler852db672019-03-06 13:46:14 -0600133 * @brief The sdbusplus bus object.
134 */
135 sdbusplus::bus::bus& _bus;
136
137 /**
138 * @brief The path to the associations JSON File.
139 */
140 const std::string _jsonFile;
Matt Spinlerc47ca582019-03-06 14:37:42 -0600141
142 /**
143 * A list of the inventory association paths that have already been handled.
144 */
145 std::vector<std::string> _handled;
Matt Spinler852db672019-03-06 13:46:14 -0600146};
147
148} // namespace associations
149} // namespace manager
150} // namespace inventory
151} // namespace phosphor