6565fdfb786cc617c02ebf945a3c4eaa41b806eb
[o-du/l2.git] / src / o1 / ves / VesEvent.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [HCL Technologies Ltd.]                          #
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
19 /* This file contains functions to support the preparation of VES Event
20    It is the parent class of Ves Event, Every event need to override at
21    least prepareEventFields function and fill specific values of event
22    fields */
23
24 #include "VesEvent.hpp"
25
26
27 /* Default constructor*/
28 VesEvent::VesEvent()
29 {
30    mHttpClient = NULL;
31 }
32
33
34 /* Default Destructor*/
35 VesEvent::~VesEvent()
36 {
37    if(mHttpClient != NULL)
38    {
39       delete mHttpClient;
40    }
41 }
42
43 /*******************************************************************
44  *
45  * @brief prepare Ves Event Fields
46  *
47  * @details
48  *
49  *    Function : prepare
50  *
51  *    Functionality:
52  *      - prepare Ves Event Fields in json format
53  *
54  * @params[in] IN   - void
55  * @return true     - success
56  *         false    - failure
57  *
58  * ****************************************************************/
59
60 bool VesEvent::prepare()
61 {
62
63    O1_LOG("O1 VES : prepare PNF Registration start\n");
64    if(!getVesCollectorDetails()) {
65       O1_LOG("O1 VES : Could not get successfully details of Ves Collector\n");
66       return false;
67    }
68
69    mHttpClient = new HttpClient(mVesServerIp, mVesServerPort, mVesServerUsername, \
70                          mVesServerPassword);
71
72    cJSON *rootNode = JsonHelper::createNode();
73    if(rootNode == 0) {
74        O1_LOG("O1 VES : could not create cJSON root Node object\n");
75        return false;
76    }
77
78    cJSON *event = JsonHelper::createNode();
79    if(event == 0) {
80       O1_LOG("O1 VES : could not create event cJSON object\n");
81       JsonHelper::deleteNode(rootNode);
82       return false;
83    }
84
85    if(JsonHelper::addJsonNodeToObject(rootNode, "event", event) == 0) {
86       O1_LOG("O1 VES : could not add event Object\n");
87       JsonHelper::deleteNode(rootNode);
88       return false;
89    }
90
91    cJSON *commHdrNode = JsonHelper::createNode();
92    if(commHdrNode == 0) {
93       O1_LOG("O1 VES : could not create common header Node JSON object");
94       return false;
95    }
96
97    VesCommonHeader vesCommHdr;
98
99    if(vesCommHdr.prepare(commHdrNode, mVesEventType))
100    {
101        if(JsonHelper::addJsonNodeToObject(event, "commonEventHeader", \
102                                 commHdrNode) == 0) {
103        O1_LOG("O1 VES : could not add commonEventHeader object\n");
104        JsonHelper::deleteNode(rootNode);
105        return false;
106       }
107       else {
108
109          //add header into the message and create pnfFields
110          mVesEventFields = JsonHelper::createNode();
111          if(mVesEventFields == 0) {
112             O1_LOG("O1 VES : could not create Ves Event Fields JSON object\n");
113             return false;
114          }
115
116          if(!prepareEventFields()) {
117          O1_LOG("O1 VES : could not prepare Ves Event Fields Node \n");
118          JsonHelper::deleteNode(rootNode);
119          return false;
120          }
121
122          if(JsonHelper::addJsonNodeToObject(event, "pnfRegistrationFields", mVesEventFields) == 0) {
123             O1_LOG("O1 VES : could not add mVesEventFields object\n");
124             JsonHelper::deleteNode(rootNode);
125             return false;
126          }
127
128       mSendData = JsonHelper::printUnformatted(rootNode);
129       O1_LOG("O1 VES : final ves request : -- \n%s\n", JsonHelper::print(rootNode));
130       }
131    }
132    else
133    {
134       O1_LOG("O1 VES : preparePnfRegistration common header preparation failed\n");
135       JsonHelper::deleteNode(rootNode);
136       return false;
137    }
138    return true;
139 }
140
141 bool VesEvent::send()
142 {
143    return mHttpClient->send(mSendData);
144 }
145
146 /*******************************************************************
147  *
148  * @brief Gets the Ves Collector Server details
149  *
150  * @details
151  *
152  *    Function : getVesCollectorDetails
153  *
154  *    Functionality:
155  *      - Gets the Ves Collector Server details by reading config file
156  *
157  * @params[in] IN   - pointer to pnfFields
158  * @return true     - success
159  *         false    - failure
160  *
161  * ****************************************************************/
162
163
164 bool VesEvent::getVesCollectorDetails()
165 {
166    return readConfigFile();
167 }
168
169 /*******************************************************************
170  *
171  * @brief Read Ves Collector config json file
172  *
173  * @details
174  *
175  *    Function : readConfigFile
176  *
177  *    Functionality:
178  *      - Reads json file.
179  *
180  *
181  * @params[in] void
182  * @return true  : success
183  *         false : failure
184  ******************************************************************/
185
186 bool VesEvent::readConfigFile()
187 {
188    cJSON *json = JsonHelper::read(VES_CONFIG);
189    if(json == NULL) {
190        O1_LOG("O1 VES : Config file reading error is  :%s\n", JsonHelper::getError());
191     return false;
192    }
193    else {
194       cJSON *rootNode = NULL;
195       rootNode = JsonHelper::getNode(json, "vesConfig");
196       if(rootNode) {
197          O1_LOG("O1 VES : Reading smoConfig.json file\n");
198          mVesServerIp = JsonHelper::getValue(rootNode, "vesV4IpAddress");
199          mVesServerPort = JsonHelper::getValue(rootNode, "vesPort");
200          mVesServerUsername = JsonHelper::getValue(rootNode, "username");
201          mVesServerPassword = JsonHelper::getValue(rootNode, "password");
202       }
203       else {
204          O1_LOG("O1 VES : smoConfig Object is not availbale in config file\n");
205          return false;
206       }
207    }
208    JsonHelper::deleteNode(json);
209    return true;
210 }