1 /**
2  * Copyright © 2020 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 
17 #include "compare_presence_action.hpp"
18 
19 #include "action_error.hpp"
20 
21 #include <exception>
22 #include <ios>
23 #include <sstream>
24 
25 namespace phosphor::power::regulators
26 {
27 
28 bool ComparePresenceAction::execute(ActionEnvironment& environment)
29 {
30     bool isEqual{false};
31     try
32     {
33         // Get actual presence value for FRU.
34         bool isPresent =
35             environment.getServices().getPresenceService().isPresent(fru);
36 
37         // Check if actual presence value equals expected presence value.
38         isEqual = (isPresent == value);
39     }
40     catch (const std::exception& e)
41     {
42         // Nest exception within an ActionError so the caller will have both the
43         // low level error information and the action information.
44         std::throw_with_nested(ActionError(*this));
45     }
46     return isEqual;
47 }
48 
49 std::string ComparePresenceAction::toString() const
50 {
51     std::ostringstream ss;
52     ss << "compare_presence: { ";
53     ss << "fru: " << fru << ", ";
54     ss << "value: " << std::boolalpha << value << " }";
55 
56     return ss.str();
57 }
58 
59 } // namespace phosphor::power::regulators
60