1710 Rebase - Second Attempt
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / AllottedResourceUtils.groovy
1 package org.openecomp.mso.bpmn.common.scripts\r
2 \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
8 \r
9 \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
13 \r
14 import groovy.util.XmlParser\r
15 import groovy.util.Node\r
16 import static org.apache.commons.lang3.StringUtils.*;\r
17 \r
18 class AllottedResourceUtils {\r
19 \r
20         private AbstractServiceTaskProcessor taskProcessor\r
21         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
22         MsoUtils utils;\r
23 \r
24         public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {\r
25                 this.taskProcessor = taskProcessor\r
26                 this.utils = taskProcessor.utils\r
27         }\r
28 \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
36         */\r
37         public String getAROrchStatus (Execution execution) {\r
38 \r
39                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
40                 utils.log("DEBUG"," ***** getAROrchStatus *****", isDebugEnabled)\r
41                 String msg = ""\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
46                 String ar = null\r
47                 String orchStatus = null\r
48                 XmlParser xmlParser = new XmlParser()\r
49                 utils.log("DEBUG","getAROrchStatus siXml:" + siXml, isDebugEnabled)\r
50                 try {\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
63                                                                 if (!isBlank(ar))\r
64                                                                 {\r
65                                                                         orchStatus = execution.getVariable("aaiAROrchStatus")\r
66                                                                         break\r
67                                                                 }\r
68                                                         }\r
69                                                 }\r
70                                         }\r
71                                 }\r
72                         }\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
76                 }\r
77                 utils.log("DEBUG"," *****Exit getAROrchStatus **** OrchStatus:" + orchStatus, isDebugEnabled)\r
78                 return orchStatus\r
79         }\r
80 \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
88                 String ar = null\r
89                 if (!isBlank(arLink))\r
90                 {\r
91                         ar = getARbyLink(execution, arLink, "")\r
92                 }\r
93                 utils.log("DEBUG", " ***** Exit GetARbyId ***** AR:" + ar, isDebugEnabled)\r
94                 return ar;\r
95         }\r
96         \r
97         public String getPSIFmARLink(Execution execution, String arLink)\r
98         {\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
106                 return siId\r
107         }\r
108 \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
115                 try {\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
120 \r
121                         utils.log("DEBUG", "getARLinkbyId url: \n" + aaiUrl, isDebugEnabled)\r
122 \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
126 \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
135                                 }else{\r
136                                         utils.log("DEBUG", "GET AR Response Does NOT Contain Data" , isDebugEnabled)\r
137                                 }\r
138                         }else if(responseCode == 404){\r
139                                 utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)\r
140                         }\r
141                         else{\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
144                         }\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
148                 }\r
149                 utils.log("DEBUG", " ***** Exit GetARLinkbyId ***** Link:" + arLink, isDebugEnabled)\r
150                 return arLink\r
151         }\r
152 \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
160                 String ar = null\r
161                 String arUrl = null\r
162                 try {\r
163                         AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)\r
164                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
165                         String arEndpoint = ""\r
166 \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
171                         }\r
172 \r
173                         arUrl = "${aai_endpoint}" + arEndpoint\r
174                 \r
175                         utils.log("DEBUG", "GET AR Aai Path is: \n" + arUrl, isDebugEnabled)\r
176 \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
180 \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
188                                         {\r
189                                                 if (utils.nodeExists(aaiResponse, "role") && role.equals(utils.getNodeText1(aaiResponse, "role"))) {\r
190                                                         ar = aaiResponse\r
191                                                 }else{\r
192                                                         utils.log("DEBUG", "AAI AR does not match input role:" + role, isDebugEnabled)\r
193                                                 }\r
194                                         }\r
195                                         else\r
196                                         {\r
197                                                 ar = aaiResponse\r
198                                         }\r
199                                 }\r
200                                 else\r
201                                 {\r
202                                         utils.log("DEBUG", "GET AR Does NOT Contain Data" , isDebugEnabled)\r
203                                 }\r
204                         }else if(responseCode == 404){\r
205                                 utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)\r
206                         }\r
207                         else{\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
210                         }\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
214                 }\r
215                 if (!isBlank(ar))\r
216                 {\r
217                         execution.setVariable("aaiARGetResponse", ar)\r
218                         execution.setVariable("aaiARPath", arUrl)\r
219                         \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
224                         }\r
225                         \r
226                         String orchStatus = null\r
227                         if (utils.nodeExists(ar, "orchestration-status")) {\r
228                                 orchStatus= utils.getNodeText1(ar, "orchestration-status")\r
229                         }\r
230                         else\r
231                         {\r
232                                 orchStatus = " "\r
233                         }\r
234                         execution.setVariable("aaiAROrchStatus", orchStatus)\r
235                 }\r
236                 utils.log("DEBUG", " ***** Exit GetARbyLink ***** AR:" + ar, isDebugEnabled)\r
237                 return ar\r
238         }\r
239 \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
243                 try{\r
244 \r
245                         String updateReq =      """\r
246                                         {\r
247                                         "orchestration-status": "${status}"\r
248                                         }\r
249                                         """\r
250 \r
251                         utils.log("DEBUG", 'AAI AR URI: ' + aaiARPath, isDebugEnabled)\r
252 \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
257 \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
262                         }else{\r
263                                 utils.log("DEBUG", "UpdateAROrchStatus Bad REST Response!", isDebugEnabled)\r
264                                 buildAAIErrorResponse(execution, aaiResponse, "Error updating AR OrchStatus in AAI")\r
265                         }\r
266 \r
267                 }catch(BpmnError b){\r
268                         utils.log("DEBUG", "Rethrowing MSOWorkflowException ", isDebugEnabled)\r
269                         throw b\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
273                 }\r
274                 utils.log("DEBUG", " *** Exit updateAROrchStatus *** ", isDebugEnabled)\r
275         }\r
276         \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
281                 try {\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
286                         \r
287                         utils.log("DEBUG", "  Delete AR response code:" + responseCode, isDebugEnabled)\r
288 \r
289                         String aaiResponse = response.getResponseBodyAsString()\r
290                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
291                         execution.setVariable("aaiARDeleteResponse", aaiResponse)\r
292 \r
293                         utils.log("DEBUG", "Delete AR Response:" + aaiResponse)\r
294                         //Process Response\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
303                         }else{\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
307                         }\r
308                 }catch(BpmnError b){\r
309                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
310                         throw b\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
314                 }\r
315                 utils.log("DEBUG", " *** Exit deleteAR *** ", isDebugEnabled)\r
316         }\r
317 \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
321 \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
325                 }else{\r
326                         exceptionUtil.buildWorkflowException(execution, 500, errorMessage)\r
327                 }\r
328 \r
329                 utils.log("DEBUG", " *** Exit BuildAAIErrorResponse Process*** ", isDebugEnabled)\r
330                 throw new BpmnError("MSOWorkflowException")\r
331         }\r
332 \r
333 }