Initial OpenECOMP MSO commit
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / DeleteVnfInfra.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 com.att.bpm.scripts
22
23
24 import javax.xml.parsers.DocumentBuilder
25 import javax.xml.parsers.DocumentBuilderFactory
26
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.runtime.Execution
30
31 import org.w3c.dom.Document
32 import org.w3c.dom.Element
33 import org.w3c.dom.Node
34 import org.w3c.dom.NodeList
35
36 import org.xml.sax.InputSource
37
38
39 import org.openecomp.mso.bpmn.core.WorkflowException
40 import org.openecomp.mso.bpmn.core.json.JsonUtils;
41
42
43 /**
44  * This class supports the DeleteVnfInfra Flow
45  * with the Deletion of a generic vnf for
46  * infrastructure.
47  */
48 class DeleteVnfInfra extends AbstractServiceTaskProcessor {
49
50         String Prefix="DELVI_"
51         ExceptionUtil exceptionUtil = new ExceptionUtil()
52         JsonUtils jsonUtil = new JsonUtils()
53         VidUtils vidUtils = new VidUtils(this)
54
55         /**
56          * This method gets and validates the incoming
57          * request.
58          *
59          * @param - execution
60          */
61         public void preProcessRequest(Execution execution) {
62                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
63                 execution.setVariable("prefix",Prefix)
64                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra PreProcessRequest Process*** ", isDebugEnabled)
65
66                 execution.setVariable("DELVI_SuccessIndicator", false)
67                 execution.setVariable("DELVI_vnfInUse", false)
68
69                 try{
70                         // Get Variables
71                         String deleteVnfRequest = execution.getVariable("bpmnRequest")
72                         execution.setVariable("DELVI_DeleteVnfRequest", deleteVnfRequest)
73                         utils.logAudit("Incoming DeleteVnfInfra Request is: \n" + deleteVnfRequest)
74
75                         if(deleteVnfRequest != null){
76
77                                 String requestId = execution.getVariable("att-mso-request-id")
78                                 execution.setVariable("DELVI_requestId", requestId)
79
80                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
81                                 execution.setVariable("DELVI_serviceInstanceId", serviceInstanceId)
82                                 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
83
84                                 String vnfId = execution.getVariable("vnfId")
85                                 execution.setVariable("DELVI_vnfId", vnfId)
86                                 utils.log("DEBUG", "Incoming Vnf(Instance) Id is: " + vnfId, isDebugEnabled)
87
88                                 String source = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.requestInfo.source")
89                                 execution.setVariable("DELVI_source", source)
90                                 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
91
92                                 //For Completion Handler & Fallout Handler
93                                 String requestInfo =
94                                 """<request-info xmlns="http://ecomp.att.com/mso/infra/vnf-request/v1">
95                                         <request-id>${requestId}</request-id>
96                                         <action>DELETE</action>
97                                         <source>${source}</source>
98                                    </request-info>"""
99
100                                 execution.setVariable("DELVI_requestInfo", requestInfo)
101
102                                 // Setting for sub flow calls
103                                 execution.setVariable("DELVI_type", "generic-vnf")
104
105                         }else{
106                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
107                         }
108
109                 }catch(BpmnError b){
110                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
111                         throw b
112                 }catch(Exception e){
113                         utils.log("DEBUG", " Error Occured in DeleteVnfInfra PreProcessRequest method!" + e, isDebugEnabled)
114                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PreProcessRequest")
115
116                 }
117                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra PreProcessRequest Process ***", isDebugEnabled)
118         }
119
120         public void sendSyncResponse (Execution execution) {
121                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
122                 execution.setVariable("prefix",Prefix)
123
124                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra SendSyncResponse Process *** ", isDebugEnabled)
125
126                 try {
127                         String requestId = execution.getVariable("DELVI_requestId")
128                         String vnfId = execution.getVariable("DELVI_vnfId")
129
130                         String DeleteVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
131
132                         utils.log("DEBUG", " DeleteVnfInfra Sync Response is: \n"  + DeleteVnfResponse, isDebugEnabled)
133                         execution.setVariable("DELVI_sentSyncResponse", true)
134
135                         sendWorkflowResponse(execution, 202, DeleteVnfResponse)
136
137                 } catch (Exception ex) {
138                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra SendSyncResponse Process " + ex.getMessage(), isDebugEnabled)
139                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra SendSyncResponse Process")
140
141                 }
142                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra SendSyncResponse Process ***", isDebugEnabled)
143         }
144
145         public void processGetVnfResponse(Execution execution){
146                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
147                 execution.setVariable("prefix",Prefix)
148                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra processGetVnfResponse Process *** ", isDebugEnabled)
149                 try {
150                         String vnf = execution.getVariable("DELVI_genericVnf")
151                         String resourceVersion = utils.getNodeText1(vnf, "resource-version")
152                         execution.setVariable("DELVI_resourceVersion", resourceVersion)
153
154                         if(utils.nodeExists(vnf, "relationship")){
155                                 InputSource source = new InputSource(new StringReader(vnf));
156                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
157                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
158                                 Document vnfXml = docBuilder.parse(source)
159
160                                 NodeList nodeList = vnfXml.getElementsByTagName("relationship")
161                                 for (int x = 0; x < nodeList.getLength(); x++) {
162                                         Node node = nodeList.item(x)
163                                         if (node.getNodeType() == Node.ELEMENT_NODE) {
164                                                 Element eElement = (Element) node
165                                                 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent()
166                                                 if(e.equals("volume-group") || e.equals("l3-network") || e.equals("vserver")){
167                                                         utils.log("DEBUG", "Generic Vnf still has relationship to OpenStack.", isDebugEnabled)
168                                                         execution.setVariable("DELVI_vnfInUse", true)
169                                                 }else{
170                                                         utils.log("DEBUG", "Relationship NOT related to OpenStack", isDebugEnabled)
171                                                 }
172                                         }
173                                 }
174                         }
175
176                         if(utils.nodeExists(vnf, "vf-module")){
177                                 execution.setVariable("DELVI_vnfInUse", true)
178                                 utils.log("DEBUG", "Generic Vnf still has vf-modules.", isDebugEnabled)
179                         }
180
181
182                 } catch (Exception ex) {
183                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra processGetVnfResponse Process " + ex.getMessage(), isDebugEnabled)
184                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra processGetVnfResponse Process")
185
186                 }
187                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra processGetVnfResponse Process ***", isDebugEnabled)
188         }
189
190         public void prepareCompletionHandlerRequest(Execution execution){
191                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
192                 execution.setVariable("prefix",Prefix)
193
194                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra PrepareCompletionHandlerRequest Process *** ", isDebugEnabled)
195
196                 try {
197                         String requestInfo = execution.getVariable("DELVI_requestInfo")
198                         requestInfo = utils.removeXmlPreamble(requestInfo)
199                         String vnfId = execution.getVariable("DELVI_vnfId")
200
201                         String request =
202                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
203                                                                         xmlns:ns="http://ecomp.att.com/mso/request/types/v1">
204                                                         ${requestInfo}
205                                                         <status-message>Vnf has been deleted successfully.</status-message>
206                                                         <vnfId>${vnfId}</vnfId>
207                                                         <mso-bpel-name>DeleteVnfInfra</mso-bpel-name>
208                                                 </aetgt:MsoCompletionRequest>"""
209
210                         execution.setVariable("DELVI_completionHandlerRequest", request)
211                         utils.log("DEBUG", "Completion Handler Request is: " + request, isDebugEnabled)
212
213                         execution.setVariable("WorkflowResponse", "Success") // for junits
214
215                 } catch (Exception ex) {
216                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage(), isDebugEnabled)
217                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process")
218
219                 }
220                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra PrepareCompletionHandlerRequest Process ***", isDebugEnabled)
221         }
222
223         public void sendErrorResponse(Execution execution){
224                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
225                 execution.setVariable("prefix",Prefix)
226
227                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra sendErrorResponse Process *** ", isDebugEnabled)
228                 try {
229                         def sentSyncResponse = execution.getVariable("DELVI_sentSyncResponse")
230                         if(sentSyncResponse == false){
231                                 utils.log("DEBUG", "Sending a Sync Error Response", isDebugEnabled)
232                                 WorkflowException wfex = execution.getVariable("WorkflowException")
233                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
234
235                                 utils.logAudit(response)
236                                 sendWorkflowResponse(execution, 500, response)
237                         }else{
238                                 utils.log("DEBUG", "A Sync Response has already been sent. Skipping Send Sync Error Response.", isDebugEnabled)
239                         }
240
241                 } catch(Exception ex) {
242                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
243                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra sendErrorResponse Process")
244                 }
245                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra sendErrorResponse Process ***", isDebugEnabled)
246         }
247
248
249
250 }