RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / model / ActionToBeSetup.cpp
1 /*
2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
17 */
18 #include"ActionToBeSetup.h"
19 namespace xapp {
20         namespace model {
21                 void from_json(const nlohmann::json & j, ActionToBeSetup& ref) {
22
23                         std::cout << __PRETTY_FUNCTION__ << std::endl;
24                         ref.validate_json(j);
25                         if (j.contains("ActionDefinition")) {
26                                 j.at("ActionDefinition").get_to(ref.ActionDefinition);
27                         }
28                         j.at("ActionID").get_to(ref.ActionID);
29                         j.at("ActionType").get_to(ref.ActionType);
30                         if (j.contains("SubsequentAction")) {
31                                 j.at("SubsequentAction").get_to(ref.m_SubsequentAction);
32                         }
33                         
34
35                 }
36
37                 void from_json(const nlohmann::json & j, std::vector<ActionToBeSetup>& ref) {
38
39                         std::cout << __PRETTY_FUNCTION__ << std::endl;
40
41                         for (auto& element : j) {
42                                 ActionToBeSetup tmp;
43                                 tmp.validate_json(element);
44                                 for (auto& val : element.items())
45                                 {
46                                         if (val.key() == "ActionDefinition") {
47
48                                                 tmp.ActionDefinition.assign(val.value().begin(), val.value().end());
49
50                                         }
51                                         else if (val.key() == "ActionID") {
52                                                 tmp.ActionID = val.value();
53                                         }
54                                         else if (val.key() == "ActionType") {
55                                                 tmp.ActionType = val.value();
56                                         }
57                                         else if (val.key() == "SubsequentAction") {
58                                                 tmp.m_SubsequentAction.SubsequentActionType = val.value()["SubsequentActionType"];
59                                                 tmp.m_SubsequentAction.TimeToWait = val.value()["TimeToWait"];
60                                         }
61
62
63                                 }
64                                 ref.push_back(tmp);
65                         }
66
67                 }
68                 void to_json(nlohmann::json& j, const ActionToBeSetup& ref) {
69
70                         j = nlohmann::json{
71                                 //{"ActionDefinition", ref.ActionDefinition},
72                                 {"ActionID", ref.ActionID},
73                                 {"ActionType", ref.ActionType}//,
74                                 //{"SubsequentAction", {{"SubsequentActionType",ref.m_SubsequentAction.SubsequentActionType},{"TimeToWait", ref.m_SubsequentAction.TimeToWait } }}
75                         };
76                         if (ref.ActionDefinition.size() > 0) {
77                                 j["ActionDefinition"] = ref.ActionDefinition;
78                         }
79                         if (ref.m_SubsequentAction.SubsequentActionType.length() > 0 && ref.m_SubsequentAction.TimeToWait.length() > 0) {
80                                 j["SubsequentAction"] = ref.m_SubsequentAction;
81                         }
82
83                 }
84
85                 void to_json(nlohmann::json& j, const std::vector<ActionToBeSetup>& ref) {
86                         /*
87                         for (int i = 0; i < ref.size(); i++) {
88                                 nlohmann::json tmp;
89                                 tmp = nlohmann::json{
90                                 {"ActionDefinition", ref[i].ActionDefinition},
91                                 {"ActionID", ref[i].ActionID},
92                                 {"ActionType", ref[i].ActionType},
93                                 {"SubsequentAction", {{"SubsequentActionType",ref[i].m_SubsequentAction.SubsequentActionType},{"TimeToWait", ref[i].m_SubsequentAction.TimeToWait } }}
94                                 };
95                                 j.push_back(tmp);
96                         }
97                         */
98                         for (int i = 0; i < ref.size(); i++) {
99                                 nlohmann::json tmp;
100                                 to_json(tmp, ref[i]);
101                                 j.push_back(tmp);
102                         }
103                 }
104         }
105 }