540fe036fc38cc56d22e1db4b78756dae9e6cada
[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 class AllottedResourceUtils {\r
10         \r
11         private AbstractServiceTaskProcessor taskProcessor\r
12         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
13         MsoUtils utils;\r
14 \r
15         public AllottedResourceUtils(AbstractServiceTaskProcessor taskProcessor) {\r
16                 this.taskProcessor = taskProcessor\r
17                 this.utils = taskProcessor.utils\r
18         }\r
19 \r
20         /*Used on Create - called from DoCreate\r
21         * Using Consuming ServiceInstanceId get related Allotted Resources Orchestration status from AAI\r
22         * 1) get related AR links for CSI 2) get AR from AR links\r
23         * return: null -> AR Not found\r
24         * return: " " -> AR found with empty orchStatus\r
25         * return: orchStatus - > AR found with this orchStatus\r
26         * setsVariable aaiARGetResponse\r
27         */\r
28         public String getAROrchStatus (Execution execution) {\r
29 \r
30                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
31                 utils.log("DEBUG"," ***** getAROrchStatus *****", isDebugEnabled)\r
32                 String msg = ""\r
33                 String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
34                 String arType = execution.getVariable("allottedResourceType")\r
35                 String arRole = execution.getVariable("allottedResourceRole")\r
36                 String siXml = execution.getVariable("CSI_service")\r
37                 String ar = null\r
38                 String orchStatus = null\r
39                 XmlParser xmlParser = new XmlParser()\r
40                 utils.log("DEBUG","getAROrchStatus siXml:" + siXml, isDebugEnabled)\r
41                 try {\r
42                         if (!isBlank(siXml)) {\r
43                                 def groovy.util.Node siNode = xmlParser.parseText(siXml)\r
44                                 def groovy.util.Node relationshipList = utils.getChildNode(siNode, 'relationship-list')\r
45                                 if (relationshipList != null) {\r
46                                         def groovy.util.NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')\r
47                                         for (groovy.util.Node relationship in relationships) {\r
48                                                 def groovy.util.Node relatedTo = utils.getChildNode(relationship, 'related-to')\r
49                                                 if ((relatedTo != null) && (relatedTo.text().equals('allotted-resource'))) {\r
50                                                         utils.log("DEBUG","getARORchStatus AR found", isDebugEnabled)\r
51                                                         def groovy.util.Node relatedLink = utils.getChildNode(relationship, 'related-link')\r
52                                                         if (relatedLink != null){\r
53                                                                 ar = getARbyLink(execution, relatedLink.text(), arRole)\r
54                                                                 if (!isBlank(ar))\r
55                                                                 {\r
56                                                                         orchStatus = execution.getVariable("aaiAROrchStatus")\r
57                                                                         break\r
58                                                                 }\r
59                                                         }\r
60                                                 }\r
61                                         }\r
62                                 }\r
63                         }\r
64                 }catch(Exception e){\r
65                         utils.log("DEBUG", " Error encountered in getAROrchStatus" + e.getMessage(), isDebugEnabled)\r
66                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in getAROrchStatus" + e.getMessage())\r
67                 }\r
68                 utils.log("DEBUG"," *****Exit getAROrchStatus **** OrchStatus:" + orchStatus, isDebugEnabled)\r
69                 return orchStatus\r
70         }\r
71 \r
72         // get Allotted Resource by AllottedResourceId\r
73         // used on Delete - called from doDeleteAR\r
74         // setsVariable aaiARGetResponse\r
75         public String getARbyId (Execution execution, String allottedResourceId) {\r
76                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
77                 utils.log("DEBUG", " ***** getARbyId ***** ", isDebugEnabled)\r
78                 String arLink = getARLinkbyId(execution, allottedResourceId)\r
79                 String ar = null\r
80                 if (!isBlank(arLink))\r
81                 {\r
82                         ar = getARbyLink(execution, arLink, "")\r
83                 }\r
84                 utils.log("DEBUG", " ***** Exit GetARbyId ***** AR:" + ar, isDebugEnabled)\r
85                 return ar;\r
86         }\r
87 \r
88         // get Allotted Resource Link by AllottedResourceId using Nodes Query\r
89         // used on Delete - called from getARbyId\r
90         public String getARLinkbyId (Execution execution, String allottedResourceId) {\r
91                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
92                 utils.log("DEBUG", " ***** getARLinkbyId ***** ", isDebugEnabled)\r
93                 String arLink = null\r
94                 try {\r
95                         AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)\r
96                         String aaiNQUri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)\r
97                         String aaiEndpoint = execution.getVariable("URN_aai_endpoint")\r
98                         String aaiUrl = "${aaiNQUri}?search-node-type=allotted-resource&filter=id:EQUALS:${allottedResourceId}"\r
99 \r
100                         utils.log("DEBUG", "getARLinkbyId url: \n" + aaiUrl, isDebugEnabled)\r
101 \r
102                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, aaiUrl)\r
103                         int responseCode = response.getStatusCode()\r
104                         utils.log("DEBUG", "  GET AR response code is: " + responseCode, isDebugEnabled)\r
105 \r
106                         String aaiResponse = response.getResponseBodyAsString()\r
107                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
108                         utils.log("DEBUG", "GET AR:" + aaiResponse, isDebugEnabled)\r
109                         if(responseCode == 200 || responseCode == 202){\r
110                                 utils.log("DEBUG", "GET AR Received a Good Response Code", isDebugEnabled)\r
111                                 if(utils.nodeExists(aaiResponse, "result-data")){\r
112                                         utils.log("DEBUG", "Query for AllottedResource Url Response Does Contain Data" , isDebugEnabled)\r
113                                         arLink = utils.getNodeText1(aaiResponse, "resource-link")\r
114                                 }else{\r
115                                         utils.log("DEBUG", "GET AR Response Does NOT Contain Data" , isDebugEnabled)\r
116                                 }\r
117                         }else if(responseCode == 404){\r
118                                 utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)\r
119                         }\r
120                         else{\r
121                                 utils.log("DEBUG", "  GET AR received a Bad Response: \n" + aaiResponse, isDebugEnabled)\r
122                                 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")\r
123                         }\r
124                 }catch(Exception e){\r
125                         utils.log("DEBUG", " Error encountered within GetAaiAR" + e.getMessage(), isDebugEnabled)\r
126                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetARbyId" + e.getMessage())\r
127                 }\r
128                 utils.log("DEBUG", " ***** Exit GetARLinkbyId ***** Link:" + arLink, isDebugEnabled)\r
129                 return arLink\r
130         }\r
131 \r
132         // get Allotted resource using Link\r
133         // used on Create called from getARORchStatus\r
134         // used on Delete called from getARbyId\r
135         // setsVariable aaiARPath - used for Patch in create\r
136         public String getARbyLink (Execution execution, String link, String role) {\r
137                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
138                 utils.log("DEBUG", " ***** getARbyLink ***** ", isDebugEnabled)\r
139                 String ar = null\r
140                 String arUrl = null\r
141                 try {\r
142                         AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)\r
143                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
144                         String arEndpoint = ""\r
145 \r
146                         if(!isBlank(link)) {\r
147                                 utils.log("DEBUG", "Incoming AR Resource Link is: " + link, isDebugEnabled)\r
148                                 String[] split = link.split("/aai/")\r
149                                 arEndpoint = "/aai/" + split[1]\r
150                         }\r
151 \r
152                         arUrl = "${aai_endpoint}" + arEndpoint\r
153                 \r
154                         utils.log("DEBUG", "GET AR Aai Path is: \n" + arUrl, isDebugEnabled)\r
155 \r
156                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, arUrl)\r
157                         int responseCode = response.getStatusCode()\r
158                         utils.log("DEBUG", "  GET AR response code is: " + responseCode, isDebugEnabled)\r
159 \r
160                         String aaiResponse = response.getResponseBodyAsString()\r
161                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
162                         utils.log("DEBUG", "GET AR:" + aaiResponse, isDebugEnabled)\r
163                         if(responseCode == 200 || responseCode == 202){\r
164                                 utils.log("DEBUG", "GET AR Received a Good Response Code", isDebugEnabled)\r
165                                 if(utils.nodeExists(aaiResponse, "allotted-resource")){\r
166                                         if (!isBlank(role))\r
167                                         {\r
168                                                 if (utils.nodeExists(aaiResponse, "role") && role.equals(utils.getNodeText1(aaiResponse, "role"))) {\r
169                                                         ar = aaiResponse\r
170                                                 }else{\r
171                                                         utils.log("DEBUG", "AAI AR does not match input role:" + role, isDebugEnabled)\r
172                                                 }\r
173                                         }\r
174                                         else\r
175                                         {\r
176                                                 ar = aaiResponse\r
177                                         }\r
178                                 }\r
179                                 else\r
180                                 {\r
181                                         utils.log("DEBUG", "GET AR Does NOT Contain Data" , isDebugEnabled)\r
182                                 }\r
183                         }else if(responseCode == 404){\r
184                                 utils.log("DEBUG", "GET AR received a Not Found (404) Response", isDebugEnabled)\r
185                         }\r
186                         else{\r
187                                 utils.log("DEBUG", "  GET AR received a Bad Response: \n" + aaiResponse, isDebugEnabled)\r
188                                 buildAAIErrorResponse(execution, aaiResponse, "Error retrieving AR from AAI")\r
189                         }\r
190                 }catch(Exception e){\r
191                         utils.log("DEBUG", " Error encountered within GetAaiAR" + e.getMessage(), isDebugEnabled)\r
192                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error in GetAaiAR" + e.getMessage())\r
193                 }\r
194                 if (!isBlank(ar))\r
195                 {\r
196                         execution.setVariable("aaiARGetResponse", ar)\r
197                         execution.setVariable("aaiARPath", arUrl)\r
198                         \r
199                         String resourceVersion = null\r
200                         if (utils.nodeExists(ar, "resource-version")) {\r
201                                 resourceVersion = utils.getNodeText1(ar, "resource-version")\r
202                                 execution.setVariable("aaiARResourceVersion", resourceVersion)\r
203                         }\r
204                         \r
205                         String orchStatus = null\r
206                         if (utils.nodeExists(ar, "orchestration-status")) {\r
207                                 orchStatus= utils.getNodeText1(ar, "orchestration-status")\r
208                         }\r
209                         else\r
210                         {\r
211                                 orchStatus = " "\r
212                         }\r
213                         execution.setVariable("aaiAROrchStatus", orchStatus)\r
214                 }\r
215                 utils.log("DEBUG", " ***** Exit GetARbyLink ***** AR:" + ar, isDebugEnabled)\r
216                 return ar\r
217         }\r
218 \r
219         public void updateAROrchStatus(Execution execution, String status, String aaiARPath){\r
220                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
221                 utils.log("DEBUG", " *** updaAROrchStatus *** ", isDebugEnabled)\r
222                 try{\r
223 \r
224                         String updateReq =      """\r
225                                 {\r
226                                 "orchestration-status": "Created""\r
227                                 }\r
228                                 """\r
229 \r
230                         utils.log("DEBUG", 'AAI AR URI: ' + aaiARPath, isDebugEnabled)\r
231 \r
232                         AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)\r
233                         APIResponse apiResponse = aaiUriUtil.executeAAIPatchCall(execution, aaiARPath, updateReq)\r
234                         def aaiResponse = StringEscapeUtils.unescapeXml(apiResponse.getResponseBodyAsString())\r
235                         def responseCode = apiResponse.getStatusCode()\r
236 \r
237                         utils.logAudit("AAI Response Code: " + responseCode)\r
238                         utils.logAudit("AAI Response: " + aaiResponse)\r
239                         if(responseCode == 200){\r
240                                 utils.log("DEBUG", "UpdateAR Good REST Response is: " + "\n" + aaiResponse, isDebugEnabled)\r
241                         }else{\r
242                                 utils.log("DEBUG", "UpdateAROrchStatus Bad REST Response!", isDebugEnabled)\r
243                                 buildAAIErrorResponse(execution, aaiResponse, "Error updating AR OrchStatus in AAI")\r
244                         }\r
245 \r
246                 }catch(BpmnError b){\r
247                         utils.log("DEBUG", "Rethrowing MSOWorkflowException ", isDebugEnabled)\r
248                         throw b\r
249                 }catch(Exception e){\r
250                         utils.log("ERROR", "Exception in updateAR. Exception is:\n" + e.getMessage(), isDebugEnabled)\r
251                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, 'Internal Error in updateAROrchStatus.' + e.getMessage())\r
252                 }\r
253                 utils.log("DEBUG", " *** Exit updateAROrchStatus *** ", isDebugEnabled)\r
254         }\r
255         \r
256         //Sets Variable "wasDeleted"\r
257         public void deleteAR(Execution execution, String aaiARPath){\r
258                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
259                 utils.log("DEBUG", " *** deleteAR *** aaiARPath:" + aaiARPath, isDebugEnabled)\r
260                 try {\r
261                         AaiUtil aaiUriUtil = new AaiUtil(taskProcessor)\r
262                         APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, aaiARPath)\r
263                         int responseCode = response.getStatusCode()\r
264                         execution.setVariable("deleteARResponseCode", responseCode)\r
265                         \r
266                         utils.log("DEBUG", "  Delete AR response code:" + responseCode, isDebugEnabled)\r
267 \r
268                         String aaiResponse = response.getResponseBodyAsString()\r
269                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
270                         execution.setVariable("aaiARDeleteResponse", aaiResponse)\r
271 \r
272                         utils.log("DEBUG", "Delete AR Response:" + aaiResponse)\r
273                         //Process Response\r
274                         if(responseCode == 204){\r
275                                 utils.log("DEBUG", "  Delete AR Received a Good Response", isDebugEnabled)\r
276                                 execution.setVariable("wasDeleted", "true")\r
277                         }else if(responseCode == 404){\r
278                                 utils.log("DEBUG", "  Delete AR Received a Not Found (404) Response", isDebugEnabled)\r
279                         }else if(responseCode == 412){\r
280                                 utils.log("DEBUG", "Delete AR Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)\r
281                                 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "DeleteAR Received a resource-version Mismatch Error Response from AAI")\r
282                         }else{\r
283                                 utils.log("DEBUG", "Delete AR Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)\r
284                                 buildAAIErrorResponse(execution, aaiResponse, "Error deleting AR in AAI")\r
285                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
286                         }\r
287                 }catch(BpmnError b){\r
288                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
289                         throw b\r
290                 }catch(Exception e){\r
291                         utils.log("DEBUG", " Error encountered in deleteAR!" + e, isDebugEnabled)\r
292                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete AR")\r
293                 }\r
294                 utils.log("DEBUG", " *** Exit deleteAR *** ", isDebugEnabled)\r
295         }\r
296 \r
297         public void buildAAIErrorResponse(Execution execution, String response, String errorMessage){\r
298                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
299                 utils.log("DEBUG", " *** BuildAAIErrorResponse*** ", isDebugEnabled)\r
300 \r
301                 if((response != null) && (response.contains("Fault") || response.contains("RESTFault"))){\r
302                         WorkflowException workflowException = exceptionUtil.MapAAIExceptionToWorkflowException(response, execution)\r
303                         execution.setVariable("WorkflowException", workflowException)\r
304                 }else{\r
305                         exceptionUtil.buildWorkflowException(execution, 500, errorMessage)\r
306                 }\r
307 \r
308                 utils.log("DEBUG", " *** Exit BuildAAIErrorResponse Process*** ", isDebugEnabled)\r
309                 throw new BpmnError("MSOWorkflowException")\r
310         }\r
311 \r
312 }