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 javax.ws.rs.NotFoundException
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.onap.so.bpmn.core.WorkflowException
32 import org.onap.so.client.aai.AAIObjectType;
33 import org.onap.so.client.aai.AAIResourcesClient
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory
36 import org.onap.so.logger.MessageEnum
37 import org.onap.so.logger.MsoLogger
38 import org.onap.so.rest.APIResponse;
42 class AllottedResourceUtils {
43 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, AllottedResourceUtils.class);
46 private AbstractServiceTaskProcessor taskProcessor
47 ExceptionUtil exceptionUtil = new ExceptionUtil()
50 public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {
51 this.taskProcessor = taskProcessor
52 this.utils = taskProcessor.utils
55 /*Used on Create - called from DoCreate
56 * Using Consuming ServiceInstanceId get related Allotted Resources Orchestration status from AAI
57 * 1) get related AR links for CSI 2) get AR from AR links
58 * return: null -> AR Not found
59 * return: " " -> AR found with empty orchStatus
60 * return: orchStatus - > AR found with this orchStatus
61 * setsVariable aaiARGetResponse
63 public String getAROrchStatus (DelegateExecution execution) {
65 msoLogger.trace("getAROrchStatus ")
67 String serviceInstanceId = execution.getVariable("serviceInstanceId")
68 String arType = execution.getVariable("allottedResourceType")
69 String arRole = execution.getVariable("allottedResourceRole")
70 String siXml = execution.getVariable("CSI_service")
72 String orchStatus = null
73 XmlParser xmlParser = new XmlParser()
74 msoLogger.debug("getAROrchStatus siXml:" + siXml)
76 if (!isBlank(siXml)) {
77 def groovy.util.Node siNode = xmlParser.parseText(siXml)
78 def groovy.util.Node relationshipList = utils.getChildNode(siNode, 'relationship-list')
79 if (relationshipList != null) {
80 def groovy.util.NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
81 for (groovy.util.Node relationship in relationships) {
82 def groovy.util.Node relatedTo = utils.getChildNode(relationship, 'related-to')
83 if ((relatedTo != null) && (relatedTo.text().equals('allotted-resource'))) {
84 msoLogger.debug("getARORchStatus AR found")
85 def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')
86 if (relatedLink != null){
87 ar = getARbyLink(execution, relatedLink.text(), arRole)
90 orchStatus = execution.getVariable("aaiAROrchStatus")
99 msoLogger.debug(" Error encountered in getAROrchStatus" + e.getMessage())
100 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())
102 msoLogger.trace(" Exit getAROrchStatus - OrchStatus:" + orchStatus)
106 // get Allotted Resource by AllottedResourceId
107 // used on Delete - called from doDeleteAR
108 // setsVariable aaiARGetResponse
109 public String getARbyId (DelegateExecution execution, String allottedResourceId) {
110 msoLogger.trace("getARbyId ")
111 AAIResourceUri arLink = getARLinkbyId(execution, allottedResourceId)
113 if (!isBlank(arLink))
115 ar = getARbyLink(execution, arLink, "")
117 msoLogger.trace(" Exit GetARbyId - AR:" + ar)
121 public String getPSIFmARLink(DelegateExecution execution, String arLink)
123 // Path: /aai/{version}/business/customers/customer/{cust}/service-subscriptions/service-subscription/{subs}/service-instances/service-instance/{psiid}/allotted-resources/allotted-resource/{arid}
124 msoLogger.trace(" getPSIFmARLink - path:" + arLink)
125 String[] split = arLink.split("/service-instance/")
126 String[] splitB = split[1].split("/allotted-resources/")
127 String siId = splitB[0]
128 msoLogger.trace(" Exit getARLinkbyId - parentServiceInstanceId:" + siId )
132 // get Allotted Resource Link by AllottedResourceId using Nodes Query
133 // used on Delete - called from getARbyId
134 public String getARLinkbyId (DelegateExecution execution, String allottedResourceId) {
135 msoLogger.trace("getARLinkbyId ")
138 AAIResourcesClient client = new AAIResourcesClient()
139 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE, allottedResourceId)
140 AaiUtil aaiUtil = new AaiUtil(taskProcessor)
141 arLink = aaiUtil.createAaiUri(uri)
142 } catch (NotFoundException e) {
143 msoLogger.debug("GET AR received a Not Found (404) Response")
144 } catch(Exception e){
145 msoLogger.debug(" Error encountered within GetAaiAR" + e.getMessage())
146 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetARbyId" + e.getMessage())
148 msoLogger.debug(" ***** Exit GetARLinkbyId ***** Link:" + arLink)
152 // get Allotted resource using Link
153 // used on Create called from getARORchStatus
154 // used on Delete called from getARbyId
155 // setsVariable aaiARPath - used for Patch in create
156 public String getARbyLink (DelegateExecution execution, String link, String role) {
157 msoLogger.trace("getARbyLink ")
161 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
162 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
163 String arEndpoint = ""
166 msoLogger.debug("Incoming AR Resource Link is: " + link)
167 String[] split = link.split("/aai/")
168 arEndpoint = "/aai/" + split[1]
171 arUrl = "${aai_endpoint}" + arEndpoint
173 msoLogger.debug("GET AR Aai Path is: \n" + arUrl)
175 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, arUrl)
176 int responseCode = response.getStatusCode()
177 msoLogger.debug(" GET AR response code is: " + responseCode)
179 String aaiResponse = response.getResponseBodyAsString()
180 msoLogger.debug("GET AR:" + aaiResponse)
181 if(responseCode == 200 || responseCode == 202){
182 msoLogger.debug("GET AR Received a Good Response Code")
183 if(utils.nodeExists(aaiResponse, "allotted-resource")){
186 if (utils.nodeExists(aaiResponse, "role") && role.equals(utils.getNodeText(aaiResponse, "role"))) {
189 msoLogger.debug("AAI AR does not match input role:" + role)
199 msoLogger.debug("GET AR Does NOT Contain Data" )
201 }else if(responseCode == 404){
202 msoLogger.debug("GET AR received a Not Found (404) Response")
205 msoLogger.debug(" GET AR received a Bad Response: \n" + aaiResponse)
206 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
209 msoLogger.debug(" Error encountered within GetAaiAR" + e.getMessage())
210 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())
214 execution.setVariable("aaiARGetResponse", ar)
215 execution.setVariable("aaiARPath", arUrl)
217 String resourceVersion = null
218 if (utils.nodeExists(ar, "resource-version")) {
219 resourceVersion = utils.getNodeText(ar, "resource-version")
220 execution.setVariable("aaiARResourceVersion", resourceVersion)
223 String orchStatus = null
224 if (utils.nodeExists(ar, "orchestration-status")) {
225 orchStatus= utils.getNodeText(ar, "orchestration-status")
231 execution.setVariable("aaiAROrchStatus", orchStatus)
233 msoLogger.trace(" Exit GetARbyLink - AR:" + ar)
237 public void updateAROrchStatus(DelegateExecution execution, String status, String aaiARPath){
238 msoLogger.trace("updaAROrchStatus ")
241 String updateReq = """
243 "orchestration-status": "${status}"
247 msoLogger.debug('AAI AR URI: ' + aaiARPath)
249 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
250 APIResponse apiResponse = aaiUriUtil.executeAAIPatchCall(execution, aaiARPath, updateReq)
251 def aaiResponse = apiResponse.getResponseBodyAsString()
252 def responseCode = apiResponse.getStatusCode()
254 msoLogger.debug("AAI Response Code: " + responseCode)
255 msoLogger.debug("AAI Response: " + aaiResponse)
256 if(responseCode == 200){
257 msoLogger.debug("UpdateAR Good REST Response is: " + "\n" + aaiResponse)
259 msoLogger.debug("UpdateAROrchStatus Bad REST Response!")
260 buildAAIErrorResponse(execution, aaiResponse, "Error updating AR OrchStatus in AAI")
264 msoLogger.debug("Rethrowing MSOWorkflowException ")
267 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in updateAR.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.getMessage());
268 exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())
270 msoLogger.trace("Exit updateAROrchStatus ")
273 //Sets Variable "wasDeleted"
274 public void deleteAR(DelegateExecution execution, String aaiARPath){
275 msoLogger.trace(" deleteAR - aaiARPath:" + aaiARPath)
277 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
278 APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, aaiARPath)
279 int responseCode = response.getStatusCode()
280 execution.setVariable("deleteARResponseCode", responseCode)
282 msoLogger.debug(" Delete AR response code:" + responseCode)
284 String aaiResponse = response.getResponseBodyAsString()
285 execution.setVariable("aaiARDeleteResponse", aaiResponse)
287 msoLogger.debug("Delete AR Response:" + aaiResponse)
290 if(responseCode == 204){
291 msoLogger.debug(" Delete AR Received a Good Response")
292 execution.setVariable("wasDeleted", "true")
293 }else if(responseCode == 404){
294 msoLogger.debug(" Delete AR Received a Not Found (404) Response")
295 }else if(responseCode == 412){
296 msoLogger.debug("Delete AR Received a Resource Version Mismatch Error: \n" + aaiResponse)
297 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")
299 msoLogger.debug("Delete AR Received a BAD REST Response: \n" + aaiResponse)
300 buildAAIErrorResponse(execution, aaiResponse, "Error deleting AR in AAI")
301 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
304 msoLogger.debug("Rethrowing MSOWorkflowException")
307 msoLogger.debug(" Error encountered in deleteAR!" + e)
308 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")
310 msoLogger.trace("Exit deleteAR ")
313 public void buildAAIErrorResponse(DelegateExecution execution, String response, String errorMessage){
314 msoLogger.trace("BuildAAIErrorResponse")
316 if((response != null) && (response.contains("Fault") || response.contains("RESTFault"))){
317 WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(response, execution)
318 execution.setVariable("WorkflowException", workflowException)
320 exceptionUtil.buildWorkflowException(execution, 500, errorMessage)
323 msoLogger.trace("Exit BuildAAIErrorResponse Process")
324 throw new BpmnError("MSOWorkflowException")
327 public String createARUrl(DelegateExecution execution, AAIResourceUri uri, String allottedResourceId) {
328 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
329 AAIResourceUri siResourceLink= uri
333 if(siResourceLink != null) {
334 msoLogger.debug("Incoming PSI Resource Link is: " + siResourceLink.build().toString())
338 String msg = "Parent Service Link in AAI is null"
340 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
342 AAIResourceUri arUri = AAIUriFactory.createResourceFromParentURI(siResourceLink, AAIObjectType.ALLOTTED_RESOURCE, allottedResourceId)
344 return aaiUriUtil.createAaiUri(arUri)