1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /* This file contains macros and functions to support the preparation of pnf
20 Registration VES Event*/
26 #include "PnfRegistration.hpp"
27 #include "JsonHelper.hpp"
28 #include "VesUtils.hpp"
30 /* Default constructor*/
31 PnfRegistration::PnfRegistration()
33 this->mVesEventType = VesEventType::PNF_REGISTRATION;
36 /* Default Destructor*/
37 PnfRegistration::~PnfRegistration()
42 /*******************************************************************
44 * @brief get today's date in YYYY-MM-DD format
48 * Function : getCurrentDate
51 * - get today's date in YYYY-MM-DD format
53 * @params[in] OUT - date string pointer
54 * @return true - success
57 * ****************************************************************/
59 string PnfRegistration::getCurrentDate()
62 char dateStr[MAX_TIME_STR];
63 strftime(dateStr, MAX_TIME_STR, "%F", localtime(&t));
64 std::ostringstream oss;
69 /*******************************************************************
71 * @brief provide mac address of netconf server
75 * Function : getMacAddr
78 * - provide mac address of netconf server
81 * @return mac address as string - success
83 * ****************************************************************/
85 string PnfRegistration::getNetconfMacAddr()
87 if(mNetconfMacAddr != "") {
88 return mNetconfMacAddr;
91 O1_LOG("O1 VES : could not get Netconf Mac Address\n");
97 /*******************************************************************
99 * @brief provide ipv4 address of netconf server
103 * Function : getNetconfV4ServerIP
106 * - provide ipv4 address of netconf server
109 * @return ipv4 address as string - success
111 * ****************************************************************/
113 string PnfRegistration::getNetconfV4ServerIP()
115 if(mNetconfIpv4 != "") {
119 O1_LOG("O1 VES : could not get Netconf IPv4 ip\n");
124 /*******************************************************************
126 * @brief provides Netconf Server Port number
130 * Function : getNetconfPort
133 * - provides Netconf Server Port number
134 * - provide default port if not provided
137 * @return port number - success
139 * ****************************************************************/
141 string PnfRegistration::getNetconfPort()
143 if(mNetconfPort != "") {
147 O1_LOG("O1 VES : Could not get Netconf Port number\n");
148 return NETCONF_DEFAULT_PORT;
152 /*******************************************************************
155 * @brief provides Netconf Server Username
159 * Function : getUsername
162 * - provides Netconf Server Username
165 * @return port number - success
167 * ****************************************************************/
170 string PnfRegistration::getUsername()
172 if(mNetconfUsername != "") {
173 return mNetconfUsername;
176 O1_LOG("O1 VES : could not get Netconf username\n");
181 /*******************************************************************
184 * @brief provides Netconf Server Password
188 * Function : getPassword
191 * - provides Netconf Server password
194 * @return port number - success
196 * ****************************************************************/
198 string PnfRegistration::getPassword()
200 if(mNetconfPassword != "") {
201 return mNetconfPassword;
204 O1_LOG("O1 VES : could not get Netconf password\n");
209 /*******************************************************************
211 * @brief provides ipv6 address of netconf server
215 * Function : getNetconfV6ServerIP
218 * - provides ipv6 address of netconf server
221 * @return ipv6 address as string - success
223 * ****************************************************************/
225 string PnfRegistration::getNetconfV6ServerIP()
227 if(mNetconfIpv6 != "") {
231 O1_LOG("O1 VES : could not get Netconf IPv6 ip\n");
236 /*******************************************************************
238 * @brief provide Serial Number
242 * Function : getSerialNumber
245 * - provide Serial Number
248 * @return serial number - success
249 * empty string - failure
251 * ****************************************************************/
253 string PnfRegistration::getSerialNumber()
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;
264 /*******************************************************************
266 * @brief provide Unit Family
270 * Function : getUnitFamily
273 * - provide Unit Family
276 * @return serial number - success
277 * empty string - failure
279 * ****************************************************************/
281 string PnfRegistration::getUnitFamily()
284 unitFamily.append(VENDER_NAME_VENDORB).append("-").append(UNIT_TYPE_7DEV);
289 /*******************************************************************
291 * @brief prepare PNF registration Fields
295 * Function : preparePnfRegistrationFields
298 * - prepare PNF registration Fields in json format
300 * @params[in] IN - pointer to pnfFields
301 * @return true - success
304 * ****************************************************************/
306 bool PnfRegistration::prepareEventFields()
309 cJSON* pnfFields = this->mVesEventFields;
310 if(!readConfigFile())
312 O1_LOG("O1 VES : config file reading failed\n");
315 if(JsonHelper::addNodeToObject(pnfFields, "pnfRegistrationFieldsVersion", \
316 PNF_REGISTRATION_VERSION_2_1) == 0) {
319 else if(JsonHelper::addNodeToObject(pnfFields, \
320 "lastServiceDate", getCurrentDate().c_str()) == 0) {
323 else if(JsonHelper::addNodeToObject(pnfFields, "macAddress", \
324 getNetconfMacAddr().c_str()) == 0) {
327 if(JsonHelper::addNodeToObject(pnfFields, \
328 "manufactureDate", getCurrentDate().c_str()) == 0) {
331 else if(JsonHelper::addNodeToObject(pnfFields, "modelNumber", \
332 MODEL_NUMBER_007_DEV) == 0) {
335 else if(JsonHelper::addNodeToObject(pnfFields, "oamV4IpAddress", \
336 getNetconfV4ServerIP().c_str()) == 0) {
339 else if(JsonHelper::addNodeToObject(pnfFields, "oamV6IpAddress", \
340 getNetconfV6ServerIP().c_str()) == 0) {
343 else if(JsonHelper::addNodeToObject(pnfFields, "serialNumber", \
344 getSerialNumber().c_str()) == 0) {
347 else if(JsonHelper::addNodeToObject(pnfFields, "softwareVersion", \
348 SOFTWARE_VERSION_2_3_5) == 0) {
351 else if(JsonHelper::addNodeToObject(pnfFields, "unitFamily", \
352 getUnitFamily().c_str()) == 0) {
355 else if(JsonHelper::addNodeToObject(pnfFields, "unitType", \
356 UNIT_TYPE_7DEV) == 0) {
359 else if(JsonHelper::addNodeToObject(pnfFields, "vendorName", \
360 VENDER_NAME_VENDORB) == 0) {
365 cJSON *addFields = JsonHelper::createNode();
367 O1_LOG("O1 VES : could not create additional fields JSON object\n");
371 if(prepareAdditionalFields(addFields))
373 O1_LOG("O1 VES : PNF parameter prepration done adding additional parameters \
376 O1_LOG("O1 VES : could not prepare additional fields cJSON object\n");
377 JsonHelper::deleteNode(pnfFields);
381 if(JsonHelper::addJsonNodeToObject(pnfFields, "additionalFields", \
383 O1_LOG("O1 VES : could not add additional fileds\n");
384 JsonHelper::deleteNode(pnfFields);
388 O1_LOG("O1 VES : Preparation on PNF registration additional fields done \
394 /*******************************************************************
396 * @brief prepare PNF registration additional Fields
400 * Function : prepareAdditionalFields
403 * - prepare PNF registration additional Fields in json formate
405 * @params[in] IN - pointer to addField
406 * @return true - success
409 * ****************************************************************/
411 bool PnfRegistration::prepareAdditionalFields(cJSON *addFields)
414 if(JsonHelper::addNodeToObject(addFields, "oamPort", getNetconfPort().c_str()) == 0) {
417 else if(JsonHelper::addNodeToObject(addFields, "protocol", \
418 NETCONF_PROTOCOL_SSH) == 0) {
422 else if(JsonHelper::addNodeToObject(addFields, "username", \
423 getUsername().c_str()) == 0) {
426 else if(JsonHelper::addNodeToObject(addFields, "password", \
427 getPassword().c_str()) == 0) {
430 else if(JsonHelper::addNodeToObject(addFields, "reconnectOnChangedSchema", \
431 RECONNECT_ON_SCHEMA_CHANGE_FALSE) == 0) {
434 else if(JsonHelper::addNodeToObject(addFields, "sleep-factor", \
435 SLEEP_FACTOR_1_5) == 0) {
438 else if(JsonHelper::addNodeToObject(addFields, "tcpOnly", \
439 TCP_ONLY_FALSE) == 0) {
442 else if(JsonHelper::addNodeToObject(addFields, "connectionTimeout", \
443 CONNECTION_TIMEOUT_20000) == 0) {
446 else if(JsonHelper::addNodeToObject(addFields, "maxConnectionAttempts", \
447 MAX_CONNECTION_ATTEMPTS_100) == 0) {
450 else if(JsonHelper::addNodeToObject(addFields, "betweenAttemptsTimeout", \
451 BETWEEN_ATTEMPTS_TIMEOUT_2000) == 0) {
454 else if(JsonHelper::addNodeToObject(addFields, "keepaliveDelay", \
455 KEEPALIVE_DELAY_120) == 0) {
458 O1_LOG("O1 VES : additonal field preparation of PNF done successfully \n");
462 /*******************************************************************
464 * @brief Read json file
468 * Function : readConfigFile
475 * @return true : success
477 ******************************************************************/
479 bool PnfRegistration::readConfigFile()
481 cJSON *json = JsonHelper::read(NETCONF_CONFIG);
483 O1_LOG("O1 VES : Config file reading error is :%s\n", JsonHelper::getError());
487 cJSON *rootNode = NULL;
488 rootNode = JsonHelper::getNode(json, "NetconfServer");
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");
499 O1_LOG("O1 VES : smoConfig Object is not availbale in config file\n");
503 JsonHelper::deleteNode(json);
507 /**********************************************************************
509 **********************************************************************/