blob: 60d26ef39c7da9cba25d670b9153454be6a4f6dd [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 Spinler19ff79f2018-04-23 11:26:48 -050032 std::placeholders::_1))
Matt Spinler743e5822018-03-27 13:44:50 -050033#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -050034 ,
35 policies(POLICY_JSON_PATH)
Matt Spinler743e5822018-03-27 13:44:50 -050036#endif
Matt Spinlere0017eb2018-03-27 11:17:38 -050037{
Matt Spinler54bfa7e2018-03-27 11:28:59 -050038 createAll();
39}
40
41void Manager::createAll()
42{
Matt Spinler259e7272018-03-29 10:57:17 -050043 auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH);
Matt Spinler54bfa7e2018-03-27 11:28:59 -050044
45 for (const auto& object : objects)
46 {
47 const auto& interfaces = object.second;
48
49 auto propertyMap = std::find_if(
Matt Spinler259e7272018-03-29 10:57:17 -050050 interfaces.begin(), interfaces.end(),
51 [](const auto& i) { return i.first == LOGGING_IFACE; });
Matt Spinler54bfa7e2018-03-27 11:28:59 -050052
53 if (propertyMap != interfaces.end())
54 {
55 create(object.first, propertyMap->second);
56 }
57 }
58}
59
Matt Spinler259e7272018-03-29 10:57:17 -050060void Manager::create(const std::string& objectPath,
61 const DbusPropertyMap& properties)
Matt Spinler54bfa7e2018-03-27 11:28:59 -050062{
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050063
64#ifdef USE_POLICY_INTERFACE
65 createPolicyInterface(objectPath, properties);
66#endif
Matt Spinler433bead2018-04-23 11:08:03 -050067
68 // Emits the interfaces added signal.
69 createDeleteInterface(objectPath);
70}
71
72void Manager::erase(const std::string& objectPath)
73{
74 auto entry = entries.find(getEntryID(objectPath));
75
76 if (entry != entries.end())
77 {
78 entries.erase(entry);
79 }
Matt Spinlere0017eb2018-03-27 11:17:38 -050080}
81
Matt Spinler2cfceb42018-04-23 11:13:04 -050082void Manager::eraseAll()
83{
84 entries.clear();
85}
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 Spinlere0017eb2018-03-27 11:17:38 -0500148}
149}