2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.common.scripts
23 import static org.apache.commons.lang3.StringUtils.*;
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.so.bpmn.core.UrnPropertiesReader
29 import org.onap.so.bpmn.core.WorkflowException
30 import org.onap.so.logger.MessageEnum
31 import org.onap.so.logger.MsoLogger
32 import org.onap.so.rest.APIResponse;
36 class AllottedResourceUtils {
37 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, AllottedResourceUtils.class);
40 private AbstractServiceTaskProcessor taskProcessor
41 ExceptionUtil exceptionUtil = new ExceptionUtil()
44 public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {
45 this.taskProcessor = taskProcessor
46 this.utils = taskProcessor.utils
49 /*Used on Create - called from DoCreate
50 * Using Consuming ServiceInstanceId get related Allotted Resources Orchestration status from AAI
51 * 1) get related AR links for CSI 2) get AR from AR links
52 * return: null -> AR Not found
53 * return: " " -> AR found with empty orchStatus
54 * return: orchStatus - > AR found with this orchStatus
55 * setsVariable aaiARGetResponse
57 public String getAROrchStatus (DelegateExecution execution) {
59 msoLogger.trace("getAROrchStatus ")
61 String serviceInstanceId = execution.getVariable("serviceInstanceId")
62 String arType = execution.getVariable("allottedResourceType")
63 String arRole = execution.getVariable("allottedResourceRole")
64 String siXml = execution.getVariable("CSI_service")
66 String orchStatus = null
67 XmlParser xmlParser = new XmlParser()
68 msoLogger.debug("getAROrchStatus siXml:" + siXml)
70 if (!isBlank(siXml)) {
71 def groovy.util.Node siNode = xmlParser.parseText(siXml)
72 def groovy.util.Node relationshipList = utils.getChildNode(siNode, 'relationship-list')
73 if (relationshipList != null) {
74 def groovy.util.NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
75 for (groovy.util.Node relationship in relationships) {
76 def groovy.util.Node relatedTo = utils.getChildNode(relationship, 'related-to')
77 if ((relatedTo != null) && (relatedTo.text().equals('allotted-resource'))) {
78 msoLogger.debug("getARORchStatus AR found")
79 def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')
80 if (relatedLink != null){
81 ar = getARbyLink(execution, relatedLink.text(), arRole)
84 orchStatus = execution.getVariable("aaiAROrchStatus")
93 msoLogger.debug(" Error encountered in getAROrchStatus" + e.getMessage())
94 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())
96 msoLogger.trace(" Exit getAROrchStatus - OrchStatus:" + orchStatus)
100 // get Allotted Resource by AllottedResourceId
101 // used on Delete - called from doDeleteAR
102 // setsVariable aaiARGetResponse
103 public String getARbyId (DelegateExecution execution, String allottedResourceId) {
104 msoLogger.trace("getARbyId ")
105 String arLink = getARLinkbyId(execution, allottedResourceId)
107 if (!isBlank(arLink))
109 ar = getARbyLink(execution, arLink, "")
111 msoLogger.trace(" Exit GetARbyId - AR:" + ar)
115 public String getPSIFmARLink(DelegateExecution execution, String arLink)
117 // Path: /aai/{version}/business/customers/customer/{cust}/service-subscriptions/service-subscription/{subs}/service-instances/service-instance/{psiid}/allotted-resources/allotted-resource/{arid}
118 msoLogger.trace(" getPSIFmARLink - path:" + arLink)
119 String[] split = arLink.split("/service-instance/")
120 String[] splitB = split[1].split("/allotted-resources/")
121 String siId = splitB[0]
122 msoLogger.trace(" Exit getARLinkbyId - parentServiceInstanceId:" + siId )
126 // get Allotted Resource Link by AllottedResourceId using Nodes Query
127 // used on Delete - called from getARbyId
128 public String getARLinkbyId (DelegateExecution execution, String allottedResourceId) {
129 msoLogger.trace("getARLinkbyId ")
132 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
133 String aaiNQUri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
134 String aaiEndpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
135 String aaiUrl = "${aaiNQUri}?search-node-type=allotted-resource&filter=id:EQUALS:${allottedResourceId}"
137 msoLogger.debug("getARLinkbyId url: \n" + aaiUrl)
139 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, aaiUrl)
140 int responseCode = response.getStatusCode()
141 msoLogger.debug(" GET AR response code is: " + responseCode)
143 String aaiResponse = response.getResponseBodyAsString()
144 msoLogger.debug("GET AR:" + aaiResponse)
145 if(responseCode == 200 || responseCode == 202){
146 msoLogger.debug("GET AR Received a Good Response Code")
147 if(utils.nodeExists(aaiResponse, "result-data")){
148 msoLogger.debug("Query for AllottedResource Url Response Does Contain Data" )
149 arLink = utils.getNodeText(aaiResponse, "resource-link")
151 msoLogger.debug("GET AR Response Does NOT Contain Data" )
153 }else if(responseCode == 404){
154 msoLogger.debug("GET AR received a Not Found (404) Response")
157 msoLogger.debug(" GET AR received a Bad Response: \n" + aaiResponse)
158 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
161 msoLogger.debug(" Error encountered within GetAaiAR" + e.getMessage())
162 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetARbyId" + e.getMessage())
164 msoLogger.trace(" Exit GetARLinkbyId - Link:" + arLink)
168 // get Allotted resource using Link
169 // used on Create called from getARORchStatus
170 // used on Delete called from getARbyId
171 // setsVariable aaiARPath - used for Patch in create
172 public String getARbyLink (DelegateExecution execution, String link, String role) {
173 msoLogger.trace("getARbyLink ")
177 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
178 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
179 String arEndpoint = ""
182 msoLogger.debug("Incoming AR Resource Link is: " + link)
183 String[] split = link.split("/aai/")
184 arEndpoint = "/aai/" + split[1]
187 arUrl = "${aai_endpoint}" + arEndpoint
189 msoLogger.debug("GET AR Aai Path is: \n" + arUrl)
191 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, arUrl)
192 int responseCode = response.getStatusCode()
193 msoLogger.debug(" GET AR response code is: " + responseCode)
195 String aaiResponse = response.getResponseBodyAsString()
196 msoLogger.debug("GET AR:" + aaiResponse)
197 if(responseCode == 200 || responseCode == 202){
198 msoLogger.debug("GET AR Received a Good Response Code")
199 if(utils.nodeExists(aaiResponse, "allotted-resource")){
202 if (utils.nodeExists(aaiResponse, "role") && role.equals(utils.getNodeText(aaiResponse, "role"))) {
205 msoLogger.debug("AAI AR does not match input role:" + role)
215 msoLogger.debug("GET AR Does NOT Contain Data" )
217 }else if(responseCode == 404){
218 msoLogger.debug("GET AR received a Not Found (404) Response")
221 msoLogger.debug(" GET AR received a Bad Response: \n" + aaiResponse)
222 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
225 msoLogger.debug(" Error encountered within GetAaiAR" + e.getMessage())
226 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())
230 execution.setVariable("aaiARGetResponse", ar)
231 execution.setVariable("aaiARPath", arUrl)
233 String resourceVersion = null
234 if (utils.nodeExists(ar, "resource-version")) {
235 resourceVersion = utils.getNodeText(ar, "resource-version")
236 execution.setVariable("aaiARResourceVersion", resourceVersion)
239 String orchStatus = null
240 if (utils.nodeExists(ar, "orchestration-status")) {
241 orchStatus= utils.getNodeText(ar, "orchestration-status")
247 execution.setVariable("aaiAROrchStatus", orchStatus)
249 msoLogger.trace(" Exit GetARbyLink - AR:" + ar)
253 public void updateAROrchStatus(DelegateExecution execution, String status, String aaiARPath){
254 msoLogger.trace("updaAROrchStatus ")
257 String updateReq = """
259 "orchestration-status": "${status}"
263 msoLogger.debug('AAI AR URI: ' + aaiARPath)
265 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
266 APIResponse apiResponse = aaiUriUtil.executeAAIPatchCall(execution, aaiARPath, updateReq)
267 def aaiResponse = apiResponse.getResponseBodyAsString()
268 def responseCode = apiResponse.getStatusCode()
270 msoLogger.debug("AAI Response Code: " + responseCode)
271 msoLogger.debug("AAI Response: " + aaiResponse)
272 if(responseCode == 200){
273 msoLogger.debug("UpdateAR Good REST Response is: " + "\n" + aaiResponse)
275 msoLogger.debug("UpdateAROrchStatus Bad REST Response!")
276 buildAAIErrorResponse(execution, aaiResponse, "Error updating AR OrchStatus in AAI")
280 msoLogger.debug("Rethrowing MSOWorkflowException ")
283 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in updateAR.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.getMessage());
284 exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())
286 msoLogger.trace("Exit updateAROrchStatus ")
289 //Sets Variable "wasDeleted"
290 public void deleteAR(DelegateExecution execution, String aaiARPath){
291 msoLogger.trace(" deleteAR - aaiARPath:" + aaiARPath)
293 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
294 APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, aaiARPath)
295 int responseCode = response.getStatusCode()
296 execution.setVariable("deleteARResponseCode", responseCode)
298 msoLogger.debug(" Delete AR response code:" + responseCode)
300 String aaiResponse = response.getResponseBodyAsString()
301 execution.setVariable("aaiARDeleteResponse", aaiResponse)
303 msoLogger.debug("Delete AR Response:" + aaiResponse)
306 if(responseCode == 204){
307 msoLogger.debug(" Delete AR Received a Good Response")
308 execution.setVariable("wasDeleted", "true")
309 }else if(responseCode == 404){
310 msoLogger.debug(" Delete AR Received a Not Found (404) Response")
311 }else if(responseCode == 412){
312 msoLogger.debug("Delete AR Received a Resource Version Mismatch Error: \n" + aaiResponse)
313 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")
315 msoLogger.debug("Delete AR Received a BAD REST Response: \n" + aaiResponse)
316 buildAAIErrorResponse(execution, aaiResponse, "Error deleting AR in AAI")
317 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
320 msoLogger.debug("Rethrowing MSOWorkflowException")
323 msoLogger.debug(" Error encountered in deleteAR!" + e)
324 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")
326 msoLogger.trace("Exit deleteAR ")
329 public void buildAAIErrorResponse(DelegateExecution execution, String response, String errorMessage){
330 msoLogger.trace("BuildAAIErrorResponse")
332 if((response != null) && (response.contains("Fault") || response.contains("RESTFault"))){
333 WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(response, execution)
334 execution.setVariable("WorkflowException", workflowException)
336 exceptionUtil.buildWorkflowException(execution, 500, errorMessage)
339 msoLogger.trace("Exit BuildAAIErrorResponse Process")
340 throw new BpmnError("MSOWorkflowException")