blob: 45982fc73eca497a136728f8fe72dae8dcdbd713 [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,
32 std::placeholders::_1)),
33 removeMatch(bus,
Matt Spinlere0017eb2018-03-27 11:17:38 -050034 sdbusplus::bus::match::rules::interfacesRemoved() +
Matt Spinler259e7272018-03-29 10:57:17 -050035 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 Spinler2cfceb42018-04-23 11:13:04 -050087void Manager::eraseAll()
88{
89 entries.clear();
90}
91
Matt Spinler491fc6f2018-04-23 11:00:52 -050092void Manager::addInterface(const std::string& objectPath, InterfaceType type,
93 std::experimental::any& object)
94{
95 auto id = getEntryID(objectPath);
96 auto entry = entries.find(id);
97
98 if (entry == entries.end())
99 {
100 InterfaceMap interfaces;
101 interfaces.emplace(type, object);
102 entries.emplace(id, std::move(interfaces));
103 }
104 else
105 {
106 entry->second.emplace(type, object);
107 }
108}
109
Matt Spinler433bead2018-04-23 11:08:03 -0500110void Manager::createDeleteInterface(const std::string& objectPath)
111{
112 std::experimental::any object =
113 std::make_shared<Delete>(bus, objectPath, *this, false);
114
115 addInterface(objectPath, InterfaceType::DELETE, object);
116}
117
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500118#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -0500119void Manager::createPolicyInterface(const std::string& objectPath,
120 const DbusPropertyMap& properties)
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500121{
122 auto values = policy::find(policies, properties);
123
Matt Spinler259e7272018-03-29 10:57:17 -0500124 auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500125
126 object->eventID(std::get<policy::EIDField>(values));
127 object->description(std::get<policy::MsgField>(values));
128
Matt Spinler491fc6f2018-04-23 11:00:52 -0500129 std::experimental::any anyObject = object;
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500130
Matt Spinler491fc6f2018-04-23 11:00:52 -0500131 addInterface(objectPath, InterfaceType::POLICY, anyObject);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500132}
133#endif
134
Matt Spinlere0017eb2018-03-27 11:17:38 -0500135void Manager::interfaceAdded(sdbusplus::message::message& msg)
136{
137 sdbusplus::message::object_path path;
138 DbusInterfaceMap interfaces;
139
140 msg.read(path, interfaces);
141
Matt Spinler259e7272018-03-29 10:57:17 -0500142 // Find the Logging.Entry interface with all of its properties
143 // to pass to create().
144 auto propertyMap =
145 std::find_if(interfaces.begin(), interfaces.end(),
146 [](const auto& i) { return i.first == LOGGING_IFACE; });
Matt Spinlere0017eb2018-03-27 11:17:38 -0500147
148 if (propertyMap != interfaces.end())
149 {
Matt Spinler54bfa7e2018-03-27 11:28:59 -0500150 create(path, propertyMap->second);
Matt Spinlere0017eb2018-03-27 11:17:38 -0500151 }
152}
153
154void Manager::interfaceRemoved(sdbusplus::message::message& msg)
155{
156 sdbusplus::message::object_path path;
157 DbusInterfaceList interfaces;
158
159 msg.read(path, interfaces);
160
Matt Spinler259e7272018-03-29 10:57:17 -0500161 // If the Logging.Entry interface was removed, then remove
162 // our object
Matt Spinlere0017eb2018-03-27 11:17:38 -0500163
Matt Spinler259e7272018-03-29 10:57:17 -0500164 auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE);
Matt Spinlere0017eb2018-03-27 11:17:38 -0500165
166 if (i != interfaces.end())
167 {
168 auto id = getEntryID(path);
169
170 auto entry = entries.find(id);
171 if (entry != entries.end())
172 {
173 entries.erase(entry);
174 }
175 }
176}
Matt Spinlere0017eb2018-03-27 11:17:38 -0500177}
178}