ce7214ddd0f2c5780bb87ecd16b534bf15f703b7
[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 #define MAX_TIME_STR 35
34
35 /*******************************************************************
36  *
37  * @brief provide next sequence number of VES event
38  *
39  * @details
40  *
41  *    Function : nextSequenceNo
42  *
43  *    Functionality:
44  *      - provide next sequence number of VES event
45  *
46  * @params[in] void
47  * @return next sequence number of event  - success
48  *
49  * ****************************************************************/
50
51 uint16_t VesCommonHeader::nextSequenceNo()
52 {
53    return seqNo++;
54 }
55
56 /*******************************************************************
57  *
58  * @brief provide current sequence number of VES event
59  *
60  * @details
61  *
62  *    Function : getSequenceNo
63  *
64  *    Functionality:
65  *      - provide current sequence number of VES event
66  *
67  * @params[in] void
68  * @return current sequence number of event  - success
69  *
70  * ****************************************************************/
71
72 uint16_t VesCommonHeader::getSequenceNo()
73 {
74    return seqNo;
75 }
76
77 /*******************************************************************
78  *
79  * @brief Convert Ves Event type to string
80  *
81  * @details
82  *
83  *    Function : getEventTypeToStr
84  *
85  *    Functionality:
86  *      - Convert Ves Event type to String
87  *
88  * @params[in] IN - void
89  * @return value of string     - success
90  *         empty string        - failure
91  *
92  * ****************************************************************/
93
94 string VesCommonHeader::getEventTypeToStr()
95 {
96    string str = "";
97    switch(mEventType)
98    {
99       case VesEventType::PNF_REGISTRATION:
100          str = "pnfRegistration";
101          break;
102       case VesEventType::FAULT_NOTIFICATION:
103          str = "faultNotification";
104          break;
105       case VesEventType::PM_NOTIFICATION:
106          str = "pmNotification";
107          break;
108       case VesEventType::PM_SLICE:
109          str = "measurement";
110          break;
111       case VesEventType::HEARTBEAT:
112          str = "heartbeat";
113          break;
114       default:
115          O1_LOG("\nO1 VesCommonHeader : VES msg Type not supported");
116          break;
117    }
118    return str;
119 }
120
121 /*******************************************************************
122  *
123  * @brief create Ves Event Id from Ves Event type
124  *
125  * @details
126  *
127  *    Function : getEventId
128  *
129  *    Functionality:
130  *      - create Ves Event Id from Ves Event type
131  *
132  * @params[in] IN - void
133  * @return value of string     - success
134  *         empty string        - failure
135  *
136  * ****************************************************************/
137
138 string VesCommonHeader::getEventId()
139 {
140   /*Currently PNF_REGISTRATION only supported. This function must be updated
141     in later releases*/
142    std::ostringstream oss;
143    oss << mLastEpochTime;
144    string stringEpochTime = oss.str().substr(0, 10);
145    string evntId = "";
146    switch(mEventType)
147    {
148       case VesEventType::PNF_REGISTRATION:
149          evntId = getSourceName() + "_" + MODEL_NUMBER_007_DEV;
150          break;
151       case VesEventType::HEARTBEAT:
152          evntId = getEventTypeToStr() + "_" + formatTime(getCurrentTime());
153          break;
154       case VesEventType::PM_SLICE:
155          evntId = "_" + stringEpochTime + "_" + "PM1min";
156      break;
157       case VesEventType::FAULT_NOTIFICATION:
158          evntId = getSourceName() + "_" + MODEL_NUMBER_007_DEV;
159      break;
160       default:
161          O1_LOG("\nO1 VesCommonHeader : this VES msg Type support in getEventId is \
162 not available");
163          break;
164    }
165    return evntId;
166 }
167
168 /*******************************************************************
169  *
170  * @brief create Ves Event Type
171  *
172  * @details
173  *
174  *    Function : getEventType
175  *
176  *    Functionality:
177  *      - create Ves Event Type
178  *
179  * @params[in] IN - void
180  * @return value of string     - success
181  *         empty string        - failure
182  *
183  * ****************************************************************/
184
185 string VesCommonHeader::getEventType()
186 {
187   /*Currently PNF_REGISTRATION only supported. This function must be updated
188     in later releases*/
189
190    string evntType = "";
191    switch(mEventType)
192    {
193       case VesEventType::PNF_REGISTRATION:
194          evntType = EVENT_TYPE_5G;
195          break;
196       case VesEventType::HEARTBEAT:
197          evntType = EVENT_TYPE_ORAN_COMPONENT;
198          break;
199       case VesEventType::PM_SLICE:
200          evntType = EVENT_TYPE_ORAN_COMPONENT_PM;
201          break;
202       case VesEventType::FAULT_NOTIFICATION:
203          evntType = EVENT_TYPE_5G;
204          break;
205       default:
206          O1_LOG("\nO1 VesCommonHeader : this VES msg Type support in getEvenType is \
207 not available");
208          break;
209    }
210    return evntType;
211 }
212
213 /*******************************************************************
214  *
215  * @brief return priority of event
216  *
217  * @details
218  *
219  *    Function : getPriority
220  *
221  *    Functionality:
222  *      - send priority of event
223  *
224  * @params[in] IN - void
225  * @return value of string     - success
226  *         empty string        - failure
227  *
228  * ****************************************************************/
229
230 string VesCommonHeader::getPriority()
231 {
232   /*Currently PNF_REGISTRATION only supported. This function must be updated
233     in later releases*/
234
235    string evntId = "";
236    switch(mEventType)
237    {
238       case VesEventType::PNF_REGISTRATION:
239          evntId = PRIORITY_LOW ;
240          break;
241       case VesEventType::HEARTBEAT:
242          evntId = PRIORITY_LOW;
243          break;
244       case VesEventType::PM_SLICE:
245          evntId = PRIORITY_LOW ;
246          break;
247       case VesEventType::FAULT_NOTIFICATION:
248          evntId = PRIORITY_LOW ;
249          break;
250       default:
251          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in getPriority is \
252 not available");
253          break;
254    }
255    return evntId;
256 }
257
258
259
260 /*******************************************************************
261  *
262  * @brief create Ves Event Name from Ves Event type
263  *
264  * @details
265  *
266  *    Function : getEventName
267  *
268  *    Functionality:
269  *      - create Ves Event Name from Ves Event type
270  *
271  * @params[in] IN - void
272  * @return value of string     - success
273  *         empty string        - failure
274  * ****************************************************************/
275
276 string VesCommonHeader::getEventName()
277 {
278   /*Currently PNF_REGISTRATION only supported. This function must be updated
279     in later releases*/
280
281    string evntName = "";
282    switch(mEventType)
283    {
284       case VesEventType::PNF_REGISTRATION:
285          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_5G;
286          break;
287       case VesEventType::HEARTBEAT:
288          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_ORAN_COMPONENT;
289          break;
290       case VesEventType::PM_SLICE:
291          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_ORAN_COMPONENT_PM;
292          break;
293       case VesEventType::FAULT_NOTIFICATION:
294          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_5G;
295          break;
296       default:
297          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in getEventName is \
298          not available");
299          break;
300    }
301    return evntName;
302 }
303
304 /*******************************************************************
305  *
306  * @brief create Ves Event Name from Ves Event type
307  *
308  * @details
309  *
310  *    Function : getReportingEntityName
311  *
312  *    Functionality:
313  *      - create Ves Event Name from Ves Event type
314  *
315  * @params[in] IN - void
316  * @return value of string     - success
317  *         empty string        - failure
318  * ****************************************************************/
319
320 string VesCommonHeader::getReportingEntityName()
321 {
322   /*Currently PNF_REGISTRATION and PM_SLICE are only supported. 
323    This function must be updated in later releases*/
324
325    string evntName = "";
326    switch(mEventType)
327    {
328       case VesEventType::PNF_REGISTRATION:
329          evntName = getSourceName();
330          break;
331       case VesEventType::PM_SLICE:
332          evntName = PM_REPORTING_ENTITY;
333          break;
334       case VesEventType::FAULT_NOTIFICATION:
335          evntName = getSourceName();
336          break;
337       default:
338          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in \
339          getReportingEntityName is not available");
340          break;
341    }
342    return evntName;
343 }
344
345 /*******************************************************************
346  *
347  * @brief create Ves Event Name from Ves Event type
348  *
349  * @details
350  *
351  *    Function : getReportingEntityName
352  *
353  *    Functionality:
354  *      - create Ves Event Name from Ves Event type
355  *
356  * @params[in] IN - void
357  * @return value of string     - success
358  *         empty string        - failure
359  * ****************************************************************/
360
361 string VesCommonHeader::getSourceName()
362 {
363    return ODU_HIGH;
364 }
365
366 /*******************************************************************
367  *
368  * @brief create Ves Event Name from Ves Event type
369  *
370  * @details
371  *
372  *    Function : getNamingCode
373  *
374  *    Functionality:
375  *      - Network function type: 4 characters (aligned with vnf and pnf
376            naming standards)
377  *
378  * @params[in] IN - void
379  * @return value of string     - success
380  *         empty string        - failure
381  * ****************************************************************/
382
383 string VesCommonHeader::getNamingCode()
384 {
385    return NAMING_CODE_ODU;
386 }
387
388
389
390 /*******************************************************************
391  *
392  * @brief provide current epoch time
393  *
394  * @details
395  *
396  *    Function : getEpochTime
397  *
398  *    Functionality:
399  *      - provide current epoch time
400  *
401  * @params[in] void
402  * @return epoch time     - success
403  *
404  * ****************************************************************/
405
406 uint64_t VesCommonHeader::getEpochTime()
407 {
408     uint64_t epochTimeInMicrosec = std::chrono::duration_cast \
409             <std::chrono::microseconds> \
410             (std::chrono::system_clock::now().time_since_epoch()).count();
411     return epochTimeInMicrosec;
412 }
413
414 /*******************************************************************
415  *
416  * @brief get current time
417  *
418  * @details
419  *
420  *    Function : getCurrentTime
421  *
422  *    Functionality:
423  *      - get current time
424  *
425  * @params[in] void
426  * @return time     - success
427  *
428  * ****************************************************************/
429
430 time_t VesCommonHeader::getCurrentTime()
431 {
432    time_t t;
433    time(&t);
434    return t;
435 }
436
437 /*******************************************************************
438  *
439  * @brief formats the current time like: 
440  *           "Thu, 14 Oct 2021 03:15:00 +0000"
441  *
442  * @details
443  *
444  *    Function : formatTime
445  *
446  *    Functionality:
447  *      - formats current time
448  *
449  * @params[in] void
450  * @return formatted time - success
451  *
452  * ****************************************************************/
453
454 std::string VesCommonHeader::formatTime(time_t t) {
455    std::ostringstream oss;
456    char dateStr[MAX_TIME_STR];
457    strftime(dateStr, sizeof(dateStr), "%a, %d %b %Y %T %z", gmtime(&t));
458    oss << dateStr;
459    return oss.str();
460 }
461
462 /*******************************************************************
463  *
464  * @brief create Ves Event Name from Ves Event type
465  *
466  * @details
467  *
468  *    Function : getEventName
469  *
470  *    Functionality:
471  *      - create Ves Event Name from Ves Event type
472  *
473  * @params[in] IN - VesEventType , OUT - Ves Event Name
474  * @return ture     - success
475  *         false    - failure
476  *
477  * ****************************************************************/
478
479 bool VesCommonHeader::prepare(cJSON *commonHeader, \
480                               VesEventType type)
481 {
482    bool ret=true;
483    string typeStr;
484    string evntId;
485    string evntName;
486    mEventType = type;
487    seqNo=0;
488    nextSequenceNo(); //update the sequence number for next message
489    //local utility variables:
490    time_t intervalStartTime = getCurrentTime();
491    time_t intervalEndTime = intervalStartTime+60; /*adding 1 min to system time*/
492    uint64_t startEpochTime = getEpochTime();
493    mLastEpochTime = startEpochTime+60*100000; /*adding 1 min to epoch time*/
494
495    if(JsonHelper::addNodeToObject(commonHeader, "domain", \
496                                   getEventTypeToStr().c_str()) == 0) {
497       ret=false;
498    }
499    else if ( JsonHelper::addNodeToObject(commonHeader, "eventId", \
500                                          getEventId().c_str()) == 0)
501    {
502       ret =false;
503    }
504    else if(JsonHelper::addNodeToObject(commonHeader, "eventName", \
505                                        getEventName().c_str()) == 0)
506    {
507       ret = false;
508    }
509    else if(JsonHelper::addNodeToObject(commonHeader, "eventType", \
510                                        getEventType().c_str()) == 0)
511    {
512       ret = false;
513    }
514
515    if (mEventType == VesEventType::PM_SLICE)
516    {
517       cJSON *internalHeaderFields = JsonHelper::createNode();
518       if(internalHeaderFields == 0)
519       {
520          ret = false;
521       }
522       else if(JsonHelper::addJsonNodeToObject(commonHeader, "internalHeaderFields", \
523                                           internalHeaderFields) == 0)
524       {
525          ret = false;
526       }
527       else if(JsonHelper::addNodeToObject(internalHeaderFields, "intervalEndTime", \
528                                           formatTime(intervalEndTime).c_str()) == 0)
529       {
530          ret = false;
531       }
532       else if(JsonHelper::addNodeToObject(internalHeaderFields, "intervalStartTime", \
533                                           formatTime(intervalStartTime).c_str()) == 0)
534       {
535          ret = false;
536       }
537       else if(JsonHelper::addNodeToObject(commonHeader, "nfcNamingCode", \
538                                        "") == 0)
539       {
540          ret = false;
541       }
542       else if(JsonHelper::addNodeToObject(commonHeader, "stndDefinedNamespace", \
543                                       "") == 0)
544       {
545          ret = false;
546       }
547    }
548    
549    if (mEventType == VesEventType::PNF_REGISTRATION)
550    {
551
552    }
553
554    if(JsonHelper::addNodeToObject(commonHeader, "sequence", \
555                                        getSequenceNo()) == 0)
556    {
557       ret = false;
558    }
559    else if(JsonHelper::addNodeToObject(commonHeader, "priority", \
560                                        getPriority().c_str()) == 0)
561    {
562       ret = false;
563    }
564    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityId", \
565                                        "") == 0)
566    {
567       ret = false;
568    }
569    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityName", \
570                                        getReportingEntityName().c_str()) == 0)
571    {
572       ret = false;
573    }
574    else if(JsonHelper::addNodeToObject(commonHeader, "sourceId", \
575                                       "") == 0)
576    {
577       ret = false;
578    }
579    else if(JsonHelper::addNodeToObject(commonHeader, "sourceName", \
580                                        getSourceName().c_str()) == 0)
581    {
582       ret = false;
583    }
584    else if(JsonHelper::addNodeToObject(commonHeader, "startEpochMicrosec", \
585                                       (double)startEpochTime) == 0)
586    {
587       ret = false;
588    }
589    else if(JsonHelper::addNodeToObject(commonHeader, "lastEpochMicrosec", \
590                                        (double)mLastEpochTime) == 0)
591    {
592       ret = false;
593    }
594    else if(JsonHelper::addNodeToObject(commonHeader, "nfNamingCode", \
595                                        (type==VesEventType::PNF_REGISTRATION) ? \
596                                        getNamingCode().c_str() : "") == 0)
597    {
598       ret = false;
599    }
600    else if(JsonHelper::addNodeToObject(commonHeader, "nfVendorName", \
601                                        "") == 0)
602    {
603       ret = false;
604    }
605    else if(JsonHelper::addNodeToObject(commonHeader, "timeZoneOffset", \
606                                       TIME_ZONE_00_00) == 0)
607    {
608       ret = false;
609    }
610
611    if (mEventType == VesEventType::PM_SLICE)
612    {
613       if(JsonHelper::addNodeToObject(commonHeader, "version", \
614                                           VERSION_4_1) == 0)
615       {
616          ret = false;
617       }
618    }
619    else 
620    {
621       if(JsonHelper::addNodeToObject(commonHeader, "version", \
622                                           VERSION_4_0_1) == 0)
623       {
624          ret = false;
625       }
626    }
627
628    if(JsonHelper::addNodeToObject(commonHeader, "vesEventListenerVersion", \
629                                        VES_EVENT_LISTENER_7_2_1) == 0)
630    {
631       ret = false;
632    }
633    else
634    {
635       O1_LOG("\nO1 VesCommonHeader : VES common Header prepared");
636    }
637    return ret;
638 }
639
640 /**********************************************************************
641   End of file
642  **********************************************************************/