Merge "Remove Swagger UI"
[so.git] / bpmn / so-bpmn-infrastructure-common / 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.bpmn.core.UrnPropertiesReader
37 import org.onap.so.logger.MsoLogger
38 import org.springframework.web.util.UriUtils;
39
40 import groovy.json.*
41
42 /**
43  * This groovy class supports the <class>DelE2EServiceInstance.bpmn</class> process.
44  *
45  */
46 public class DeleteCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
47
48         String Prefix="DELSI_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         VidUtils vidUtils = new VidUtils()
52         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteCustomE2EServiceInstance.class);
53         
54         public void preProcessRequest (DelegateExecution execution) {
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                 msoLogger.trace("Staring sendSyncResponse")
124
125                 try {
126                         String operationId = execution.getVariable("operationId")
127                         String syncResponse = """{"operationId":"${operationId}"}""".trim()
128                         msoLogger.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
129                         sendWorkflowResponse(execution, 202, syncResponse)
130
131                 } catch (Exception ex) {
132                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
133                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
134                 }
135                 msoLogger.trace("Exit sendSyncResopnse")
136         }
137         
138         public void sendSyncError (DelegateExecution execution) {
139                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
140                 msoLogger.info("Starting sendSyncError")
141
142                 try {
143                         String errorMessage = ""
144                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
145                                 WorkflowException wfe = execution.getVariable("WorkflowException")
146                                 errorMessage = wfe.getErrorMessage()
147                         } else {
148                                 errorMessage = "Sending Sync Error."
149                         }
150
151                         String buildworkflowException =
152                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
153                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
154                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
155                                    </aetgt:WorkflowException>"""
156
157                         msoLogger.info(buildworkflowException)
158                         sendWorkflowResponse(execution, 500, buildworkflowException)
159
160                 } catch (Exception ex) {
161                         msoLogger.info("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
162                 }
163
164         }
165         
166         public void prepareCompletionRequest (DelegateExecution execution) {
167                 msoLogger.trace("Starting prepareCompletion")
168
169                 try {
170                         String requestId = execution.getVariable("msoRequestId")
171                         String source = execution.getVariable("source")
172                         String msoCompletionRequest =
173                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
174                                                                 xmlns:ns="http://org.onap/so/request/types/v1">
175                                                 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
176                                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
177                                                         <action>DELETE</action>
178                                                         <source>${MsoUtils.xmlEscape(source)}</source>
179                                                 </request-info>
180                                                 <aetgt:status-message>E2E Service Instance was deleted successfully.</aetgt:status-message>
181                                                 <aetgt:mso-bpel-name>DeleteCustomE2EServiceInstance</aetgt:mso-bpel-name>
182                                         </aetgt:MsoCompletionRequest>"""
183
184                         // Format Response
185                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
186
187                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
188                         msoLogger.info(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
189
190                 } catch (Exception ex) {
191                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
192                         msoLogger.info(msg)
193                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
194                 }
195                 msoLogger.trace("Exit prepareCompletionRequest")
196         }
197         
198         public void prepareFalloutRequest(DelegateExecution execution){
199                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
200                 msoLogger.trace("Starting prepareFalloutRequest ")
201
202                 try {
203                         WorkflowException wfex = execution.getVariable("WorkflowException")
204                         msoLogger.info(" Input Workflow Exception: " + wfex.toString())
205                         String requestId = execution.getVariable("msoRequestId")
206                         String source = execution.getVariable("source")
207                         String requestInfo =
208                         """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
209                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
210                                         <action>DELETE</action>
211                                         <source>${MsoUtils.xmlEscape(source)}</source>
212                                    </request-info>"""
213
214                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
215                         execution.setVariable("falloutRequest", falloutRequest)
216                 } catch (Exception ex) {
217                         msoLogger.info("Exception prepareFalloutRequest:" + ex.getMessage())
218                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
219                         String requestId = execution.getVariable("msoRequestId")
220                         String falloutRequest =
221                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
222                                                                      xmlns:ns="http://org.onap/so/request/types/v1"
223                                                                      xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
224                                            <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
225                                               <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
226                                               <action>DELETE</action>
227                                               <source>VID</source>
228                                            </request-info>
229                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
230                                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorException)}</aetgt:ErrorMessage>
231                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
232                                                 </aetgt:WorkflowException>
233                                         </aetgt:FalloutHandlerRequest>"""
234
235                         execution.setVariable("falloutRequest", falloutRequest)
236                 }
237                 msoLogger.trace("Exit prepareFalloutRequest")
238         }
239         
240
241         // *******************************
242         //     Build DB request Section
243         // *******************************
244         public void prepareDBRequest (DelegateExecution execution) {
245                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
246                 execution.setVariable("prefix", Prefix)
247
248                 try {
249                         msoLogger.info("Starting prepareDBRequest")
250
251                         String requestId = execution.getVariable("DELSI_requestId")
252                         String statusMessage = "E2E Service Instance successfully deleted."
253
254                         //TODO - verify the format for Service Instance Delete,
255                         String dbRequest =
256                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
257                                                 <soapenv:Header/>
258                                                 <soapenv:Body>
259                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
260                                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
261                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
262                                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
263                                                                 <responseBody></responseBody>
264                                                                 <requestStatus>COMPLETED</requestStatus>
265                                                                 <progress>100</progress>
266                                                         </ns:updateInfraRequest>
267                                                    </soapenv:Body>
268                                            </soapenv:Envelope>"""
269
270                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)
271                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)
272                   msoLogger.info(buildDeleteDBRequestAsString)
273
274                 } catch (Exception ex) {
275                         // try error in method block
276                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()
277                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
278
279                 }
280
281          }
282
283         // *******************************
284         //     Build Error Section
285         // *******************************
286         public void prepareDBRequestError (DelegateExecution execution) {
287                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
288                 execution.setVariable("prefix", Prefix)
289
290                 msoLogger.trace("Start prepareDBRequestError")
291
292                 try {
293                         String requestId = execution.getVariable("DELSI_requestId")
294                         String statusMessage = ""
295                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
296                                 WorkflowException wfe = execution.getVariable("WorkflowException")
297                                 statusMessage = wfe.getErrorMessage()
298
299                         } else {
300                                 statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "
301                         }
302
303                         //TODO - verify the format for Service Instance Create,
304                         String dbRequest =
305                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
306                                                 <soapenv:Header/>
307                                                 <soapenv:Body>
308                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
309                                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
310                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
311                                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
312                                                                 <responseBody></responseBody>
313                                                                 <requestStatus>FAILED</requestStatus>
314                                                         </ns:updateInfraRequest>
315                                                    </soapenv:Body>
316                                            </soapenv:Envelope>"""
317
318                    String buildDBRequestAsString = utils.formatXml(dbRequest)
319                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)
320                    msoLogger.info(buildDBRequestAsString)
321
322                 } catch (Exception ex) {
323                         // try error in method block
324                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()
325                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
326
327                 }
328
329          }
330
331         public void processJavaException(DelegateExecution execution) {
332                 //TODO:
333         }
334
335         /**
336          * Init the service Operation Status
337          */
338         public void prepareInitServiceOperationStatus(DelegateExecution execution){
339                 msoLogger.debug("======== STARTED prepareInitServiceOperationStatus Process ======== ")
340                 try{
341                         String serviceId = execution.getVariable("serviceInstanceId")
342                         String operationId = execution.getVariable("operationId")
343                         String userId = ""
344                         String result = "processing"
345                         String progress = "0"
346                         String reason = ""
347                         String operationContent = "Prepare service creation"
348                         msoLogger.debug("Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId)
349                         serviceId = UriUtils.encode(serviceId,"UTF-8")
350
351                         def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
352                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
353                         msoLogger.debug("DB Adapter Endpoint is: " + dbAdapterEndpoint)
354
355                         String payload =
356                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
357                         xmlns:ns="http://org.onap.so/requestsdb">
358                         <soapenv:Header/>
359                         <soapenv:Body>
360                             <ns:updateServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
361                             <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
362                             <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
363                             <operationType>DELETE</operationType>
364                             <userId>${MsoUtils.xmlEscape(userId)}</userId>
365                             <result>${MsoUtils.xmlEscape(result)}</result>
366                             <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
367                             <progress>${MsoUtils.xmlEscape(progress)}</progress>
368                             <reason>${MsoUtils.xmlEscape(reason)}</reason>
369                         </ns:updateServiceOperationStatus>
370                     </soapenv:Body>
371                 </soapenv:Envelope>"""
372
373                         payload = utils.formatXml(payload)
374                         execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
375                         msoLogger.debug("Outgoing updateServiceOperStatusRequest: \n" + payload)
376
377                 }catch(Exception e){
378                         msoLogger.error("Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e)
379                         execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
380                 }
381                 msoLogger.debug("======== COMPLETED prepareInitServiceOperationStatus Process ======== ")
382         }
383 }