1 package org.openecomp.mso.bpmn.common.scripts
\r
3 import org.apache.commons.lang3.StringEscapeUtils;
\r
4 import org.camunda.bpm.engine.delegate.BpmnError
\r
5 import org.camunda.bpm.engine.runtime.Execution;
\r
6 import org.openecomp.mso.bpmn.core.WorkflowException
\r
7 import org.openecomp.mso.rest.APIResponse;
\r
10 import org.apache.commons.lang3.*
\r
11 import org.camunda.bpm.engine.delegate.BpmnError
\r
12 import org.camunda.bpm.engine.runtime.Execution;
\r
14 import groovy.util.XmlParser
\r
15 import groovy.util.Node
\r
16 import static org.apache.commons.lang3.StringUtils.*;
\r
18 class AllottedResourceUtils {
\r
20 private AbstractServiceTaskProcessor taskProcessor
\r
21 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
24 public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {
\r
25 this.taskProcessor = taskProcessor
\r
26 this.utils = taskProcessor.utils
\r
29 /*Used on Create - called from DoCreate
\r
30 * Using Consuming ServiceInstanceId get related Allotted Resources Orchestration status from AAI
\r
31 * 1) get related AR links for CSI 2) get AR from AR links
\r
32 * return: null -> AR Not found
\r
33 * return: " " -> AR found with empty orchStatus
\r
34 * return: orchStatus - > AR found with this orchStatus
\r
35 * setsVariable aaiARGetResponse
\r
37 public String getAROrchStatus (Execution execution) {
\r
39 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
\r
40 utils.log("DEBUG"," ***** getAROrchStatus *****", isDebugEnabled)
\r
42 String serviceInstanceId = execution.getVariable("serviceInstanceId")
\r
43 String arType = execution.getVariable("allottedResourceType")
\r
44 String arRole = execution.getVariable("allottedResourceRole")
\r
45 String siXml = execution.getVariable("CSI_service")
\r
47 String orchStatus = null
\r
48 XmlParser xmlParser = new XmlParser()
\r
49 utils.log("DEBUG","getAROrchStatus siXml:" + siXml, isDebugEnabled)
\r
51 if (!isBlank(siXml)) {
\r
52 def groovy.util.Node siNode = xmlParser.parseText(siXml)
\r
53 def groovy.util.Node relationshipList = utils.getChildNode(siNode, 'relationship-list')
\r
54 if (relationshipList != null) {
\r
55 def groovy.util.NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
\r
56 for (groovy.util.Node relationship in relationships) {
\r
57 def groovy.util.Node relatedTo = utils.getChildNode(relationship, 'related-to')
\r
58 if ((relatedTo != null) && (relatedTo.text().equals('allotted-resource'))) {
\r
59 utils.log("DEBUG","getARORchStatus AR found", isDebugEnabled)
\r
60 def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')
\r
61 if (relatedLink != null){
\r
62 ar = getARbyLink(execution, relatedLink.text(), arRole)
\r
65 orchStatus = execution.getVariable("aaiAROrchStatus")
\r
73 }catch(Exception e){
\r
74 utils.log("DEBUG", " Error encountered in getAROrchStatus" + e.getMessage(), isDebugEnabled)
\r
75 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())
\r
77 utils.log("DEBUG"," *****Exit getAROrchStatus **** OrchStatus:" + orchStatus, isDebugEnabled)
\r
81 // get Allotted Resource by AllottedResourceId
\r
82 // used on Delete - called from doDeleteAR
\r
83 // setsVariable aaiARGetResponse
\r
84 public String getARbyId (Execution execution, String allottedResourceId) {
\r
85 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
86 utils.log("DEBUG", " ***** getARbyId ***** ", isDebugEnabled)
\r
87 String arLink = getARLinkbyId(execution, allottedResourceId)
\r
89 if (!isBlank(arLink))
\r
91 ar = getARbyLink(execution, arLink, "")
\r
93 utils.log("DEBUG", " ***** Exit GetARbyId ***** AR:" + ar, isDebugEnabled)
\r
97 public String getPSIFmARLink(Execution execution, String arLink)
\r
99 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
100 // Path: /aai/{version}/business/customers/customer/{cust}/service-subscriptions/service-subscription/{subs}/service-instances/service-instance/{psiid}/allotted-resources/allotted-resource/{arid}
\r
101 utils.log("DEBUG", " ***** getPSIFmARLink ***** path:" + arLink, isDebugEnabled)
\r
102 String[] split = arLink.split("/service-instance/")
\r
103 String[] splitB = split[1].split("/allotted-resources/")
\r
104 String siId = splitB[0]
\r
105 utils.log("DEBUG", " ***** Exit getARLinkbyId ***** parentServiceInstanceId:" + siId , isDebugEnabled)
\r
109 // get Allotted Resource Link by AllottedResourceId using Nodes Query
\r
110 // used on Delete - called from getARbyId
\r
111 public String getARLinkbyId (Execution execution, String allottedResourceId) {
\r
112 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
113 utils.log("DEBUG", " ***** getARLinkbyId ***** ", isDebugEnabled)
\r
114 String arLink = null
\r
116 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
\r
117 String aaiNQUri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
\r
118 String aaiEndpoint = execution.getVariable("URN_aai_endpoint")
\r
119 String aaiUrl = "${aaiNQUri}?search-node-type=allotted-resource&filter=id:EQUALS:${allottedResourceId}"
\r
121 utils.log("DEBUG", "getARLinkbyId url: \n" + aaiUrl, isDebugEnabled)
\r
123 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, aaiUrl)
\r
124 int responseCode = response.getStatusCode()
\r
125 utils.log("DEBUG", " GET AR response code is: " + responseCode, isDebugEnabled)
\r
127 String aaiResponse = response.getResponseBodyAsString()
\r
128 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
\r
129 utils.log("DEBUG", "GET AR:" + aaiResponse, isDebugEnabled)
\r
130 if(responseCode == 200 || responseCode == 202){
\r
131 utils.log("DEBUG", "GET AR Received a Good Response Code", isDebugEnabled)
\r
132 if(utils.nodeExists(aaiResponse, "result-data")){
\r
133 utils.log("DEBUG", "Query for AllottedResource Url Response Does Contain Data" , isDebugEnabled)
\r
134 arLink = utils.getNodeText1(aaiResponse, "resource-link")
\r
136 utils.log("DEBUG", "GET AR Response Does NOT Contain Data" , isDebugEnabled)
\r
138 }else if(responseCode == 404){
\r
139 utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)
\r
142 utils.log("DEBUG", " GET AR received a Bad Response: \n" + aaiResponse, isDebugEnabled)
\r
143 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
\r
145 }catch(Exception e){
\r
146 utils.log("DEBUG", " Error encountered within GetAaiAR" + e.getMessage(), isDebugEnabled)
\r
147 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetARbyId" + e.getMessage())
\r
149 utils.log("DEBUG", " ***** Exit GetARLinkbyId ***** Link:" + arLink, isDebugEnabled)
\r
153 // get Allotted resource using Link
\r
154 // used on Create called from getARORchStatus
\r
155 // used on Delete called from getARbyId
\r
156 // setsVariable aaiARPath - used for Patch in create
\r
157 public String getARbyLink (Execution execution, String link, String role) {
\r
158 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
159 utils.log("DEBUG", " ***** getARbyLink ***** ", isDebugEnabled)
\r
161 String arUrl = null
\r
163 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
\r
164 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
\r
165 String arEndpoint = ""
\r
167 if(!isBlank(link)) {
\r
168 utils.log("DEBUG", "Incoming AR Resource Link is: " + link, isDebugEnabled)
\r
169 String[] split = link.split("/aai/")
\r
170 arEndpoint = "/aai/" + split[1]
\r
173 arUrl = "${aai_endpoint}" + arEndpoint
\r
175 utils.log("DEBUG", "GET AR Aai Path is: \n" + arUrl, isDebugEnabled)
\r
177 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, arUrl)
\r
178 int responseCode = response.getStatusCode()
\r
179 utils.log("DEBUG", " GET AR response code is: " + responseCode, isDebugEnabled)
\r
181 String aaiResponse = response.getResponseBodyAsString()
\r
182 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
\r
183 utils.log("DEBUG", "GET AR:" + aaiResponse, isDebugEnabled)
\r
184 if(responseCode == 200 || responseCode == 202){
\r
185 utils.log("DEBUG", "GET AR Received a Good Response Code", isDebugEnabled)
\r
186 if(utils.nodeExists(aaiResponse, "allotted-resource")){
\r
187 if (!isBlank(role))
\r
189 if (utils.nodeExists(aaiResponse, "role") && role.equals(utils.getNodeText1(aaiResponse, "role"))) {
\r
192 utils.log("DEBUG", "AAI AR does not match input role:" + role, isDebugEnabled)
\r
202 utils.log("DEBUG", "GET AR Does NOT Contain Data" , isDebugEnabled)
\r
204 }else if(responseCode == 404){
\r
205 utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)
\r
208 utils.log("DEBUG", " GET AR received a Bad Response: \n" + aaiResponse, isDebugEnabled)
\r
209 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")
\r
211 }catch(Exception e){
\r
212 utils.log("DEBUG", " Error encountered within GetAaiAR" + e.getMessage(), isDebugEnabled)
\r
213 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())
\r
217 execution.setVariable("aaiARGetResponse", ar)
\r
218 execution.setVariable("aaiARPath", arUrl)
\r
220 String resourceVersion = null
\r
221 if (utils.nodeExists(ar, "resource-version")) {
\r
222 resourceVersion = utils.getNodeText1(ar, "resource-version")
\r
223 execution.setVariable("aaiARResourceVersion", resourceVersion)
\r
226 String orchStatus = null
\r
227 if (utils.nodeExists(ar, "orchestration-status")) {
\r
228 orchStatus= utils.getNodeText1(ar, "orchestration-status")
\r
234 execution.setVariable("aaiAROrchStatus", orchStatus)
\r
236 utils.log("DEBUG", " ***** Exit GetARbyLink ***** AR:" + ar, isDebugEnabled)
\r
240 public void updateAROrchStatus(Execution execution, String status, String aaiARPath){
\r
241 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
\r
242 utils.log("DEBUG", " *** updaAROrchStatus *** ", isDebugEnabled)
\r
245 String updateReq = """
\r
247 "orchestration-status": "${status}"
\r
251 utils.log("DEBUG", 'AAI AR URI: ' + aaiARPath, isDebugEnabled)
\r
253 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
\r
254 APIResponse apiResponse = aaiUriUtil.executeAAIPatchCall(execution, aaiARPath, updateReq)
\r
255 def aaiResponse = StringEscapeUtils.unescapeXml(apiResponse.getResponseBodyAsString())
\r
256 def responseCode = apiResponse.getStatusCode()
\r
258 utils.logAudit("AAI Response Code: " + responseCode)
\r
259 utils.logAudit("AAI Response: " + aaiResponse)
\r
260 if(responseCode == 200){
\r
261 utils.log("DEBUG", "UpdateAR Good REST Response is: " + "\n" + aaiResponse, isDebugEnabled)
\r
263 utils.log("DEBUG", "UpdateAROrchStatus Bad REST Response!", isDebugEnabled)
\r
264 buildAAIErrorResponse(execution, aaiResponse, "Error updating AR OrchStatus in AAI")
\r
267 }catch(BpmnError b){
\r
268 utils.log("DEBUG", "Rethrowing MSOWorkflowException ", isDebugEnabled)
\r
270 }catch(Exception e){
\r
271 utils.log("ERROR", "Exception in updateAR. Exception is:\n" + e.getMessage(), isDebugEnabled)
\r
272 exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())
\r
274 utils.log("DEBUG", " *** Exit updateAROrchStatus *** ", isDebugEnabled)
\r
277 //Sets Variable "wasDeleted"
\r
278 public void deleteAR(Execution execution, String aaiARPath){
\r
279 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
280 utils.log("DEBUG", " *** deleteAR *** aaiARPath:" + aaiARPath, isDebugEnabled)
\r
282 AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)
\r
283 APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, aaiARPath)
\r
284 int responseCode = response.getStatusCode()
\r
285 execution.setVariable("deleteARResponseCode", responseCode)
\r
287 utils.log("DEBUG", " Delete AR response code:" + responseCode, isDebugEnabled)
\r
289 String aaiResponse = response.getResponseBodyAsString()
\r
290 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
\r
291 execution.setVariable("aaiARDeleteResponse", aaiResponse)
\r
293 utils.log("DEBUG", "Delete AR Response:" + aaiResponse)
\r
295 if(responseCode == 204){
\r
296 utils.log("DEBUG", " Delete AR Received a Good Response", isDebugEnabled)
\r
297 execution.setVariable("wasDeleted", "true")
\r
298 }else if(responseCode == 404){
\r
299 utils.log("DEBUG", " Delete AR Received a Not Found (404) Response", isDebugEnabled)
\r
300 }else if(responseCode == 412){
\r
301 utils.log("DEBUG", "Delete AR Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)
\r
302 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")
\r
304 utils.log("DEBUG", "Delete AR Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
\r
305 buildAAIErrorResponse(execution, aaiResponse, "Error deleting AR in AAI")
\r
306 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
\r
308 }catch(BpmnError b){
\r
309 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
\r
311 }catch(Exception e){
\r
312 utils.log("DEBUG", " Error encountered in deleteAR!" + e, isDebugEnabled)
\r
313 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")
\r
315 utils.log("DEBUG", " *** Exit deleteAR *** ", isDebugEnabled)
\r
318 public void buildAAIErrorResponse(Execution execution, String response, String errorMessage){
\r
319 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
320 utils.log("DEBUG", " *** BuildAAIErrorResponse*** ", isDebugEnabled)
\r
322 if((response != null) && (response.contains("Fault") || response.contains("RESTFault"))){
\r
323 WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(response, execution)
\r
324 execution.setVariable("WorkflowException", workflowException)
\r
326 exceptionUtil.buildWorkflowException(execution, 500, errorMessage)
\r
329 utils.log("DEBUG", " *** Exit BuildAAIErrorResponse Process*** ", isDebugEnabled)
\r
330 throw new BpmnError("MSOWorkflowException")
\r