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 "PnfRegistrationEvent.hpp"
27 #include "JsonHelper.hpp"
28 #include "VesUtils.hpp"
30 /* Default constructor*/
31 PnfRegistrationEvent::PnfRegistrationEvent()
32 : VesEvent(VesEventType::PNF_REGISTRATION)
36 /* Default Destructor*/
37 PnfRegistrationEvent::~PnfRegistrationEvent()
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 PnfRegistrationEvent::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 PnfRegistrationEvent::getNetconfMacAddr()
87 if(mNetconfMacAddr != "") {
88 return mNetconfMacAddr;
91 O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf Mac Address");
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 PnfRegistrationEvent::getNetconfV4ServerIP()
115 if(mNetconfIpv4 != "") {
119 O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf IPv4 ip");
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 PnfRegistrationEvent::getNetconfPort()
143 if(mNetconfPort != "") {
147 O1_LOG("\nO1 PnfRegistrationEvent : Could not get Netconf Port number");
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 PnfRegistrationEvent::getUsername()
172 if(mNetconfUsername != "") {
173 return mNetconfUsername;
176 O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf username");
181 /*******************************************************************
184 * @brief provides Netconf Server Password
188 * Function : getPassword
191 * - provides Netconf Server password
194 * @return port number - success
196 * ****************************************************************/
198 string PnfRegistrationEvent::getPassword()
200 if(mNetconfPassword != "") {
201 return mNetconfPassword;
204 O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf password");
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 PnfRegistrationEvent::getNetconfV6ServerIP()
227 if(mNetconfIpv6 != "") {
231 O1_LOG("\nO1 PnfRegistrationEvent : could not get Netconf IPv6 ip");
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 PnfRegistrationEvent::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 PnfRegistrationEvent::getUnitFamily()
284 unitFamily.append(VENDER_NAME_VENDORB).append("-").append(UNIT_TYPE_7DEV);
289 /*******************************************************************
291 * @brief prepare PNF registration Fields
295 * Function : preparePnfRegistrationEventFields
298 * - prepare PNF registration Fields in json format
300 * @params[in] IN - pointer to pnfFields
301 * @return true - success
304 * ****************************************************************/
306 bool PnfRegistrationEvent::prepareEventFields(const Message* msg)
309 cJSON* pnfFields = this->mVesEventFields;
313 if(JsonHelper::addNodeToObject(pnfFields, "pnfRegistrationFieldsVersion", \
314 PNF_REGISTRATION_VERSION_2_1) == 0) {
317 else if(JsonHelper::addNodeToObject(pnfFields, \
318 "lastServiceDate", getCurrentDate().c_str()) == 0) {
321 else if(JsonHelper::addNodeToObject(pnfFields, "macAddress", \
322 getNetconfMacAddr().c_str()) == 0) {
325 if(JsonHelper::addNodeToObject(pnfFields, \
326 "manufactureDate", getCurrentDate().c_str()) == 0) {
329 else if(JsonHelper::addNodeToObject(pnfFields, "modelNumber", \
330 MODEL_NUMBER_007_DEV) == 0) {
333 else if(JsonHelper::addNodeToObject(pnfFields, "oamV4IpAddress", \
334 getNetconfV4ServerIP().c_str()) == 0) {
337 else if(getNetconfV6ServerIP() != "" && (JsonHelper::addNodeToObject( \
338 pnfFields, "oamV6IpAddress", \
339 getNetconfV6ServerIP().c_str()) == 0)) {
342 else if(JsonHelper::addNodeToObject(pnfFields, "serialNumber", \
343 getSerialNumber().c_str()) == 0) {
346 else if(JsonHelper::addNodeToObject(pnfFields, "softwareVersion", \
347 SOFTWARE_VERSION_2_3_5) == 0) {
350 else if(JsonHelper::addNodeToObject(pnfFields, "unitFamily", \
351 getUnitFamily().c_str()) == 0) {
354 else if(JsonHelper::addNodeToObject(pnfFields, "unitType", \
355 UNIT_TYPE_7DEV) == 0) {
358 else if(JsonHelper::addNodeToObject(pnfFields, "vendorName", \
359 VENDER_NAME_VENDORB) == 0) {
364 cJSON *addFields = JsonHelper::createNode();
366 O1_LOG("\nO1 PnfRegistrationEvent : could not create additional fields JSON object");
370 if(prepareAdditionalFields(addFields))
373 O1_LOG("\nO1 PnfRegistrationEvent : could not prepare additional fields cJSON object");
374 JsonHelper::deleteNode(pnfFields);
378 if(JsonHelper::addJsonNodeToObject(pnfFields, "additionalFields", \
380 O1_LOG("\nO1 PnfRegistrationEvent : could not add additional fields");
381 JsonHelper::deleteNode(pnfFields);
385 O1_LOG("\nO1 PnfRegistrationEvent : Event fields prepared for PNF registration");
390 /*******************************************************************
392 * @brief prepare PNF registration additional Fields
396 * Function : prepareAdditionalFields
399 * - prepare PNF registration additional Fields in json formate
401 * @params[in] IN - pointer to addField
402 * @return true - success
405 * ****************************************************************/
407 bool PnfRegistrationEvent::prepareAdditionalFields(cJSON *addFields)
410 if(JsonHelper::addNodeToObject(addFields, "oamPort", getNetconfPort().c_str()) == 0) {
413 else if(JsonHelper::addNodeToObject(addFields, "protocol", \
414 NETCONF_PROTOCOL_SSH) == 0) {
418 else if(JsonHelper::addNodeToObject(addFields, "username", \
419 getUsername().c_str()) == 0) {
422 else if(JsonHelper::addNodeToObject(addFields, "password", \
423 getPassword().c_str()) == 0) {
426 else if(JsonHelper::addNodeToObject(addFields, "reconnectOnChangedSchema", \
427 RECONNECT_ON_SCHEMA_CHANGE_FALSE) == 0) {
430 else if(JsonHelper::addNodeToObject(addFields, "sleep-factor", \
431 SLEEP_FACTOR_1_5) == 0) {
434 else if(JsonHelper::addNodeToObject(addFields, "tcpOnly", \
435 TCP_ONLY_FALSE) == 0) {
438 else if(JsonHelper::addNodeToObject(addFields, "connectionTimeout", \
439 CONNECTION_TIMEOUT_20000) == 0) {
442 else if(JsonHelper::addNodeToObject(addFields, "maxConnectionAttempts", \
443 MAX_CONNECTION_ATTEMPTS_100) == 0) {
446 else if(JsonHelper::addNodeToObject(addFields, "betweenAttemptsTimeout", \
447 BETWEEN_ATTEMPTS_TIMEOUT_2000) == 0) {
450 else if(JsonHelper::addNodeToObject(addFields, "keepaliveDelay", \
451 KEEPALIVE_DELAY_120) == 0) {
454 O1_LOG("\nO1 PnfRegistrationEvent : Additional fields prepared for PNF registration");
457 /*******************************************************************
459 * @brief Read json file
463 * Function : readConfigFile
470 * @return true : success
472 ******************************************************************/
474 void PnfRegistrationEvent::getNetconfConfig()
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();
485 /**********************************************************************
487 **********************************************************************/