e18af172dee4c518e3dd40085a164fa6aec5b70e
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DeleteNetworkInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
24 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
25 import org.openecomp.mso.bpmn.common.scripts.NetworkUtils
26 import org.openecomp.mso.bpmn.common.scripts.VidUtils
27 import org.openecomp.mso.bpmn.core.WorkflowException
28 import org.openecomp.mso.bpmn.core.json.JsonUtils;
29 import org.openecomp.mso.rest.APIResponse
30 import java.util.UUID;
31 import org.camunda.bpm.engine.delegate.BpmnError
32 import org.camunda.bpm.engine.runtime.Execution
33 import org.apache.commons.lang3.*
34 import org.apache.commons.codec.binary.Base64;
35 import org.springframework.web.util.UriUtils
36 import groovy.xml.XmlUtil
37 import groovy.json.*
38
39 public class DeleteNetworkInstance extends AbstractServiceTaskProcessor {
40         String Prefix="DELNI_"
41         String groovyClassName = "DeleteNetworkInstance"
42         ExceptionUtil exceptionUtil = new ExceptionUtil()
43         JsonUtils jsonUtil = new JsonUtils()
44         VidUtils vidUtils = new VidUtils(this)
45         NetworkUtils networkUtils = new NetworkUtils()
46
47
48         public InitializeProcessVariables(Execution execution){
49                 
50                 execution.setVariable(Prefix + "Success", false)
51                 
52                 execution.setVariable(Prefix + "CompleteMsoProcessRequest", "")
53                 execution.setVariable(Prefix + "FalloutHandlerRequest", "")
54                 execution.setVariable(Prefix + "isSilentSuccess", false)
55                 
56         }
57         
58         // **************************************************
59         //     Pre or Prepare Request Section
60         // **************************************************
61
62         public void preProcessRequest (Execution execution) {
63                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
64                 execution.setVariable("prefix",Prefix)
65
66                 utils.log("DEBUG", " ***** Inside preProcessRequest() of " + groovyClassName + " ***** ", isDebugEnabled)
67
68                 try {
69                         // initialize flow variables
70                         InitializeProcessVariables(execution)
71                                                         
72                         String sdncVersion = execution.getVariable("sdncVersion")
73                         if (sdncVersion == null || sdncVersion == '1610') {                             
74                                 // 'a-la-cart' default, sdncVersion = '1610' 
75                                 execution.setVariable("sdncVersion", "1610")
76                                 String bpmnRequest = execution.getVariable("bpmnRequest")
77                                 // set 'disableRollback'
78                                 if (bpmnRequest != null) {
79                                         String disableRollback = jsonUtil.getJsonValue(bpmnRequest, "requestDetails.requestInfo.suppressRollback")
80                                         if (disableRollback != null) {
81                                                 execution.setVariable("disableRollback", disableRollback)
82                                                 utils.log("DEBUG", "Received 'suppressRollback': " + disableRollback , isDebugEnabled)
83                                         } else {
84                                             execution.setVariable("disableRollback", false)
85                                         }
86                                         utils.log("DEBUG", " Set 'disableRollback' : " + execution.getVariable("disableRollback") , isDebugEnabled)
87                                 } else {
88                                         String dataErrorMessage = " Invalid 'bpmnRequest' request."
89                                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)
90                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
91                                 }
92
93                         } else {
94                             // 'macro' test ONLY, sdncVersion = '1702'
95                             utils.log("DEBUG", " 'disableRollback' : " + execution.getVariable("disableRollback") , isDebugEnabled)
96                         }       
97                         
98                         // get/set 'msoRequestId' and 'mso-request-id'
99                         String requestId = execution.getVariable("msoRequestId")
100                         if (requestId != null) {
101                                 execution.setVariable("mso-request-id", requestId)
102                         } else {
103                                 requestId = execution.getVariable("mso-request-id")
104                         }
105                         execution.setVariable(Prefix + "requestId", requestId)
106                         
107                         // get/set 'requestId' 
108                     if (execution.getVariable("requestId") == null) {
109                                 execution.setVariable("requestId", requestId)
110                         }
111                         
112                         // set action to "DELETE"
113                         execution.setVariable("action", "DELETE")
114                         
115                         //Place holder for additional code.
116
117                         // TODO ???
118                         // userParams???  1) pre-loads indicator, 2) 'auto-activation'  
119                         // Tag/Value parameters
120                         //
121                         // Map: 'networkInputParams': 'auto-activation''
122                         // Sample format? 
123                         // "requestParameters": {
124                         //     "userParams": [  
125             //          {
126                         //               "name": "someUserParam1",
127                         //               "value": "someValue1"
128                         //          }
129             //     ]
130                     //   }
131                         // 
132                         // String userParams = //use json util to extract "userParams"// 
133                 // execution.setVariable("networkInputParams", userParams)
134                         // else: execution.setVariable("networkInputParams", null)
135                         //
136
137
138                 } catch (Exception ex){
139                         sendSyncError(execution)
140                         String exceptionMessage = "Exception Encountered in " + groovyClassName + ", PreProcessRequest() - " + ex.getMessage()
141                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
142                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
143
144                 }
145
146         }
147
148         public void getNetworkModelInfo (Execution execution) {
149                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
150                 execution.setVariable("prefix", Prefix)
151
152                 utils.log("DEBUG", " ***** Inside getNetworkModelInfo() of DeleteNetworkInstance ***** ", isDebugEnabled)
153                 
154                 try {
155                         
156                         // "networkModelInfo" is expected to be sent
157                         String networkModelInfo = execution.getVariable("networkModelInfo")
158                         utils.log("DEBUG", " networkModelInfo - " + networkModelInfo, isDebugEnabled)
159                                                  
160                                                                                  
161                 } catch (Exception ex) {
162                         sendSyncError(execution)
163                    String exceptionMessage = "Bpmn error encountered in DeleteNetworkInstance flow. getNetworkModelInfo() - " + ex.getMessage()
164                    utils.log("DEBUG", exceptionMessage, isDebugEnabled)
165                    exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
166                         
167                 }
168
169         }
170         
171         public void sendSyncResponse (Execution execution) {
172                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
173                 execution.setVariable("prefix",Prefix)
174
175                 utils.log("DEBUG", " ***** Inside sendSyncResponse() of DeleteNetworkInstance ***** ", isDebugEnabled)
176
177                 try {
178                         String requestId = execution.getVariable("mso-request-id")
179                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
180
181                         // RESTResponse (for API Handler (APIH) Reply Task)
182                         String deleteNetworkRestRequest = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()
183
184                         utils.log("DEBUG", " sendSyncResponse to APIH - " + "\n" + deleteNetworkRestRequest, isDebugEnabled)
185
186                         sendWorkflowResponse(execution, 202, deleteNetworkRestRequest)
187
188                 } catch (Exception ex) {
189                          // caught exception
190                         String exceptionMessage = "Exception Encountered in  DeleteNetworkInstance, sendSyncResponse() - " + ex.getMessage()
191                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
192                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
193
194                 }
195
196         }
197
198         public void prepareCompletion (Execution execution) {
199                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
200                 execution.setVariable("prefix",Prefix)
201
202                 utils.log("DEBUG", " ***** Inside prepareCompletion() of CreateNetworkInstance ***** ", isDebugEnabled)
203
204                 try {
205
206                         String requestId = execution.getVariable("mso-request-id")
207                         String source = execution.getVariable(Prefix + "source")
208
209                         String msoCompletionRequest =
210                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
211                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">
212                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
213                                                         <request-id>${requestId}</request-id>
214                                                         <action>DELETE</action>
215                                                         <source>VID</source>
216                                                 </request-info>
217                                                 <aetgt:status-message>Network has been deleted successfully.</aetgt:status-message>
218                                                 <aetgt:mso-bpel-name>BPMN Network action: DELETE</aetgt:mso-bpel-name>
219                                         </aetgt:MsoCompletionRequest>"""
220
221                                 // Format Response
222                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
223
224                         // normal path
225                         execution.setVariable(Prefix + "CompleteMsoProcessRequest", xmlMsoCompletionRequest)
226                         utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
227                 
228                 } catch (Exception ex) {
229                         String exceptionMessage = " Bpmn error encountered in CreateNetworkInstance flow. prepareCompletion() - " + ex.getMessage()
230                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
231                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
232
233                 }
234
235
236         }
237         
238         public void prepareDBRequestError (Execution execution) {
239                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
240                 execution.setVariable("prefix", Prefix)
241
242                 try {
243                         utils.log("DEBUG", " ***** Inside prepareDBRequestError of DeleteNetworkInstance ***** ", isDebugEnabled)
244
245                         WorkflowException wfe = execution.getVariable("WorkflowException")
246                         String statusMessage = wfe.getErrorMessage()
247                         String requestId = execution.getVariable(Prefix +"requestId")
248
249                         String dbRequest =
250                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
251                                                 <soapenv:Header/>
252                                                 <soapenv:Body>
253                                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">
254                                                                 <requestId>${requestId}</requestId>
255                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
256                                                                 <statusMessage>${statusMessage}</statusMessage>
257                                                                 <responseBody></responseBody>
258                                                                 <requestStatus>FAILED</requestStatus>
259                                                                 <progress></progress>
260                                                                 <vnfOutputs>&lt;network-outputs xmlns="http://org.openecomp/mso/infra/vnf-request/v1" xmlns:aetgt="http://org.openecomp/mso/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"/&gt;</vnfOutputs>
261                                                         </ns:updateInfraRequest>
262                                                 </soapenv:Body>
263                                            </soapenv:Envelope>"""
264
265                    execution.setVariable(Prefix + "deleteDBRequest", dbRequest)
266                    utils.log("DEBUG", " DB Adapter Request - " + "\n" + dbRequest, isDebugEnabled)
267
268                 } catch (Exception ex) {
269                         // caught exception
270                         String exceptionMessage = "Bpmn error encountered in  DeleteNetworkInstance, prepareDBRequestError() - " + ex.getMessage()
271                         logError(exceptionMessage)
272                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
273                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
274
275                 }
276
277          }
278
279         // **************************************************
280         //     Post or Validate Response Section
281         // **************************************************
282
283         public void postProcessResponse (Execution execution) {
284                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
285                 execution.setVariable("prefix", Prefix)
286         
287                 utils.log("DEBUG", " ***** Inside postProcessResponse() of DeleteNetworkInstance ***** ", isDebugEnabled)
288                 
289                 try {
290                         if (execution.getVariable("CMSO_ResponseCode") == "200") {
291                            execution.setVariable(Prefix + "Success", true)
292                            utils.log("DEBUG", " ***** DeleteNetworkInstance Success ***** ", isDebugEnabled)
293                            //   Place holder for additional code.
294                            
295                         } else {
296                            execution.setVariable(Prefix + "Success", false)
297                            utils.log("DEBUG", " ***** DeleteNetworkInstance Failed in CompletionMsoProces flow!. ***** ", isDebugEnabled)
298                         
299                         }   
300                         
301         
302                 } catch (Exception ex) {
303                         String exceptionMessage = " Bpmn error encountered in DeleteNetworkInstance flow. postProcessResponse() - " + ex.getMessage()
304                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
305                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
306         
307             }
308         
309         }
310
311
312         // *******************************
313         //     Build Error Section
314         // *******************************
315
316         // Prepare for FalloutHandler
317         public void buildErrorResponse (Execution execution) {
318                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
319                 execution.setVariable("prefix", Prefix)
320
321                 utils.log("DEBUG", " ***** Prepare for FalloutHandler. FAILURE - prepare request for sub-process FalloutHandler. *****", isDebugEnabled)
322
323                 String dbReturnCode = execution.getVariable(Prefix + "dbReturnCode")
324                 utils.log("DEBUG", " ***** DB Update Response Code  : " + dbReturnCode, isDebugEnabled)
325                 utils.log("DEBUG", " ***** DB Update Response String: " + '\n' + execution.getVariable(Prefix + "deleteDBResponse"), isDebugEnabled)
326
327                 String falloutHandlerRequest = ""
328                 String requestId = execution.getVariable("mso-request-id")
329                 String source = execution.getVariable(Prefix + "source")
330                 execution.setVariable(Prefix + "Success", false)
331                 try {
332                         WorkflowException wfe = execution.getVariable("WorkflowException")
333                         String errorCode = String.valueOf(wfe.getErrorCode())
334                         String errorMessage = wfe.getErrorMessage()
335
336                         falloutHandlerRequest =
337                             """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
338                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"
339                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
340                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
341                                               <request-id>${requestId}</request-id>
342                                               <action>DELETE</action>
343                                               <source>${source}</source>
344                                            </request-info>
345                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
346                                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
347                                                         <aetgt:ErrorCode>${errorCode}</aetgt:ErrorCode>
348                                                 </aetgt:WorkflowException>
349                                         </aetgt:FalloutHandlerRequest>"""
350
351                         utils.logAudit(falloutHandlerRequest)
352                         execution.setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequest)
353                         utils.log("ERROR", "  Overall Error Response going to FalloutHandler: " + "\n" + falloutHandlerRequest, isDebugEnabled)
354
355                 } catch (Exception ex) {
356                         // caught exception
357                         String exceptionMessage = "Bpmn error encountered in DeleteNetworkInstance, buildErrorResponse() - " + ex.getMessage()
358                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
359                         falloutHandlerRequest =
360                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
361                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"
362                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
363                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
364                                               <request-id>${requestId}</request-id>
365                                               <action>DELEtE</action>
366                                               <source>${source}</source>
367                                            </request-info>
368                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
369                                                         <aetgt:ErrorMessage>${exceptionMessage}</aetgt:ErrorMessage>
370                                                         <aetgt:ErrorCode>9999</aetgt:ErrorCode>
371                                                 </aetgt:WorkflowException>
372                                         </aetgt:FalloutHandlerRequest>"""
373                         execution.setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequest)
374                         utils.log("DEBUG", "  Overall Error Response going to FalloutHandler: " + "\n" + falloutHandlerRequest, isDebugEnabled)
375                 }
376         }
377
378
379         public void sendSyncError (Execution execution) {
380                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
381                 execution.setVariable("prefix", Prefix)
382
383                 try {
384                         String requestId = execution.getVariable("mso-request-id")
385                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
386
387                         // RESTResponse (for API Handler (APIH) Reply Task)
388                         String deleteNetworkRestError = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()
389
390                         utils.log("DEBUG", " sendSyncResponse to APIH - " + "\n" + deleteNetworkRestError, isDebugEnabled)
391
392                         sendWorkflowResponse(execution, 500, deleteNetworkRestError)
393
394                 } catch (Exception ex) {
395                         utils.log("DEBUG", " Sending Sync Error Activity Failed -  DeleteNetworkInstance, sendSyncError(): " + "\n" + ex.getMessage(), isDebugEnabled)
396                 }
397         }
398
399         public void processJavaException(Execution execution){
400                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
401                 execution.setVariable("prefix",Prefix)
402                 try{
403                         utils.log("DEBUG", "Caught a Java Exception", isDebugEnabled)
404                         utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)
405                         utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)
406                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception")  // Adding this line temporarily until this flows error handling gets updated
407                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
408                         
409                 }catch(Exception e){
410                         utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)
411                         execution.setVariable("UnexpectedError", "Exception in processJavaException method")  // Adding this line temporarily until this flows error handling gets updated
412                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method")
413                 }
414                 utils.log("DEBUG", "Completed processJavaException Method of " + Prefix, isDebugEnabled)
415         }
416
417 }