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" |
Matt Spinler | 433bead | 2018-04-23 11:08:03 -0500 | [diff] [blame] | 17 | #include "delete.hpp" |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 18 | #include "manager.hpp" |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 19 | #include "policy_find.hpp" |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 20 | |
| 21 | namespace ibm |
| 22 | { |
| 23 | namespace logging |
| 24 | { |
| 25 | |
| 26 | Manager::Manager(sdbusplus::bus::bus& bus) : |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 27 | 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 Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 34 | sdbusplus::bus::match::rules::interfacesRemoved() + |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 35 | sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH), |
| 36 | std::bind(std::mem_fn(&Manager::interfaceRemoved), this, |
| 37 | std::placeholders::_1)) |
Matt Spinler | 743e582 | 2018-03-27 13:44:50 -0500 | [diff] [blame] | 38 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 39 | , |
| 40 | policies(POLICY_JSON_PATH) |
Matt Spinler | 743e582 | 2018-03-27 13:44:50 -0500 | [diff] [blame] | 41 | #endif |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 42 | { |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 43 | createAll(); |
| 44 | } |
| 45 | |
| 46 | void Manager::createAll() |
| 47 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 48 | auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 49 | |
| 50 | for (const auto& object : objects) |
| 51 | { |
| 52 | const auto& interfaces = object.second; |
| 53 | |
| 54 | auto propertyMap = std::find_if( |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 55 | interfaces.begin(), interfaces.end(), |
| 56 | [](const auto& i) { return i.first == LOGGING_IFACE; }); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 57 | |
| 58 | if (propertyMap != interfaces.end()) |
| 59 | { |
| 60 | create(object.first, propertyMap->second); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 65 | void Manager::create(const std::string& objectPath, |
| 66 | const DbusPropertyMap& properties) |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 67 | { |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 68 | |
| 69 | #ifdef USE_POLICY_INTERFACE |
| 70 | createPolicyInterface(objectPath, properties); |
| 71 | #endif |
Matt Spinler | 433bead | 2018-04-23 11:08:03 -0500 | [diff] [blame] | 72 | |
| 73 | // Emits the interfaces added signal. |
| 74 | createDeleteInterface(objectPath); |
| 75 | } |
| 76 | |
| 77 | void 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 Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Matt Spinler | 2cfceb4 | 2018-04-23 11:13:04 -0500 | [diff] [blame^] | 87 | void Manager::eraseAll() |
| 88 | { |
| 89 | entries.clear(); |
| 90 | } |
| 91 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 92 | void 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 Spinler | 433bead | 2018-04-23 11:08:03 -0500 | [diff] [blame] | 110 | void 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 Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 118 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 119 | void Manager::createPolicyInterface(const std::string& objectPath, |
| 120 | const DbusPropertyMap& properties) |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 121 | { |
| 122 | auto values = policy::find(policies, properties); |
| 123 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 124 | auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 125 | |
| 126 | object->eventID(std::get<policy::EIDField>(values)); |
| 127 | object->description(std::get<policy::MsgField>(values)); |
| 128 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 129 | std::experimental::any anyObject = object; |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 130 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 131 | addInterface(objectPath, InterfaceType::POLICY, anyObject); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 132 | } |
| 133 | #endif |
| 134 | |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 135 | void Manager::interfaceAdded(sdbusplus::message::message& msg) |
| 136 | { |
| 137 | sdbusplus::message::object_path path; |
| 138 | DbusInterfaceMap interfaces; |
| 139 | |
| 140 | msg.read(path, interfaces); |
| 141 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 142 | // 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 Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 147 | |
| 148 | if (propertyMap != interfaces.end()) |
| 149 | { |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 150 | create(path, propertyMap->second); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | void Manager::interfaceRemoved(sdbusplus::message::message& msg) |
| 155 | { |
| 156 | sdbusplus::message::object_path path; |
| 157 | DbusInterfaceList interfaces; |
| 158 | |
| 159 | msg.read(path, interfaces); |
| 160 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 161 | // If the Logging.Entry interface was removed, then remove |
| 162 | // our object |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 163 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 164 | auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 165 | |
| 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 Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 177 | } |
| 178 | } |