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 | |
| 53 | auto propertyMap = std::find_if( |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 54 | interfaces.begin(), interfaces.end(), |
| 55 | [](const auto& i) { return i.first == LOGGING_IFACE; }); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 56 | |
| 57 | if (propertyMap != interfaces.end()) |
| 58 | { |
| 59 | create(object.first, propertyMap->second); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 64 | void Manager::create(const std::string& objectPath, |
| 65 | const DbusPropertyMap& properties) |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 66 | { |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 67 | |
| 68 | #ifdef USE_POLICY_INTERFACE |
| 69 | createPolicyInterface(objectPath, properties); |
| 70 | #endif |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 73 | void Manager::addInterface(const std::string& objectPath, InterfaceType type, |
| 74 | std::experimental::any& object) |
| 75 | { |
| 76 | auto id = getEntryID(objectPath); |
| 77 | auto entry = entries.find(id); |
| 78 | |
| 79 | if (entry == entries.end()) |
| 80 | { |
| 81 | InterfaceMap interfaces; |
| 82 | interfaces.emplace(type, object); |
| 83 | entries.emplace(id, std::move(interfaces)); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | entry->second.emplace(type, object); |
| 88 | } |
| 89 | } |
| 90 | |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 91 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 92 | void Manager::createPolicyInterface(const std::string& objectPath, |
| 93 | const DbusPropertyMap& properties) |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 94 | { |
| 95 | auto values = policy::find(policies, properties); |
| 96 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 97 | auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 98 | |
| 99 | object->eventID(std::get<policy::EIDField>(values)); |
| 100 | object->description(std::get<policy::MsgField>(values)); |
| 101 | |
Matt Spinler | 9bea4ea | 2018-05-09 15:53:04 -0500 | [diff] [blame^] | 102 | object->emit_object_added(); |
| 103 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 104 | std::experimental::any anyObject = object; |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 105 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 106 | addInterface(objectPath, InterfaceType::POLICY, anyObject); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 107 | } |
| 108 | #endif |
| 109 | |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 110 | void Manager::interfaceAdded(sdbusplus::message::message& msg) |
| 111 | { |
| 112 | sdbusplus::message::object_path path; |
| 113 | DbusInterfaceMap interfaces; |
| 114 | |
| 115 | msg.read(path, interfaces); |
| 116 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 117 | // Find the Logging.Entry interface with all of its properties |
| 118 | // to pass to create(). |
| 119 | auto propertyMap = |
| 120 | std::find_if(interfaces.begin(), interfaces.end(), |
| 121 | [](const auto& i) { return i.first == LOGGING_IFACE; }); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 122 | |
| 123 | if (propertyMap != interfaces.end()) |
| 124 | { |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 125 | create(path, propertyMap->second); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 126 | } |
| 127 | } |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 128 | |
| 129 | void Manager::interfaceRemoved(sdbusplus::message::message& msg) |
| 130 | { |
| 131 | sdbusplus::message::object_path path; |
| 132 | DbusInterfaceList interfaces; |
| 133 | |
| 134 | msg.read(path, interfaces); |
| 135 | |
| 136 | // If the Logging.Entry interface was removed, then remove |
| 137 | // our object |
| 138 | |
| 139 | auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE); |
| 140 | |
| 141 | if (i != interfaces.end()) |
| 142 | { |
| 143 | auto id = getEntryID(path); |
| 144 | |
| 145 | auto entry = entries.find(id); |
| 146 | if (entry != entries.end()) |
| 147 | { |
| 148 | entries.erase(entry); |
| 149 | } |
| 150 | } |
| 151 | } |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 152 | } |
| 153 | } |