AT&T 1712 and 1802 release code
[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 \r
43 /**\r
44  * This groovy class supports the <class>DoDeleteVFCNetworkServiceInstance.bpmn</class> process.\r
45  * flow for E2E ServiceInstance Delete\r
46  */\r
47 public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {\r
48 \r
49             \r
50     String vfcUrl = "/vfc/rest/v1/vfcadapter"\r
51     \r
52     String host = "http://mso.mso.testlab.openecomp.org:8080"\r
53     \r
54     ExceptionUtil exceptionUtil = new ExceptionUtil()\r
55 \r
56     JsonUtils jsonUtil = new JsonUtils()\r
57 \r
58     /**\r
59      * Pre Process the BPMN Flow Request\r
60      * Inclouds:\r
61      * generate the nsOperationKey\r
62      */\r
63     public void preProcessRequest (DelegateExecution execution) {\r
64         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
65         String msg = ""\r
66         utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)\r
67         try {\r
68             //deal with operation key\r
69             String globalSubscriberId = execution.getVariable("globalSubscriberId")\r
70             utils.log("INFO", "globalSubscriberId:" + globalSubscriberId, isDebugEnabled)\r
71             String serviceType = execution.getVariable("serviceType")\r
72             utils.log("INFO", "serviceType:" + serviceType, isDebugEnabled)\r
73             String serviceId = execution.getVariable("serviceId")\r
74             utils.log("INFO", "serviceId:" + serviceId, isDebugEnabled)\r
75             String operationId = execution.getVariable("operationId")\r
76             utils.log("INFO", "serviceType:" + serviceType, isDebugEnabled)\r
77             String nodeTemplateUUID = execution.getVariable("resourceTemplateId")\r
78             utils.log("INFO", "nodeTemplateUUID:" + nodeTemplateUUID, isDebugEnabled)\r
79             String nsInstanceId = execution.getVariable("resourceInstanceId")\r
80             utils.log("INFO", "nsInstanceId:" + nsInstanceId, isDebugEnabled)\r
81             execution.setVariable("nsInstanceId",nsInstanceId)\r
82             String nsOperationKey = """{\r
83             "globalSubscriberId":"${globalSubscriberId}",\r
84             "serviceType":"${serviceType}",\r
85             "serviceId":"${serviceId}",\r
86             "operationId":"${operationId}",\r
87             "nodeTemplateUUID":"${nodeTemplateUUID}"\r
88              }"""\r
89             execution.setVariable("nsOperationKey", nsOperationKey);\r
90             utils.log("INFO", "nsOperationKey:" + nsOperationKey, isDebugEnabled)\r
91         } catch (BpmnError e) {\r
92             throw e;\r
93         } catch (Exception ex){\r
94             msg = "Exception in preProcessRequest " + ex.getMessage()\r
95             utils.log("INFO", msg, isDebugEnabled)\r
96             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
97         }\r
98         utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
99         }\r
100 \r
101     /**\r
102      * delete NS task\r
103      */\r
104     public void deleteNetworkService(DelegateExecution execution) {\r
105         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
106         utils.log("INFO", " *** deleteNetworkService  start *** ", isDebugEnabled)\r
107         String nsOperationKey = execution.getVariable("nsOperationKey");\r
108         String url = host + vfcUrl + "/ns/" + execution.getVariable("nsInstanceId") \r
109         APIResponse apiResponse = deleteRequest(execution, url, nsOperationKey)\r
110         String returnCode = apiResponse.getStatusCode()\r
111         String apiResponseAsString = apiResponse.getResponseBodyAsString()\r
112         String operationStatus = "error";\r
113         if(returnCode== "200" || returnCode== "202"){\r
114             operationStatus = "finished"\r
115         }\r
116         execution.setVariable("operationStatus", operationStatus)\r
117         \r
118         utils.log("INFO", " *** deleteNetworkService  end *** ", isDebugEnabled)\r
119     }\r
120 \r
121     /**\r
122      * instantiate NS task\r
123      */\r
124     public void terminateNetworkService(DelegateExecution execution) {\r
125         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
126         utils.log("INFO", " *** terminateNetworkService  start *** ", isDebugEnabled)\r
127         String nsOperationKey = execution.getVariable("nsOperationKey") \r
128         String url =  host + vfcUrl + "/ns/" + execution.getVariable("nsInstanceId") + "/terminate"\r
129         APIResponse apiResponse = postRequest(execution, url, nsOperationKey)\r
130         String returnCode = apiResponse.getStatusCode()\r
131         String aaiResponseAsString = apiResponse.getResponseBodyAsString()\r
132         String jobId = "";\r
133         if(returnCode== "200" || returnCode== "202"){\r
134             jobId =  jsonUtil.getJsonValue(aaiResponseAsString, "jobId")\r
135         }\r
136         execution.setVariable("jobId", jobId)   \r
137         utils.log("INFO", " *** terminateNetworkService  end *** ", isDebugEnabled)\r
138     }\r
139 \r
140     /**\r
141      * query NS task\r
142      */\r
143     public void queryNSProgress(DelegateExecution execution) {\r
144         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
145         utils.log("INFO", " *** queryNSProgress  start *** ", isDebugEnabled)\r
146         String jobId = execution.getVariable("jobId")\r
147         String nsOperationKey = execution.getVariable("nsOperationKey");\r
148         String url =  host + vfcUrl + "/jobs/" +  execution.getVariable("jobId") \r
149         APIResponse apiResponse = postRequest(execution, url, nsOperationKey)\r
150         String returnCode = apiResponse.getStatusCode()\r
151         String apiResponseAsString = apiResponse.getResponseBodyAsString()\r
152         String operationProgress = "100"\r
153         if(returnCode== "200"){\r
154             operationProgress = jsonUtil.getJsonValue(apiResponseAsString, "responseDescriptor.progress")\r
155         }\r
156         execution.setVariable("operationProgress", operationProgress)\r
157         utils.log("INFO", " *** queryNSProgress  end *** ", isDebugEnabled)\r
158     }\r
159 \r
160     /**\r
161      * delay 5 sec \r
162      */\r
163     public void timeDelay(DelegateExecution execution) {\r
164         try {\r
165             Thread.sleep(5000);\r
166         } catch(InterruptedException e) {           \r
167             utils.log("INFO", "Time Delay exception" + e, isDebugEnabled)\r
168         }\r
169     }\r
170 \r
171     /**\r
172      * finish NS task\r
173      */\r
174     public void finishNSDelete(DelegateExecution execution) {\r
175         //no need to do anything util now\r
176     }\r
177 \r
178     /**\r
179      * post request\r
180      * url: the url of the request\r
181      * requestBody: the body of the request\r
182      */\r
183     private APIResponse postRequest(DelegateExecution execution, String url, String requestBody){\r
184         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
185         utils.log("INFO", " ======== Started Execute VFC adapter Post Process ======== ", isDebugEnabled)\r
186         utils.log("INFO", "url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)\r
187         APIResponse apiResponse = null\r
188         try{\r
189             RESTConfig config = new RESTConfig(url);\r
190             RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk");;\r
191             apiResponse = client.httpPost(requestBody)\r
192             utils.log("INFO", "response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled)\r
193             utils.log("INFO", "======== Completed Execute VF-C adapter Post Process ======== ", isDebugEnabled)\r
194         }catch(Exception e){\r
195             utils.log("ERROR", "Exception occured while executing VF-C Post Call. Exception is: \n" + e, isDebugEnabled)\r
196             throw new BpmnError("MSOWorkflowException")\r
197         }        \r
198         return apiResponse\r
199     }\r
200     /**\r
201      * delete request\r
202      * url: the url of the request\r
203      * requestBody: the body of the request\r
204      */\r
205     private APIResponse deleteRequest(DelegateExecution execution, String url, String requestBody){\r
206         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
207         utils.log("INFO", " ======== Started Execute VFC adapter Delete Process ======== ", isDebugEnabled)       \r
208         utils.log("INFO", "url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)\r
209         APIResponse apiResponse = null\r
210         try{\r
211             RESTConfig config = new RESTConfig(url);\r
212             RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk");\r
213             apiResponse = client.httpDelete(requestBody)\r
214             utils.log("INFO", "response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled) \r
215             utils.log("INFO", "======== Completed Execute VF-C adapter Delete Process ======== ", isDebugEnabled) \r
216         }catch(Exception e){\r
217             utils.log("ERROR", "Exception occured while executing VF-C Post Call. Exception is: \n" + e, isDebugEnabled) \r
218             throw new BpmnError("MSOWorkflowException")\r
219         }        \r
220         return apiResponse\r
221     }\r
222 }\r