blob: 94b4af14379370ab217067b8f7b02f2d14e5bd63 [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"
17#include "manager.hpp"
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050018#include "policy_find.hpp"
Matt Spinlere0017eb2018-03-27 11:17:38 -050019
20namespace ibm
21{
22namespace logging
23{
24
25Manager::Manager(sdbusplus::bus::bus& bus) :
Matt Spinler259e7272018-03-29 10:57:17 -050026 bus(bus),
27 addMatch(bus,
28 sdbusplus::bus::match::rules::interfacesAdded() +
29 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
30 std::bind(std::mem_fn(&Manager::interfaceAdded), this,
Matt Spinler055da3b2018-05-09 15:51:20 -050031 std::placeholders::_1)),
32 removeMatch(bus,
33 sdbusplus::bus::match::rules::interfacesRemoved() +
34 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
35 std::bind(std::mem_fn(&Manager::interfaceRemoved), this,
36 std::placeholders::_1))
Matt Spinler743e5822018-03-27 13:44:50 -050037#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -050038 ,
39 policies(POLICY_JSON_PATH)
Matt Spinler743e5822018-03-27 13:44:50 -050040#endif
Matt Spinlere0017eb2018-03-27 11:17:38 -050041{
Matt Spinler54bfa7e2018-03-27 11:28:59 -050042 createAll();
43}
44
45void Manager::createAll()
46{
Matt Spinler259e7272018-03-29 10:57:17 -050047 auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH);
Matt Spinler54bfa7e2018-03-27 11:28:59 -050048
49 for (const auto& object : objects)
50 {
51 const auto& interfaces = object.second;
52
53 auto propertyMap = std::find_if(
Matt Spinler259e7272018-03-29 10:57:17 -050054 interfaces.begin(), interfaces.end(),
55 [](const auto& i) { return i.first == LOGGING_IFACE; });
Matt Spinler54bfa7e2018-03-27 11:28:59 -050056
57 if (propertyMap != interfaces.end())
58 {
59 create(object.first, propertyMap->second);
60 }
61 }
62}
63
Matt Spinler259e7272018-03-29 10:57:17 -050064void Manager::create(const std::string& objectPath,
65 const DbusPropertyMap& properties)
Matt Spinler54bfa7e2018-03-27 11:28:59 -050066{
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050067
68#ifdef USE_POLICY_INTERFACE
69 createPolicyInterface(objectPath, properties);
70#endif
Matt Spinlere0017eb2018-03-27 11:17:38 -050071}
72
Matt Spinlera1390352018-05-23 10:48:19 -050073void Manager::erase(EntryID id)
74{
75 entries.erase(id);
76}
77
Matt Spinler491fc6f2018-04-23 11:00:52 -050078void Manager::addInterface(const std::string& objectPath, InterfaceType type,
79 std::experimental::any& object)
80{
81 auto id = getEntryID(objectPath);
82 auto entry = entries.find(id);
83
84 if (entry == entries.end())
85 {
86 InterfaceMap interfaces;
87 interfaces.emplace(type, object);
88 entries.emplace(id, std::move(interfaces));
89 }
90 else
91 {
92 entry->second.emplace(type, object);
93 }
94}
95
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050096#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -050097void Manager::createPolicyInterface(const std::string& objectPath,
98 const DbusPropertyMap& properties)
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050099{
100 auto values = policy::find(policies, properties);
101
Matt Spinler259e7272018-03-29 10:57:17 -0500102 auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500103
104 object->eventID(std::get<policy::EIDField>(values));
105 object->description(std::get<policy::MsgField>(values));
106
Matt Spinler9bea4ea2018-05-09 15:53:04 -0500107 object->emit_object_added();
108
Matt Spinler491fc6f2018-04-23 11:00:52 -0500109 std::experimental::any anyObject = object;
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500110
Matt Spinler491fc6f2018-04-23 11:00:52 -0500111 addInterface(objectPath, InterfaceType::POLICY, anyObject);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500112}
113#endif
114
Matt Spinlere0017eb2018-03-27 11:17:38 -0500115void Manager::interfaceAdded(sdbusplus::message::message& msg)
116{
117 sdbusplus::message::object_path path;
118 DbusInterfaceMap interfaces;
119
120 msg.read(path, interfaces);
121
Matt Spinler259e7272018-03-29 10:57:17 -0500122 // Find the Logging.Entry interface with all of its properties
123 // to pass to create().
124 auto propertyMap =
125 std::find_if(interfaces.begin(), interfaces.end(),
126 [](const auto& i) { return i.first == LOGGING_IFACE; });
Matt Spinlere0017eb2018-03-27 11:17:38 -0500127
128 if (propertyMap != interfaces.end())
129 {
Matt Spinler54bfa7e2018-03-27 11:28:59 -0500130 create(path, propertyMap->second);
Matt Spinlere0017eb2018-03-27 11:17:38 -0500131 }
132}
Matt Spinler055da3b2018-05-09 15:51:20 -0500133
134void Manager::interfaceRemoved(sdbusplus::message::message& msg)
135{
136 sdbusplus::message::object_path path;
137 DbusInterfaceList interfaces;
138
139 msg.read(path, interfaces);
140
141 // If the Logging.Entry interface was removed, then remove
142 // our object
143
144 auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE);
145
146 if (i != interfaces.end())
147 {
Matt Spinlera1390352018-05-23 10:48:19 -0500148 erase(getEntryID(path));
Matt Spinler055da3b2018-05-09 15:51:20 -0500149 }
150}
Matt Spinlere0017eb2018-03-27 11:17:38 -0500151}
152}