PNF Registration to be sent after odu stack is up
[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    if(!readConfigFile()) {
64       O1_LOG("\nO1 VesEvent : Could not get SMO details");
65       return false;
66    }
67
68    mHttpClient = new HttpClient(mVesServerIp, mVesServerPort, mVesServerUsername, \
69                          mVesServerPassword);
70
71    cJSON *rootNode = JsonHelper::createNode();
72    if(rootNode == 0) {
73        O1_LOG("\nO1 VesEvent : could not create cJSON root Node object");
74        return false;
75    }
76
77    cJSON *event = JsonHelper::createNode();
78    if(event == 0) {
79       O1_LOG("\nO1 VesEvent : could not create event cJSON object");
80       JsonHelper::deleteNode(rootNode);
81       return false;
82    }
83
84    if(JsonHelper::addJsonNodeToObject(rootNode, "event", event) == 0) {
85       O1_LOG("\nO1 VesEvent : could not add event Object");
86       JsonHelper::deleteNode(rootNode);
87       return false;
88    }
89
90    cJSON *commHdrNode = JsonHelper::createNode();
91    if(commHdrNode == 0) {
92       O1_LOG("\nO1 VesEvent : could not create common header Node JSON object");
93       return false;
94    }
95
96    VesCommonHeader vesCommHdr;
97
98    if(vesCommHdr.prepare(commHdrNode, mVesEventType))
99    {
100        if(JsonHelper::addJsonNodeToObject(event, "commonEventHeader", \
101                                 commHdrNode) == 0) {
102        O1_LOG("\nO1 VesEvent : could not add commonEventHeader object");
103        JsonHelper::deleteNode(rootNode);
104        return false;
105       }
106       else {
107
108          //add header into the message and create pnfFields
109          mVesEventFields = JsonHelper::createNode();
110          if(mVesEventFields == 0) {
111             O1_LOG("\nO1 VesEvent : could not create Ves Event Fields JSON object");
112             return false;
113          }
114
115          if(!prepareEventFields()) {
116          O1_LOG("\nO1 VesEvent : could not prepare Ves Event Fields Node");
117          JsonHelper::deleteNode(rootNode);
118          return false;
119          }
120
121          if(JsonHelper::addJsonNodeToObject(event, "pnfRegistrationFields", mVesEventFields) == 0) {
122             O1_LOG("\nO1 VesEvent : could not add mVesEventFields object");
123             JsonHelper::deleteNode(rootNode);
124             return false;
125          }
126
127       mSendData = JsonHelper::printUnformatted(rootNode);
128       O1_LOG("\nO1 VesEvent : VES request : -- \n%s\n", JsonHelper::print(rootNode));
129       }
130    }
131    else
132    {
133       O1_LOG("\nO1 VesEvent : Failed to prepare preparePnfRegistration common header");
134       JsonHelper::deleteNode(rootNode);
135       return false;
136    }
137    return true;
138 }
139
140 bool VesEvent::send()
141 {
142    return mHttpClient->send(mSendData);
143 }
144
145 /*******************************************************************
146  *
147  * @brief Read Ves Collector config json file
148  *
149  * @details
150  *
151  *    Function : readConfigFile
152  *
153  *    Functionality:
154  *      - Reads json file.
155  *
156  *
157  * @params[in] void
158  * @return true  : success
159  *         false : failure
160  ******************************************************************/
161
162 bool VesEvent::readConfigFile()
163 {
164    cJSON *json = JsonHelper::read(VES_CONFIG);
165    if(json == NULL) {
166        O1_LOG("\nO1 VesEvent : Error reading config file  :%s", JsonHelper::getError());
167        return false;
168    }
169    else {
170       cJSON *rootNode = NULL;
171       rootNode = JsonHelper::getNode(json, "vesConfig");
172       if(rootNode) {
173          O1_LOG("\nO1 VesEvent : Reading smoConfig.json file\n");
174          mVesServerIp = JsonHelper::getValue(rootNode, "vesV4IpAddress");
175          mVesServerPort = JsonHelper::getValue(rootNode, "vesPort");
176          mVesServerUsername = JsonHelper::getValue(rootNode, "username");
177          mVesServerPassword = JsonHelper::getValue(rootNode, "password");
178       }
179       else {
180          O1_LOG("\nO1 VesEvent : smoConfig Object is not available in config file");
181          return false;
182       }
183    }
184    JsonHelper::deleteNode(json);
185    return true;
186 }