Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 2 | |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 3 | #include "item_updater.hpp" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 4 | |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 9 | |
| 10 | namespace openpower |
| 11 | { |
| 12 | namespace software |
| 13 | { |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 14 | namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 15 | { |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 16 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Adriana Kobylak | b66ac3a | 2017-03-28 13:33:20 -0500 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 19 | void ItemUpdater::createActiveAssociation(const std::string& path) |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 20 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 21 | assocs.emplace_back( |
| 22 | std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path)); |
| 23 | associations(assocs); |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 24 | } |
| 25 | |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 26 | void ItemUpdater::updateFunctionalAssociation(const std::string& versionId) |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 27 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 28 | std::string path = std::string{SOFTWARE_OBJPATH} + '/' + versionId; |
| 29 | // remove all functional associations |
| 30 | for (auto iter = assocs.begin(); iter != assocs.end();) |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 31 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 32 | if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0) |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 33 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 34 | iter = assocs.erase(iter); |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 35 | } |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 36 | else |
Saqib Khan | 2be9ba9 | 2017-09-26 22:44:10 -0500 | [diff] [blame] | 37 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 38 | ++iter; |
Saqib Khan | 2be9ba9 | 2017-09-26 22:44:10 -0500 | [diff] [blame] | 39 | } |
Saqib Khan | 4c5d744 | 2017-07-18 00:43:52 -0500 | [diff] [blame] | 40 | } |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 41 | assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION, |
| 42 | FUNCTIONAL_REV_ASSOCIATION, path)); |
| 43 | associations(assocs); |
Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 46 | void ItemUpdater::removeAssociation(const std::string& path) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 47 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 48 | for (auto iter = assocs.begin(); iter != assocs.end();) |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 49 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 50 | if ((std::get<2>(*iter)).compare(path) == 0) |
| 51 | { |
| 52 | iter = assocs.erase(iter); |
| 53 | associations(assocs); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | ++iter; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool ItemUpdater::erase(std::string entryId) |
| 63 | { |
| 64 | if (isVersionFunctional(entryId) && isChassisOn()) |
| 65 | { |
| 66 | log<level::ERR>(("Error: Version " + entryId + |
| 67 | " is currently active and running on the host." |
| 68 | " Unable to remove.") |
| 69 | .c_str()); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // Removing entry in versions map |
| 74 | auto it = versions.find(entryId); |
| 75 | if (it == versions.end()) |
| 76 | { |
| 77 | log<level::ERR>(("Error: Failed to find version " + entryId + |
| 78 | " in item updater versions map." |
| 79 | " Unable to remove.") |
| 80 | .c_str()); |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 81 | } |
| 82 | else |
| 83 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 84 | versions.erase(entryId); |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 85 | } |
Saqib Khan | a8ade7e | 2017-04-12 10:27:56 -0500 | [diff] [blame] | 86 | |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 87 | // Removing entry in activations map |
| 88 | auto ita = activations.find(entryId); |
| 89 | if (ita == activations.end()) |
Adriana Kobylak | 0bd6505 | 2018-07-09 10:55:56 -0500 | [diff] [blame] | 90 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 91 | log<level::ERR>(("Error: Failed to find version " + entryId + |
| 92 | " in item updater activations map." |
| 93 | " Unable to remove.") |
| 94 | .c_str()); |
Adriana Kobylak | 0bd6505 | 2018-07-09 10:55:56 -0500 | [diff] [blame] | 95 | } |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 96 | else |
Michael Tritz | bb31f02 | 2017-11-22 11:51:29 -0600 | [diff] [blame] | 97 | { |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame^] | 98 | removeAssociation(ita->second->path); |
| 99 | activations.erase(entryId); |
Michael Tritz | bb31f02 | 2017-11-22 11:51:29 -0600 | [diff] [blame] | 100 | } |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool ItemUpdater::isChassisOn() |
| 105 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 106 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 107 | MAPPER_INTERFACE, "GetObject"); |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 108 | |
| 109 | mapperCall.append(CHASSIS_STATE_PATH, |
| 110 | std::vector<std::string>({CHASSIS_STATE_OBJ})); |
| 111 | auto mapperResponseMsg = bus.call(mapperCall); |
| 112 | if (mapperResponseMsg.is_method_error()) |
| 113 | { |
| 114 | log<level::ERR>("Error in Mapper call"); |
| 115 | elog<InternalFailure>(); |
| 116 | } |
| 117 | using MapperResponseType = std::map<std::string, std::vector<std::string>>; |
| 118 | MapperResponseType mapperResponse; |
| 119 | mapperResponseMsg.read(mapperResponse); |
| 120 | if (mapperResponse.empty()) |
| 121 | { |
| 122 | log<level::ERR>("Invalid Response from mapper"); |
| 123 | elog<InternalFailure>(); |
| 124 | } |
| 125 | |
| 126 | auto method = bus.new_method_call((mapperResponse.begin()->first).c_str(), |
| 127 | CHASSIS_STATE_PATH, |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 128 | SYSTEMD_PROPERTY_INTERFACE, "Get"); |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 129 | method.append(CHASSIS_STATE_OBJ, "CurrentPowerState"); |
| 130 | auto response = bus.call(method); |
| 131 | if (response.is_method_error()) |
| 132 | { |
| 133 | log<level::ERR>("Error in fetching current Chassis State", |
Gunnar Mills | 850d5f6 | 2017-10-19 17:04:38 -0500 | [diff] [blame] | 134 | entry("MAPPERRESPONSE=%s", |
Eddie James | 13fc66a | 2017-08-31 15:36:44 -0500 | [diff] [blame] | 135 | (mapperResponse.begin()->first).c_str())); |
| 136 | elog<InternalFailure>(); |
| 137 | } |
| 138 | sdbusplus::message::variant<std::string> currentChassisState; |
| 139 | response.read(currentChassisState); |
| 140 | auto strParam = |
| 141 | sdbusplus::message::variant_ns::get<std::string>(currentChassisState); |
| 142 | return (strParam != CHASSIS_STATE_OFF); |
| 143 | } |
| 144 | |
Adriana Kobylak | befe5ce | 2017-04-05 15:57:44 -0500 | [diff] [blame] | 145 | } // namespace updater |
Adriana Kobylak | 2d8fa22 | 2017-03-15 12:34:32 -0500 | [diff] [blame] | 146 | } // namespace software |
| 147 | } // namespace openpower |