3c4b1f95b9dc298e67fb2d6022b1cee44b64d3ee
[o-du/l2.git] / src / o1 / ves / VesCommonHeader.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 common header
20    parameters*/
21
22 #include <stdio.h>
23 #include <typeinfo>
24 #include <chrono>
25 #include <sys/time.h>
26 #include <ctime>
27 #include <sstream>
28 #include "JsonHelper.hpp"
29 #include "VesCommonHeader.hpp"
30
31 static uint16_t seqNo = 0;
32
33 /*******************************************************************
34  *
35  * @brief provide next sequence number of VES event
36  *
37  * @details
38  *
39  *    Function : nextSequenceNo
40  *
41  *    Functionality:
42  *      - provide next sequence number of VES event
43  *
44  * @params[in] void
45  * @return next sequence number of event  - success
46  *
47  * ****************************************************************/
48
49 uint16_t VesCommonHeader::nextSequenceNo()
50 {
51    return seqNo++;
52 }
53
54 /*******************************************************************
55  *
56  * @brief provide current sequence number of VES event
57  *
58  * @details
59  *
60  *    Function : getSequenceNo
61  *
62  *    Functionality:
63  *      - provide current sequence number of VES event
64  *
65  * @params[in] void
66  * @return current sequence number of event  - success
67  *
68  * ****************************************************************/
69
70 uint16_t VesCommonHeader::getSequenceNo()
71 {
72    return seqNo;
73 }
74
75 /*******************************************************************
76  *
77  * @brief Convert Ves Event type to string
78  *
79  * @details
80  *
81  *    Function : getEventTypeToStr
82  *
83  *    Functionality:
84  *      - Convert Ves Event type to String
85  *
86  * @params[in] IN - void
87  * @return value of string     - success
88  *         empty string        - failure
89  *
90  * ****************************************************************/
91
92 string VesCommonHeader::getEventTypeToStr()
93 {
94    string str = "";
95    switch(mEventType)
96    {
97       case VesEventType::PNF_REGISTRATION:
98          str = "pnfRegistration";
99          break;
100       case VesEventType::FAULT_NOTIFICATION:
101          str = "faultNotification";
102          break;
103       case VesEventType::PM_NOTIFICATION:
104          str = "pmNotification";
105          break;
106       case VesEventType::HEARTBEAT:
107          str = "heartbeat";
108          break;
109       default:
110          O1_LOG("O1 VES : VES msg Type is not avilable\n");
111          break;
112    }
113    return str;
114 }
115
116 /*******************************************************************
117  *
118  * @brief create Ves Event Id from Ves Event type
119  *
120  * @details
121  *
122  *    Function : getEventId
123  *
124  *    Functionality:
125  *      - create Ves Event Id from Ves Event type
126  *
127  * @params[in] IN - void
128  * @return value of string     - success
129  *         empty string        - failure
130  *
131  * ****************************************************************/
132
133 string VesCommonHeader::getEventId()
134 {
135   /*Currently PNF_REGISTRATION only supported. This function must be updated
136     in later releases*/
137    string evntId = "";
138    switch(mEventType)
139    {
140       case VesEventType::PNF_REGISTRATION:
141          evntId = getSourceName() + "_" + MODEL_NUMBER_007_DEV;
142          break;
143       case VesEventType::HEARTBEAT:
144          evntId = getEventTypeToStr() + "_" + getCurrentTime();
145          break;
146       default:
147          O1_LOG("O1 VES : this VES msg Type support in getEventId is \
148 not available\n");
149          break;
150    }
151    return evntId;
152
153 }
154
155 /*******************************************************************
156  *
157  * @brief create Ves Event Type
158  *
159  * @details
160  *
161  *    Function : getEventType
162  *
163  *    Functionality:
164  *      - create Ves Event Type
165  *
166  * @params[in] IN - void
167  * @return value of string     - success
168  *         empty string        - failure
169  *
170  * ****************************************************************/
171
172 string VesCommonHeader::getEventType()
173 {
174   /*Currently PNF_REGISTRATION only supported. This function must be updated
175     in later releases*/
176
177    string evntType = "";
178    switch(mEventType)
179    {
180       case VesEventType::PNF_REGISTRATION:
181          evntType = EVENT_TYPE_5G;
182          break;
183       case VesEventType::HEARTBEAT:
184          evntType = EVENT_TYPE_ORAN_COMPONENET;
185          break;
186       default:
187          O1_LOG("O1 VES : this VES msg Type support in getEvenType is \
188 not available\n");
189          break;
190    }
191    return evntType;
192 }
193
194 /*******************************************************************
195  *
196  * @brief return priority of event
197  *
198  * @details
199  *
200  *    Function : getPriority
201  *
202  *    Functionality:
203  *      - send priority of event
204  *
205  * @params[in] IN - void
206  * @return value of string     - success
207  *         empty string        - failure
208  *
209  * ****************************************************************/
210
211 string VesCommonHeader::getPriority()
212 {
213   /*Currently PNF_REGISTRATION only supported. This function must be updated
214     in later releases*/
215
216    string evntId = "";
217    switch(mEventType)
218    {
219       case VesEventType::PNF_REGISTRATION:
220          evntId = PRIORITY_LOW ;
221          break;
222       case VesEventType::HEARTBEAT:
223          evntId = PRIORITY_LOW;
224          break;
225       default:
226          O1_LOG("O1 VES : This VES msg Type support in getPriority is \
227 not available\n");
228          break;
229    }
230    return evntId;
231 }
232
233
234
235 /*******************************************************************
236  *
237  * @brief create Ves Event Name from Ves Event type
238  *
239  * @details
240  *
241  *    Function : getEventName
242  *
243  *    Functionality:
244  *      - create Ves Event Name from Ves Event type
245  *
246  * @params[in] IN - void
247  * @return value of string     - success
248  *         empty string        - failure
249  * ****************************************************************/
250
251 string VesCommonHeader::getEventName()
252 {
253   /*Currently PNF_REGISTRATION only supported. This function must be updated
254     in later releases*/
255
256    string evntName = "";
257    switch(mEventType)
258    {
259       case VesEventType::PNF_REGISTRATION:
260          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_5G;
261          break;
262       case VesEventType::HEARTBEAT:
263          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_ORAN_COMPONENET;
264          break;
265       default:
266          O1_LOG("O1 VES : This VES msg Type support in getEventName is \
267 not available\n");
268          break;
269    }
270    return evntName;
271 }
272
273 /*******************************************************************
274  *
275  * @brief create Ves Event Name from Ves Event type
276  *
277  * @details
278  *
279  *    Function : getReportingEntityName
280  *
281  *    Functionality:
282  *      - create Ves Event Name from Ves Event type
283  *
284  * @params[in] IN - void
285  * @return value of string     - success
286  *         empty string        - failure
287  * ****************************************************************/
288
289 string VesCommonHeader::getReportingEntityName()
290 {
291   /*Currently PNF_REGISTRATION only supported. This function must be updated
292     in later releases*/
293
294    string evntName = "";
295    switch(mEventType)
296    {
297       case VesEventType::PNF_REGISTRATION:
298          evntName = getSourceName();
299          break;
300       default:
301          O1_LOG("O1 VES : This VES msg Type support in \
302 getReportingEntityName is not available\n");
303          break;
304    }
305    return evntName;
306 }
307
308 /*******************************************************************
309  *
310  * @brief create Ves Event Name from Ves Event type
311  *
312  * @details
313  *
314  *    Function : getReportingEntityName
315  *
316  *    Functionality:
317  *      - create Ves Event Name from Ves Event type
318  *
319  * @params[in] IN - void
320  * @return value of string     - success
321  *         empty string        - failure
322  * ****************************************************************/
323
324 string VesCommonHeader::getSourceName()
325 {
326   /*Currently PNF_REGISTRATION only supported. This function need to be updated
327     in later releases*/
328    return ODU_HIGH;
329 }
330
331 /*******************************************************************
332  *
333  * @brief create Ves Event Name from Ves Event type
334  *
335  * @details
336  *
337  *    Function : getNamingCode
338  *
339  *    Functionality:
340  *      - Network function type: 4 characters (aligned with vnf and pnf
341            naming standards)
342  *
343  * @params[in] IN - void
344  * @return value of string     - success
345  *         empty string        - failure
346  * ****************************************************************/
347
348 string VesCommonHeader::getNamingCode()
349 {
350   /*Currently PNF_REGISTRATION only supported. This function need to be updated
351     in later releases*/
352    return NAMING_CODE_ODU;
353 }
354
355
356
357 /*******************************************************************
358  *
359  * @brief provide current epoch time
360  *
361  * @details
362  *
363  *    Function : getEpochTime
364  *
365  *    Functionality:
366  *      - provide current epoch time
367  *
368  * @params[in] void
369  * @return epoch time     - success
370  *
371  * ****************************************************************/
372
373 uint64_t VesCommonHeader::getEpochTime()
374 {
375     uint64_t epochTimeInMicrosec = std::chrono::duration_cast \
376             <std::chrono::microseconds> \
377             (std::chrono::system_clock::now().time_since_epoch()).count();
378     return epochTimeInMicrosec;
379 }
380
381 /*******************************************************************
382  *
383  * @brief get current date-time
384  *
385  * @details
386  *
387  *    Function : getCurrentTime
388  *
389  *    Functionality:
390  *      - get current date-time
391  *
392  * @params[in] void
393  * @return time-date     - success
394  *
395  * ****************************************************************/
396
397 string VesCommonHeader::getCurrentTime()
398 {
399    time_t t = time(0);
400    std::ostringstream oss;
401    char *dateStr;
402    strftime(dateStr, MAX_TIME_STR, "%F", localtime(&t));
403     oss << dateStr;
404     return oss.str();
405 }
406
407 /*******************************************************************
408  *
409  * @brief create Ves Event Name from Ves Event type
410  *
411  * @details
412  *
413  *    Function : getEventName
414  *
415  *    Functionality:
416  *      - create Ves Event Name from Ves Event type
417  *
418  * @params[in] IN - VesEventType , OUT - Ves Event Name
419  * @return ture     - success
420  *         false    - failure
421  *
422  * ****************************************************************/
423
424 bool VesCommonHeader::prepare(cJSON *commonHeader, \
425                               VesEventType type)
426 {
427    O1_LOG("O1 VES : createCommonHeader fuction started\n");
428    bool ret=true;
429    string typeStr;
430    string evntId;
431    string evntName;
432    mEventType = type;
433    seqNo=0;
434    nextSequenceNo(); //update the sequence number for next message
435    if(JsonHelper::addNodeToObject(commonHeader, "domain", \
436        getEventTypeToStr().c_str()) == 0) {
437       ret=false;
438    }
439    else if ( JsonHelper::addNodeToObject(commonHeader, "eventId", \
440                                          getEventId().c_str()) == 0)
441    {
442       ret =false;
443    }
444    else if(JsonHelper::addNodeToObject(commonHeader, "eventName", \
445                                        getEventName().c_str()) == 0)
446    {
447       ret = false;
448    }
449    else if(JsonHelper::addNodeToObject(commonHeader, "eventType", \
450                                        getEventType().c_str()) == 0)
451    {
452       ret = false;
453    }
454    else if(JsonHelper::addNodeToObject(commonHeader, "sequence", \
455                                        getSequenceNo()) == 0)
456    {
457       ret = false;
458    }
459    else if(JsonHelper::addNodeToObject(commonHeader, "priority", \
460                                        getPriority().c_str()) == 0)
461    {
462       ret = false;
463    }
464    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityId", \
465                                        "") == 0)
466    {
467       ret = false;
468    }
469    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityName", \
470                                        getReportingEntityName().c_str()) == 0)
471    {
472       ret = false;
473    }
474   else if(JsonHelper::addNodeToObject(commonHeader, "sourceId", \
475                                       "") == 0)
476    {
477       ret = false;
478    }
479    else if(JsonHelper::addNodeToObject(commonHeader, "sourceName", \
480                                        getSourceName().c_str()) == 0)
481    {
482       ret = false;
483    }
484   else if(JsonHelper::addNodeToObject(commonHeader, "startEpochMicrosec", \
485                                       (double) getEpochTime()) == 0)
486    {
487       ret = false;
488    }
489    else if(JsonHelper::addNodeToObject(commonHeader, "lastEpochMicrosec", \
490                                        (double) getEpochTime()) == 0)
491    {
492       ret = false;
493    }
494    else if(JsonHelper::addNodeToObject(commonHeader, "nfNamingCode", \
495                                        getNamingCode().c_str()) == 0)
496    {
497       ret = false;
498    }
499    else if(JsonHelper::addNodeToObject(commonHeader, "nfVendorName", \
500                                        "") == 0)
501    {
502       ret = false;
503    }
504   else if(JsonHelper::addNodeToObject(commonHeader, "timeZoneOffset", \
505                                       TIME_ZONE_00_00) == 0)
506    {
507       ret = false;
508    }
509    else if(JsonHelper::addNodeToObject(commonHeader, "version", \
510                                        VERSION_4_0_1) == 0)
511    {
512       ret = false;
513    }
514    else if(JsonHelper::addNodeToObject(commonHeader, "vesEventListenerVersion", \
515                                        VES_EVENT_LISTENER_7_2_1) == 0)
516    {
517       ret = false;
518    }
519    else
520    {
521       O1_LOG("O1 VES : VES common Header prepared successfully \n");
522    }
523    return ret;
524
525 }
526
527 /**********************************************************************
528   End of file
529  **********************************************************************/