diff options
Diffstat (limited to 'policy/apex/logic/AAIServiceAssignedTask.js')
-rw-r--r-- | policy/apex/logic/AAIServiceAssignedTask.js | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/policy/apex/logic/AAIServiceAssignedTask.js b/policy/apex/logic/AAIServiceAssignedTask.js new file mode 100644 index 0000000..0cb4967 --- /dev/null +++ b/policy/apex/logic/AAIServiceAssignedTask.js @@ -0,0 +1,260 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Huawei. All rights reserved. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +executor.logger.info("Begin Execution AAIServiceAssignedTask.js"); +executor.logger.info(executor.subject.id); +executor.logger.info(executor.inFields); + +var attachmentPoint = executor.inFields.get("attachmentPoint"); +var requestID = executor.inFields.get("requestID"); +var serviceInstanceId = executor.inFields.get("serviceInstanceId"); + +var NomadicONTContext = executor.getContextAlbum("NomadicONTContextAlbum").get(attachmentPoint); +executor.logger.info(NomadicONTContext); + +var jsonObj; +var aaiUpdateResult = true; + +var wbClient = org.onap.policy.apex.examples.bbs.WebClient; +var client = new wbClient(); + +/* Get AAI URL from Configuration file. */ +var AAI_URL = "localhost:8080"; +var CUSTOMER_ID = requestID; +var SERVICE_INSTANCE_ID = serviceInstanceId; +var AAI_VERSION = "v14"; +var resource_version; +var relationship_list; +var HTTP_PROTOCOL = "https://"; +var results; +var putUrl; +var service_instance; + +AAI_URL = executor.parameters.get("AAI_URL"); +AAI_USERNAME = executor.parameters.get("AAI_USERNAME"); +AAI_PASSWORD = executor.parameters.get("AAI_PASSWORD"); +AAI_VERSION = executor.parameters.get("AAI_VERSION"); + +executor.logger.info("AAI_URL " + AAI_URL); + +/* Get service instance Id from AAI */ +try { + var urlGet = HTTP_PROTOCOL + AAI_URL + "/aai/" + AAI_VERSION + "/nodes/service-instances/service-instance/" + + SERVICE_INSTANCE_ID + "?format=resource_and_url"; + + executor.logger.info("Query url" + urlGet); + + result = client.httpRequest(urlGet, "GET", null, AAI_USERNAME, AAI_PASSWORD, "application/json"); + executor.logger.info("Data received From " + urlGet + " " + result); + jsonObj = JSON.parse(result.toString()); + + executor.logger.info(JSON.stringify(jsonObj, null, 4)); + /* Retrieve the service instance id */ + results = jsonObj['results'][0]; + putUrl = results['url']; + service_instance = results['service-instance']; + service_instance_id = service_instance['service-instance-id']; + resource_version = service_instance['resource-version']; + relationship_list = service_instance['relationship-list']; + executor.logger.info("After Parse service_instance " + JSON.stringify(service_instance, null, 4) + "\n url " + + putUrl + "\n Service instace Id " + service_instance_id); + + if (result == "") { + aaiUpdateResult = false; + } +} catch (err) { + executor.logger.info("Failed to retrieve data " + err); + aaiUpdateResult = false; +} + +/* BBS Policy updates orchestration status of {{bbs-cfs-service-instance-UUID}} [ active --> assigned ] */ +var putUpddateServInstance; +putUpddateServInstance = service_instance; +try { + if (aaiUpdateResult == true) { + putUpddateServInstance["orchestration-status"] = "active"; + executor.logger.info("ready to putAfter Parse " + JSON.stringify(putUpddateServInstance, null, 4)); + var urlPut = HTTP_PROTOCOL + AAI_URL + putUrl + "?resource_version=" + resource_version; + result = client.httpRequest(urlPut, "PUT", JSON.stringify(putUpddateServInstance), AAI_USERNAME, AAI_PASSWORD, + "application/json"); + executor.logger.info("Data received From " + urlPut + " " + result); + /* If failure to retrieve data proceed to Failure */ + if (result != "") { + aaiUpdateResult = false; + } + } +} catch (err) { + executor.logger.info("Failed to retrieve data " + err); + aaiUpdateResult = false; +} + +if (!service_instance.hasOwnProperty('input-parameters') || !service_instance.hasOwnProperty('metadata')) { + aaiUpdateResult = false; + executor.logger.info("Validate data failed. input-parameters or metadata is missing"); +} + +/* update logical link in pnf */ +var oldLinkName = ""; +try { + if (aaiUpdateResult == true) { + var pnfName = ""; + var pnfResponse; + var pnfUpdate; + var relationShips = relationship_list["relationship"]; + + for (var i = 0; i < relationShips.length; i++) { + if (relationShips[i]["related-to"] == "pnf") { + var relationship_data = relationShips[i]["relationship-data"]; + for (var j = 0; j < relationship_data.length; j++) { + if (relationship_data[j]["relationship-key"] == "pnf.pnf-name") { + pnfName = relationship_data[j]['relationship-value']; + break; + } + } + } + } + executor.logger.info("pnf-name found " + pnfName); + /* 1. Get PNF */ + var urlGetPnf = HTTP_PROTOCOL + AAI_URL + "/aai/" + AAI_VERSION + "/network/pnfs/pnf/" + pnfName; + executor.logger.info("*********************** Executing call to fetch PNF"); + pnfResponse = client.httpRequest(urlGetPnf, "GET", null, AAI_USERNAME, AAI_PASSWORD, "application/json"); + executor.logger.info("Data received From " + urlGetPnf + " >" + pnfResponse + "<"); + /* If failure to retrieve data proceed to Failure */ + if (!pnfResponse) { + executor.logger.info("*********************** Fetch PNF with relation marked it ERROR"); + aaiUpdateResult = false; + } + pnfUpdate = JSON.parse(pnfResponse.toString()); + executor.logger.info(JSON.stringify(pnfUpdate, null, 4)); + + /* 2. Create logical link */ + var link_name = attachmentPoint; + var logicalLink = {}; + logicalLink["link-name"] = String(link_name); + logicalLink["in-maint"] = String(false); + logicalLink["link-type"] = "attachment-point"; + // var logicalLink = "{\"link-name\":\"" + link_name + "\",\"in-maint\":false, \"link-type\":\"attachment-point\"}"; + // var logicalLink = { + // "link-name" : link_name, + // "in-maint" : false, + // "link-type" : "attachment-point" + // }; + var urlNewLogicalLink = HTTP_PROTOCOL + AAI_URL + "/aai/" + AAI_VERSION + + "/network/logical-links/logical-link/" + link_name; + executor.logger.info("URL to be used >>" + urlNewLogicalLink + "<<"); + var logicalLinkStr = JSON.stringify(logicalLink); + executor.logger.info("Body to be used >>" + logicalLinkStr + "<<"); + executor.logger.info("*********************** Executing call to create new Logical Link"); + result = client.httpRequest(urlNewLogicalLink, "PUT", logicalLinkStr, AAI_USERNAME, AAI_PASSWORD, + "application/json"); + executor.logger.info("Data received From " + urlNewLogicalLink + " >" + result + "<"); + /* If failure to retrieve data proceed to Failure */ + if (result != "") { + executor.logger.info("*********************** CREATE NEW LOGICAL LINK marked it ERROR"); + aaiUpdateResult = false; + } + + /* 3. Update pnf with new relation */ + for (var i = 0; i < pnfUpdate["relationship-list"]["relationship"].length; i++) { + if (pnfUpdate["relationship-list"]["relationship"][i]['related-to'] == 'logical-link') { + pnfUpdate["relationship-list"]["relationship"][i]['related-link'] = String("/aai/" + AAI_VERSION + + "/network/logical-links/logical-link/" + link_name); + for (var j = 0; j < pnfUpdate["relationship-list"]["relationship"][i]['relationship-data'].length; j++) { + if (pnfUpdate["relationship-list"]["relationship"][i]['relationship-data'][j]['relationship-key'] == "logical-link.link-name") { + oldLinkName = pnfUpdate["relationship-list"]["relationship"][i]['relationship-data'][j]['relationship-value']; + pnfUpdate["relationship-list"]["relationship"][i]['relationship-data'][j]['relationship-value'] = String(link_name); + break; + } + } + break; + } + } + executor.logger.info("*********************** Just before calling stringify"); + var pnfUpdateStr = JSON.stringify(pnfUpdate); + executor.logger.info("Put pnf to aai " + pnfUpdateStr); + var urlPutPnf = HTTP_PROTOCOL + AAI_URL + "/aai/" + AAI_VERSION + "/network/pnfs/pnf/" + pnfName; + executor.logger.info("*********************** Executing call to update PNF with new relation"); + result = client.httpRequest(urlPutPnf, "PUT", pnfUpdateStr, AAI_USERNAME, AAI_PASSWORD, + "application/json"); + executor.logger.info("Data received From " + urlPutPnf + " >" + result + "<"); + + /* If failure to retrieve data proceed to Failure */ + if (result != "") { + executor.logger.info("*********************** UPDATE PNF with relation marked it ERROR"); + aaiUpdateResult = false; + } + /* Get and Delete the Stale logical link */ + var oldLinkResult; + var linkResult; + var urlOldLogicalLink = HTTP_PROTOCOL + AAI_URL + "/aai/" + AAI_VERSION + + "/network/logical-links/logical-link/" + oldLinkName; + executor.logger.info("*********************** Executing call to fetch old logical link"); + linkResult = client.httpRequest(urlOldLogicalLink, "GET", null, AAI_USERNAME, AAI_PASSWORD, "application/json"); + executor.logger.info("Data received From " + urlOldLogicalLink + " " + linkResult + " " + + linkResult.hasOwnProperty("link-name")); + oldLinkResult = JSON.parse(linkResult.toString()); + if (oldLinkResult.hasOwnProperty("link-name") == true) { + var res_version = oldLinkResult["resource-version"]; + var urlDelOldLogicalLink = urlOldLogicalLink + "?resource-version=" + res_version; + executor.logger.info("Delete called for " + urlDelOldLogicalLink); + executor.logger.info("*********************** Executing call to delete old logical link"); + result = client.httpRequest(urlDelOldLogicalLink, "DELETE", null, AAI_USERNAME, AAI_PASSWORD, + "application/json"); + executor.logger.info("Delete called for " + urlDelOldLogicalLink + " result " + result); + } + } +} catch (err) { + executor.logger.info("Failed to retrieve data " + err); + aaiUpdateResult = false; +} + +/* If Success then Fill output schema */ +if (aaiUpdateResult == true) { + executor.outFields.put("result", "SUCCESS"); + NomadicONTContext.put("result", "SUCCESS"); + executor.logger.info("*********************** Just before calling stringify when output to context (service-instance)"); + NomadicONTContext.put("aai_message", JSON.stringify(service_instance)); + executor.logger.info("*********************** Just after calling stringify when output to context (service-instance)"); + NomadicONTContext.put("url", putUrl); +} else { + executor.outFields.put("result", "FAILURE"); + NomadicONTContext.put("result", "FAILURE"); +} + +executor.outFields.put("requestID", requestID); +executor.outFields.put("attachmentPoint", attachmentPoint); +executor.outFields.put("serviceInstanceId", executor.inFields.get("serviceInstanceId")); + +executor.logger.info(executor.outFields); +executor.logger.info("End Execution AAIServiceAssignedTask.js"); + +true; + +/* Utility functions Begin */ +function IsValidJSONString(str) { + try { + JSON.parse(str); + } catch (e) { + return false; + } + return true; +} +/* Utility functions End */ |