37eae20ca56e258fd2c69a829833c48f00f50e14
[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("O1 VES : could not get Netconf Mac Address\n");
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("O1 VES : could not get Netconf IPv4 ip\n");
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("O1 VES : Could not get Netconf Port number\n");
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("O1 VES : could not get Netconf username\n");
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("O1 VES : could not get Netconf password\n");
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("O1 VES : could not get Netconf IPv6 ip\n");
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("O1 VES : config file reading failed\n");
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(JsonHelper::addNodeToObject(pnfFields, "oamV6IpAddress", \
340                                        getNetconfV6ServerIP().c_str()) == 0) {
341       ret = false;
342    }
343    else if(JsonHelper::addNodeToObject(pnfFields, "serialNumber", \
344                                        getSerialNumber().c_str()) == 0) {
345       ret = false;
346    }
347    else if(JsonHelper::addNodeToObject(pnfFields, "softwareVersion", \
348                                        SOFTWARE_VERSION_2_3_5) == 0) {
349       ret = false;
350    }
351    else if(JsonHelper::addNodeToObject(pnfFields, "unitFamily", \
352                                        getUnitFamily().c_str()) == 0) {
353       ret = false;
354    }
355    else if(JsonHelper::addNodeToObject(pnfFields, "unitType", \
356                                        UNIT_TYPE_7DEV) == 0) {
357       ret = false;
358    }
359    else if(JsonHelper::addNodeToObject(pnfFields, "vendorName", \
360                                        VENDER_NAME_VENDORB) == 0) {
361       ret = false;
362    }
363    else
364    {
365       cJSON *addFields = JsonHelper::createNode();
366       if(addFields == 0) {
367          O1_LOG("O1 VES : could not create additional fields JSON object\n");
368          return false;
369       }
370
371       if(prepareAdditionalFields(addFields))
372       {
373          O1_LOG("O1 VES : PNF parameter prepration done adding additional parameters \
374 if required \n");
375       if(addFields == 0) {
376          O1_LOG("O1 VES : could not prepare additional fields cJSON object\n");
377          JsonHelper::deleteNode(pnfFields);
378          return false;
379          }
380
381       if(JsonHelper::addJsonNodeToObject(pnfFields, "additionalFields", \
382                                addFields) == 0) {
383          O1_LOG("O1 VES : could not add additional fileds\n");
384          JsonHelper::deleteNode(pnfFields);
385          return false;
386          }
387        }
388        O1_LOG("O1 VES : Preparation on PNF registration additional fields done \
389 successfully \n");
390    }
391    return ret;
392 }
393
394 /*******************************************************************
395  *
396  * @brief prepare PNF registration additional Fields
397  *
398  * @details
399  *
400  *    Function : prepareAdditionalFields
401  *
402  *    Functionality:
403  *      - prepare PNF registration additional Fields in json formate
404  *
405  * @params[in] IN   - pointer to addField
406  * @return true     - success
407  *         false    - failure
408  *
409  * ****************************************************************/
410
411 bool PnfRegistration::prepareAdditionalFields(cJSON *addFields)
412 {
413    bool ret = true;
414    if(JsonHelper::addNodeToObject(addFields, "oamPort", getNetconfPort().c_str()) == 0) {
415       ret = false;
416    }
417    else if(JsonHelper::addNodeToObject(addFields, "protocol", \
418                                        NETCONF_PROTOCOL_SSH) == 0) {
419       ret = false;
420    }
421
422    else if(JsonHelper::addNodeToObject(addFields, "username", \
423                                        getUsername().c_str()) == 0) {
424       ret = false;
425    }
426    else if(JsonHelper::addNodeToObject(addFields, "password", \
427                                        getPassword().c_str()) == 0) {
428       ret = false;
429    }
430    else if(JsonHelper::addNodeToObject(addFields, "reconnectOnChangedSchema", \
431                                        RECONNECT_ON_SCHEMA_CHANGE_FALSE) == 0) {
432       ret = false;
433    }
434    else if(JsonHelper::addNodeToObject(addFields, "sleep-factor", \
435                                        SLEEP_FACTOR_1_5) == 0) {
436       ret = false;
437    }
438    else if(JsonHelper::addNodeToObject(addFields, "tcpOnly", \
439                                        TCP_ONLY_FALSE) == 0) {
440       ret = false;
441    }
442    else if(JsonHelper::addNodeToObject(addFields, "connectionTimeout", \
443                                        CONNECTION_TIMEOUT_20000) == 0) {
444       ret = false;
445    }
446    else if(JsonHelper::addNodeToObject(addFields, "maxConnectionAttempts", \
447                                        MAX_CONNECTION_ATTEMPTS_100) == 0) {
448       ret = false;
449    }
450    else if(JsonHelper::addNodeToObject(addFields, "betweenAttemptsTimeout", \
451                                        BETWEEN_ATTEMPTS_TIMEOUT_2000) == 0) {
452       ret = false;
453    }
454    else if(JsonHelper::addNodeToObject(addFields, "keepaliveDelay", \
455                                        KEEPALIVE_DELAY_120) == 0) {
456       ret = false;
457    }
458    O1_LOG("O1 VES : additonal field preparation of PNF done successfully \n");
459    return ret;
460 }
461
462 /*******************************************************************
463  *
464  * @brief Read json file
465  *
466  * @details
467  *
468  *    Function : readConfigFile
469  *
470  *    Functionality:
471  *      - Read json file
472  *
473  *
474  * @params[in] void
475  * @return true  : success
476  *         false : failure
477  ******************************************************************/
478
479 bool PnfRegistration::readConfigFile()
480 {
481    cJSON *json = JsonHelper::read(NETCONF_CONFIG);
482    if(json == NULL) {
483        O1_LOG("O1 VES : Config file reading error is  :%s\n", JsonHelper::getError());
484     return false;
485     }
486     else {
487        cJSON *rootNode = NULL;
488        rootNode = JsonHelper::getNode(json, "NetconfServer");
489        if(rootNode) {
490           O1_LOG("O1 VES : Reading NetconfServer config file\n");
491           mNetconfMacAddr = JsonHelper::getValue(rootNode, "MacAddress");
492           mNetconfIpv4 = JsonHelper::getValue(rootNode, "NetconfServerIpv4");
493           mNetconfIpv6 = JsonHelper::getValue(rootNode, "NetconfServerIpv6");
494           mNetconfPort = JsonHelper::getValue(rootNode, "NetconfPort");
495           mNetconfUsername = JsonHelper::getValue(rootNode, "NetconfUsername");
496           mNetconfPassword = JsonHelper::getValue(rootNode, "NetconfPassword");
497       }
498       else {
499          O1_LOG("O1 VES : smoConfig Object is not availbale in config file\n");
500          return false;
501       }
502    }
503    JsonHelper::deleteNode(json);
504    return true;
505 }
506
507 /**********************************************************************
508   End of file
509  **********************************************************************/