forked from ibm-openbmc/phosphor-inventory-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassociation_manager.cpp
317 lines (280 loc) · 9.29 KB
/
association_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "association_manager.hpp"
#include <phosphor-logging/lg2.hpp>
#include <filesystem>
#include <fstream>
namespace phosphor
{
namespace inventory
{
namespace manager
{
namespace associations
{
namespace fs = std::filesystem;
Manager::Manager(sdbusplus::bus_t& bus, const std::string& jsonPath) :
_bus(bus), _jsonFile(jsonPath)
{
// If there aren't any conditional associations files, look for
// that default nonconditional one.
if (!loadConditions())
{
if (fs::exists(_jsonFile))
{
std::ifstream file{_jsonFile};
auto json = nlohmann::json::parse(file, nullptr, true);
load(json);
}
}
}
/**
* @brief Throws an exception if 'num' is zero. Used for JSON
* sanity checking.
*
* @param[in] num - the number to check
*/
void throwIfZero(int num)
{
if (!num)
{
throw std::invalid_argument("Invalid empty field in JSON");
}
}
bool Manager::loadConditions()
{
auto dir = _jsonFile.parent_path();
for (const auto& dirent : fs::recursive_directory_iterator(dir))
{
const auto& path = dirent.path();
if (path.extension() == ".json")
{
std::ifstream file{path};
auto json = nlohmann::json::parse(file, nullptr, true);
if (json.is_object() && json.contains("condition"))
{
const auto& conditionJSON = json.at("condition");
if (!conditionJSON.contains("path") ||
!conditionJSON.contains("interface") ||
!conditionJSON.contains("property") ||
!conditionJSON.contains("values"))
{
lg2::error(
"Invalid JSON in associations condition entry in {PATH}. Skipping file.",
"PATH", path);
continue;
}
Condition c;
c.file = path;
c.path = conditionJSON["path"].get<std::string>();
if (c.path.front() != '/')
{
c.path = '/' + c.path;
}
fprintf(stderr, "found conditions file %s\n", c.file.c_str());
c.interface = conditionJSON["interface"].get<std::string>();
c.property = conditionJSON["property"].get<std::string>();
// The values are in an array, and need to be
// converted to an InterfaceVariantType.
for (const auto& value : conditionJSON["values"])
{
if (value.is_array())
{
std::vector<uint8_t> variantValue;
for (const auto& v : value)
{
variantValue.push_back(v.get<uint8_t>());
}
c.values.push_back(variantValue);
continue;
}
// Try the remaining types
auto s = value.get_ptr<const std::string*>();
auto i = value.get_ptr<const int64_t*>();
auto b = value.get_ptr<const bool*>();
if (s)
{
c.values.push_back(*s);
}
else if (i)
{
c.values.push_back(*i);
}
else if (b)
{
c.values.push_back(*b);
}
else
{
lg2::error(
"Invalid condition property value in {FILE}:",
"FILE", c.file);
throw std::runtime_error(
"Invalid condition property value");
}
}
_conditions.push_back(std::move(c));
}
}
}
return !_conditions.empty();
}
bool Manager::conditionMatch(const sdbusplus::message::object_path& objectPath,
const Object& object)
{
fs::path foundPath;
for (const auto& condition : _conditions)
{
if (condition.path != objectPath)
{
continue;
}
auto interface = std::find_if(object.begin(), object.end(),
[&condition](const auto& i) {
return i.first == condition.interface;
});
if (interface == object.end())
{
continue;
}
auto property = std::find_if(interface->second.begin(),
interface->second.end(),
[&condition](const auto& p) {
return condition.property == p.first;
});
if (property == interface->second.end())
{
continue;
}
auto match = std::find(condition.values.begin(), condition.values.end(),
property->second);
if (match != condition.values.end())
{
foundPath = condition.file;
break;
}
}
if (!foundPath.empty())
{
std::ifstream file{foundPath};
auto json = nlohmann::json::parse(file, nullptr, true);
load(json["associations"]);
_conditions.clear();
return true;
}
return false;
}
bool Manager::conditionMatch()
{
fs::path foundPath;
for (const auto& condition : _conditions)
{
// Compare the actualValue field against the values in the
// values vector to see if there is a condition match.
auto found = std::find(condition.values.begin(), condition.values.end(),
condition.actualValue);
if (found != condition.values.end())
{
foundPath = condition.file;
break;
}
}
if (!foundPath.empty())
{
std::ifstream file{foundPath};
auto json = nlohmann::json::parse(file, nullptr, true);
load(json["associations"]);
_conditions.clear();
return true;
}
return false;
}
void Manager::load(const nlohmann::json& json)
{
const std::string root{INVENTORY_ROOT};
for (const auto& jsonAssoc : json)
{
// Only add the slash if necessary
std::string path = jsonAssoc.at("path");
throwIfZero(path.size());
if (path.front() != '/')
{
path = root + "/" + path;
}
else
{
path = root + path;
}
auto& assocEndpoints = _associations[path];
for (const auto& endpoint : jsonAssoc.at("endpoints"))
{
std::string ftype = endpoint.at("types").at("fType");
std::string rtype = endpoint.at("types").at("rType");
throwIfZero(ftype.size());
throwIfZero(rtype.size());
Types types{std::move(ftype), std::move(rtype)};
Paths paths = endpoint.at("paths");
throwIfZero(paths.size());
assocEndpoints.emplace_back(std::move(types), std::move(paths));
}
}
}
void Manager::createAssociations(const std::string& objectPath,
bool deferSignal)
{
auto endpoints = _associations.find(objectPath);
if (endpoints == _associations.end())
{
return;
}
if (std::find(_handled.begin(), _handled.end(), objectPath) !=
_handled.end())
{
return;
}
_handled.push_back(objectPath);
for (const auto& endpoint : endpoints->second)
{
const auto& types = std::get<typesPos>(endpoint);
const auto& paths = std::get<pathsPos>(endpoint);
for (const auto& endpointPath : paths)
{
const auto& forwardType = std::get<forwardTypePos>(types);
const auto& reverseType = std::get<reverseTypePos>(types);
createAssociation(objectPath, forwardType, endpointPath,
reverseType, deferSignal);
}
}
}
void Manager::createAssociation(const std::string& forwardPath,
const std::string& forwardType,
const std::string& reversePath,
const std::string& reverseType,
bool deferSignal)
{
auto object = _associationIfaces.find(forwardPath);
if (object == _associationIfaces.end())
{
auto a = std::make_unique<AssociationObject>(
_bus, forwardPath.c_str(), AssociationObject::action::defer_emit);
using AssociationProperty =
std::vector<std::tuple<std::string, std::string, std::string>>;
AssociationProperty prop;
prop.emplace_back(forwardType, reverseType, reversePath);
a->associations(std::move(prop));
if (!deferSignal)
{
a->emit_object_added();
}
_associationIfaces.emplace(forwardPath, std::move(a));
}
else
{
// Interface exists, just update the property
auto prop = object->second->associations();
prop.emplace_back(forwardType, reverseType, reversePath);
object->second->associations(std::move(prop), deferSignal);
}
}
} // namespace associations
} // namespace manager
} // namespace inventory
} // namespace phosphor