Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteVFCNetworkServiceInstance.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
22 \r
23 import static org.apache.commons.lang3.StringUtils.*\r
24 import groovy.xml.XmlUtil\r
25 import groovy.json.*\r
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor \r
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil \r
28 import org.openecomp.mso.bpmn.core.WorkflowException \r
29 import org.openecomp.mso.bpmn.core.json.JsonUtils \r
30 import org.openecomp.mso.rest.APIResponse\r
31 \r
32 import java.util.UUID;\r
33 \r
34 import org.camunda.bpm.engine.delegate.BpmnError \r
35 import org.camunda.bpm.engine.delegate.DelegateExecution\r
36 import org.apache.commons.lang3.*\r
37 import org.apache.commons.codec.binary.Base64;\r
38 import org.springframework.web.util.UriUtils \r
39 import org.openecomp.mso.rest.RESTClient \r
40 import org.openecomp.mso.rest.RESTConfig\r
41 import org.openecomp.mso.rest.APIResponse;\r
42 import org.openecomp.mso.rest.RESTConfig\r
43 import org.openecomp.mso.rest.RESTClient\r
44 \r
45 \r
46 \r
47 /**\r
48  * This groovy class supports the <class>DoDeleteVFCNetworkServiceInstance.bpmn</class> process.\r
49  * flow for E2E ServiceInstance Delete\r
50  */\r
51 public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {\r
52 \r
53             \r
54     String vfcUrl = "/vfc/rest/v1/vfcadapter"\r
55     \r
56     String host = "http://mso.mso.testlab.openecomp.org:8080"\r
57     \r
58     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
59 \r
60     JsonUtils jsonUtil = new JsonUtils()\r
61 \r
62     /**\r
63      * Pre Process the BPMN Flow Request\r
64      * Inclouds:\r
65      * generate the nsOperationKey\r
66      */\r
67     public void preProcessRequest (DelegateExecution execution) {\r
68         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
69         String msg = ""\r
70         utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)\r
71         try {\r
72             //deal with operation key\r
73             String globalSubscriberId = execution.getVariable("globalSubscriberId")\r
74             utils.log("INFO", "globalSubscriberId:" + globalSubscriberId, isDebugEnabled)\r
75             String serviceType = execution.getVariable("serviceType")\r
76             utils.log("INFO", "serviceType:" + serviceType, isDebugEnabled)\r
77             String serviceId = execution.getVariable("serviceId")\r
78             utils.log("INFO", "serviceId:" + serviceId, isDebugEnabled)\r
79             String operationId = execution.getVariable("operationId")\r
80             utils.log("INFO", "serviceType:" + serviceType, isDebugEnabled)\r
81             String nodeTemplateUUID = execution.getVariable("resourceTemplateId")\r
82             utils.log("INFO", "nodeTemplateUUID:" + nodeTemplateUUID, isDebugEnabled)\r
83             String nsInstanceId = execution.getVariable("resourceInstanceId")\r
84             utils.log("INFO", "nsInstanceId:" + nsInstanceId, isDebugEnabled)\r
85             execution.setVariable("nsInstanceId",nsInstanceId)\r
86             String nsOperationKey = """{\r
87             "globalSubscriberId":"${globalSubscriberId}",\r
88             "serviceType":"${serviceType}",\r
89             "serviceId":"${serviceId}",\r
90             "operationId":"${operationId}",\r
91             "nodeTemplateUUID":"${nodeTemplateUUID}"\r
92              }"""\r
93             execution.setVariable("nsOperationKey", nsOperationKey);\r
94             utils.log("INFO", "nsOperationKey:" + nsOperationKey, isDebugEnabled)\r
95         } catch (BpmnError e) {\r
96             throw e;\r
97         } catch (Exception ex){\r
98             msg = "Exception in preProcessRequest " + ex.getMessage()\r
99             utils.log("INFO", msg, isDebugEnabled)\r
100             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
101         }\r
102         utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
103         }\r
104 \r
105     /**\r
106      * unwind NS from AAI relationship\r
107      */\r
108     public void deleteNSRelationship(DelegateExecution execution) {\r
109         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
110         utils.log("INFO"," ***** deleteNSRelationship *****",  isDebugEnabled)\r
111         String nsInstanceId = execution.getVariable("resourceInstanceId")\r
112         if(nsInstanceId == null || nsInstanceId == ""){\r
113             utils.log("INFO"," Delete NS failed",  isDebugEnabled)\r
114             return\r
115         }\r
116         String globalSubscriberId = execution.getVariable("globalSubscriberId")\r
117         String serviceType = execution.getVariable("serviceType")\r
118         String serviceId = execution.getVariable("serviceId")\r
119         String deleteRelationPayload = """<relationship xmlns="http://org.openecomp.aai.inventory/v11">\r
120                                             <related-to>service-instance</related-to>\r
121                                             <related-link>/aai/v11/business/customers/customer/${globalSubscriberId}/service-subscriptions/service-subscription/${serviceType}/service-instances/service-instance/${nsInstanceId}</related-link>\r
122                                             <relationship-data>\r
123                                                 <relationship-key>customer.global-customer-id</relationship-key>\r
124                                                 <relationship-value>${globalSubscriberId}</relationship-value>\r
125                                             </relationship-data>\r
126                                             <relationship-data>\r
127                                                 <relationship-key>service-subscription.service-type</relationship-key>\r
128                                                 <relationship-value>${serviceType}</relationship-value>\r
129                                             </relationship-data>\r
130                                            <relationship-data>\r
131                                                 <relationship-key>service-instance.service-instance-id</relationship-key>\r
132                                                 <relationship-value>${nsInstanceId}</relationship-value>\r
133                                             </relationship-data>           \r
134                                         </relationship>"""\r
135         String endpoint = execution.getVariable("URN_aai_endpoint")\r
136         utils.log("INFO","Delete Relationship req:\n" + deleteRelationPayload,  isDebugEnabled)\r
137         String url = endpoint + "/aai/v11/business/customers/customer/" + globalSubscriberId + "/service-subscriptions/service-subscription/" + serviceType + "/service-instances/service-instance/" + serviceId + "/relationship-list/relationship"\r
138 \r
139         APIResponse aaiRsp = executeAAIDeleteCall(execution, url, deleteRelationPayload)\r
140         utils.log("INFO","aai response status code:" + aaiRsp.getStatusCode(),  isDebugEnabled)\r
141         utils.log("INFO","aai response content:" + aaiRsp.getResponseBodyAsString(),  isDebugEnabled)\r
142         utils.log("INFO"," *****Exit deleteNSRelationship *****",  isDebugEnabled)\r
143     }\r
144 \r
145     public APIResponse executeAAIDeleteCall(DelegateExecution execution, String url, String payload){\r
146         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
147         utils.log("INFO", " ======== Started Execute AAI Delete Process ======== ",  isDebugEnabled)\r
148         APIResponse apiResponse = null\r
149         try{\r
150             String uuid = utils.getRequestID()\r
151             utils.log("INFO","Generated uuid is: " + uuid,  isDebugEnabled)\r
152             utils.log("INFO","URL to be used is: " + url,  isDebugEnabled)\r
153             String userName = execution.getVariable("URN_aai_auth")\r
154             String password = execution.getVariable("URN_mso_msoKey")\r
155             String basicAuthCred = utils.getBasicAuth(userName,password)\r
156             RESTConfig config = new RESTConfig(url);\r
157             RESTClient client = new RESTClient(config).addHeader("X-FromAppId", "MSO").addHeader("X-TransactionId", uuid).addHeader("Content-Type", "application/xml").addHeader("Accept","application/xml");\r
158             if (basicAuthCred != null && !"".equals(basicAuthCred)) {\r
159                 client.addAuthorizationHeader(basicAuthCred)\r
160             }\r
161             apiResponse = client.httpDelete(payload)\r
162             utils.log("INFO","======== Completed Execute AAI Delete Process ======== ",  isDebugEnabled)\r
163         }catch(Exception e){\r
164             utils.log("ERROR","Exception occured while executing AAI Put Call. Exception is: \n" + e,  isDebugEnabled)\r
165             throw new BpmnError("MSOWorkflowException")\r
166         }\r
167         return apiResponse\r
168     }\r
169 \r
170     /**\r
171      * delete NS task\r
172      */\r
173     public void deleteNetworkService(DelegateExecution execution) {\r
174         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
175         utils.log("INFO", " *** deleteNetworkService  start *** ", isDebugEnabled)\r
176         String nsOperationKey = execution.getVariable("nsOperationKey");\r
177         String url = host + vfcUrl + "/ns/" + execution.getVariable("nsInstanceId") \r
178         APIResponse apiResponse = deleteRequest(execution, url, nsOperationKey)\r
179         String returnCode = apiResponse.getStatusCode()\r
180         String apiResponseAsString = apiResponse.getResponseBodyAsString()\r
181         String operationStatus = "error";\r
182         if(returnCode== "200" || returnCode== "202"){\r
183             operationStatus = "finished"\r
184         }\r
185         execution.setVariable("operationStatus", operationStatus)\r
186         \r
187         utils.log("INFO", " *** deleteNetworkService  end *** ", isDebugEnabled)\r
188     }\r
189 \r
190     /**\r
191      * instantiate NS task\r
192      */\r
193     public void terminateNetworkService(DelegateExecution execution) {\r
194         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
195         utils.log("INFO", " *** terminateNetworkService  start *** ", isDebugEnabled)\r
196         String nsOperationKey = execution.getVariable("nsOperationKey") \r
197         String url =  host + vfcUrl + "/ns/" + execution.getVariable("nsInstanceId") + "/terminate"\r
198         APIResponse apiResponse = postRequest(execution, url, nsOperationKey)\r
199         String returnCode = apiResponse.getStatusCode()\r
200         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
201         String jobId = "";\r
202         if(returnCode== "200" || returnCode== "202"){\r
203             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")\r
204         }\r
205         execution.setVariable("jobId", jobId)   \r
206         utils.log("INFO", " *** terminateNetworkService  end *** ", isDebugEnabled)\r
207     }\r
208 \r
209     /**\r
210      * query NS task\r
211      */\r
212     public void queryNSProgress(DelegateExecution execution) {\r
213         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
214         utils.log("INFO", " *** queryNSProgress  start *** ", isDebugEnabled)\r
215         String jobId = execution.getVariable("jobId")\r
216         String nsOperationKey = execution.getVariable("nsOperationKey");\r
217         String url =  host + vfcUrl + "/jobs/" +  execution.getVariable("jobId") \r
218         APIResponse apiResponse = postRequest(execution, url, nsOperationKey)\r
219         String returnCode = apiResponse.getStatusCode()\r
220         String apiResponseAsString = apiResponse.getResponseBodyAsString()\r
221         String operationProgress = "100"\r
222         if(returnCode== "200"){\r
223             operationProgress = jsonUtil.getJsonValue(apiResponseAsString, "responseDescriptor.progress")\r
224         }\r
225         execution.setVariable("operationProgress", operationProgress)\r
226         utils.log("INFO", " *** queryNSProgress  end *** ", isDebugEnabled)\r
227     }\r
228 \r
229     /**\r
230      * delay 5 sec \r
231      */\r
232     public void timeDelay(DelegateExecution execution) {\r
233         try {\r
234             Thread.sleep(5000);\r
235         } catch(InterruptedException e) {           \r
236             utils.log("INFO", "Time Delay exception" + e, isDebugEnabled)\r
237         }\r
238     }\r
239 \r
240     /**\r
241      * finish NS task\r
242      */\r
243     public void finishNSDelete(DelegateExecution execution) {\r
244         //no need to do anything util now\r
245     }\r
246 \r
247     /**\r
248      * post request\r
249      * url: the url of the request\r
250      * requestBody: the body of the request\r
251      */\r
252     private APIResponse postRequest(DelegateExecution execution, String url, String requestBody){\r
253         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
254         utils.log("INFO", " ======== Started Execute VFC adapter Post Process ======== ", isDebugEnabled)\r
255         utils.log("INFO", "url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)\r
256         APIResponse apiResponse = null\r
257         try{\r
258             RESTConfig config = new RESTConfig(url);\r
259             RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk");;\r
260             apiResponse = client.httpPost(requestBody)\r
261             utils.log("INFO", "response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled)\r
262             utils.log("INFO", "======== Completed Execute VF-C adapter Post Process ======== ", isDebugEnabled)\r
263         }catch(Exception e){\r
264             utils.log("ERROR", "Exception occured while executing VF-C Post Call. Exception is: \n" + e, isDebugEnabled)\r
265             throw new BpmnError("MSOWorkflowException")\r
266         }        \r
267         return apiResponse\r
268     }\r
269     /**\r
270      * delete request\r
271      * url: the url of the request\r
272      * requestBody: the body of the request\r
273      */\r
274     private APIResponse deleteRequest(DelegateExecution execution, String url, String requestBody){\r
275         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
276         utils.log("INFO", " ======== Started Execute VFC adapter Delete Process ======== ", isDebugEnabled)       \r
277         utils.log("INFO", "url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)\r
278         APIResponse apiResponse = null\r
279         try{\r
280             RESTConfig config = new RESTConfig(url);\r
281             RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk");\r
282             apiResponse = client.httpDelete(requestBody)\r
283             utils.log("INFO", "response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled) \r
284             utils.log("INFO", "======== Completed Execute VF-C adapter Delete Process ======== ", isDebugEnabled) \r
285         }catch(Exception e){\r
286             utils.log("ERROR", "Exception occured while executing VF-C Post Call. Exception is: \n" + e, isDebugEnabled) \r
287             throw new BpmnError("MSOWorkflowException")\r
288         }        \r
289         return apiResponse\r
290     }\r
291 }\r