PNF Registration to be sent after odu stack is up
[o-du/l2.git] / src / o1 / ves / PnfRegistration.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 "PnfRegistration.hpp"
27 #include "JsonHelper.hpp"
28 #include "VesUtils.hpp"
29
30 /* Default constructor*/
31 PnfRegistration::PnfRegistration()
32 {
33    this->mVesEventType = VesEventType::PNF_REGISTRATION;
34 }
35
36 /* Default Destructor*/
37 PnfRegistration::~PnfRegistration()
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 PnfRegistration::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 PnfRegistration::getNetconfMacAddr()
86 {
87    if(mNetconfMacAddr != "") {
88       return mNetconfMacAddr;
89    }
90    else {
91       O1_LOG("\nO1 PnfRegistration : 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 PnfRegistration::getNetconfV4ServerIP()
114 {
115    if(mNetconfIpv4 != "") {
116       return mNetconfIpv4;
117    }
118    else {
119       O1_LOG("\nO1 PnfRegistration : 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 PnfRegistration::getNetconfPort()
142 {
143    if(mNetconfPort != "") {
144       return mNetconfPort;
145    }
146    else {
147       O1_LOG("\nO1 PnfRegistration : 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 PnfRegistration::getUsername()
171 {
172    if(mNetconfUsername != "") {
173       return mNetconfUsername;
174    }
175    else {
176       O1_LOG("\nO1 PnfRegistration : 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 PnfRegistration::getPassword()
199 {
200    if(mNetconfPassword != "") {
201       return mNetconfPassword;
202    }
203    else {
204       O1_LOG("\nO1 PnfRegistration : 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 PnfRegistration::getNetconfV6ServerIP()
226 {
227    if(mNetconfIpv6 != "") {
228       return mNetconfIpv6;
229    }
230    else {
231       O1_LOG("\nO1 PnfRegistration : 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 PnfRegistration::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 PnfRegistration::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 : preparePnfRegistrationFields
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 PnfRegistration::prepareEventFields()
307 {
308    bool ret = true;
309    cJSON* pnfFields = this->mVesEventFields;
310    if(!readConfigFile())
311    {
312       O1_LOG("\nO1 PnfRegistration : Failed to read config file");
313       return false;
314    }
315    if(JsonHelper::addNodeToObject(pnfFields, "pnfRegistrationFieldsVersion", \
316                                        PNF_REGISTRATION_VERSION_2_1) == 0) {
317       ret = false;
318    }
319    else if(JsonHelper::addNodeToObject(pnfFields, \
320                        "lastServiceDate", getCurrentDate().c_str()) == 0) {
321       ret = false;
322    }
323    else if(JsonHelper::addNodeToObject(pnfFields, "macAddress", \
324                                        getNetconfMacAddr().c_str()) == 0) {
325       ret = false;
326    }
327    if(JsonHelper::addNodeToObject(pnfFields, \
328                   "manufactureDate", getCurrentDate().c_str()) == 0) {
329       ret = false;
330    }
331    else if(JsonHelper::addNodeToObject(pnfFields, "modelNumber", \
332                                        MODEL_NUMBER_007_DEV) == 0) {
333       ret = false;
334    }
335    else if(JsonHelper::addNodeToObject(pnfFields, "oamV4IpAddress", \
336                                        getNetconfV4ServerIP().c_str()) == 0) {
337       ret = false;
338    }
339    else if(getNetconfV6ServerIP() != "" && (JsonHelper::addNodeToObject( \
340                                        pnfFields, "oamV6IpAddress", \
341                                        getNetconfV6ServerIP().c_str()) == 0)) {
342          ret = false;
343    }
344    else if(JsonHelper::addNodeToObject(pnfFields, "serialNumber", \
345                                        getSerialNumber().c_str()) == 0) {
346       ret = false;
347    }
348    else if(JsonHelper::addNodeToObject(pnfFields, "softwareVersion", \
349                                        SOFTWARE_VERSION_2_3_5) == 0) {
350       ret = false;
351    }
352    else if(JsonHelper::addNodeToObject(pnfFields, "unitFamily", \
353                                        getUnitFamily().c_str()) == 0) {
354       ret = false;
355    }
356    else if(JsonHelper::addNodeToObject(pnfFields, "unitType", \
357                                        UNIT_TYPE_7DEV) == 0) {
358       ret = false;
359    }
360    else if(JsonHelper::addNodeToObject(pnfFields, "vendorName", \
361                                        VENDER_NAME_VENDORB) == 0) {
362       ret = false;
363    }
364    else
365    {
366       cJSON *addFields = JsonHelper::createNode();
367       if(addFields == 0) {
368          O1_LOG("\nO1 PnfRegistration : could not create additional fields JSON object");
369          return false;
370       }
371
372       if(prepareAdditionalFields(addFields))
373       {
374          if(addFields == 0) {
375             O1_LOG("\nO1 PnfRegistration : could not prepare additional fields cJSON object");
376             JsonHelper::deleteNode(pnfFields);
377             return false;
378          }
379
380          if(JsonHelper::addJsonNodeToObject(pnfFields, "additionalFields", \
381                                addFields) == 0) {
382             O1_LOG("\nO1 PnfRegistration : could not add additional fields");
383             JsonHelper::deleteNode(pnfFields);
384             return false;
385          }
386       }
387       O1_LOG("\nO1 PnfRegistration : Event fields prepared for PNF registration");
388    }
389    return ret;
390 }
391
392 /*******************************************************************
393  *
394  * @brief prepare PNF registration additional Fields
395  *
396  * @details
397  *
398  *    Function : prepareAdditionalFields
399  *
400  *    Functionality:
401  *      - prepare PNF registration additional Fields in json formate
402  *
403  * @params[in] IN   - pointer to addField
404  * @return true     - success
405  *         false    - failure
406  *
407  * ****************************************************************/
408
409 bool PnfRegistration::prepareAdditionalFields(cJSON *addFields)
410 {
411    bool ret = true;
412    if(JsonHelper::addNodeToObject(addFields, "oamPort", getNetconfPort().c_str()) == 0) {
413       ret = false;
414    }
415    else if(JsonHelper::addNodeToObject(addFields, "protocol", \
416                                        NETCONF_PROTOCOL_SSH) == 0) {
417       ret = false;
418    }
419
420    else if(JsonHelper::addNodeToObject(addFields, "username", \
421                                        getUsername().c_str()) == 0) {
422       ret = false;
423    }
424    else if(JsonHelper::addNodeToObject(addFields, "password", \
425                                        getPassword().c_str()) == 0) {
426       ret = false;
427    }
428    else if(JsonHelper::addNodeToObject(addFields, "reconnectOnChangedSchema", \
429                                        RECONNECT_ON_SCHEMA_CHANGE_FALSE) == 0) {
430       ret = false;
431    }
432    else if(JsonHelper::addNodeToObject(addFields, "sleep-factor", \
433                                        SLEEP_FACTOR_1_5) == 0) {
434       ret = false;
435    }
436    else if(JsonHelper::addNodeToObject(addFields, "tcpOnly", \
437                                        TCP_ONLY_FALSE) == 0) {
438       ret = false;
439    }
440    else if(JsonHelper::addNodeToObject(addFields, "connectionTimeout", \
441                                        CONNECTION_TIMEOUT_20000) == 0) {
442       ret = false;
443    }
444    else if(JsonHelper::addNodeToObject(addFields, "maxConnectionAttempts", \
445                                        MAX_CONNECTION_ATTEMPTS_100) == 0) {
446       ret = false;
447    }
448    else if(JsonHelper::addNodeToObject(addFields, "betweenAttemptsTimeout", \
449                                        BETWEEN_ATTEMPTS_TIMEOUT_2000) == 0) {
450       ret = false;
451    }
452    else if(JsonHelper::addNodeToObject(addFields, "keepaliveDelay", \
453                                        KEEPALIVE_DELAY_120) == 0) {
454       ret = false;
455    }
456    O1_LOG("\nO1 PnfRegistration : Additional fields prepared for PNF registration");
457    return ret;
458 }
459
460 /*******************************************************************
461  *
462  * @brief Read json file
463  *
464  * @details
465  *
466  *    Function : readConfigFile
467  *
468  *    Functionality:
469  *      - Read json file
470  *
471  *
472  * @params[in] void
473  * @return true  : success
474  *         false : failure
475  ******************************************************************/
476
477 bool PnfRegistration::readConfigFile()
478 {
479    cJSON *json = JsonHelper::read(NETCONF_CONFIG);
480    if(json == NULL) {
481        O1_LOG("\nO1 PnfRegistration : Config file reading error is  :%s", JsonHelper::getError());
482     return false;
483     }
484     else {
485        cJSON *rootNode = NULL;
486        rootNode = JsonHelper::getNode(json, "NetconfServer");
487        if(rootNode) {
488           O1_LOG("\nO1 PnfRegistration : Reading NetconfServer config file");
489           mNetconfMacAddr = JsonHelper::getValue(rootNode, "MacAddress");
490           mNetconfIpv4 = JsonHelper::getValue(rootNode, "NetconfServerIpv4");
491           mNetconfIpv6 = JsonHelper::getValue(rootNode, "NetconfServerIpv6");
492           mNetconfPort = JsonHelper::getValue(rootNode, "NetconfPort");
493           mNetconfUsername = JsonHelper::getValue(rootNode, "NetconfUsername");
494           mNetconfPassword = JsonHelper::getValue(rootNode, "NetconfPassword");
495       }
496       else {
497          O1_LOG("\nO1 PnfRegistration : smoConfig Object is not available in config file");
498          return false;
499       }
500    }
501    JsonHelper::deleteNode(json);
502    return true;
503 }
504
505 /**********************************************************************
506   End of file
507  **********************************************************************/