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