2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.core.json.JsonUtils
30 import org.onap.so.client.HttpClient
31 import org.onap.so.client.HttpClientFactory
32 import org.onap.so.client.aai.AAIObjectType
33 import org.onap.so.client.aai.entities.uri.AAIResourceUri
34 import org.onap.so.client.aai.entities.uri.AAIUriFactory
35 import org.slf4j.Logger
36 import org.slf4j.LoggerFactory
37 import org.onap.so.utils.TargetEntity
38 import org.onap.so.bpmn.core.UrnPropertiesReader
40 import javax.ws.rs.core.Response
42 * This groovy class supports the <class>DoDeleteVFCNetworkServiceInstance.bpmn</class> process.
43 * flow for E2E ServiceInstance Delete
45 public class DoDeleteVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {
46 private static final Logger logger = LoggerFactory.getLogger( DoDeleteVFCNetworkServiceInstance.class);
48 ExceptionUtil exceptionUtil = new ExceptionUtil()
50 JsonUtils jsonUtil = new JsonUtils()
51 private final HttpClientFactory httpClientFactory = new HttpClientFactory()
54 * Pre Process the BPMN Flow Request
56 * generate the nsOperationKey
58 public void preProcessRequest (DelegateExecution execution) {
61 logger.trace("preProcessRequest() ")
63 //deal with operation key
64 String globalSubscriberId = execution.getVariable("globalSubscriberId")
65 logger.info("globalSubscriberId:" + globalSubscriberId)
66 String serviceType = execution.getVariable("serviceType")
67 logger.info("serviceType:" + serviceType)
68 String serviceId = execution.getVariable("serviceId")
69 logger.info("serviceId:" + serviceId)
70 String operationId = execution.getVariable("operationId")
71 logger.info("serviceType:" + serviceType)
72 String nodeTemplateUUID = execution.getVariable("resourceTemplateId")
73 logger.info("nodeTemplateUUID:" + nodeTemplateUUID)
74 String nsInstanceId = execution.getVariable("resourceInstanceId")
75 logger.info("nsInstanceId:" + nsInstanceId)
76 execution.setVariable("nsInstanceId",nsInstanceId)
77 String nsOperationKey = """{
78 "globalSubscriberId":"${globalSubscriberId}",
79 "serviceType":"${serviceType}",
80 "serviceId":"${serviceId}",
81 "operationId":"${operationId}",
82 "nodeTemplateUUID":"${nodeTemplateUUID}"
84 execution.setVariable("nsOperationKey", nsOperationKey);
85 logger.info("nsOperationKey:" + nsOperationKey)
87 String vfcAdapterUrl = UrnPropertiesReader.getVariable("mso.adapters.vfc.rest.endpoint", execution)
89 if (vfcAdapterUrl == null || vfcAdapterUrl.isEmpty()) {
90 msg = getProcessKey(execution) + ': mso:adapters:vfcc:rest:endpoint URN mapping is not defined'
94 while (vfcAdapterUrl.endsWith('/')) {
95 vfcAdapterUrl = vfcAdapterUrl.substring(0, vfcAdapterUrl.length()-1)
98 execution.setVariable("vfcAdapterUrl", vfcAdapterUrl)
100 } catch (BpmnError e) {
102 } catch (Exception ex){
103 msg = "Exception in preProcessRequest " + ex.getMessage()
105 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
107 logger.trace("Exit preProcessRequest ")
111 * unwind NS from AAI relationship
113 public void deleteNSRelationship(DelegateExecution execution) {
114 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
115 logger.info(" ***** deleteNSRelationship *****")
116 String nsInstanceId = execution.getVariable("resourceInstanceId")
117 if(nsInstanceId == null || nsInstanceId == ""){
118 logger.info(" Delete NS failed")
121 String globalSubscriberId = execution.getVariable("globalSubscriberId")
122 String serviceType = execution.getVariable("serviceType")
123 String serviceId = execution.getVariable("serviceId")
124 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceId)
125 AAIResourceUri nsServiceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, nsInstanceId)
127 getAAIClient().disconnect(serviceInstanceUri, nsServiceInstanceUri)
129 exceptionUtil.buildAndThrowWorkflowException(execution,25000,"Exception occured while NS disconnect call: " + e.getMessage())
131 logger.info(" *****Exit deleteNSRelationship *****")
137 public void deleteNetworkService(DelegateExecution execution) {
139 logger.trace("deleteNetworkService start ")
140 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
141 String nsOperationKey = execution.getVariable("nsOperationKey");
142 String url = vfcAdapterUrl + "/ns/" + execution.getVariable("nsInstanceId")
143 Response apiResponse = deleteRequest(execution, url, nsOperationKey)
144 String returnCode = apiResponse.getStatus()
145 String operationStatus = "error";
146 if(returnCode== "200" || returnCode== "202"){
147 operationStatus = "finished"
149 execution.setVariable("operationStatus", operationStatus)
151 logger.trace("deleteNetworkService end ")
157 public void terminateNetworkService(DelegateExecution execution) {
159 logger.trace("terminateNetworkService start ")
160 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
161 String nsOperationKey = execution.getVariable("nsOperationKey")
162 String url = vfcAdapterUrl + "/ns/" + execution.getVariable("nsInstanceId") + "/terminate"
163 Response apiResponse = postRequest(execution, url, nsOperationKey)
164 String returnCode = apiResponse.getStatus()
165 String aaiResponseAsString = apiResponse.readEntity(String.class)
167 if(returnCode== "200" || returnCode== "202"){
168 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
170 execution.setVariable("jobId", jobId)
171 logger.trace("terminateNetworkService end ")
177 public void queryNSProgress(DelegateExecution execution) {
179 logger.trace("queryNSProgress start ")
180 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
181 String jobId = execution.getVariable("jobId")
182 String nsOperationKey = execution.getVariable("nsOperationKey");
183 String url = vfcAdapterUrl + "/jobs/" + execution.getVariable("jobId")
184 Response apiResponse = postRequest(execution, url, nsOperationKey)
185 String returnCode = apiResponse.getStatus()
186 String apiResponseAsString = apiResponse.readEntity(String.class)
187 String operationProgress = "100"
188 if(returnCode== "200"){
189 operationProgress = jsonUtil.getJsonValue(apiResponseAsString, "responseDescriptor.progress")
191 execution.setVariable("operationProgress", operationProgress)
192 logger.trace("queryNSProgress end ")
198 public void timeDelay(DelegateExecution execution) {
201 } catch(InterruptedException e) {
202 logger.info("Time Delay exception" + e)
209 public void finishNSDelete(DelegateExecution execution) {
210 //no need to do anything util now
215 * url: the url of the request
216 * requestBody: the body of the request
218 private Response postRequest(DelegateExecution execution, String urlString, String requestBody){
220 logger.trace("Started Execute VFC adapter Post Process ")
221 logger.info("url:"+urlString +"\nrequestBody:"+ requestBody)
222 Response apiResponse = null
224 URL url = new URL(urlString);
226 // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
227 // user 'bepl' authHeader is the same with mso.db.auth
228 String basicAuthValuedb = UrnPropertiesReader.getVariable("mso.db.auth", execution)
229 HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.VNF_ADAPTER)
230 httpClient.addAdditionalHeader("Accept", "application/json")
231 httpClient.addAdditionalHeader("Authorization", basicAuthValuedb)
233 apiResponse = httpClient.post(requestBody)
235 logger.debug("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
237 logger.trace("Completed Execute VF-C adapter Post Process ")
239 logger.error("Exception occured while executing VF-C Post Call. Exception is: \n" + e.getMessage());
240 throw new BpmnError("MSOWorkflowException")
246 * url: the url of the request
247 * requestBody: the body of the request
249 private Response deleteRequest(DelegateExecution execution, String url, String requestBody){
251 logger.trace("Started Execute VFC adapter Delete Process ")
252 logger.info("url:"+url +"\nrequestBody:"+ requestBody)
256 URL Url = new URL(url)
257 // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
258 // user 'bepl' authHeader is the same with mso.db.auth
259 String basicAuthValuedb = UrnPropertiesReader.getVariable("mso.db.auth", execution)
260 HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.VNF_ADAPTER)
261 httpClient.addAdditionalHeader("Accept", "application/json")
262 httpClient.addAdditionalHeader("Authorization", basicAuthValuedb)
263 httpClient.addAdditionalHeader("Content-Type", "application/json")
264 r = httpClient.delete(requestBody)
266 logger.trace("Completed Execute VF-C adapter Delete Process ")
268 logger.error("Exception occured while executing VF-C Post Call. Exception is: \n" + e.getMessage());
269 throw new BpmnError("MSOWorkflowException")