xref: /openbmc/ibm-logging/manager.cpp (revision 62011e26)
1 /**
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"
18 #include "policy_find.hpp"
19 
20 namespace ibm
21 {
22 namespace logging
23 {
24 
25 Manager::Manager(sdbusplus::bus::bus& bus) :
26         bus(bus),
27         addMatch(
28                 bus,
29                 sdbusplus::bus::match::rules::interfacesAdded() +
30                 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
31                 std::bind(std::mem_fn(&Manager::interfaceAdded),
32                           this, std::placeholders::_1)),
33         removeMatch(
34                 bus,
35                 sdbusplus::bus::match::rules::interfacesRemoved() +
36                 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
37                 std::bind(std::mem_fn(&Manager::interfaceRemoved),
38                           this, std::placeholders::_1))
39 #ifdef USE_POLICY_INTERFACE
40         , policies(POLICY_JSON_PATH)
41 #endif
42 {
43     createAll();
44 }
45 
46 void Manager::createAll()
47 {
48     auto objects = getManagedObjects(
49             bus, LOGGING_BUSNAME, LOGGING_PATH);
50 
51     for (const auto& object : objects)
52     {
53         const auto& interfaces = object.second;
54 
55         auto propertyMap = std::find_if(
56                 interfaces.begin(),
57                 interfaces.end(),
58                 [](const auto& i)
59                 {
60                     return i.first == LOGGING_IFACE;
61                 });
62 
63         if (propertyMap != interfaces.end())
64         {
65             create(object.first, propertyMap->second);
66         }
67     }
68 }
69 
70 void Manager::create(
71         const std::string& objectPath,
72         const DbusPropertyMap& properties)
73 {
74 
75 #ifdef USE_POLICY_INTERFACE
76     createPolicyInterface(objectPath, properties);
77 #endif
78 
79 }
80 
81 #ifdef USE_POLICY_INTERFACE
82 void Manager::createPolicyInterface(
83         const std::string& objectPath,
84         const DbusPropertyMap& properties)
85 {
86     auto values = policy::find(policies, properties);
87 
88     auto object = std::make_shared<PolicyObject>(
89             bus, objectPath.c_str(), true);
90 
91     object->eventID(std::get<policy::EIDField>(values));
92     object->description(std::get<policy::MsgField>(values));
93 
94     object->emit_object_added();
95 
96     auto id = getEntryID(objectPath);
97     auto entry = entries.find(id);
98 
99     if (entry == entries.end())
100     {
101         InterfaceMap interfaces;
102         interfaces.emplace(InterfaceType::POLICY, object);
103         entries.emplace(id, interfaces);
104     }
105     else
106     {
107         entry->second.emplace(InterfaceType::POLICY, object);
108     }
109 }
110 #endif
111 
112 
113 void Manager::interfaceAdded(sdbusplus::message::message& msg)
114 {
115     sdbusplus::message::object_path path;
116     DbusInterfaceMap interfaces;
117 
118     msg.read(path, interfaces);
119 
120     //Find the Logging.Entry interface with all of its properties
121     //to pass to create().
122     auto propertyMap = std::find_if(
123             interfaces.begin(),
124             interfaces.end(),
125             [](const auto& i)
126             {
127                 return i.first == LOGGING_IFACE;
128             });
129 
130     if (propertyMap != interfaces.end())
131     {
132         create(path, propertyMap->second);
133     }
134 }
135 
136 void Manager::interfaceRemoved(sdbusplus::message::message& msg)
137 {
138     sdbusplus::message::object_path path;
139     DbusInterfaceList interfaces;
140 
141     msg.read(path, interfaces);
142 
143     //If the Logging.Entry interface was removed, then remove
144     //our object
145 
146     auto i = std::find(
147             interfaces.begin(),
148             interfaces.end(),
149             LOGGING_IFACE);
150 
151     if (i != interfaces.end())
152     {
153         auto id = getEntryID(path);
154 
155         auto entry = entries.find(id);
156         if (entry != entries.end())
157         {
158             entries.erase(entry);
159         }
160     }
161 }
162 
163 }
164 }
165