Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 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" |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 18 | #include "policy_find.hpp" |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 19 | |
| 20 | namespace ibm |
| 21 | { |
| 22 | namespace logging |
| 23 | { |
| 24 | |
| 25 | Manager::Manager(sdbusplus::bus::bus& bus) : |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 26 | 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 Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 31 | 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 Spinler | 743e582 | 2018-03-27 13:44:50 -0500 | [diff] [blame] | 37 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 38 | , |
| 39 | policies(POLICY_JSON_PATH) |
Matt Spinler | 743e582 | 2018-03-27 13:44:50 -0500 | [diff] [blame] | 40 | #endif |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 41 | { |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 42 | createAll(); |
| 43 | } |
| 44 | |
| 45 | void Manager::createAll() |
| 46 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 47 | auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 48 | |
| 49 | for (const auto& object : objects) |
| 50 | { |
| 51 | const auto& interfaces = object.second; |
| 52 | |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 53 | auto propertyMap = interfaces.find(LOGGING_IFACE); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 54 | |
| 55 | if (propertyMap != interfaces.end()) |
| 56 | { |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 57 | createWithRestore(object.first, interfaces); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 62 | void Manager::createWithRestore(const std::string& objectPath, |
| 63 | const DbusInterfaceMap& interfaces) |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 64 | { |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 65 | createObject(objectPath, interfaces); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 66 | |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 67 | // TODO |
| 68 | // restoreCalloutObjects(objectPath, interfaces); |
| 69 | } |
| 70 | |
| 71 | void Manager::create(const std::string& objectPath, |
| 72 | const DbusInterfaceMap& interfaces) |
| 73 | { |
| 74 | createObject(objectPath, interfaces); |
| 75 | |
| 76 | // TODO |
| 77 | // createCalloutObjects(objectPath, interfaces); |
| 78 | } |
| 79 | |
| 80 | void Manager::createObject(const std::string& objectPath, |
| 81 | const DbusInterfaceMap& interfaces) |
| 82 | { |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 83 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 84 | auto logInterface = interfaces.find(LOGGING_IFACE); |
| 85 | createPolicyInterface(objectPath, logInterface->second); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 86 | #endif |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Matt Spinler | a139035 | 2018-05-23 10:48:19 -0500 | [diff] [blame] | 89 | void Manager::erase(EntryID id) |
| 90 | { |
Matt Spinler | 677143b | 2018-05-23 10:53:27 -0500 | [diff] [blame] | 91 | childEntries.erase(id); |
Matt Spinler | a139035 | 2018-05-23 10:48:19 -0500 | [diff] [blame] | 92 | entries.erase(id); |
| 93 | } |
| 94 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 95 | void Manager::addInterface(const std::string& objectPath, InterfaceType type, |
| 96 | std::experimental::any& object) |
| 97 | { |
| 98 | auto id = getEntryID(objectPath); |
| 99 | auto entry = entries.find(id); |
| 100 | |
| 101 | if (entry == entries.end()) |
| 102 | { |
| 103 | InterfaceMap interfaces; |
| 104 | interfaces.emplace(type, object); |
| 105 | entries.emplace(id, std::move(interfaces)); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | entry->second.emplace(type, object); |
| 110 | } |
| 111 | } |
| 112 | |
Matt Spinler | 677143b | 2018-05-23 10:53:27 -0500 | [diff] [blame] | 113 | void Manager::addChildInterface(const std::string& objectPath, |
| 114 | InterfaceType type, |
| 115 | std::experimental::any& object) |
| 116 | { |
| 117 | auto id = getEntryID(objectPath); |
| 118 | auto entry = childEntries.find(id); |
| 119 | |
| 120 | // childEntries is: |
| 121 | // A map of error log entry IDs to: |
| 122 | // a map of interface types to: |
| 123 | // a vector of interface objects |
| 124 | |
| 125 | if (entry == childEntries.end()) |
| 126 | { |
| 127 | ObjectList objects{object}; |
| 128 | InterfaceMapMulti interfaces; |
| 129 | interfaces.emplace(type, std::move(objects)); |
| 130 | childEntries.emplace(id, std::move(interfaces)); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | auto i = entry->second.find(type); |
| 135 | if (i == entry->second.end()) |
| 136 | { |
| 137 | ObjectList objects{objects}; |
| 138 | entry->second.emplace(type, objects); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | i->second.emplace_back(object); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 147 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 148 | void Manager::createPolicyInterface(const std::string& objectPath, |
| 149 | const DbusPropertyMap& properties) |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 150 | { |
| 151 | auto values = policy::find(policies, properties); |
| 152 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 153 | auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 154 | |
| 155 | object->eventID(std::get<policy::EIDField>(values)); |
| 156 | object->description(std::get<policy::MsgField>(values)); |
| 157 | |
Matt Spinler | 9bea4ea | 2018-05-09 15:53:04 -0500 | [diff] [blame] | 158 | object->emit_object_added(); |
| 159 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 160 | std::experimental::any anyObject = object; |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 161 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 162 | addInterface(objectPath, InterfaceType::POLICY, anyObject); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 163 | } |
| 164 | #endif |
| 165 | |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 166 | void Manager::interfaceAdded(sdbusplus::message::message& msg) |
| 167 | { |
| 168 | sdbusplus::message::object_path path; |
| 169 | DbusInterfaceMap interfaces; |
| 170 | |
| 171 | msg.read(path, interfaces); |
| 172 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 173 | // Find the Logging.Entry interface with all of its properties |
| 174 | // to pass to create(). |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 175 | if (interfaces.find(LOGGING_IFACE) != interfaces.end()) |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 176 | { |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame^] | 177 | create(path, interfaces); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 178 | } |
| 179 | } |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 180 | |
| 181 | void Manager::interfaceRemoved(sdbusplus::message::message& msg) |
| 182 | { |
| 183 | sdbusplus::message::object_path path; |
| 184 | DbusInterfaceList interfaces; |
| 185 | |
| 186 | msg.read(path, interfaces); |
| 187 | |
| 188 | // If the Logging.Entry interface was removed, then remove |
| 189 | // our object |
| 190 | |
| 191 | auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE); |
| 192 | |
| 193 | if (i != interfaces.end()) |
| 194 | { |
Matt Spinler | a139035 | 2018-05-23 10:48:19 -0500 | [diff] [blame] | 195 | erase(getEntryID(path)); |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 196 | } |
| 197 | } |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 198 | } |
| 199 | } |