blob: 06f3bc805ea6e254084eb283ed7d90e5f432aa4d [file] [log] [blame]
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05001#include "config.h"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05002
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05003#include "item_updater.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05004
Gunnar Millsf6ed5892018-09-07 17:08:02 -05005#include "xyz/openbmc_project/Common/error.hpp"
6
Gunnar Millsf6ed5892018-09-07 17:08:02 -05007#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/log.hpp>
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05009
10namespace openpower
11{
12namespace software
13{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050014namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050015{
Eddie James13fc66a2017-08-31 15:36:44 -050016using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050017using namespace phosphor::logging;
18
Lei YUf3ce4332019-02-21 14:09:49 +080019void ItemUpdater::createActiveAssociation(const std::string& path)
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050020{
Lei YUf3ce4332019-02-21 14:09:49 +080021 assocs.emplace_back(
22 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
23 associations(assocs);
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050024}
25
Lei YUf3ce4332019-02-21 14:09:49 +080026void ItemUpdater::updateFunctionalAssociation(const std::string& versionId)
Saqib Khan7254f0e2017-04-10 21:45:37 -050027{
Lei YUf3ce4332019-02-21 14:09:49 +080028 std::string path = std::string{SOFTWARE_OBJPATH} + '/' + versionId;
29 // remove all functional associations
30 for (auto iter = assocs.begin(); iter != assocs.end();)
Saqib Khan4c5d7442017-07-18 00:43:52 -050031 {
Lei YUf3ce4332019-02-21 14:09:49 +080032 if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0)
Saqib Khan4c5d7442017-07-18 00:43:52 -050033 {
Lei YUf3ce4332019-02-21 14:09:49 +080034 iter = assocs.erase(iter);
Saqib Khan4c5d7442017-07-18 00:43:52 -050035 }
Lei YUf3ce4332019-02-21 14:09:49 +080036 else
Saqib Khan2be9ba92017-09-26 22:44:10 -050037 {
Lei YUf3ce4332019-02-21 14:09:49 +080038 ++iter;
Saqib Khan2be9ba92017-09-26 22:44:10 -050039 }
Saqib Khan4c5d7442017-07-18 00:43:52 -050040 }
Lei YUf3ce4332019-02-21 14:09:49 +080041 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
42 FUNCTIONAL_REV_ASSOCIATION, path));
43 associations(assocs);
Saqib Khan7254f0e2017-04-10 21:45:37 -050044}
45
Lei YUf3ce4332019-02-21 14:09:49 +080046void ItemUpdater::removeAssociation(const std::string& path)
Saqib Khana8ade7e2017-04-12 10:27:56 -050047{
Lei YUf3ce4332019-02-21 14:09:49 +080048 for (auto iter = assocs.begin(); iter != assocs.end();)
Saqib Khana8ade7e2017-04-12 10:27:56 -050049 {
Lei YUf3ce4332019-02-21 14:09:49 +080050 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
62bool 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 Khana8ade7e2017-04-12 10:27:56 -050081 }
82 else
83 {
Lei YUf3ce4332019-02-21 14:09:49 +080084 versions.erase(entryId);
Saqib Khana8ade7e2017-04-12 10:27:56 -050085 }
Saqib Khana8ade7e2017-04-12 10:27:56 -050086
Lei YUf3ce4332019-02-21 14:09:49 +080087 // Removing entry in activations map
88 auto ita = activations.find(entryId);
89 if (ita == activations.end())
Adriana Kobylak0bd65052018-07-09 10:55:56 -050090 {
Lei YUf3ce4332019-02-21 14:09:49 +080091 log<level::ERR>(("Error: Failed to find version " + entryId +
92 " in item updater activations map."
93 " Unable to remove.")
94 .c_str());
Adriana Kobylak0bd65052018-07-09 10:55:56 -050095 }
Lei YUf3ce4332019-02-21 14:09:49 +080096 else
Michael Tritzbb31f022017-11-22 11:51:29 -060097 {
Lei YUf3ce4332019-02-21 14:09:49 +080098 removeAssociation(ita->second->path);
99 activations.erase(entryId);
Michael Tritzbb31f022017-11-22 11:51:29 -0600100 }
Eddie James13fc66a2017-08-31 15:36:44 -0500101 return true;
102}
103
104bool ItemUpdater::isChassisOn()
105{
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600106 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
107 MAPPER_INTERFACE, "GetObject");
Eddie James13fc66a2017-08-31 15:36:44 -0500108
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 Kobylak70dcb632018-02-27 15:46:52 -0600128 SYSTEMD_PROPERTY_INTERFACE, "Get");
Eddie James13fc66a2017-08-31 15:36:44 -0500129 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 Mills850d5f62017-10-19 17:04:38 -0500134 entry("MAPPERRESPONSE=%s",
Eddie James13fc66a2017-08-31 15:36:44 -0500135 (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 Kobylakbefe5ce2017-04-05 15:57:44 -0500145} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500146} // namespace software
147} // namespace openpower