RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / model / SubscriptionDetail.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"SubscriptionDetail.h"
19 namespace xapp {
20         namespace model {
21                 void from_json(const nlohmann::json& j, SubscriptionDetail& ref) {
22
23                         std::cout << __PRETTY_FUNCTION__ << std::endl;
24                         ref.validate_json(j);
25
26                         j.at("XappEventInstanceId").get_to(ref.XappEventInstanceId);
27                         j.at("EventTriggers").get_to(ref.EventTriggers);
28                         j.at("ActionToBeSetupList").get_to(ref.Data);
29                 }
30
31                 void from_json(const nlohmann::json& j, std::vector<SubscriptionDetail>& ref) {
32
33                         std::cout << __PRETTY_FUNCTION__ << std::endl;
34
35
36                         for (auto& element : j) {
37                                 SubscriptionDetail tmp;
38                                 tmp.validate_json(element);
39                                 for (auto& val : element.items()) {
40                                         if (val.key() == "XappEventInstanceId") {
41
42                                                 //tmp.SetXappEventInstanceId(val.value());
43                                                 tmp.XappEventInstanceId = val.value();
44
45                                         }
46                                         else if (val.key() == "EventTriggers") {
47                                                 //tmp.setEventTriggers(val.value());
48                                                 tmp.EventTriggers.assign(val.value().begin(), val.value().end());
49
50
51                                         }
52                                         else if (val.key() == "ActionToBeSetupList") {
53
54                                                 nlohmann::json tmp_j(val.value());
55                                                 from_json(tmp_j, tmp.Data);
56                                         }
57                                 }
58                                 ref.push_back(tmp);
59
60                         }
61
62                         /*
63                         for (int i = 0; i < j.size(); i++)
64                         {
65                                 ref[i].validate_json(j.at(i));
66                                 j.at(i).at("XappEventInstanceId").get_to(ref[i].XappEventInstanceId);
67                                 j.at(i).at("EventTriggers").get_to(ref[i].EventTriggers);
68                                 j.at(i).at("ActionToBeSetupList").get_to(ref[i].Data);
69                         }
70                         */
71
72                 }
73
74                 void to_json(nlohmann::json& j, const SubscriptionDetail & ref) {
75
76                         j = nlohmann::json{
77                                 {"XappEventInstanceId", ref.XappEventInstanceId},
78                                 {"EventTriggers", ref.EventTriggers}
79                         };
80                         nlohmann::json tmp_j;
81                         to_json(tmp_j, ref.Data);
82                         j["ActionToBeSetupList"] = tmp_j;
83                         //for (int i = 0; i < ref.Data.size(); i++) {
84                         //      nlohmann::json tmp_j;
85                         //      to_json(tmp_j, ref.Data[i]);
86                         //      j["ActionToBeSetupList"] += tmp_j;
87                         //}
88                 }
89                 void to_json(nlohmann::json& j, const std::vector<SubscriptionDetail> & ref) {
90                         for (int i = 0; i < ref.size(); i++)
91                         {
92                                 nlohmann::json tmp_j;
93                                 to_json(tmp_j, ref[i]);
94                                 j += tmp_j;
95                         }
96                 }
97         }
98 }