blob: d47b3adbb378a4afc695500c6bfe974557f0bf03 [file] [log] [blame]
Matt Spinlere0017eb2018-03-27 11:17:38 -05001/**
2 * Copyright © 2018 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "config.h"
Matt Spinler433bead2018-04-23 11:08:03 -050017#include "delete.hpp"
Matt Spinlere0017eb2018-03-27 11:17:38 -050018#include "manager.hpp"
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050019#include "policy_find.hpp"
Matt Spinlere0017eb2018-03-27 11:17:38 -050020
21namespace ibm
22{
23namespace logging
24{
25
26Manager::Manager(sdbusplus::bus::bus& bus) :
Matt Spinler259e7272018-03-29 10:57:17 -050027 bus(bus),
28 addMatch(bus,
29 sdbusplus::bus::match::rules::interfacesAdded() +
30 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
31 std::bind(std::mem_fn(&Manager::interfaceAdded), this,
Matt Spinler055da3b2018-05-09 15:51:20 -050032 std::placeholders::_1)),
33 removeMatch(bus,
34 sdbusplus::bus::match::rules::interfacesRemoved() +
35 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
36 std::bind(std::mem_fn(&Manager::interfaceRemoved), this,
37 std::placeholders::_1))
Matt Spinler743e5822018-03-27 13:44:50 -050038#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -050039 ,
40 policies(POLICY_JSON_PATH)
Matt Spinler743e5822018-03-27 13:44:50 -050041#endif
Matt Spinlere0017eb2018-03-27 11:17:38 -050042{
Matt Spinler54bfa7e2018-03-27 11:28:59 -050043 createAll();
44}
45
46void Manager::createAll()
47{
Matt Spinler259e7272018-03-29 10:57:17 -050048 auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH);
Matt Spinler54bfa7e2018-03-27 11:28:59 -050049
50 for (const auto& object : objects)
51 {
52 const auto& interfaces = object.second;
53
54 auto propertyMap = std::find_if(
Matt Spinler259e7272018-03-29 10:57:17 -050055 interfaces.begin(), interfaces.end(),
56 [](const auto& i) { return i.first == LOGGING_IFACE; });
Matt Spinler54bfa7e2018-03-27 11:28:59 -050057
58 if (propertyMap != interfaces.end())
59 {
60 create(object.first, propertyMap->second);
61 }
62 }
63}
64
Matt Spinler259e7272018-03-29 10:57:17 -050065void Manager::create(const std::string& objectPath,
66 const DbusPropertyMap& properties)
Matt Spinler54bfa7e2018-03-27 11:28:59 -050067{
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050068
69#ifdef USE_POLICY_INTERFACE
70 createPolicyInterface(objectPath, properties);
71#endif
Matt Spinler433bead2018-04-23 11:08:03 -050072
73 // Emits the interfaces added signal.
74 createDeleteInterface(objectPath);
75}
76
77void Manager::erase(const std::string& objectPath)
78{
79 auto entry = entries.find(getEntryID(objectPath));
80
81 if (entry != entries.end())
82 {
83 entries.erase(entry);
84 }
Matt Spinlere0017eb2018-03-27 11:17:38 -050085}
86
Matt Spinler491fc6f2018-04-23 11:00:52 -050087void Manager::addInterface(const std::string& objectPath, InterfaceType type,
88 std::experimental::any& object)
89{
90 auto id = getEntryID(objectPath);
91 auto entry = entries.find(id);
92
93 if (entry == entries.end())
94 {
95 InterfaceMap interfaces;
96 interfaces.emplace(type, object);
97 entries.emplace(id, std::move(interfaces));
98 }
99 else
100 {
101 entry->second.emplace(type, object);
102 }
103}
104
Matt Spinler433bead2018-04-23 11:08:03 -0500105void Manager::createDeleteInterface(const std::string& objectPath)
106{
107 std::experimental::any object =
108 std::make_shared<Delete>(bus, objectPath, *this, false);
109
110 addInterface(objectPath, InterfaceType::DELETE, object);
111}
112
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500113#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -0500114void Manager::createPolicyInterface(const std::string& objectPath,
115 const DbusPropertyMap& properties)
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500116{
117 auto values = policy::find(policies, properties);
118
Matt Spinler259e7272018-03-29 10:57:17 -0500119 auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500120
121 object->eventID(std::get<policy::EIDField>(values));
122 object->description(std::get<policy::MsgField>(values));
123
Matt Spinler491fc6f2018-04-23 11:00:52 -0500124 std::experimental::any anyObject = object;
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500125
Matt Spinler491fc6f2018-04-23 11:00:52 -0500126 addInterface(objectPath, InterfaceType::POLICY, anyObject);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500127}
128#endif
129
Matt Spinlere0017eb2018-03-27 11:17:38 -0500130void Manager::interfaceAdded(sdbusplus::message::message& msg)
131{
132 sdbusplus::message::object_path path;
133 DbusInterfaceMap interfaces;
134
135 msg.read(path, interfaces);
136
Matt Spinler259e7272018-03-29 10:57:17 -0500137 // Find the Logging.Entry interface with all of its properties
138 // to pass to create().
139 auto propertyMap =
140 std::find_if(interfaces.begin(), interfaces.end(),
141 [](const auto& i) { return i.first == LOGGING_IFACE; });
Matt Spinlere0017eb2018-03-27 11:17:38 -0500142
143 if (propertyMap != interfaces.end())
144 {
Matt Spinler54bfa7e2018-03-27 11:28:59 -0500145 create(path, propertyMap->second);
Matt Spinlere0017eb2018-03-27 11:17:38 -0500146 }
147}
Matt Spinler055da3b2018-05-09 15:51:20 -0500148
149void Manager::interfaceRemoved(sdbusplus::message::message& msg)
150{
151 sdbusplus::message::object_path path;
152 DbusInterfaceList interfaces;
153
154 msg.read(path, interfaces);
155
156 // If the Logging.Entry interface was removed, then remove
157 // our object
158
159 auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE);
160
161 if (i != interfaces.end())
162 {
163 auto id = getEntryID(path);
164
165 auto entry = entries.find(id);
166 if (entry != entries.end())
167 {
168 entries.erase(entry);
169 }
170 }
171}
Matt Spinlere0017eb2018-03-27 11:17:38 -0500172}
173}