2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import org.json.JSONObject
26 import static org.apache.commons.lang3.StringUtils.*;
27 import groovy.xml.XmlUtil
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.common.recipe.ResourceInput;
31 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
35 import org.onap.so.logger.MsoLogger
36 import org.onap.so.rest.APIResponse
37 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
39 import java.util.UUID;
41 import org.camunda.bpm.engine.delegate.BpmnError
42 import org.camunda.bpm.engine.delegate.DelegateExecution
43 import org.apache.commons.lang3.*
44 import org.apache.commons.codec.binary.Base64;
45 import org.springframework.web.util.UriUtils
46 import org.onap.so.rest.RESTClient
47 import org.onap.so.rest.RESTConfig
48 import org.onap.so.rest.APIResponse;
49 import org.onap.so.bpmn.common.scripts.AaiUtil
52 * This groovy class supports the <class>DeleteDeviceResource.bpmn</class> process.
53 * flow for Device Resource Delete
55 public class DeleteDeviceResource extends AbstractServiceTaskProcessor {
57 String Prefix="DELDEVRES_"
59 ExceptionUtil exceptionUtil = new ExceptionUtil()
61 JsonUtils jsonUtil = new JsonUtils()
63 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteDeviceResource.class)
65 public void preProcessRequest(DelegateExecution execution){
66 msoLogger.info(" ***** Started preProcessRequest *****")
69 //get bpmn inputs from resource request.
70 String requestId = execution.getVariable("mso-request-id")
71 String requestAction = execution.getVariable("requestAction")
72 msoLogger.info("The requestAction is: " + requestAction)
73 String recipeParamsFromRequest = execution.getVariable("recipeParams")
74 msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
75 String resourceInput = execution.getVariable("resourceInput")
76 msoLogger.info("The resourceInput is: " + resourceInput)
77 //Get ResourceInput Object
78 ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
79 execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
80 String resourceInputPrameters = resourceInputObj.getResourceParameters()
81 String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
82 JSONObject inputParameters = new JSONObject(inputParametersJson)
83 execution.setVariable(Prefix + "ResourceRequestInputs", inputParameters)
85 //Deal with recipeParams
86 String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
87 String resourceName = resourceInputObj.getResourceInstanceName()
89 String resourceInstanceId = resourceInputObj.getResourceInstancenUuid()
90 String deviceId = resourceInstanceId
91 execution.setVariable(Prefix + "DeviceId", deviceId)
93 getDeviceInAAI(execution)
95 execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
96 execution.setVariable("mso-request-id", requestId)
98 } catch (BpmnError e) {
100 } catch (Exception ex){
101 String msg = "Exception in preProcessRequest " + ex.getMessage()
103 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
107 private void getDeviceInAAI(DelegateExecution execution) {
108 msoLogger.info(" ***** Started getDeviceInAAI *****")
110 String deviceId = execution.getVariable(Prefix + "DeviceId")
111 AaiUtil aaiUriUtil = new AaiUtil()
112 String aai_uri = aaiUriUtil.getNetworkDeviceUri(execution)
113 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
114 String serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(deviceId,"UTF-8")
115 execution.setVariable(Prefix + "ServiceAaiPath", serviceAaiPath)
117 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceAaiPath)
118 int responseCode = response.getStatusCode()
119 execution.setVariable(Prefix + "GetDeviceResponseCode", responseCode)
120 msoLogger.debug(" Get device response code is: " + responseCode)
122 String aaiResponse = response.getResponseBodyAsString()
123 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
124 aaiResponse = aaiResponse.replaceAll("&", "&")
125 execution.setVariable(Prefix + "GetDeviceResponse", aaiResponse)
128 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
129 //200 OK 201 CREATED 202 ACCEPTED
131 msoLogger.debug("GET Device Received a Good Response")
132 execution.setVariable(Prefix + "SuccessIndicator", true)
133 execution.setVariable(Prefix + "FoundIndicator", true)
135 String devClass = utils.getNodeText1(aaiResponse, "device_class")
136 execution.setVariable(Prefix + "DeviceClass", devClass)
137 msoLogger.debug(" DeviceClass is: " + devClass)
142 msoLogger.debug("Get DeviceInAAI Received a Bad Response Code. Response Code is: " + responseCode)
146 msoLogger.info(" ***** Exit getDeviceInAAI *****")
149 public void checkDevType(DelegateExecution execution){
150 msoLogger.info(" ***** Started checkDevType *****")
153 String devType = execution.getVariable(Prefix + "DeviceClass")
155 if(StringUtils.isBlank(devType)) {
159 execution.setVariable("device_class", devType)
161 } catch (Exception ex){
162 String msg = "Exception in checkDevType " + ex.getMessage()
163 msoLogger.debug( msg)
164 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
168 private void setProgressUpdateVariables(DelegateExecution execution, String body) {
169 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
170 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
171 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
174 public void prepareUpdateProgress(DelegateExecution execution) {
175 msoLogger.info(" ***** Started prepareUpdateProgress *****")
176 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
177 String operType = resourceInputObj.getOperationType()
178 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
179 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
180 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
181 String operationId = resourceInputObj.getOperationId()
182 String progress = execution.getVariable("progress")
183 String status = execution.getVariable("status")
184 String statusDescription = execution.getVariable("statusDescription")
187 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
188 xmlns:ns="http://org.openecomp.mso/requestsdb">
191 <ns:updateResourceOperationStatus>
192 <operType>${operType}</operType>
193 <operationId>${operationId}</operationId>
194 <progress>${progress}</progress>
195 <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
196 <serviceId>${ServiceInstanceId}</serviceId>
197 <status>${status}</status>
198 <statusDescription>${statusDescription}</statusDescription>
199 </ns:updateResourceOperationStatus>
201 </soapenv:Envelope>"""
203 setProgressUpdateVariables(execution, body)
204 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
207 public void getVNFTemplatefromSDC(DelegateExecution execution){
208 msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
213 } catch (Exception ex){
214 String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
215 msoLogger.debug( msg)
216 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
220 public void postVNFInfoProcess(DelegateExecution execution){
221 msoLogger.info(" ***** Started postVNFInfoProcess *****")
226 } catch (Exception ex){
227 String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
228 msoLogger.debug( msg)
229 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
233 public void sendSyncResponse (DelegateExecution execution) {
234 msoLogger.debug( " *** sendSyncResponse *** ")
237 String operationStatus = "finished"
238 // RESTResponse for main flow
239 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
240 msoLogger.debug( " sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
241 sendWorkflowResponse(execution, 202, resourceOperationResp)
242 execution.setVariable("sentSyncResponse", true)
244 } catch (Exception ex) {
245 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
246 msoLogger.debug( msg)
247 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
249 msoLogger.debug(" ***** Exit sendSyncResopnse *****")