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