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=========================================================
20 package org.openecomp.mso.bpmn.infrastructure.scripts
22 import org.apache.http.HttpResponse
23 import org.json.JSONArray
24 import org.openecomp.mso.bpmn.common.recipe.BpmnRestClient
25 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
26 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition;
28 import static org.apache.commons.lang3.StringUtils.*;
29 import groovy.xml.XmlUtil
32 import org.openecomp.mso.bpmn.core.domain.ModelInfo
33 import org.openecomp.mso.bpmn.core.domain.Resource
34 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
35 import org.openecomp.mso.bpmn.core.json.JsonUtils
36 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
37 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
38 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
39 import org.openecomp.mso.bpmn.core.WorkflowException
40 import org.openecomp.mso.rest.APIResponse;
41 import org.openecomp.mso.rest.RESTClient
42 import org.openecomp.mso.rest.RESTConfig
44 import java.util.List;
45 import java.util.UUID;
46 import javax.xml.parsers.DocumentBuilder
47 import javax.xml.parsers.DocumentBuilderFactory
49 import org.camunda.bpm.engine.delegate.BpmnError
50 import org.camunda.bpm.engine.delegate.DelegateExecution
51 import org.camunda.bpm.engine.runtime.Execution
52 import org.json.JSONObject;
53 import org.apache.commons.lang3.*
54 import org.apache.commons.codec.binary.Base64;
55 import org.springframework.web.util.UriUtils;
56 import org.w3c.dom.Document
57 import org.w3c.dom.Element
58 import org.w3c.dom.Node
59 import org.w3c.dom.NodeList
60 import org.xml.sax.InputSource
62 import com.fasterxml.jackson.jaxrs.json.annotation.JSONP.Def;
65 * This groovy class supports the <class>DoDeleteE2EServiceInstance.bpmn</class> process.
68 * @param - msoRequestId
69 * @param - globalSubscriberId - O
70 * @param - subscriptionServiceType - O
71 * @param - serviceInstanceId
72 * @param - serviceInstanceName - O
73 * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
74 * @param - sdncVersion
75 * @param - failNotFound - TODO
76 * @param - serviceInputParams - TODO
78 * @param - delResourceList
79 * @param - serviceRelationShip
82 * @param - WorkflowException
86 public class DoDeleteE2EServiceInstance extends AbstractServiceTaskProcessor {
88 String Prefix="DDEESI_"
89 ExceptionUtil exceptionUtil = new ExceptionUtil()
90 JsonUtils jsonUtil = new JsonUtils()
92 public void preProcessRequest (DelegateExecution execution) {
93 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
94 utils.log("INFO"," ***** preProcessRequest *****", isDebugEnabled)
98 String requestId = execution.getVariable("msoRequestId")
99 execution.setVariable("prefix",Prefix)
102 //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
103 String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
104 if (globalSubscriberId == null)
106 execution.setVariable("globalSubscriberId", "")
109 //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
110 String serviceType = execution.getVariable("serviceType")
111 if (serviceType == null)
113 execution.setVariable("serviceType", "")
116 //Generated in parent for AAI PUT
117 String serviceInstanceId = execution.getVariable("serviceInstanceId")
118 if (isBlank(serviceInstanceId)){
119 msg = "Input serviceInstanceId is null"
120 utils.log("INFO", msg, isDebugEnabled)
121 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
124 String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
125 if (isBlank(sdncCallbackUrl)) {
126 msg = "URN_mso_workflow_sdncadapter_callback is null"
127 utils.log("INFO", msg, isDebugEnabled)
128 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
130 execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
131 utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
133 StringBuilder sbParams = new StringBuilder()
134 Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
135 if (paramsMap != null)
137 sbParams.append("<service-input-parameters>")
138 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
140 String paramName = entry.getKey()
141 String paramValue = entry.getValue()
144 <name>${paramName}</name>
145 <value>${paramValue}</value>
148 sbParams.append(paramsXml)
150 sbParams.append("</service-input-parameters>")
152 String siParamsXml = sbParams.toString()
153 if (siParamsXml == null)
155 execution.setVariable("siParamsXml", siParamsXml)
157 } catch (BpmnError e) {
159 } catch (Exception ex){
160 msg = "Exception in preProcessRequest " + ex.getMessage()
161 utils.log("INFO", msg, isDebugEnabled)
162 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
164 utils.log("INFO"," ***** Exit preProcessRequest *****", isDebugEnabled)
167 public void postProcessAAIGET(DelegateExecution execution) {
168 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
169 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
173 String serviceInstanceId = execution.getVariable("serviceInstanceId")
174 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
175 String serviceType = ""
178 utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
180 String siData = execution.getVariable("GENGS_service")
181 utils.log("INFO", "SI Data", isDebugEnabled)
184 msg = "Could not retrive ServiceInstance data from AAI to delete id:" + serviceInstanceId
185 utils.log("INFO", msg, isDebugEnabled)
186 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
190 // get model invariant id
191 // Get Template uuid and version
192 if (utils.nodeExists(siData, "model-invariant-id") && utils.nodeExists(siData, "model-version-id") ) {
193 utils.log("INFO", "SI Data model-invariant-id and model-version-id exist:", isDebugEnabled)
194 def modelInvariantId = serviceXml.getElementsByTagName("model-invariant-id").item(0).getTextContent()
195 def modelVersionId = serviceXml.getElementsByTagName("model-version-id").item(0).getTextContent()
197 // Set Original Template info
198 execution.setVariable("model-invariant-id-original", modelInvariantId)
199 execution.setVariable("model-version-id-original", modelVersionId)
202 utils.log("INFO", "SI Data" + siData, isDebugEnabled)
203 //Confirm there are no related service instances (vnf/network or volume)
204 if (utils.nodeExists(siData, "relationship-list")) {
205 utils.log("INFO", "SI Data relationship-list exists:", isDebugEnabled)
206 InputSource source = new InputSource(new StringReader(siData));
207 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
208 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
209 Document serviceXml = docBuilder.parse(source)
210 serviceXml.getDocumentElement().normalize()
212 NodeList nodeList = serviceXml.getElementsByTagName("relationship")
213 JSONArray jArray = new JSONArray()
214 for (int x = 0; x < nodeList.getLength(); x++) {
215 Node node = nodeList.item(x)
216 if (node.getNodeType() == Node.ELEMENT_NODE) {
217 Element eElement = (Element) node
218 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent() //for ns
219 if(e.equals("service-instance")){
220 def relatedObject = eElement.getElementsByTagName("related-link").item(0).getTextContent()
221 utils.log("INFO", "ServiceInstance Related NS :" + relatedObject, isDebugEnabled)
222 NodeList dataList = node.getChildNodes()
223 if(null != dataList) {
224 JSONObject jObj = new JSONObject()
225 for (int i = 0; i < dataList.getLength(); i++) {
226 Node dNode = dataList.item(i)
227 if(dNode.getNodeName() == "relationship-data") {
228 Element rDataEle = (Element)dNode
229 def eKey = rDataEle.getElementsByTagName("relationship-key").item(0).getTextContent()
230 def eValue = rDataEle.getElementsByTagName("relationship-value").item(0).getTextContent()
231 if(eKey.equals("service-instance.service-instance-id")){
232 jObj.put("resourceInstanceId", eValue)
235 else if(dNode.getNodeName() == "related-to-property"){
236 Element rDataEle = (Element)dNode
237 def eKey = rDataEle.getElementsByTagName("property-key").item(0).getTextContent()
238 def eValue = rDataEle.getElementsByTagName("property-value").item(0).getTextContent()
239 if(eKey.equals("service-instance.service-instance-name")){
240 jObj.put("resourceType", eValue)
244 utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
247 //for overlay/underlay
248 }else if (e.equals("configuration")){
249 def relatedObject = eElement.getElementsByTagName("related-link").item(0).getTextContent()
250 utils.log("INFO", "ServiceInstance Related Configuration :" + relatedObject, isDebugEnabled)
251 NodeList dataList = node.getChildNodes()
252 if(null != dataList) {
253 JSONObject jObj = new JSONObject()
254 for (int i = 0; i < dataList.getLength(); i++) {
255 Node dNode = dataList.item(i)
256 if(dNode.getNodeName() == "relationship-data") {
257 Element rDataEle = (Element)dNode
258 def eKey = rDataEle.getElementsByTagName("relationship-key").item(0).getTextContent()
259 def eValue = rDataEle.getElementsByTagName("relationship-value").item(0).getTextContent()
260 if(eKey.equals("configuration.configuration-id")){
261 jObj.put("resourceInstanceId", eValue)
264 else if(dNode.getNodeName() == "related-to-property"){
265 Element rDataEle = (Element)dNode
266 def eKey = rDataEle.getElementsByTagName("property-key").item(0).getTextContent()
267 def eValue = rDataEle.getElementsByTagName("property-value").item(0).getTextContent()
268 if(eKey.equals("configuration.configuration-type")){
269 jObj.put("resourceType", eValue)
273 utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
279 execution.setVariable("serviceRelationShip", jArray.toString())
283 boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
285 utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceId, isDebugEnabled)
286 WorkflowException workflowException = execution.getVariable("WorkflowException")
287 utils.logAudit("workflowException: " + workflowException)
288 if(workflowException != null){
289 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
293 msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
294 utils.log("INFO", msg, isDebugEnabled)
295 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
299 utils.log("INFO","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)
301 }catch (BpmnError e) {
303 } catch (Exception ex) {
304 msg = "Exception in DoDeleteE2EServiceInstance.postProcessAAIGET. " + ex.getMessage()
305 utils.log("INFO", msg, isDebugEnabled)
306 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
308 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
311 public void getCurrentNS(execution){
312 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
313 utils.log("INFO", "======== Start getCurrentNS Process ======== ", isDebugEnabled)
315 def currentIndex = execution.getVariable("currentNSIndex")
316 List<String> nsSequence = execution.getVariable("nsSequence")
317 String nsResourceType = nsSequence.get(currentIndex)
319 // GET AAI by Name, not ID, for process convenient
320 execution.setVariable("GENGS_type", "service-instance")
321 execution.setVariable("GENGS_serviceInstanceId", "")
322 execution.setVariable("GENGS_serviceInstanceName", nsResourceType)
324 utils.log("INFO", "======== COMPLETED getCurrentNS Process ======== ", isDebugEnabled)
327 public void prepareDecomposeService(DelegateExecution execution) {
328 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
331 utils.log("DEBUG", " ***** Inside prepareDecomposeService of create generic e2e service ***** ", isDebugEnabled)
332 String modelInvariantUuid = execution.getVariable("model-invariant-id")
333 //here modelVersion is not set, we use modelUuid to decompose the service.
334 String serviceModelInfo = """{
335 "modelInvariantUuid":"${modelInvariantUuid}",
336 "modelUuid":"${modelUuid}",
339 execution.setVariable("serviceModelInfo", serviceModelInfo)
341 utils.log("DEBUG", " ***** Completed prepareDecomposeService of create generic e2e service ***** ", isDebugEnabled)
342 } catch (Exception ex) {
343 // try error in method block
344 String exceptionMessage = "Bpmn error encountered in create generic e2e service flow. Unexpected Error from method prepareDecomposeService() - " + ex.getMessage()
345 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
349 public void postDecomposeService(DelegateExecution execution) {
350 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
352 utils.log("DEBUG", " ***** Inside processDecomposition() of create generic e2e service flow ***** ", isDebugEnabled)
354 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
355 List<Resource> deleteResourceList = serviceDecomposition.getServiceResources()
356 execution.setVariable("deleteResourceList", deleteResourceList)
357 } catch (Exception ex) {
358 String exceptionMessage = "Bpmn error encountered in create generic e2e service flow. processDecomposition() - " + ex.getMessage()
359 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
360 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
364 public void preInitResourcesOperStatus(DelegateExecution execution){
365 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
367 utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
369 String serviceId = execution.getVariable("serviceInstanceId")
370 String operationId = execution.getVariable("operationId")
371 String operationType = execution.getVariable("operationType")
372 String resourceTemplateUUIDs = ""
373 String result = "processing"
374 String progress = "0"
376 String operationContent = "Prepare service creation"
377 utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
378 serviceId = UriUtils.encode(serviceId,"UTF-8")
379 execution.setVariable("serviceInstanceId", serviceId)
380 execution.setVariable("operationId", operationId)
381 execution.setVariable("operationType", operationType)
383 String serviceRelationShip = execution.getVariable("serviceRelationShip")
385 def jsonSlurper = new JsonSlurper()
386 def jsonOutput = new JsonOutput()
387 List relationShipList = jsonSlurper.parseText(serviceRelationShip)
389 if (relationShipList != null) {
390 relationShipList.each {
391 resourceTemplateUUIDs = resourceTemplateUUIDs + it.resourceInstanceId + ":"
394 execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
397 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
398 xmlns:ns="http://org.openecomp.mso/requestsdb">
401 <ns:initResourceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
402 <serviceId>${serviceId}</serviceId>
403 <operationId>${operationId}</operationId>
404 <operationType>${operationType}</operationType>
405 <resourceTemplateUUIDs>${resourceTemplateUUIDs}</resourceTemplateUUIDs>
406 </ns:initResourceOperationStatus>
408 </soapenv:Envelope>"""
410 payload = utils.formatXml(payload)
411 execution.setVariable("CVFMI_initResOperStatusRequest", payload)
412 utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
413 utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
416 utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
417 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
419 utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
422 public void postProcessAAIDEL(DelegateExecution execution) {
423 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
424 utils.log("INFO"," ***** postProcessAAIDEL ***** ", isDebugEnabled)
427 String serviceInstanceId = execution.getVariable("serviceInstanceId")
428 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
430 msg = "Error deleting Service-instance in AAI" + serviceInstanceId
431 utils.log("INFO", msg, isDebugEnabled)
432 WorkflowException workflowException = execution.getVariable("WorkflowException")
433 utils.logAudit("workflowException: " + workflowException)
434 if(workflowException != null){
435 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
439 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
442 } catch (BpmnError e) {
444 } catch (Exception ex) {
445 msg = "Exception in DoDeleteE2EServiceInstance.postProcessAAIDEL. " + ex.getMessage()
446 utils.log("INFO", msg, isDebugEnabled)
447 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
449 utils.log("INFO"," *** Exit postProcessAAIDEL *** ", isDebugEnabled)
453 * post config request.
455 public void postConfigRequest(execution){