2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.mso.bpmn.infrastructure.scripts;
\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
32 import java.util.UUID;
\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
48 * This groovy class supports the <class>DoDeleteVFCNetworkServiceInstance.bpmn</class> process.
\r
49 * flow for E2E ServiceInstance Delete
\r
51 public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {
\r
54 String vfcUrl = "/vfc/rest/v1/vfcadapter"
\r
56 String host = "http://mso.mso.testlab.openecomp.org:8080"
\r
58 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
60 JsonUtils jsonUtil = new JsonUtils()
\r
63 * Pre Process the BPMN Flow Request
\r
65 * generate the nsOperationKey
\r
67 public void preProcessRequest (DelegateExecution execution) {
\r
68 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
70 utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)
\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
93 execution.setVariable("nsOperationKey", nsOperationKey);
\r
94 utils.log("INFO", "nsOperationKey:" + nsOperationKey, isDebugEnabled)
\r
95 } catch (BpmnError 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
102 utils.log("INFO"," ***** Exit preProcessRequest *****", isDebugEnabled)
\r
106 * unwind NS from AAI relationship
\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
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
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
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
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
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
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
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
185 execution.setVariable("operationStatus", operationStatus)
\r
187 utils.log("INFO", " *** deleteNetworkService end *** ", isDebugEnabled)
\r
191 * instantiate NS task
\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
202 if(returnCode== "200" || returnCode== "202"){
\r
203 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
\r
205 execution.setVariable("jobId", jobId)
\r
206 utils.log("INFO", " *** terminateNetworkService end *** ", isDebugEnabled)
\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
225 execution.setVariable("operationProgress", operationProgress)
\r
226 utils.log("INFO", " *** queryNSProgress end *** ", isDebugEnabled)
\r
232 public void timeDelay(DelegateExecution execution) {
\r
234 Thread.sleep(5000);
\r
235 } catch(InterruptedException e) {
\r
236 utils.log("INFO", "Time Delay exception" + e, isDebugEnabled)
\r
243 public void finishNSDelete(DelegateExecution execution) {
\r
244 //no need to do anything util now
\r
249 * url: the url of the request
\r
250 * requestBody: the body of the request
\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
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
271 * url: the url of the request
\r
272 * requestBody: the body of the request
\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
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