VES PM data for slicing use case. [Issue-Id: ODUHIGH-384]
[o-du/l2.git] / src / o1 / ves / PnfRegistrationEvent.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 macros and functions to support the preparation of pnf
20    Registration VES Event*/
21
22 #include <ctime>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sstream>
26 #include "PnfRegistrationEvent.hpp"
27 #include "JsonHelper.hpp"
28 #include "VesUtils.hpp"
29
30 /* Default constructor*/
31 PnfRegistrationEvent::PnfRegistrationEvent()
32                : VesEvent(VesEventType::PNF_REGISTRATION)
33 {
34 }
35
36 /* Default Destructor*/
37 PnfRegistrationEvent::~PnfRegistrationEvent()
38 {
39 }
40
41  
42 /*******************************************************************
43  *
44  * @brief get today's date in YYYY-MM-DD format
45  *
46  * @details
47  *
48  *    Function : getCurrentDate
49  *
50  *    Functionality:
51  *      - get today's date in YYYY-MM-DD format
52  *
53  * @params[in] OUT - date string pointer
54  * @return true     - success
55  *         false    - failure
56  *
57  * ****************************************************************/
58
59 string PnfRegistrationEvent::getCurrentDate()
60 {
61    time_t t = time(0);
62    char dateStr[MAX_TIME_STR];
63    strftime(dateStr, MAX_TIME_STR, "%F", localtime(&t));
64    std::ostringstream oss;
65    oss<< dateStr;
66    return oss.str();
67 }
68
69 /*******************************************************************
70  *
71  * @brief provide mac address of netconf server
72  *
73  * @details
74  *
75  *    Function : getMacAddr
76  *
77  *    Functionality:
78  *      - provide mac address of netconf server
79  *
80  * @params[in] void
81  * @return mac address as string - success
82  *
83  * ****************************************************************/
84
85 string PnfRegistrationEvent::getNetconfMacAddr()
86 {
87    if(mNetconfMacAddr != "") {
88       return mNetconfMacAddr;
89    }
90    else {
91       O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf Mac Address");
92       return "";
93    }
94 }
95
96
97 /*******************************************************************
98  *
99  * @brief provide ipv4 address of netconf server
100  *
101  * @details
102  *
103  *    Function : getNetconfV4ServerIP
104  *
105  *    Functionality:
106  *      - provide ipv4 address of netconf server
107  *
108  * @params[in] void
109  * @return ipv4 address as string - success
110  *
111  * ****************************************************************/
112
113 string PnfRegistrationEvent::getNetconfV4ServerIP()
114 {
115    if(mNetconfIpv4 != "") {
116       return mNetconfIpv4;
117    }
118    else {
119       O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf IPv4 ip");
120       return "";
121    }
122 }
123
124 /*******************************************************************
125  *
126  * @brief provides Netconf Server Port number
127  *
128  * @details
129  *
130  *    Function : getNetconfPort
131  *
132  *    Functionality:
133  *      - provides Netconf Server Port number
134  *      - provide default port if not provided
135  *
136  * @params[in] void
137  * @return port number - success
138  *
139  * ****************************************************************/
140
141 string PnfRegistrationEvent::getNetconfPort()
142 {
143    if(mNetconfPort != "") {
144       return mNetconfPort;
145    }
146    else {
147       O1_LOG("\nO1 PnfRegistrationEvent : Could not get Netconf Port number");
148       return NETCONF_DEFAULT_PORT;
149    }
150 }
151
152 /*******************************************************************
153  *
154  *
155  * @brief provides Netconf Server Username
156  *
157  * @details
158  *
159  *    Function : getUsername
160  *
161  *    Functionality:
162  *      - provides Netconf Server Username
163  *
164  * @params[in] void
165  * @return port number - success
166  *
167  * ****************************************************************/
168
169
170 string PnfRegistrationEvent::getUsername()
171 {
172    if(mNetconfUsername != "") {
173       return mNetconfUsername;
174    }
175    else {
176       O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf username");
177       return "";
178    }
179 }
180
181 /*******************************************************************
182  *
183  *
184  * @brief provides Netconf Server Password
185  *
186  * @details
187  *
188  *    Function : getPassword
189  *
190  *    Functionality:
191  *      - provides Netconf Server password
192  *
193  * @params[in] void
194  * @return port number - success
195  *
196  * ****************************************************************/
197
198 string PnfRegistrationEvent::getPassword()
199 {
200    if(mNetconfPassword != "") {
201       return mNetconfPassword;
202    }
203    else {
204       O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf password");
205       return "";
206    }
207 }
208
209 /*******************************************************************
210  *
211  * @brief provides ipv6 address of netconf server
212  *
213  * @details
214  *
215  *    Function : getNetconfV6ServerIP
216  *
217  *    Functionality:
218  *      - provides ipv6 address of netconf server
219  *
220  * @params[in] void
221  * @return ipv6 address as string - success
222  *
223  * ****************************************************************/
224
225 string PnfRegistrationEvent::getNetconfV6ServerIP()
226 {
227    if(mNetconfIpv6 != "") {
228       return mNetconfIpv6;
229    }
230    else {
231       O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf IPv6 ip");
232       return "";
233    }
234 }
235
236 /*******************************************************************
237  *
238  * @brief provide Serial Number
239  *
240  * @details
241  *
242  *    Function : getSerialNumber
243  *
244  *    Functionality:
245  *      - provide  Serial Number
246  *
247  * @params[in] void
248  * @return serial number - success
249  *         empty string  - failure
250  *
251  * ****************************************************************/
252
253 string PnfRegistrationEvent::getSerialNumber()
254 {
255       string serialNum;
256       serialNum.append(VENDER_NAME_VENDORB).append("-").append(UNIT_TYPE_7DEV);
257       serialNum.append("-").append(getNetconfV4ServerIP()).append("-");
258       serialNum.append(MODEL_NUMBER_007_DEV);
259       //string serialNum = VENDER_NAME_VENDORB + "-" + UNIT_TYPE_7DEV + "-" \
260                          + getNetconfV4ServerIP() + "-" + MODEL_NUMBER_007_DEV;
261    return serialNum;
262 }
263
264 /*******************************************************************
265  *
266  * @brief provide Unit Family
267  *
268  * @details
269  *
270  *    Function : getUnitFamily
271  *
272  *    Functionality:
273  *      - provide  Unit Family
274  *
275  * @params[in] void
276  * @return serial number - success
277  *         empty string  - failure
278  *
279  * ****************************************************************/
280
281 string PnfRegistrationEvent::getUnitFamily()
282 {
283    string unitFamily;
284    unitFamily.append(VENDER_NAME_VENDORB).append("-").append(UNIT_TYPE_7DEV);
285    return unitFamily;
286 }
287
288
289 /*******************************************************************
290  *
291  * @brief prepare PNF registration Fields
292  *
293  * @details
294  *
295  *    Function : preparePnfRegistrationEventFields
296  *
297  *    Functionality:
298  *      - prepare PNF registration Fields in json format
299  *
300  * @params[in] IN   - pointer to pnfFields 
301  * @return true     - success
302  *         false    - failure
303  *
304  * ****************************************************************/
305
306 bool PnfRegistrationEvent::prepareEventFields(const Message* msg)
307 {
308    bool ret = true;
309    cJSON* pnfFields = this->mVesEventFields;
310    
311    getNetconfConfig();
312    
313    if(JsonHelper::addNodeToObject(pnfFields, "pnfRegistrationFieldsVersion", \
314                                        PNF_REGISTRATION_VERSION_2_1) == 0) {
315       ret = false;
316    }
317    else if(JsonHelper::addNodeToObject(pnfFields, \
318                        "lastServiceDate", getCurrentDate().c_str()) == 0) {
319       ret = false;
320    }
321    else if(JsonHelper::addNodeToObject(pnfFields, "macAddress", \
322                                        getNetconfMacAddr().c_str()) == 0) {
323       ret = false;
324    }
325    if(JsonHelper::addNodeToObject(pnfFields, \
326                   "manufactureDate", getCurrentDate().c_str()) == 0) {
327       ret = false;
328    }
329    else if(JsonHelper::addNodeToObject(pnfFields, "modelNumber", \
330                                        MODEL_NUMBER_007_DEV) == 0) {
331       ret = false;
332    }
333    else if(JsonHelper::addNodeToObject(pnfFields, "oamV4IpAddress", \
334                                        getNetconfV4ServerIP().c_str()) == 0) {
335       ret = false;
336    }
337    else if(getNetconfV6ServerIP() != "" && (JsonHelper::addNodeToObject( \
338                                        pnfFields, "oamV6IpAddress", \
339                                        getNetconfV6ServerIP().c_str()) == 0)) {
340          ret = false;
341    }
342    else if(JsonHelper::addNodeToObject(pnfFields, "serialNumber", \
343                                        getSerialNumber().c_str()) == 0) {
344       ret = false;
345    }
346    else if(JsonHelper::addNodeToObject(pnfFields, "softwareVersion", \
347                                        SOFTWARE_VERSION_2_3_5) == 0) {
348       ret = false;
349    }
350    else if(JsonHelper::addNodeToObject(pnfFields, "unitFamily", \
351                                        getUnitFamily().c_str()) == 0) {
352       ret = false;
353    }
354    else if(JsonHelper::addNodeToObject(pnfFields, "unitType", \
355                                        UNIT_TYPE_7DEV) == 0) {
356       ret = false;
357    }
358    else if(JsonHelper::addNodeToObject(pnfFields, "vendorName", \
359                                        VENDER_NAME_VENDORB) == 0) {
360       ret = false;
361    }
362    else
363    {
364       cJSON *addFields = JsonHelper::createNode();
365       if(addFields == 0) {
366          O1_LOG("\nO1 PnfRegistrationEvent : could not create additional fields JSON object");
367          return false;
368       }
369
370       if(prepareAdditionalFields(addFields))
371       {
372          if(addFields == 0) {
373             O1_LOG("\nO1 PnfRegistrationEvent : could not prepare additional fields cJSON object");
374             JsonHelper::deleteNode(pnfFields);
375             return false;
376          }
377
378          if(JsonHelper::addJsonNodeToObject(pnfFields, "additionalFields", \
379                                addFields) == 0) {
380             O1_LOG("\nO1 PnfRegistrationEvent : could not add additional fields");
381             JsonHelper::deleteNode(pnfFields);
382             return false;
383          }
384       }
385       O1_LOG("\nO1 PnfRegistrationEvent : Event fields prepared for PNF registration");
386    }
387    return ret;
388 }
389
390 /*******************************************************************
391  *
392  * @brief prepare PNF registration additional Fields
393  *
394  * @details
395  *
396  *    Function : prepareAdditionalFields
397  *
398  *    Functionality:
399  *      - prepare PNF registration additional Fields in json formate
400  *
401  * @params[in] IN   - pointer to addField
402  * @return true     - success
403  *         false    - failure
404  *
405  * ****************************************************************/
406
407 bool PnfRegistrationEvent::prepareAdditionalFields(cJSON *addFields)
408 {
409    bool ret = true;
410    if(JsonHelper::addNodeToObject(addFields, "oamPort", getNetconfPort().c_str()) == 0) {
411       ret = false;
412    }
413    else if(JsonHelper::addNodeToObject(addFields, "protocol", \
414                                        NETCONF_PROTOCOL_SSH) == 0) {
415       ret = false;
416    }
417
418    else if(JsonHelper::addNodeToObject(addFields, "username", \
419                                        getUsername().c_str()) == 0) {
420       ret = false;
421    }
422    else if(JsonHelper::addNodeToObject(addFields, "password", \
423                                        getPassword().c_str()) == 0) {
424       ret = false;
425    }
426    else if(JsonHelper::addNodeToObject(addFields, "reconnectOnChangedSchema", \
427                                        RECONNECT_ON_SCHEMA_CHANGE_FALSE) == 0) {
428       ret = false;
429    }
430    else if(JsonHelper::addNodeToObject(addFields, "sleep-factor", \
431                                        SLEEP_FACTOR_1_5) == 0) {
432       ret = false;
433    }
434    else if(JsonHelper::addNodeToObject(addFields, "tcpOnly", \
435                                        TCP_ONLY_FALSE) == 0) {
436       ret = false;
437    }
438    else if(JsonHelper::addNodeToObject(addFields, "connectionTimeout", \
439                                        CONNECTION_TIMEOUT_20000) == 0) {
440       ret = false;
441    }
442    else if(JsonHelper::addNodeToObject(addFields, "maxConnectionAttempts", \
443                                        MAX_CONNECTION_ATTEMPTS_100) == 0) {
444       ret = false;
445    }
446    else if(JsonHelper::addNodeToObject(addFields, "betweenAttemptsTimeout", \
447                                        BETWEEN_ATTEMPTS_TIMEOUT_2000) == 0) {
448       ret = false;
449    }
450    else if(JsonHelper::addNodeToObject(addFields, "keepaliveDelay", \
451                                        KEEPALIVE_DELAY_120) == 0) {
452       ret = false;
453    }
454    O1_LOG("\nO1 PnfRegistrationEvent : Additional fields prepared for PNF registration");
455    return ret;
456 }
457 /*******************************************************************
458  *
459  * @brief Read json file
460  *
461  * @details
462  *
463  *    Function : readConfigFile
464  *
465  *    Functionality:
466  *      - Read json file
467  *
468  *
469  * @params[in] void
470  * @return true  : success
471  *         false : failure
472  ******************************************************************/
473
474 void PnfRegistrationEvent::getNetconfConfig()
475 {
476    mNetconfMacAddr = ConfigLoader::instance().getNetconfConfigFile().getNetconfMacAddr();
477    mNetconfIpv4 = ConfigLoader::instance().getNetconfConfigFile().getNetconfIpv4();
478    mNetconfIpv6 = ConfigLoader::instance().getNetconfConfigFile().getNetconfIpv6();
479    mNetconfPort = ConfigLoader::instance().getNetconfConfigFile().getNetconfPort();
480    mNetconfUsername =ConfigLoader::instance().getNetconfConfigFile().getNetconfUsername();
481    mNetconfPassword = ConfigLoader::instance().getNetconfConfigFile().getNetconfPassword();
482 }
483
484
485 /**********************************************************************
486   End of file
487  **********************************************************************/