RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / model / SubscriptionData.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"SubscriptionData.h"
19 namespace xapp {
20         namespace model {
21                 void from_json(const nlohmann::json& j, SubscriptionData& ref) {
22
23                         std::cout << __PRETTY_FUNCTION__ << std::endl;
24                         ref.validate_json(j);
25                         if (j.contains("SubscriptionId")) {
26                                 j.at("SubscriptionId").get_to(ref.SubscriptionID);
27                         }
28                         if (j.contains("SubscriptionInstances")) {
29                                 j.at("SubscriptionInstances").get_to(ref.m_SubscriptionInstances);
30                         }
31                         
32                         if (j.contains("Meid")) {
33                                 j.at("Meid").get_to(ref.Meid);
34                         }
35                         
36                         if (j.contains("ClientEndpoint")) {
37                                 j.at("ClientEndpoint").get_to(ref.ClientEndpoint);
38                         }
39                         
40                 }
41
42                 void from_json(const nlohmann::json& j, std::vector<SubscriptionData>& ref) {
43                         for (auto& element : j) {
44                                 SubscriptionData tmp;
45                                 tmp.validate_json(element);
46                                 for (auto& val : element.items())
47                                 {
48                                         if (val.key() == "SubscriptionId") {
49                                                 tmp.SubscriptionID = val.value();
50                                         }
51                                         else if (val.key() == "Meid") {
52                                                 tmp.Meid = val.value();
53                                         }
54                                         else if (val.key() == "ClientEndpoint") {
55
56                                                 tmp.ClientEndpoint.assign(val.value().begin(), val.value().end());
57                                         }
58                                         else if (val.key() == "SubscriptionInstances") {
59                                                 from_json(val.value(), tmp.m_SubscriptionInstances);
60                                                 /*
61                                                 tmp.m_SubscriptionInstances.E2EventInstanceId= val.value()["E2EventInstanceId"];
62                                                 tmp.m_SubscriptionInstances.XappEventInstanceId= val.value()["XappEventInstanceId"];
63                                                 if (val.value().contains("ErrorCause"))
64                                                 {
65                                                         tmp.m_SubscriptionInstances.ErrorCause= val.value()["ErrorCause"];
66                                                 }
67                                                 if (val.value().contains("ErrorSource"))
68                                                 {
69                                                         tmp.m_SubscriptionInstances.ErrorCause = val.value()["ErrorSource"];
70                                                 }
71                                                 if (val.value().contains("TimeoutType"))
72                                                 {
73                                                         tmp.m_SubscriptionInstances.ErrorCause = val.value()["TimeoutType"];
74                                                 }
75                                                 */
76
77                                         }
78                                 }
79                                 ref.push_back(tmp);
80                         }
81
82                 }
83
84                 void to_json(nlohmann::json& j, const SubscriptionData& ref) {
85                         /*
86                         j = nlohmann::json{
87                                 {"SubscriptionId",ref.SubscriptionID},
88                                 {"Meid",ref.Meid},
89                                 {"ClientEndpoint", ref.ClientEndpoint}//,
90                                 //{"SubscriptionInstances", {{"XappEventInstanceId", ref.m_SubscriptionInstances.XappEventInstanceId},{"E2EventInstanceId", ref.m_SubscriptionInstances.E2EventInstanceId},{"ErrorCause", ref.m_SubscriptionInstances.ErrorCause},{"ErrorSource", ref.m_SubscriptionInstances.ErrorSource},{"TimeoutType", ref.m_SubscriptionInstances.TimeoutType}}}
91
92                         };
93                         nlohmann::json tmp_j;
94                         to_json(tmp_j, ref.m_SubscriptionInstances);
95                         j["SubscriptionInstances"] = tmp_j;
96                         */
97                         if (ref.SubscriptionID != "") {
98                                 j["SubscriptionId"] = ref.SubscriptionID;
99                         }
100                         if (ref.Meid.length() > 0) {
101                                 j["Meid"] = ref.Meid;
102                         }
103                         if (ref.ClientEndpoint.size() > 0) {
104                                 j["ClientEndpoint"] = ref.ClientEndpoint;
105                         }
106                         if (ref.m_SubscriptionInstances.size() > 0) {
107                                 nlohmann::json tmp_j;
108                                 to_json(tmp_j, ref.m_SubscriptionInstances);
109                                 j["SubscriptionInstances"] = tmp_j;
110                         }
111                 }
112                 void to_json(nlohmann::json& j, const std::vector<SubscriptionData>& ref) {
113                         for (int i = 0; i < ref.size(); i++)
114                         {
115                                 nlohmann::json tmp_j;
116                                 to_json(tmp_j, ref[i]);
117                                 j += tmp_j;
118                         }
119                 }
120         }
121 }