blob: e38fff1333457c1100a62ff0db74cc9da19ef36e [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)
Brad Bishopa83db302020-12-06 14:51:23 -050072 {}
Matt Spinler852db672019-03-06 13:46:14 -060073
74 /**
75 * @brief Creates any association D-Bus interfaces required based on
76 * the JSON associations definition for the object path passed
77 * in.
78 *
79 * Called after PIM creates a new inventory D-Bus interface on objectPath.
80 *
81 * @param[in] objectPath - the D-Bus object path to check for associations
82 */
83 void createAssociations(const std::string& objectPath);
84
Matt Spinler99e66a02019-03-06 14:11:22 -060085 /**
86 * @brief Returned the association configuration.
87 * Used for testing.
88 *
89 * @return AssociationMap& - the association config
90 */
91 const AssociationMap& getAssociationsConfig()
92 {
93 return _associations;
94 }
95
Matt Spinler852db672019-03-06 13:46:14 -060096 private:
97 /**
Matt Spinler99e66a02019-03-06 14:11:22 -060098 * @brief Loads the association YAML into the _associations data
99 * structure. This file is optional, so if it doesn't exist
100 * it will just not load anything.
101 */
102 void load();
103
104 /**
Matt Spinlerc47ca582019-03-06 14:37:42 -0600105 * @brief Creates an instance of an org.openbmc.Associations
106 * interface using the passed in properties.
107 *
108 * @param[in] forwardPath - the path of the forward association
109 * @param[in] forwardType - the type of the forward association
110 * @param[in] reversePath - the path of the reverse association
111 * @param[in] reverseType - the type of the reverse association
112 */
113 void createAssociation(const std::string& forwardPath,
114 const std::string& forwardType,
115 const std::string& reversePath,
116 const std::string& reverseType);
117
118 /**
Matt Spinler99e66a02019-03-06 14:11:22 -0600119 * @brief The map of association data that is loaded from its
120 * JSON definition. Association D-Bus objects will be
121 * created from this data.
122 */
123 AssociationMap _associations;
124
125 /**
Matt Spinlerc47ca582019-03-06 14:37:42 -0600126 * @brief The map of org.openbmc_project.Associations D-Bus
127 * interfaces objects based on their object path.
128 */
129 AssociationIfaceMap _associationIfaces;
130
131 /**
Matt Spinler852db672019-03-06 13:46:14 -0600132 * @brief The sdbusplus bus object.
133 */
134 sdbusplus::bus::bus& _bus;
135
136 /**
137 * @brief The path to the associations JSON File.
138 */
139 const std::string _jsonFile;
Matt Spinlerc47ca582019-03-06 14:37:42 -0600140
141 /**
142 * A list of the inventory association paths that have already been handled.
143 */
144 std::vector<std::string> _handled;
Matt Spinler852db672019-03-06 13:46:14 -0600145};
146
147} // namespace associations
148} // namespace manager
149} // namespace inventory
150} // namespace phosphor