Fix the structure in the doc for bpmn
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteVnfInfra.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.bpmn.infrastructure.scripts
22
23
24 import org.apache.commons.lang3.*
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor;
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
29 import org.onap.so.bpmn.common.scripts.MsoUtils
30 import org.onap.so.bpmn.common.scripts.VidUtils;
31 import org.onap.so.bpmn.core.WorkflowException
32 import org.onap.so.bpmn.core.json.JsonUtils;
33 import org.onap.so.logger.MsoLogger
34
35
36 /**
37  * This class supports the DeleteVnfInfra Flow
38  * with the Deletion of a generic vnf for
39  * infrastructure.
40  */
41 class DeleteVnfInfra extends AbstractServiceTaskProcessor {
42
43         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteVnfInfra.class);
44         
45         String Prefix="DELVI_"
46         ExceptionUtil exceptionUtil = new ExceptionUtil()
47         JsonUtils jsonUtil = new JsonUtils()
48         VidUtils vidUtils = new VidUtils(this)
49
50         /**
51          * This method gets and validates the incoming
52          * request.
53          *
54          * @param - execution
55          */
56         public void preProcessRequest(DelegateExecution execution) {
57                 execution.setVariable("prefix",Prefix)
58                 msoLogger.trace("STARTED DeleteVnfInfra PreProcessRequest Process")
59
60                 execution.setVariable("DELVI_SuccessIndicator", false)
61                 execution.setVariable("DELVI_vnfInUse", false)
62
63                 try{
64                         // Get Variables
65                         String deleteVnfRequest = execution.getVariable("bpmnRequest")
66                         execution.setVariable("DELVI_DeleteVnfRequest", deleteVnfRequest)
67                         msoLogger.debug("Incoming DeleteVnfInfra Request is: \n" + deleteVnfRequest)
68
69                         if(deleteVnfRequest != null){
70
71                                 String requestId = execution.getVariable("mso-request-id")
72                                 execution.setVariable("DELVI_requestId", requestId)
73
74                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
75                                 execution.setVariable("DELVI_serviceInstanceId", serviceInstanceId)
76                                 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
77
78                                 String vnfId = execution.getVariable("vnfId")
79                                 execution.setVariable("DELVI_vnfId", vnfId)
80                                 msoLogger.debug("Incoming Vnf(Instance) Id is: " + vnfId)
81
82                                 String source = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.requestInfo.source")
83                                 execution.setVariable("DELVI_source", source)
84                                 msoLogger.debug("Incoming Source is: " + source)
85                                 
86                                 def cloudConfiguration = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.cloudConfiguration")
87                                 execution.setVariable("DELVI_cloudConfiguration", cloudConfiguration)
88                                 
89                                 boolean cascadeDelete = false
90                                 Boolean cascadeDeleteObj = jsonUtil.getJsonRawValue(deleteVnfRequest, "requestDetails.requestParameters.cascadeDelete")
91                                 if(cascadeDeleteObj!=null){
92                                         cascadeDelete = cascadeDeleteObj.booleanValue()
93                                 }
94                                 execution.setVariable("DELVI_cascadeDelete", cascadeDelete)
95                                 msoLogger.debug("Incoming cascadeDelete is: " + cascadeDelete)
96
97                                 //For Completion Handler & Fallout Handler
98                                 String requestInfo =
99                                 """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
100                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
101                                         <action>DELETE</action>
102                                         <source>${MsoUtils.xmlEscape(source)}</source>
103                                    </request-info>"""
104
105                                 execution.setVariable("DELVI_requestInfo", requestInfo)
106
107                                 // Setting for sub flow calls
108                                 execution.setVariable("DELVI_type", "generic-vnf")
109
110                         }else{
111                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
112                         }
113
114                 }catch(BpmnError b){
115                         msoLogger.debug("Rethrowing MSOWorkflowException")
116                         throw b
117                 }catch(Exception e){
118                         msoLogger.debug(" Error Occured in DeleteVnfInfra PreProcessRequest method!" + e)
119                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PreProcessRequest")
120
121                 }
122                 msoLogger.trace("COMPLETED DeleteVnfInfra PreProcessRequest Process")
123         }
124
125         public void sendSyncResponse (DelegateExecution execution) {
126                 execution.setVariable("prefix",Prefix)
127
128                 msoLogger.trace("STARTED DeleteVnfInfra SendSyncResponse Process")
129
130                 try {
131                         String requestId = execution.getVariable("DELVI_requestId")
132                         String vnfId = execution.getVariable("DELVI_vnfId")
133
134                         String DeleteVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
135
136                         msoLogger.debug("DeleteVnfInfra Sync Response is: \n"  + DeleteVnfResponse)
137                         execution.setVariable("DELVI_sentSyncResponse", true)
138
139                         sendWorkflowResponse(execution, 202, DeleteVnfResponse)
140
141                 } catch (Exception ex) {
142                         msoLogger.debug("Error Occured in DeleteVnfInfra SendSyncResponse Process " + ex.getMessage())
143                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra SendSyncResponse Process")
144
145                 }
146                 msoLogger.trace("COMPLETED DeleteVnfInfra SendSyncResponse Process")
147         }
148
149         public void prepareCompletionHandlerRequest(DelegateExecution execution){
150                 execution.setVariable("prefix",Prefix)
151
152                 msoLogger.trace("STARTED DeleteVnfInfra PrepareCompletionHandlerRequest Process")
153
154                 try {
155                         String requestInfo = execution.getVariable("DELVI_requestInfo")
156                         requestInfo = utils.removeXmlPreamble(requestInfo)
157                         String vnfId = execution.getVariable("DELVI_vnfId")
158
159                         String request =
160                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
161                                                                         xmlns:ns="http://org.onap/so/request/types/v1">
162                                                         ${requestInfo}
163                                                         <status-message>Vnf has been deleted successfully.</status-message>
164                                                         <vnfId>${MsoUtils.xmlEscape(vnfId)}</vnfId>
165                                                         <mso-bpel-name>DeleteVnfInfra</mso-bpel-name>
166                                                 </aetgt:MsoCompletionRequest>"""
167
168                         execution.setVariable("DELVI_completionHandlerRequest", request)
169                         msoLogger.debug("Completion Handler Request is: " + request)
170
171                         execution.setVariable("WorkflowResponse", "Success") // for junits
172
173                 } catch (Exception ex) {
174                         msoLogger.debug("Error Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage())
175                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process")
176
177                 }
178                 msoLogger.trace("COMPLETED DeleteVnfInfra PrepareCompletionHandlerRequest Process")
179         }
180
181         public void sendErrorResponse(DelegateExecution execution){
182                 execution.setVariable("prefix",Prefix)
183
184                 msoLogger.trace("STARTED DeleteVnfInfra sendErrorResponse Process")
185                 try {
186                         def sentSyncResponse = execution.getVariable("DELVI_sentSyncResponse")
187                         if(sentSyncResponse == false){
188                                 msoLogger.debug("Sending a Sync Error Response")
189                                 WorkflowException wfex = execution.getVariable("WorkflowException")
190                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
191
192                                 msoLogger.debug(response)
193                                 sendWorkflowResponse(execution, 500, response)
194                         }else{
195                                 msoLogger.debug("A Sync Response has already been sent. Skipping Send Sync Error Response.")
196                         }
197
198                 } catch(Exception ex) {
199                         msoLogger.debug("Error Occured in DeleteVnfInfra sendErrorResponse Process " + ex.getMessage())
200                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra sendErrorResponse Process")
201                 }
202                 msoLogger.trace("COMPLETED DeleteVnfInfra sendErrorResponse Process")
203         }
204
205
206
207 }