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 "configuration.hpp"
18 
19 #include "action_environment.hpp"
20 #include "action_utils.hpp"
21 #include "chassis.hpp"
22 #include "device.hpp"
23 #include "exception_utils.hpp"
24 #include "journal.hpp"
25 #include "rail.hpp"
26 #include "system.hpp"
27 
28 #include <exception>
29 
30 namespace phosphor::power::regulators
31 {
32 
33 void Configuration::execute(System& system, Chassis& chassis, Device& device)
34 {
35     execute(system, chassis, device, device.getID());
36 }
37 
38 void Configuration::execute(System& system, Chassis& chassis, Device& device,
39                             Rail& rail)
40 {
41     execute(system, chassis, device, rail.getID());
42 }
43 
44 void Configuration::execute(System& system, Chassis& /*chassis*/,
45                             Device& device, const std::string& deviceOrRailID)
46 {
47     try
48     {
49         // Log debug message in journal
50         std::string message{"Configuring " + deviceOrRailID};
51         if (volts.has_value())
52         {
53             message += ": volts=" + std::to_string(volts.value());
54         }
55         journal::logDebug(message);
56 
57         // Create ActionEnvironment
58         ActionEnvironment environment{system.getIDMap(), device.getID()};
59         if (volts.has_value())
60         {
61             environment.setVolts(volts.value());
62         }
63 
64         // Execute the actions
65         action_utils::execute(actions, environment);
66     }
67     catch (const std::exception& e)
68     {
69         // Log error messages in journal
70         exception_utils::log(e);
71         journal::logErr("Unable to configure " + deviceOrRailID);
72 
73         // TODO: Create error log entry
74     }
75 }
76 
77 } // namespace phosphor::power::regulators
78