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.UrnPropertiesReader
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
36 import org.onap.so.client.HttpClient
37 import org.onap.so.logger.MsoLogger
38 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
40 import java.util.UUID;
41 import javax.ws.rs.core.Response
42 import org.camunda.bpm.engine.delegate.BpmnError
43 import org.camunda.bpm.engine.delegate.DelegateExecution
44 import org.apache.commons.lang3.*
45 import javax.ws.rs.core.MediaType
46 import org.apache.commons.codec.binary.Base64;
47 import org.springframework.web.util.UriUtils
48 import org.onap.so.utils.TargetEntity
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 URL url = new URL(serviceAaiPath)
118 HttpClient client = new HttpClient(url, MediaType.APPLICATION_XML, TargetEntity.AAI)
119 client.addBasicAuthHeader(UrnPropertiesReader.getVariable("aai.auth", execution), UrnPropertiesReader.getVariable("mso.msoKey", execution))
120 client.addAdditionalHeader("X-FromAppId", "MSO")
121 client.addAdditionalHeader("X-TransactionId", utils.getRequestID())
122 client.addAdditionalHeader("Accept", MediaType.APPLICATION_XML)
123 Response response = client.get()
127 int responseCode = response.getStatus()
128 execution.setVariable(Prefix + "GetDeviceResponseCode", responseCode)
129 msoLogger.debug(" Get device response code is: " + responseCode)
131 String aaiResponse = response.readEntity(String.class)
132 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
133 aaiResponse = aaiResponse.replaceAll("&", "&")
134 execution.setVariable(Prefix + "GetDeviceResponse", aaiResponse)
137 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
138 //200 OK 201 CREATED 202 ACCEPTED
140 msoLogger.debug("GET Device Received a Good Response")
141 execution.setVariable(Prefix + "SuccessIndicator", true)
142 execution.setVariable(Prefix + "FoundIndicator", true)
144 String devClass = utils.getNodeText(aaiResponse, "device_class")
145 execution.setVariable(Prefix + "DeviceClass", devClass)
146 msoLogger.debug(" DeviceClass is: " + devClass)
151 msoLogger.debug("Get DeviceInAAI Received a Bad Response Code. Response Code is: " + responseCode)
155 msoLogger.info(" ***** Exit getDeviceInAAI *****")
158 public void checkDevType(DelegateExecution execution){
159 msoLogger.info(" ***** Started checkDevType *****")
162 String devType = execution.getVariable(Prefix + "DeviceClass")
164 if(StringUtils.isBlank(devType)) {
168 execution.setVariable("device_class", devType)
170 } catch (Exception ex){
171 String msg = "Exception in checkDevType " + ex.getMessage()
172 msoLogger.debug( msg)
173 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
177 private void setProgressUpdateVariables(DelegateExecution execution, String body) {
178 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
179 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
180 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
183 public void prepareUpdateProgress(DelegateExecution execution) {
184 msoLogger.info(" ***** Started prepareUpdateProgress *****")
185 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
186 String operType = resourceInputObj.getOperationType()
187 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
188 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
189 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
190 String operationId = resourceInputObj.getOperationId()
191 String progress = execution.getVariable("progress")
192 String status = execution.getVariable("status")
193 String statusDescription = execution.getVariable("statusDescription")
196 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
197 xmlns:ns="http://org.openecomp.mso/requestsdb">
200 <ns:updateResourceOperationStatus>
201 <operType>${operType}</operType>
202 <operationId>${operationId}</operationId>
203 <progress>${progress}</progress>
204 <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
205 <serviceId>${ServiceInstanceId}</serviceId>
206 <status>${status}</status>
207 <statusDescription>${statusDescription}</statusDescription>
208 </ns:updateResourceOperationStatus>
210 </soapenv:Envelope>"""
212 setProgressUpdateVariables(execution, body)
213 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
216 public void getVNFTemplatefromSDC(DelegateExecution execution){
217 msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
222 } catch (Exception ex){
223 String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
224 msoLogger.debug( msg)
225 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
229 public void postVNFInfoProcess(DelegateExecution execution){
230 msoLogger.info(" ***** Started postVNFInfoProcess *****")
235 } catch (Exception ex){
236 String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
237 msoLogger.debug( msg)
238 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
242 public void sendSyncResponse (DelegateExecution execution) {
243 msoLogger.debug( " *** sendSyncResponse *** ")
246 String operationStatus = "finished"
247 // RESTResponse for main flow
248 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
249 msoLogger.debug( " sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
250 sendWorkflowResponse(execution, 202, resourceOperationResp)
251 execution.setVariable("sentSyncResponse", true)
253 } catch (Exception ex) {
254 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
255 msoLogger.debug( msg)
256 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
258 msoLogger.debug(" ***** Exit sendSyncResopnse *****")