1 /**
2  * Copyright © 2021 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 "net_target_decrease.hpp"
17 
18 #include "../manager.hpp"
19 #include "../zone.hpp"
20 #include "action.hpp"
21 #include "group.hpp"
22 
23 #include <nlohmann/json.hpp>
24 #include <phosphor-logging/log.hpp>
25 
26 #include <algorithm>
27 #include <format>
28 #include <variant>
29 
30 namespace phosphor::fan::control::json
31 {
32 
33 using json = nlohmann::json;
34 using namespace phosphor::logging;
35 
36 NetTargetDecrease::NetTargetDecrease(const json& jsonObj,
37                                      const std::vector<Group>& groups) :
38     ActionBase(jsonObj, groups)
39 {
40     setState(jsonObj);
41     setDelta(jsonObj);
42 }
43 
44 void NetTargetDecrease::run(Zone& zone)
45 {
46     if (!_stateParameter.empty())
47     {
48         auto s = Manager::getParameter(_stateParameter);
49         if (!s)
50         {
51             return;
52         }
53         _state = *s;
54     }
55 
56     auto netDelta = zone.getDecDelta();
57     for (const auto& group : _groups)
58     {
59         for (const auto& member : group.getMembers())
60         {
61             try
62             {
63                 auto value = Manager::getObjValueVariant(
64                     member, group.getInterface(), group.getProperty());
65                 if (std::holds_alternative<int64_t>(value) ||
66                     std::holds_alternative<double>(value))
67                 {
68                     if (value >= _state)
69                     {
70                         // No decrease allowed for this group
71                         netDelta = 0;
72                         break;
73                     }
74                     else
75                     {
76                         // Decrease factor is the difference in configured state
77                         // to the current value's state
78                         uint64_t deltaFactor = 0;
79                         if (auto dblPtr = std::get_if<double>(&value))
80                         {
81                             deltaFactor = static_cast<uint64_t>(
82                                 std::get<double>(_state) - *dblPtr);
83                         }
84                         else
85                         {
86                             deltaFactor = static_cast<uint64_t>(
87                                 std::get<int64_t>(_state) -
88                                 std::get<int64_t>(value));
89                         }
90 
91                         // Multiply the decrease factor by the configured delta
92                         // to get the net decrease delta for the given group
93                         // member. The lowest net decrease delta of the entire
94                         // group is the decrease requested.
95                         if (netDelta == 0)
96                         {
97                             netDelta = deltaFactor * _delta;
98                         }
99                         else
100                         {
101                             netDelta = std::min(netDelta, deltaFactor * _delta);
102                         }
103                     }
104                 }
105                 else if (std::holds_alternative<bool>(value) ||
106                          std::holds_alternative<std::string>(value))
107                 {
108                     // Where a group of booleans or strings equal the state
109                     // provided, request a decrease of the configured delta
110                     if (_state == value)
111                     {
112                         if (netDelta == 0)
113                         {
114                             netDelta = _delta;
115                         }
116                         else
117                         {
118                             netDelta = std::min(netDelta, _delta);
119                         }
120                     }
121                 }
122                 else
123                 {
124                     // Unsupported group member type for this action
125                     log<level::ERR>(
126                         std::format("Action {}: Unsupported group member type "
127                                     "given. [object = {} : {} : {}]",
128                                     ActionBase::getName(), member,
129                                     group.getInterface(), group.getProperty())
130                             .c_str());
131                 }
132             }
133             catch (const std::out_of_range& oore)
134             {
135                 // Property value not found, netDelta unchanged
136             }
137         }
138         // Update group's decrease allowed state
139         zone.setDecreaseAllow(group.getName(), !(netDelta == 0));
140     }
141     // Request target decrease to occur on decrease interval
142     zone.requestDecrease(netDelta);
143 }
144 
145 void NetTargetDecrease::setState(const json& jsonObj)
146 {
147     if (jsonObj.contains("state"))
148     {
149         _state = getJsonValue(jsonObj["state"]);
150     }
151     else if (jsonObj.contains("state_parameter_name"))
152     {
153         _stateParameter = jsonObj["state_parameter_name"].get<std::string>();
154     }
155     else
156     {
157         throw ActionParseError{
158             ActionBase::getName(),
159             "Missing required state or state_parameter_name value"};
160     }
161 }
162 
163 void NetTargetDecrease::setDelta(const json& jsonObj)
164 {
165     if (!jsonObj.contains("delta"))
166     {
167         throw ActionParseError{ActionBase::getName(),
168                                "Missing required delta value"};
169     }
170     _delta = jsonObj["delta"].get<uint64_t>();
171 }
172 
173 } // namespace phosphor::fan::control::json
174