Cell down alarm notification [Issue-Id: ODUHIGH-430]
[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 #include "VesUtils.hpp"
31
32 static double seqNo = 0;
33
34 #define MAX_TIME_STR 35
35
36 /*******************************************************************
37  *
38  * @brief provide next sequence number of VES event
39  *
40  * @details
41  *
42  *    Function : nextSequenceNo
43  *
44  *    Functionality:
45  *      - provide next sequence number of VES event
46  *
47  * @params[in] void
48  * @return next sequence number of event  - success
49  *
50  * ****************************************************************/
51
52 double VesCommonHeader::nextSequenceNo()
53 {
54    return seqNo++;
55 }
56
57 /*******************************************************************
58  *
59  * @brief provide current sequence number of VES event
60  *
61  * @details
62  *
63  *    Function : getSequenceNo
64  *
65  *    Functionality:
66  *      - provide current sequence number of VES event
67  *
68  * @params[in] void
69  * @return current sequence number of event  - success
70  *
71  * ****************************************************************/
72
73 double VesCommonHeader::getSequenceNo()
74 {
75    return seqNo;
76 }
77
78 /*******************************************************************
79  *
80  * @brief Convert Ves Event type to string
81  *
82  * @details
83  *
84  *    Function : getEventTypeToStr
85  *
86  *    Functionality:
87  *      - Convert Ves Event type to String
88  *
89  * @params[in] IN - void
90  * @return value of string     - success
91  *         empty string        - failure
92  *
93  * ****************************************************************/
94
95 string VesCommonHeader::getEventTypeToStr()
96 {
97    string str = "";
98    switch(mEventType)
99    {
100       case VesEventType::PNF_REGISTRATION:
101          str = "pnfRegistration";
102          break;
103       case VesEventType::FAULT_NOTIFICATION:
104          #ifdef StdDef
105          str = "stndDefined";
106          #else
107          str = "fault";
108          #endif
109          break;
110       case VesEventType::PM_NOTIFICATION:
111          str = "pmNotification";
112          break;
113       case VesEventType::PM_SLICE:
114          str = "measurement";
115          break;
116       case VesEventType::HEARTBEAT:
117          str = "heartbeat";
118          break;
119       default:
120          O1_LOG("\nO1 VesCommonHeader : VES msg Type not supported");
121          break;
122    }
123    return str;
124 }
125
126 /*******************************************************************
127  *
128  * @brief create Ves Event Id from Ves Event type
129  *
130  * @details
131  *
132  *    Function : getEventId
133  *
134  *    Functionality:
135  *      - create Ves Event Id from Ves Event type
136  *
137  * @params[in] IN - void
138  * @return value of string     - success
139  *         empty string        - failure
140  *
141  * ****************************************************************/
142
143 string VesCommonHeader::getEventId()
144 {
145   /*Currently PNF_REGISTRATION only supported. This function must be updated
146     in later releases*/
147    std::ostringstream oss;
148    oss << mLastEpochTime;
149    string stringEpochTime = oss.str().substr(0, 10);
150    string evntId = "";
151    switch(mEventType)
152    {
153       case VesEventType::PNF_REGISTRATION:
154          evntId = getSourceName() + "_" + MODEL_NUMBER_007_DEV;
155          break;
156       case VesEventType::HEARTBEAT:
157          evntId = getEventTypeToStr() + "_" + formatTime(getCurrentTime());
158          break;
159       case VesEventType::PM_SLICE:
160          evntId = "_" + stringEpochTime + "_" + "PM1min";
161      break;
162       case VesEventType::FAULT_NOTIFICATION:
163          #ifdef StdDef
164          evntId = FAULT_EVENTID;
165          #else
166          evntId ="O_RAN_COMPONENT_Alarms-61";
167          #endif
168      break;
169       default:
170          O1_LOG("\nO1 VesCommonHeader : this VES msg Type support in getEventId is \
171 not available");
172          break;
173    }
174    return evntId;
175 }
176
177 /*******************************************************************
178  *
179  * @brief create Ves Event Type from Ves Event type
180  *
181  * @details
182  *
183  *    Function : getEventType
184  *
185  *    Functionality:
186  *      - create Ves Event Type from Ves Event type
187  *
188  * @params[in] IN - void
189  * @return value of string     - success
190  *         empty string        - failure
191  *
192  * ****************************************************************/
193
194 string VesCommonHeader::getEventType()
195 {
196   /*Currently PNF_REGISTRATION only supported. This function must be updated
197     in later releases*/
198
199    string evntType = "";
200    switch(mEventType)
201    {
202       case VesEventType::PNF_REGISTRATION:
203          evntType = EVENT_TYPE_5G;
204          break;
205       case VesEventType::HEARTBEAT:
206          evntType = EVENT_TYPE_ORAN_COMPONENT;
207          break;
208       case VesEventType::PM_SLICE:
209          evntType = EVENT_TYPE_ORAN_COMPONENT_PM;
210          break;
211       case VesEventType::FAULT_NOTIFICATION:
212          evntType = FAULT_TYPE;
213          break;
214       default:
215          O1_LOG("\nO1 VesCommonHeader : this VES msg Type support in getEvenType is \
216 not available");
217          break;
218    }
219    return evntType;
220 }
221
222 /*******************************************************************
223  *
224  * @brief return priority of event
225  *
226  * @details
227  *
228  *    Function : getPriority
229  *
230  *    Functionality:
231  *      - send priority of event
232  *
233  * @params[in] IN - void
234  * @return value of string     - success
235  *         empty string        - failure
236  *
237  * ****************************************************************/
238
239 string VesCommonHeader::getPriority()
240 {
241   /*Currently PNF_REGISTRATION only supported. This function must be updated
242     in later releases*/
243
244    string evntId = "";
245    switch(mEventType)
246    {
247       case VesEventType::PNF_REGISTRATION:
248          evntId = PRIORITY_LOW ;
249          break;
250       case VesEventType::HEARTBEAT:
251          evntId = PRIORITY_LOW;
252          break;
253       case VesEventType::PM_SLICE:
254          evntId = PRIORITY_LOW ;
255          break;
256       case VesEventType::FAULT_NOTIFICATION:
257          evntId = PRIORITY_LOW ;
258          break;
259       default:
260          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in getPriority is \
261 not available");
262          break;
263    }
264    return evntId;
265 }
266
267
268
269 /*******************************************************************
270  *
271  * @brief create Ves Event Name from Ves Event type
272  *
273  * @details
274  *
275  *    Function : getEventName
276  *
277  *    Functionality:
278  *      - create Ves Event Name from Ves Event type
279  *
280  * @params[in] IN - void
281  * @return value of string     - success
282  *         empty string        - failure
283  * ****************************************************************/
284
285 string VesCommonHeader::getEventName()
286 {
287   /*Currently PNF_REGISTRATION only supported. This function must be updated
288     in later releases*/
289
290    string evntName = "";
291    switch(mEventType)
292    {
293       case VesEventType::PNF_REGISTRATION:
294          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_5G;
295          break;
296       case VesEventType::HEARTBEAT:
297          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_ORAN_COMPONENT;
298          break;
299       case VesEventType::PM_SLICE:
300          evntName = getEventTypeToStr() + "_" + EVENT_TYPE_ORAN_COMPONENT_PM;
301          break;
302       case VesEventType::FAULT_NOTIFICATION:
303          #ifdef StdDef
304          evntName = FAULT_EVENT_NAME;
305          #else
306          evntName ="O_RAN_COMPONENT_Alarms";
307          #endif
308          break;
309       default:
310          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in getEventName is \
311          not available");
312          break;
313    }
314    return evntName;
315 }
316
317 /*******************************************************************
318  *
319  * @brief create Ves Event Name from Ves Event type
320  *
321  * @details
322  *
323  *    Function : getReportingEntityId
324  *
325  *    Functionality:
326  *      - create Ves Event Name from Ves Event type
327  *
328  * @params[in] IN - void
329  * @return value of string     - success
330  *         empty string        - failure
331  * ****************************************************************/
332
333 string VesCommonHeader::getReportingEntityId()
334 {
335   /*Currently PNF_REGISTRATION and PM_SLICE are only supported.
336    This function must be updated in later releases*/
337
338    string evntName = "";
339    switch(mEventType)
340    {
341       case VesEventType::PNF_REGISTRATION:
342          evntName = ODU_HIGH;
343          break;
344
345       case VesEventType::FAULT_NOTIFICATION:
346          evntName = ODU_HIGH;
347          break;
348       default:
349          break;
350    }
351    return evntName;
352 }
353
354 /*******************************************************************
355  *
356  * @brief create Reporting Entity Name from Ves Event type
357  *
358  * @details
359  *
360  *    Function : getReportingEntityName
361  *
362  *    Functionality:
363  *      - create Reporting Entity Name from Ves Event type
364  *
365  * @params[in] IN - void
366  * @return value of string     - success
367  *         empty string        - failure
368  * ****************************************************************/
369
370 string VesCommonHeader::getReportingEntityName()
371 {
372    string evntName = "";
373    switch(mEventType)
374    {
375       case VesEventType::PNF_REGISTRATION:
376          evntName = getSourceName();
377          break;
378       case VesEventType::PM_SLICE:
379          evntName = PM_REPORTING_ENTITY;
380          break;
381       case VesEventType::FAULT_NOTIFICATION:
382          evntName = ODU_HIGH;
383          break;
384       default:
385          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in \
386          getReportingEntityName is not available");
387          break;
388    }
389    return evntName;
390 }
391
392 /*******************************************************************
393  *
394  * @brief create Source Name from Ves Event type
395  *
396  * @details
397  *
398  *    Function : getSourceName
399  *
400  *    Functionality:
401  *      - create Source Name from Ves Event type
402  *
403  * @params[in] IN - void
404  * @return value of string     - success
405  *         empty string        - failure
406  * ****************************************************************/
407
408 string VesCommonHeader::getSourceName()
409 {
410    string sourceName = "";
411    switch(mEventType)
412    
413    {
414       case VesEventType::PNF_REGISTRATION:
415          sourceName = ODU_HIGH;
416          break;
417       case VesEventType::FAULT_NOTIFICATION:
418          sourceName = ODU_HIGH;
419          break;
420       default:
421          break;
422    }
423    return sourceName;
424 }
425
426 /*******************************************************************
427  *
428  * @brief create Ves Event SourceId from Ves Event type
429  *
430  * @details
431  *
432  *    Function : getSourceId
433  *
434  *    Functionality:
435  *      - create Ves Event SourceId from Ves Event type
436  *
437  * @params[in] IN - void
438  * @return value of string     - success
439  *         empty string        - failure
440  * ****************************************************************/
441 string VesCommonHeader::getSourceId()
442 {
443    string sourceId = "";
444    switch(mEventType)
445    {
446       case VesEventType::FAULT_NOTIFICATION:
447          sourceId = SOURCE_ID;
448          break;
449       default:
450          break;
451    }
452    return sourceId;
453 }
454
455 /*******************************************************************
456  *
457  * @brief create Ves Event Name from Ves Event type
458  *
459  * @details
460  *
461  *    Function : getnfcNamingCode
462  *
463  *    Functionality: create Ves Event nfc naming code
464  *
465  * @params[in] IN - void
466  * @return value of string     - success
467  *         empty string        - failure
468  * ****************************************************************/
469
470 string VesCommonHeader::getnfcNamingCode()
471 {
472    string name = "";
473    switch(mEventType)
474    {
475       case VesEventType::FAULT_NOTIFICATION:
476          name = NFC_NAMING_CODE;
477          break;
478       default:
479          break;
480    }
481    return name;
482 }
483
484 /*******************************************************************
485  *
486  * @brief create Ves Event Name from Ves Event type
487  *
488  * @details
489  *
490  *    Function : getNamingCode
491  *
492  *    Functionality:
493  *      - Network function type: 4 characters (aligned with vnf and pnf
494            naming standards)
495  *
496  * @params[in] IN - void
497  * @return value of string     - success
498  *         empty string        - failure
499  * ****************************************************************/
500
501 string VesCommonHeader::getNamingCode()
502 {
503
504    string nammingcdoe = "";
505    switch(mEventType)
506    {
507       case VesEventType::PNF_REGISTRATION:
508          nammingcdoe = NAMING_CODE_ODU;
509          break;
510       case VesEventType::PM_SLICE:
511          break;
512       case VesEventType::FAULT_NOTIFICATION:
513          nammingcdoe = NAMING_CODE_ODU;
514          break;
515       default:
516          O1_LOG("\nO1 VesCommonHeader : This VES msg Type support in \
517          getReportingEntityName is not available");
518          break;
519    }
520    return nammingcdoe;
521 }
522
523
524
525 /*******************************************************************
526  *
527  * @brief provide current epoch time
528  *
529  * @details
530  *
531  *    Function : getEpochTime
532  *
533  *    Functionality:
534  *      - provide current epoch time
535  *
536  * @params[in] void
537  * @return epoch time     - success
538  *
539  * ****************************************************************/
540
541 double VesCommonHeader::getEpochTime()
542 {
543     double epochTimeInMicrosec = std::chrono::duration_cast \
544             <std::chrono::microseconds> \
545             (std::chrono::system_clock::now().time_since_epoch()).count();
546     return epochTimeInMicrosec;
547 }
548
549 /*******************************************************************
550  *
551  * @brief get current time
552  *
553  * @details
554  *
555  *    Function : getCurrentTime
556  *
557  *    Functionality:
558  *      - get current time
559  *
560  * @params[in] void
561  * @return time     - success
562  *
563  * ****************************************************************/
564
565 time_t VesCommonHeader::getCurrentTime()
566 {
567    time_t t;
568    time(&t);
569    return t;
570 }
571
572 /*******************************************************************
573  *
574  * @brief formats the current time like: 
575  *           "Thu, 14 Oct 2021 03:15:00 +0000"
576  *
577  * @details
578  *
579  *    Function : formatTime
580  *
581  *    Functionality:
582  *      - formats current time
583  *
584  * @params[in] void
585  * @return formatted time - success
586  *
587  * ****************************************************************/
588
589 std::string VesCommonHeader::formatTime(time_t t) {
590    std::ostringstream oss;
591    char dateStr[MAX_TIME_STR];
592    strftime(dateStr, sizeof(dateStr), "%a, %d %b %Y %T %z", gmtime(&t));
593    oss << dateStr;
594    return oss.str();
595 }
596
597
598 /*******************************************************************
599  *
600  * @brief create Ves stndDefinedNamespace from Ves Event type
601  *
602  * @details
603  *
604  *    Function : getstndDefinedNamespace
605  *
606  *    Functionality: create Ves tndDefinedNamespace
607  *
608  * @params[in] IN - void
609  * @return value of string     - success
610  *         empty string        - failure
611  * ****************************************************************/
612
613 string VesCommonHeader::getstndDefinedNamespace()
614 {
615    string stndDefinedNamespace="";
616    switch(mEventType)
617    {
618       case VesEventType::FAULT_NOTIFICATION:
619          stndDefinedNamespace = STND_DEFINED_NAMESPACE;
620          break;
621       default:
622          break;
623    }
624    return stndDefinedNamespace;
625
626 }
627
628 /*******************************************************************
629  *
630  * @brief Prepare VES Message
631  *
632  * @details
633  *
634  *    Function : prepare
635  *
636  *    Functionality:
637  *      - prepare VES event
638  *
639  * @params[in] void
640  * @return true     - success
641  *         false    - failure
642  *
643  * ****************************************************************/
644
645
646 bool VesCommonHeader::prepare(cJSON *commonHeader, \
647                               VesEventType type)
648 {
649    bool ret=true;
650    string typeStr;
651    string evntId;
652    string evntName;
653    mEventType = type;
654    seqNo=0;
655    nextSequenceNo(); //update the sequence number for next message
656    //local utility variables:
657    time_t intervalStartTime = getCurrentTime();
658    time_t intervalEndTime = intervalStartTime+60; /*adding 1 min to system time*/
659    double startEpochTime = getEpochTime();
660    mLastEpochTime = startEpochTime+60*100000; /*adding 1 min to epoch time*/
661
662    if(JsonHelper::addNodeToObject(commonHeader, "domain", \
663                                   getEventTypeToStr().c_str()) == 0) {
664       ret=false;
665    }
666    else if ( JsonHelper::addNodeToObject(commonHeader, "eventId", \
667                                          getEventId().c_str()) == 0)
668    {
669       ret =false;
670    }
671    else if(JsonHelper::addNodeToObject(commonHeader, "eventName", \
672                                        getEventName().c_str()) == 0)
673    {
674       ret = false;
675    }
676    else if(JsonHelper::addNodeToObject(commonHeader, "eventType", \
677                                        getEventType().c_str()) == 0)
678    {
679       ret = false;
680    }
681
682    if (mEventType == VesEventType::PM_SLICE)
683    {
684       cJSON *internalHeaderFields = JsonHelper::createNode();
685       if(internalHeaderFields == 0)
686       {
687          ret = false;
688       }
689       else if(JsonHelper::addJsonNodeToObject(commonHeader, "internalHeaderFields", \
690                                           internalHeaderFields) == 0)
691       {
692          ret = false;
693       }
694       else if(JsonHelper::addNodeToObject(internalHeaderFields, "intervalEndTime", \
695                                           formatTime(intervalEndTime).c_str()) == 0)
696       {
697          ret = false;
698       }
699       else if(JsonHelper::addNodeToObject(internalHeaderFields, "intervalStartTime", \
700                                           formatTime(intervalStartTime).c_str()) == 0)
701       {
702          ret = false;
703       }
704    }
705
706    if (mEventType == VesEventType::PNF_REGISTRATION)
707    {
708
709    }
710
711    if(JsonHelper::addNodeToObject(commonHeader, "sequence", \
712                                        getSequenceNo()) == 0)
713    {
714       ret = false;
715    }
716    else if(JsonHelper::addNodeToObject(commonHeader, "priority", \
717                                        getPriority().c_str()) == 0)
718    {
719       ret = false;
720    }
721    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityId", \
722                                        getReportingEntityId().c_str() ) == 0)
723    {
724       ret = false;
725    }
726    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityName", \
727                                        getReportingEntityName().c_str()) == 0)
728    {
729       ret = false;
730    }
731    else if(JsonHelper::addNodeToObject(commonHeader, "sourceId", \
732                                       getSourceId().c_str() ) == 0)
733    {
734       ret = false;
735    }
736    else if(JsonHelper::addNodeToObject(commonHeader, "sourceName", \
737                                        getSourceName().c_str()) == 0)
738    {
739       ret = false;
740    }
741    else if(JsonHelper::addNodeToObject(commonHeader, "startEpochMicrosec", \
742                                       startEpochTime) == 0)
743    {
744       ret = false;
745    }
746    else if(JsonHelper::addNodeToObject(commonHeader, "lastEpochMicrosec", \
747                                       mLastEpochTime) == 0)
748    {
749       ret = false;
750    }
751    else if(JsonHelper::addNodeToObject(commonHeader, "nfNamingCode", \
752                                        getNamingCode().c_str() ) == 0)
753    {
754       ret = false;
755    }
756    else if(JsonHelper::addNodeToObject(commonHeader, "nfVendorName", \
757                                        "POC") == 0)
758    {
759       ret = false;
760    }
761    else if(JsonHelper::addNodeToObject(commonHeader, "nfcNamingCode", \
762                                        getnfcNamingCode().c_str() ) == 0)
763    {
764       ret = false;
765    }
766    else if(JsonHelper::addNodeToObject(commonHeader, "timeZoneOffset", \
767                                       TIME_ZONE_00_00) == 0)
768    {
769       ret = false;
770    }
771
772    if (mEventType == VesEventType::PM_SLICE)
773    {
774       if(JsonHelper::addNodeToObject(commonHeader, "version", \
775                                           VERSION_4_1) == 0)
776       {
777          ret = false;
778       }
779    }
780    else
781    {
782       if(JsonHelper::addNodeToObject(commonHeader, "version", \
783                                           COMMON_HEADER_VERSION) == 0)
784       {
785          ret = false;
786       }
787    }
788    if(JsonHelper::addNodeToObject(commonHeader, "stndDefinedNamespace", \
789                                        getstndDefinedNamespace().c_str())== 0)            
790    {
791       ret = false;
792    }
793    if(JsonHelper::addNodeToObject(commonHeader, "vesEventListenerVersion", \
794                                        VES_EVENT_LISNERT_VERSION) == 0)
795    {
796       ret = false;
797    }
798    else
799    {
800       O1_LOG("\nO1 VesCommonHeader : VES common Header prepared");
801    }
802    return ret;
803 }
804
805 /**********************************************************************
806   End of file
807  **********************************************************************/