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, |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 32 | std::placeholders::_1)), |
| 33 | removeMatch(bus, |
| 34 | sdbusplus::bus::match::rules::interfacesRemoved() + |
| 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 | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 87 | void 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 Spinler | 433bead | 2018-04-23 11:08:03 -0500 | [diff] [blame] | 105 | void 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 Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 113 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 114 | void Manager::createPolicyInterface(const std::string& objectPath, |
| 115 | const DbusPropertyMap& properties) |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 116 | { |
| 117 | auto values = policy::find(policies, properties); |
| 118 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 119 | auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 120 | |
| 121 | object->eventID(std::get<policy::EIDField>(values)); |
| 122 | object->description(std::get<policy::MsgField>(values)); |
| 123 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 124 | std::experimental::any anyObject = object; |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 125 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 126 | addInterface(objectPath, InterfaceType::POLICY, anyObject); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 127 | } |
| 128 | #endif |
| 129 | |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 130 | void Manager::interfaceAdded(sdbusplus::message::message& msg) |
| 131 | { |
| 132 | sdbusplus::message::object_path path; |
| 133 | DbusInterfaceMap interfaces; |
| 134 | |
| 135 | msg.read(path, interfaces); |
| 136 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 137 | // 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 Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 142 | |
| 143 | if (propertyMap != interfaces.end()) |
| 144 | { |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 145 | create(path, propertyMap->second); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 146 | } |
| 147 | } |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 148 | |
| 149 | void Manager::interfaceRemoved(sdbusplus::message::message& msg) |
| 150 | { |
| 151 | sdbusplus::message::object_path path; |
| 152 | DbusInterfaceList interfaces; |
| 153 | |
| 154 | msg.read(path, interfaces); |
| 155 | |
| 156 | // If the Logging.Entry interface was removed, then remove |
| 157 | // our object |
| 158 | |
| 159 | auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE); |
| 160 | |
| 161 | if (i != interfaces.end()) |
| 162 | { |
| 163 | auto id = getEntryID(path); |
| 164 | |
| 165 | auto entry = entries.find(id); |
| 166 | if (entry != entries.end()) |
| 167 | { |
| 168 | entries.erase(entry); |
| 169 | } |
| 170 | } |
| 171 | } |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 172 | } |
| 173 | } |