Slice PM Counters VES Standard defined
[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 #include "Message.hpp"
26
27 /* Constructor */
28 VesEvent::VesEvent(VesEventType eventType) 
29                : mVesEventType(eventType) {
30
31           
32 }; 
33
34
35 /* Default Destructor*/
36 VesEvent::~VesEvent()
37 {
38    if ( mHttpClient != NULL )
39    {
40       delete mHttpClient;
41    }
42    free(mSendData);
43 }
44
45
46 /*******************************************************************
47  *
48  * @brief Initialize the Ves Event
49  *
50  * @details
51  *
52  *    Function : init
53  *
54  *    Functionality:
55  *      - Intialize the Ves event with configuration
56  *        Sets the URL
57  *        Instantiates the http client
58  *
59  * @params[in] IN   - void
60  * @return void
61  *
62  * ****************************************************************/
63
64 void VesEvent::init(){
65
66    getConfig();
67    createUrl();
68    mHttpClient = new HttpClient(mVesUrl, mVesServerUsername, mVesServerPassword);
69
70 }
71
72 /*******************************************************************
73  *
74  * @brief prepare Ves Event Fields
75  *
76  * @details
77  *
78  *    Function : prepare
79  *
80  *    Functionality:
81  *      - prepare Ves Event Fields in json format
82  *
83  * @params[in] IN   - void
84  * @return true     - success
85  *         false    - failure
86  *
87  * ****************************************************************/
88
89 bool VesEvent::prepare(const Message* msg)
90 {
91
92    cJSON *rootNode = JsonHelper::createNode();
93    if(rootNode == 0) {
94        O1_LOG("\nO1 VesEvent : could not create cJSON root Node object");
95        return false;
96    }
97    cJSON *event = JsonHelper::createNode();
98    if(event == 0) {
99       O1_LOG("\nO1 VesEvent : could not create event cJSON object");
100       JsonHelper::deleteNode(rootNode);
101       return false;
102    }
103    if(JsonHelper::addJsonNodeToObject(rootNode, "event", event) == 0) {
104       O1_LOG("\nO1 VesEvent : could not add event Object");
105       JsonHelper::deleteNode(rootNode);
106       return false;
107    }
108    cJSON *commHdrNode = JsonHelper::createNode();
109    if(commHdrNode == 0) {
110       O1_LOG("\nO1 VesEvent : could not create common header Node JSON object");
111       JsonHelper::deleteNode(rootNode);
112       return false;
113    }
114    VesCommonHeader vesCommHdr;
115    if(vesCommHdr.prepare(commHdrNode, mVesEventType))
116    {
117       if(JsonHelper::addJsonNodeToObject(event, "commonEventHeader", commHdrNode) == 0) 
118       {
119          O1_LOG("\nO1 VesEvent : could not add commonEventHeader object");
120          JsonHelper::deleteNode(rootNode);
121          return false;
122       }
123       else {
124          //add header into the message and create pnfFields
125          mVesEventFields = JsonHelper::createNode();
126          if(mVesEventFields == 0) {
127             O1_LOG("\nO1 VesEvent : could not create Ves Event Fields JSON object");
128             JsonHelper::deleteNode(rootNode);
129             return false;
130          }
131          if(!prepareEventFields(msg)) {
132             O1_LOG("\nO1 VesEvent : could not prepare Ves Event Fields Node");
133             JsonHelper::deleteNode(rootNode);
134             return false;
135          }
136          if(JsonHelper::addJsonNodeToObject(event, getEventFieldName().c_str(), mVesEventFields) == 0) {
137             O1_LOG("\nO1 VesEvent : could not add mVesEventFields object");
138             JsonHelper::deleteNode(rootNode);
139             return false;
140          }
141          mSendData = JsonHelper::printUnformatted(rootNode);
142          char* rootNode_string = JsonHelper::print(rootNode);
143          O1_LOG("\nO1 VesEvent : VES request : -- \n%s\n", rootNode_string);
144          free(rootNode_string);
145          JsonHelper::deleteNode(rootNode);  //deleting the rootNode here; (after getting the string version of the json created)
146       }
147    }
148    else
149    {
150       O1_LOG("\nO1 VesEvent : Failed to prepare preparePnfRegistration common header");
151       JsonHelper::deleteNode(rootNode);
152       return false;
153    }
154    return true;
155 }
156
157 /*******************************************************************
158  *
159  * @brief Send the Ves event over Http
160  *
161  * @details
162  *
163  *    Function : send
164  *
165  *    Functionality:
166  *      - Sends the Ves event over http
167  *
168  * @params[in] IN   - void
169  * @return true     - success
170  *         false    - failure
171  *
172  * ****************************************************************/
173
174 bool VesEvent::send()
175 {
176    return mHttpClient->send(mSendData);
177 }
178
179 /*******************************************************************
180  *
181  * @brief gets Event Type name from VesEventType Enum
182  *
183  * @details
184  *
185  *    Function : getEventFieldName
186  *
187  *    Functionality:
188  *      - returns VesEvent name
189  *
190  *
191  * @params[in] void
192  * @return string : Ves Event Name
193  ******************************************************************/
194
195 string VesEvent::getEventFieldName()
196 {
197
198    switch(mVesEventType)
199    {
200       case VesEventType::PNF_REGISTRATION:
201       {
202          return "pnfRegistrationFields";
203       }
204       case VesEventType::FAULT_NOTIFICATION:
205       {
206          #ifdef StdDef
207          return "stndDefinedFields";
208          #else
209          return "faultFields";
210          #endif
211       }
212       case VesEventType::PM_SLICE:
213       {
214          #ifdef StdDef
215          return "stndDefinedFields";
216          #else
217          return "measurementFields";
218          #endif
219       }
220       case VesEventType::HEARTBEAT:
221       {
222          return "heartbeatFields";
223       }
224       default:
225          return "eventFields";
226    }
227 }
228
229 /*******************************************************************
230  *
231  * @brief Get Ves Collector configuration 
232  *
233  * @details
234  *
235  *    Function : getConfig
236  *
237  *    Functionality:
238  *      - Gets Ves Collector configuration
239  *
240  *
241  * @params[in] void
242  * @return void
243  ******************************************************************/
244 void VesEvent::getConfig()
245 {
246     mVesServerIp = ConfigLoader::instance().getOamConfigFile().getVesServerIp();
247     mVesServerPort = ConfigLoader::instance().getOamConfigFile().getVesServerPort();
248     mVesServerUsername = ConfigLoader::instance().getOamConfigFile().getVesServerUsername();
249     mVesServerPassword = ConfigLoader::instance().getOamConfigFile().getVesServerPassword();
250 }
251
252 /*******************************************************************
253  *
254  * @brief Create the URL for sending VES messages
255  *
256  * @details
257  *
258  *    Function : createUrl
259  *
260  *    Functionality:
261  *      - Creates the VES URL
262  *
263  *
264  * @params[in] void
265  * @return void
266  ******************************************************************/
267 void VesEvent::createUrl()
268 {
269    mVesUrl = "https://" + mVesServerIp + ":" + mVesServerPort + "/eventListener/v7";
270 }
271
272
273
274 /**********************************************************************
275   End of file
276  **********************************************************************/
277