Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteCustomE2EServiceInstance.groovy
1 /*-
2
3  * ============LICENSE_START=======================================================
4  * ONAP - SO
5  * ================================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved. 
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts;
24
25 import static org.apache.commons.lang3.StringUtils.*;
26
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.delegate.DelegateExecution
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.VidUtils
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.logger.MsoLogger
37 import org.springframework.web.util.UriUtils;
38
39 import groovy.json.*
40
41 /**
42  * This groovy class supports the <class>DelE2EServiceInstance.bpmn</class> process.
43  *
44  */
45 public class DeleteCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
46
47         String Prefix="DELSI_"
48         ExceptionUtil exceptionUtil = new ExceptionUtil()
49         JsonUtils jsonUtil = new JsonUtils()
50         VidUtils vidUtils = new VidUtils()
51         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteCustomE2EServiceInstance.class);
52         
53         public void preProcessRequest (DelegateExecution execution) {
54                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
55                 execution.setVariable("prefix",Prefix)
56                 String msg = ""
57                 
58                 msoLogger.info("Starting preProcessRequest")
59
60                 try {
61                         // check for incoming json message/input
62                         String siRequest = execution.getVariable("bpmnRequest")
63                         msoLogger.debug(siRequest)
64                         
65                         
66                         String requestId = execution.getVariable("mso-request-id")
67                         execution.setVariable("msoRequestId", requestId)
68                         msoLogger.info("Input Request:" + siRequest + " reqId:" + requestId)
69                         
70                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
71                         if (isBlank(serviceInstanceId)) {
72                                 msg = "Input serviceInstanceId' is null"
73                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
74                         }
75                 
76                                 
77                         //requestInfo
78 //                      String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
79 //                      if (isBlank(productFamilyId))
80 //                      {
81 //                              msg = "Input productFamilyId is null"
82 //                              utils.log("INFO", msg, isDebugEnabled)
83 //                              //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
84 //                      } else {
85 //                              execution.setVariable("productFamilyId", productFamilyId)
86 //                      }
87                         String source = jsonUtil.getJsonValue(siRequest, "source")
88                         execution.setVariable("source", source)
89                         
90                         //subscriberInfo
91                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "globalSubscriberId")
92                         if (isBlank(globalSubscriberId)) {
93                                 msg = "Input globalSubscriberId' is null"
94                                 msoLogger.info(msg)
95                         } else {
96                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
97                         }
98                         
99                         //requestParameters
100                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "serviceType")
101                         if (isBlank(subscriptionServiceType)) {
102                                 msg = "Input subscriptionServiceType is null"
103                                 msoLogger.debug(msg)
104                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105                         } else {
106                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
107                         }
108                         String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
109                         execution.setVariable("operationId", operationId)
110                         
111                         execution.setVariable("operationType", "DELETE") 
112                 } catch (BpmnError e) {
113                         throw e;
114                 } catch (Exception ex){
115                         msg = "Exception in preProcessRequest " + ex.getMessage()
116                         msoLogger.info(msg)
117                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
118                 }
119                 msoLogger.trace("Exit preProcessRequest")
120         }
121
122         public void sendSyncResponse (DelegateExecution execution) {
123                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
124                 msoLogger.trace("Staring sendSyncResponse")
125
126                 try {
127                         String operationId = execution.getVariable("operationId")
128                         String syncResponse = """{"operationId":"${operationId}"}""".trim()
129                         msoLogger.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
130                         sendWorkflowResponse(execution, 202, syncResponse)
131
132                 } catch (Exception ex) {
133                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
134                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
135                 }
136                 msoLogger.trace("Exit sendSyncResopnse")
137         }
138         
139         public void sendSyncError (DelegateExecution execution) {
140                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
141                 msoLogger.info("Starting sendSyncError")
142
143                 try {
144                         String errorMessage = ""
145                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
146                                 WorkflowException wfe = execution.getVariable("WorkflowException")
147                                 errorMessage = wfe.getErrorMessage()
148                         } else {
149                                 errorMessage = "Sending Sync Error."
150                         }
151
152                         String buildworkflowException =
153                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
154                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
155                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
156                                    </aetgt:WorkflowException>"""
157
158                         msoLogger.info(buildworkflowException)
159                         sendWorkflowResponse(execution, 500, buildworkflowException)
160
161                 } catch (Exception ex) {
162                         msoLogger.info("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
163                 }
164
165         }
166         
167         public void prepareCompletionRequest (DelegateExecution execution) {
168                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
169                 msoLogger.trace("Starting prepareCompletion")
170
171                 try {
172                         String requestId = execution.getVariable("msoRequestId")
173                         String source = execution.getVariable("source")
174                         String msoCompletionRequest =
175                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
176                                                                 xmlns:ns="http://org.onap/so/request/types/v1">
177                                                 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
178                                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
179                                                         <action>DELETE</action>
180                                                         <source>${MsoUtils.xmlEscape(source)}</source>
181                                                 </request-info>
182                                                 <aetgt:status-message>E2E Service Instance was deleted successfully.</aetgt:status-message>
183                                                 <aetgt:mso-bpel-name>DeleteCustomE2EServiceInstance</aetgt:mso-bpel-name>
184                                         </aetgt:MsoCompletionRequest>"""
185
186                         // Format Response
187                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
188
189                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
190                         msoLogger.info(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
191
192                 } catch (Exception ex) {
193                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
194                         msoLogger.info(msg)
195                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
196                 }
197                 msoLogger.trace("Exit prepareCompletionRequest")
198         }
199         
200         public void prepareFalloutRequest(DelegateExecution execution){
201                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
202                 msoLogger.trace("Starting prepareFalloutRequest ")
203
204                 try {
205                         WorkflowException wfex = execution.getVariable("WorkflowException")
206                         msoLogger.info(" Input Workflow Exception: " + wfex.toString())
207                         String requestId = execution.getVariable("msoRequestId")
208                         String source = execution.getVariable("source")
209                         String requestInfo =
210                         """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
211                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
212                                         <action>DELETE</action>
213                                         <source>${MsoUtils.xmlEscape(source)}</source>
214                                    </request-info>"""
215
216                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
217                         execution.setVariable("falloutRequest", falloutRequest)
218                 } catch (Exception ex) {
219                         msoLogger.info("Exception prepareFalloutRequest:" + ex.getMessage())
220                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
221                         String requestId = execution.getVariable("msoRequestId")
222                         String falloutRequest =
223                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
224                                                                      xmlns:ns="http://org.onap/so/request/types/v1"
225                                                                      xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
226                                            <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
227                                               <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
228                                               <action>DELETE</action>
229                                               <source>VID</source>
230                                            </request-info>
231                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
232                                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorException)}</aetgt:ErrorMessage>
233                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
234                                                 </aetgt:WorkflowException>
235                                         </aetgt:FalloutHandlerRequest>"""
236
237                         execution.setVariable("falloutRequest", falloutRequest)
238                 }
239                 msoLogger.trace("Exit prepareFalloutRequest")
240         }
241         
242
243         // *******************************
244         //     Build DB request Section
245         // *******************************
246         public void prepareDBRequest (DelegateExecution execution) {
247                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
248                 execution.setVariable("prefix", Prefix)
249
250                 try {
251                         msoLogger.info("Starting prepareDBRequest")
252
253                         String requestId = execution.getVariable("DELSI_requestId")
254                         String statusMessage = "E2E Service Instance successfully deleted."
255
256                         //TODO - verify the format for Service Instance Delete,
257                         String dbRequest =
258                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
259                                                 <soapenv:Header/>
260                                                 <soapenv:Body>
261                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
262                                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
263                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
264                                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
265                                                                 <responseBody></responseBody>
266                                                                 <requestStatus>COMPLETED</requestStatus>
267                                                                 <progress>100</progress>
268                                                         </ns:updateInfraRequest>
269                                                    </soapenv:Body>
270                                            </soapenv:Envelope>"""
271
272                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)
273                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)
274                   msoLogger.info(buildDeleteDBRequestAsString)
275
276                 } catch (Exception ex) {
277                         // try error in method block
278                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()
279                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
280
281                 }
282
283          }
284
285         // *******************************
286         //     Build Error Section
287         // *******************************
288         public void prepareDBRequestError (DelegateExecution execution) {
289                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
290                 execution.setVariable("prefix", Prefix)
291
292                 msoLogger.trace("Start prepareDBRequestError")
293
294                 try {
295                         String requestId = execution.getVariable("DELSI_requestId")
296                         String statusMessage = ""
297                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
298                                 WorkflowException wfe = execution.getVariable("WorkflowException")
299                                 statusMessage = wfe.getErrorMessage()
300
301                         } else {
302                                 statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "
303                         }
304
305                         //TODO - verify the format for Service Instance Create,
306                         String dbRequest =
307                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
308                                                 <soapenv:Header/>
309                                                 <soapenv:Body>
310                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
311                                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
312                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
313                                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
314                                                                 <responseBody></responseBody>
315                                                                 <requestStatus>FAILED</requestStatus>
316                                                         </ns:updateInfraRequest>
317                                                    </soapenv:Body>
318                                            </soapenv:Envelope>"""
319
320                    String buildDBRequestAsString = utils.formatXml(dbRequest)
321                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)
322                    msoLogger.info(buildDBRequestAsString)
323
324                 } catch (Exception ex) {
325                         // try error in method block
326                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()
327                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
328
329                 }
330
331          }
332
333         public void processJavaException(DelegateExecution execution) {
334                 //TODO:
335         }
336
337         /**
338          * Init the service Operation Status
339          */
340         public void prepareInitServiceOperationStatus(DelegateExecution execution){
341                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
342                 utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
343                 try{
344                         String serviceId = execution.getVariable("serviceInstanceId")
345                         String operationId = execution.getVariable("operationId")
346                         String userId = ""
347                         String result = "processing"
348                         String progress = "0"
349                         String reason = ""
350                         String operationContent = "Prepare service creation"
351                         utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
352                         serviceId = UriUtils.encode(serviceId,"UTF-8")
353
354                         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
355                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
356                         utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
357
358                         String payload =
359                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
360                         xmlns:ns="http://org.onap.so/requestsdb">
361                         <soapenv:Header/>
362                         <soapenv:Body>
363                             <ns:updateServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
364                             <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
365                             <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
366                             <operationType>DELETE</operationType>
367                             <userId>${MsoUtils.xmlEscape(userId)}</userId>
368                             <result>${MsoUtils.xmlEscape(result)}</result>
369                             <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
370                             <progress>${MsoUtils.xmlEscape(progress)}</progress>
371                             <reason>${MsoUtils.xmlEscape(reason)}</reason>
372                         </ns:updateServiceOperationStatus>
373                     </soapenv:Body>
374                 </soapenv:Envelope>"""
375
376                         payload = utils.formatXml(payload)
377                         execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
378                         utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)
379
380                 }catch(Exception e){
381                         utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
382                         execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
383                 }
384                 utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
385         }
386 }