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