Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DeleteSDNCNetworkResource.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.infrastructure.scripts
22
23 import org.json.JSONObject
24 import org.json.XML;
25
26 import static org.apache.commons.lang3.StringUtils.*;
27 import groovy.xml.XmlUtil
28 import groovy.json.*
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor 
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
31 import org.openecomp.mso.bpmn.common.recipe.ResourceInput;
32 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder 
33 import org.openecomp.mso.bpmn.core.WorkflowException 
34 import org.openecomp.mso.bpmn.core.json.JsonUtils
35 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
36 import org.openecomp.mso.rest.APIResponse
37 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
38
39 import java.util.UUID;
40
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.openecomp.mso.rest.RESTClient 
47 import org.openecomp.mso.rest.RESTConfig
48 import org.openecomp.mso.rest.APIResponse;
49
50 /**
51  * This groovy class supports the <class>DeleteSDNCCNetworkResource.bpmn</class> process.
52  * flow for SDNC Network Resource 
53  */
54 public class DeleteSDNCNetworkResource extends AbstractServiceTaskProcessor {
55
56     String Prefix="DELSDNCRES_"
57             
58     ExceptionUtil exceptionUtil = new ExceptionUtil()
59
60     JsonUtils jsonUtil = new JsonUtils()
61
62     SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
63     
64     public void preProcessRequest(DelegateExecution execution){
65         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
66         utils.log("INFO"," ***** Started preProcessRequest *****",  isDebugEnabled)
67         try {           
68             
69             //get bpmn inputs from resource request.
70             String requestId = execution.getVariable("mso-request-id")
71             String requestAction = execution.getVariable("requestAction")
72             utils.log("INFO","The requestAction is: " + requestAction,  isDebugEnabled)
73             String recipeParamsFromRequest = execution.getVariable("recipeParams")
74             utils.log("INFO","The recipeParams is: " + recipeParamsFromRequest,  isDebugEnabled)
75             String resourceInput = execution.getVariable("resourceInput")
76             utils.log("INFO","The resourceInput is: " + resourceInput,  isDebugEnabled)
77             //Get ResourceInput Object
78             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
79             execution.setVariable(Prefix + "resourceInput", resourceInputObj)
80             
81             //Deal with recipeParams
82             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
83             String resourceModelName = resourceInputObj.getResourceModelInfo().getModelName()            
84             //For sdnc requestAction default is "NetworkInstance"
85             String operationType = "Network"    
86             if(!StringUtils.isBlank(recipeParamsFromRequest) && "null" != recipeParamsFromRequest){
87                 //the operationType from worflow(first node) is second priority.
88                 operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")
89             }
90             if(!StringUtils.isBlank(recipeParamsFromWf)){
91                 //the operationType from worflow(first node) is highest priority.
92                 operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")
93             }
94             
95             
96             //For sdnc, generate svc_action and request_action
97             String sdnc_svcAction = "delete"
98             if(StringUtils.containsIgnoreCase(resourceModelName, "overlay")){
99                 //This will be resolved in R3.
100                 sdnc_svcAction ="deactivate"
101                 operationType = "NCINetwork"        
102             }
103             if(StringUtils.containsIgnoreCase(resourceModelName, "underlay")){
104                 //This will be resolved in R3.
105                 operationType ="Network"
106             }        
107             String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + operationType +"Instance"                    
108             execution.setVariable(Prefix + "svcAction", sdnc_svcAction)        
109             execution.setVariable(Prefix + "requestAction", sdnc_requestAction)
110             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
111             execution.setVariable("mso-request-id", requestId)
112             execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
113             //TODO Here build networkrequest
114             
115         } catch (BpmnError e) {
116             throw e;
117         } catch (Exception ex){
118             msg = "Exception in preProcessRequest " + ex.getMessage()
119             utils.log("DEBUG", msg, isDebugEnabled)
120             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
121         }
122     }
123     
124     /**
125      * Pre Process the BPMN Flow Request
126      * Inclouds:
127      * generate the nsOperationKey
128      * generate the nsParameters
129      */
130     public void prepareSDNCRequest (DelegateExecution execution) {
131         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
132         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)
133
134         try {
135             // get variables
136             String sdnc_svcAction = execution.getVariable(Prefix + "svcAction")        
137             String sdnc_requestAction = execution.getVariable(Prefix + "requestAction")
138             String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
139             String deleteNetworkInput = execution.getVariable(Prefix + "networkRequest")
140
141             String hdrRequestId = execution.getVariable("mso-request-id")
142             String serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")
143             String source = execution.getVariable("source")
144             String sdnc_service_id = execution.getVariable(Prefix + "sdncServiceId")
145             ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
146             String serviceType = resourceInputObj.getServiceType()
147             String serviceModelInvariantUuid = resourceInputObj.getServiceModelInfo().getModelInvariantUuid()
148             String serviceModelUuid = resourceInputObj.getServiceModelInfo().getModelUuid()
149             String serviceModelVersion = resourceInputObj.getServiceModelInfo().getModelVersion()
150             String serviceModelName = resourceInputObj.getServiceModelInfo().getModelName()
151             String globalCustomerId = resourceInputObj.getGlobalSubscriberId()
152             String modelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid();
153             String modelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
154             String modelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
155             String modelName = resourceInputObj.getResourceModelInfo().getModelName()
156             String modelVersion = resourceInputObj.getResourceModelInfo().getModelVersion()
157             // 1. prepare assign topology via SDNC Adapter SUBFLOW call
158             String sndcTopologyDeleteRequest =
159                     """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
160                                                               xmlns:sdncadapter="http://org.openecomp.mso/workflow/sdnc/adapter/schema/v1" 
161                                                               xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1">
162                                  <sdncadapter:RequestHeader>
163                                     <sdncadapter:RequestId>${hdrRequestId}</sdncadapter:RequestId>
164                                     <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>
165                                     <sdncadapter:SvcAction>${sdnc_svcAction}</sdncadapter:SvcAction>
166                                     <sdncadapter:SvcOperation>network-topology-operation</sdncadapter:SvcOperation>
167                                     <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
168                                     <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
169                                  </sdncadapter:RequestHeader>
170                                  <sdncadapterworkflow:SDNCRequestData>
171                                      <request-information>
172                                         <request-id>${hdrRequestId}</request-id>
173                                         <request-action>${sdnc_requestAction}</request-action>
174                                         <source>${source}</source>
175                                         <notification-url></notification-url>
176                                         <order-number></order-number>
177                                         <order-version></order-version>
178                                      </request-information>
179                                      <service-information>
180                                         <service-id>${serviceInstanceId}</service-id>
181                                         <subscription-service-type>${serviceType}</subscription-service-type>
182                                         <onap-model-information>
183                                              <model-invariant-uuid>${serviceModelInvariantUuid}</model-invariant-uuid>
184                                              <model-uuid>${serviceModelUuid}</model-uuid>
185                                              <model-version>${serviceModelVersion}</model-version>
186                                              <model-name>${serviceModelName}</model-name>
187                                         </onap-model-information>
188                                         <service-instance-id>${serviceInstanceId}</service-instance-id>
189                                         <global-customer-id>${globalCustomerId}</global-customer-id>
190                                      </service-information>
191                                      <network-information>
192                                         <onap-model-information>
193                                              <model-invariant-uuid>${modelInvariantUuid}</model-invariant-uuid>
194                                              <model-customization-uuid>${modelCustomizationUuid}</model-customization-uuid>
195                                              <model-uuid>${modelUuid}</model-uuid>
196                                              <model-version>${modelVersion}</model-version>
197                                              <model-name>${modelName}</model-name>
198                                         </onap-model-information>
199                                      </network-information>
200                                      <network-request-input>
201                                        <network-input-parameters></network-input-parameters>
202                                      </network-request-input>
203                                 </sdncadapterworkflow:SDNCRequestData>
204                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()
205             
206             String sndcTopologyDeleteRequesAsString = utils.formatXml(sndcTopologyDeleteRequest)
207             utils.logAudit(sndcTopologyDeleteRequesAsString)
208             execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyDeleteRequesAsString)
209             utils.log("INFO","sdncAdapterWorkflowRequest - " + "\n" +  sndcTopologyDeleteRequesAsString, isDebugEnabled)
210
211         } catch (Exception ex) {
212             String exceptionMessage = " Bpmn error encountered in DeleteSDNCCNetworkResource flow. prepareSDNCRequest() - " + ex.getMessage()
213             utils.log("DEBUG", exceptionMessage, isDebugEnabled)
214             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
215
216         }
217        utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)
218         }
219
220     private void setProgressUpdateVariables(DelegateExecution execution, String body) {
221         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
222         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
223         execution.setVariable("CVFMI_updateResOperStatusRequest", body)
224     }
225
226     public void prepareUpdateBeforeDeleteSDNCResource(DelegateExecution execution) {
227         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
228         String operType = resourceInputObj.getOperationType()
229         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
230         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
231         String operationId = resourceInputObj.getOperationId()
232         String progress = "20"
233         String status = "processing"
234         String statusDescription = "SDCN resource delete invoked"
235
236         //String operationId = execution.getVariable("operationId")
237
238         String body = """
239                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
240                         xmlns:ns="http://org.openecomp.mso/requestsdb">
241                         <soapenv:Header/>
242                 <soapenv:Body>
243                     <ns:updateResourceOperationStatus>
244                                <operType>${operType}</operType>
245                                <operationId>${operationId}</operationId>
246                                <progress>${progress}</progress>
247                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
248                                <serviceId>${serviceInstanceId}</serviceId>
249                                <status>${status}</status>
250                                <statusDescription>${statusDescription}</statusDescription>
251                     </ns:updateResourceOperationStatus>
252                 </soapenv:Body>
253                 </soapenv:Envelope>""";
254
255         setProgressUpdateVariables(execution, body)
256
257     }
258
259     public void prepareUpdateAfterDeleteSDNCResource(DelegateExecution execution) {
260         ResourceInput resourceInputObj = execution.getVariable(Prefix + "resourceInput")
261         String operType = resourceInputObj.getOperationType()
262         String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
263         String serviceInstanceId = resourceInputObj.getServiceInstanceId()
264         String operationId = resourceInputObj.getOperationId()
265         String progress = "100"
266         String status = "finished"
267         String statusDescription = "SDCN resource delete completed"
268
269         //String operationId = execution.getVariable("operationId")
270
271         String body = """
272                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
273                         xmlns:ns="http://org.openecomp.mso/requestsdb">
274                         <soapenv:Header/>
275                 <soapenv:Body>
276                     <ns:updateResourceOperationStatus>
277                                <operType>${operType}</operType>
278                                <operationId>${operationId}</operationId>
279                                <progress>${progress}</progress>
280                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
281                                <serviceId>${serviceInstanceId}</serviceId>
282                                <status>${status}</status>
283                                <statusDescription>${statusDescription}</statusDescription>
284                     </ns:updateResourceOperationStatus>
285                 </soapenv:Body>
286                 </soapenv:Envelope>""";
287
288         setProgressUpdateVariables(execution, body)
289     }
290
291     public void postDeleteSDNCCall(DelegateExecution execution){
292         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
293         utils.log("INFO"," ***** Started prepareSDNCRequest *****",  isDebugEnabled)
294         String responseCode = execution.getVariable(Prefix + "sdncDeleteReturnCode")
295         String responseObj = execution.getVariable(Prefix + "SuccessIndicator")
296         
297         utils.log("INFO","response from sdnc, response code :" + responseCode + "  response object :" + responseObj,  isDebugEnabled)
298         utils.log("INFO"," ***** Exit prepareSDNCRequest *****",  isDebugEnabled)
299     }
300     
301         public void sendSyncResponse (DelegateExecution execution) {
302                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
303                 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
304
305                 try {
306                         String operationStatus = "finished"
307                         // RESTResponse for main flow
308                         String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
309                         utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)
310                         sendWorkflowResponse(execution, 202, resourceOperationResp)
311                         execution.setVariable("sentSyncResponse", true)
312
313                 } catch (Exception ex) {
314                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
315                         utils.log("DEBUG", msg, isDebugEnabled)
316                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
317                 }
318                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
319         }
320 }