PNF Registration to be sent after odu stack is up
[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("\nO1 VesCommonHeader : VES msg Type not supported");
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("\nO1 VesCommonHeader : this VES msg Type support in getEventId is \
148 not available");
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("\nO1 VesCommonHeader : this VES msg Type support in getEvenType is \
188 not available");
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("\nO1 VesCommonHeader : This VES msg Type support in getPriority is \
227 not available");
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("\nO1 VesCommonHeader : This VES msg Type support in getEventName is \
267 not available");
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("\nO1 VesCommonHeader : This VES msg Type support in \
302 getReportingEntityName is not available");
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    bool ret=true;
428    string typeStr;
429    string evntId;
430    string evntName;
431    mEventType = type;
432    seqNo=0;
433    nextSequenceNo(); //update the sequence number for next message
434    if(JsonHelper::addNodeToObject(commonHeader, "domain", \
435        getEventTypeToStr().c_str()) == 0) {
436       ret=false;
437    }
438    else if ( JsonHelper::addNodeToObject(commonHeader, "eventId", \
439                                          getEventId().c_str()) == 0)
440    {
441       ret =false;
442    }
443    else if(JsonHelper::addNodeToObject(commonHeader, "eventName", \
444                                        getEventName().c_str()) == 0)
445    {
446       ret = false;
447    }
448    else if(JsonHelper::addNodeToObject(commonHeader, "eventType", \
449                                        getEventType().c_str()) == 0)
450    {
451       ret = false;
452    }
453    else if(JsonHelper::addNodeToObject(commonHeader, "sequence", \
454                                        getSequenceNo()) == 0)
455    {
456       ret = false;
457    }
458    else if(JsonHelper::addNodeToObject(commonHeader, "priority", \
459                                        getPriority().c_str()) == 0)
460    {
461       ret = false;
462    }
463    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityId", \
464                                        "") == 0)
465    {
466       ret = false;
467    }
468    else if(JsonHelper::addNodeToObject(commonHeader, "reportingEntityName", \
469                                        getReportingEntityName().c_str()) == 0)
470    {
471       ret = false;
472    }
473   else if(JsonHelper::addNodeToObject(commonHeader, "sourceId", \
474                                       "") == 0)
475    {
476       ret = false;
477    }
478    else if(JsonHelper::addNodeToObject(commonHeader, "sourceName", \
479                                        getSourceName().c_str()) == 0)
480    {
481       ret = false;
482    }
483   else if(JsonHelper::addNodeToObject(commonHeader, "startEpochMicrosec", \
484                                       (double) getEpochTime()) == 0)
485    {
486       ret = false;
487    }
488    else if(JsonHelper::addNodeToObject(commonHeader, "lastEpochMicrosec", \
489                                        (double) getEpochTime()) == 0)
490    {
491       ret = false;
492    }
493    else if(JsonHelper::addNodeToObject(commonHeader, "nfNamingCode", \
494                                        getNamingCode().c_str()) == 0)
495    {
496       ret = false;
497    }
498    else if(JsonHelper::addNodeToObject(commonHeader, "nfVendorName", \
499                                        "") == 0)
500    {
501       ret = false;
502    }
503   else if(JsonHelper::addNodeToObject(commonHeader, "timeZoneOffset", \
504                                       TIME_ZONE_00_00) == 0)
505    {
506       ret = false;
507    }
508    else if(JsonHelper::addNodeToObject(commonHeader, "version", \
509                                        VERSION_4_0_1) == 0)
510    {
511       ret = false;
512    }
513    else if(JsonHelper::addNodeToObject(commonHeader, "vesEventListenerVersion", \
514                                        VES_EVENT_LISTENER_7_2_1) == 0)
515    {
516       ret = false;
517    }
518    else
519    {
520       O1_LOG("\nO1 VesCommonHeader : VES common Header prepared");
521    }
522    return ret;
523
524 }
525
526 /**********************************************************************
527   End of file
528  **********************************************************************/