5b726a083582f781edbf3a097206b9fd2bfd30c5
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / DoDeleteVfModule.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 import java.io.Serializable;
23
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.runtime.Execution
26
27 import org.openecomp.mso.rest.APIResponse
28 import org.openecomp.mso.rest.RESTClient
29 import org.openecomp.mso.rest.RESTConfig
30 import org.openecomp.mso.bpmn.core.RollbackData
31 import org.openecomp.mso.bpmn.core.WorkflowException
32
33
34
35 public class DoDeleteVfModule extends AbstractServiceTaskProcessor{
36
37         def Prefix="DoDVfMod_"
38
39         public void initProcessVariables(Execution execution) {
40                 execution.setVariable("prefix",Prefix)
41         }
42
43         // parse the incoming DELETE_VF_MODULE request for the Generic Vnf and Vf Module Ids
44         // and formulate the outgoing request for PrepareUpdateAAIVfModuleRequest
45         public void preProcessRequest(Execution execution) {
46                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
47                 def xml = execution.getVariable("DoDeleteVfModuleRequest")
48                 utils.logAudit("DoDeleteVfModule Request: " + xml)
49                 
50                 utils.log("DEBUG", "input request xml: " + xml, isDebugEnabled)
51                 initProcessVariables(execution)
52                 def vnfId = utils.getNodeText1(xml,"vnf-id")
53                 def vfModuleId = utils.getNodeText1(xml,"vf-module-id")
54 //              execution.setVariable("DoDVfMod_vnfId", vnfId)
55 //              execution.setVariable("DoDVfMod_vfModuleId", vfModuleId)
56                 // formulate the request for PrepareUpdateAAIVfModule
57                 String request = """<PrepareUpdateAAIVfModuleRequest>
58                                                                 <vnf-id>${vnfId}</vnf-id>
59                                                                 <vf-module-id>${vfModuleId}</vf-module-id>
60                                                                 <orchestration-status>pending-delete</orchestration-status>
61                                                         </PrepareUpdateAAIVfModuleRequest>""" as String
62                 utils.log("DEBUG", "PrepareUpdateAAIVfModuleRequest :" + request, isDebugEnabled)
63                 utils.logAudit("UpdateAAIVfModule Request: " + request)
64                 execution.setVariable("PrepareUpdateAAIVfModuleRequest", request)
65                 execution.setVariable("DoDVfMod_vfModuleFromAAI", null)
66         }
67
68         // build a SDNC vnf-topology-operation request for the specified action
69         // (note: the action passed is expected to be 'changedelete' or 'delete')
70         public void prepSDNCAdapterRequest(Execution execution, String action) {
71                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
72                 def xml = execution.getVariable("DoDeleteVfModuleRequest")
73                 utils.logAudit("DoDeleteVfModule Request: " + xml)
74                 
75                 def srvInstId = execution.getVariable("att-mso-service-instance-id")
76                 def callbackUrl = execution.getVariable("URN_mso_workflow_sdncadapter_callback")
77                 utils.log("DEBUG", "input request xml: " + xml, isDebugEnabled)
78
79                 String requestId = ""
80                 try {
81                         requestId = execution.getVariable("att-mso-request-id")
82                 } catch (Exception ex) {
83                         requestId = utils.getNodeText1(xml, "request-id")
84                 }
85
86                 String source = utils.getNodeText1(xml, "source")
87                 String serviceId = utils.getNodeText1(xml, "service-id")
88                 String vnfId = utils.getNodeText1(xml, "vnf-id")
89                 String vnfType = utils.getNodeText1(xml, "vnf-type")
90                 String vnfName = utils.getNodeText1(xml, "vnf-name")
91                 String tenantId = utils.getNodeText1(xml, "tenant-id")
92                 String vfModuleId = utils.getNodeText1(xml, "vf-module-id")
93                 String serviceInstanceIdToSdnc = ""
94                 if (xml.contains("service-instance-id")) {
95                         serviceInstanceIdToSdnc = utils.getNodeText1(xml, "service-instance-id")
96                 } else {
97                     serviceInstanceIdToSdnc = vfModuleId
98                 }
99                 String vfModuleName = utils.getNodeText1(xml, "vf-module-name")
100                 // Get vfModuleName from AAI response if it was not specified on the request
101                 if (vfModuleName == null || vfModuleName.isEmpty()) {
102                         if (execution.getVariable("DoDVfMod_vfModuleFromAAI") != null) {
103                                 VfModule vfModuleFromAAI = execution.getVariable("DoDVfMod_vfModuleFromAAI")
104                                 vfModuleName = vfModuleFromAAI.getElementText("vf-module-name")                 
105                         }               
106                 }
107                 String vfModuleModelName = utils.getNodeText1(xml, "vf-module-model-name")
108                 String cloudSiteId = utils.getNodeText1(xml, "aic-cloud-region")
109                 String request = """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://ecomp.att.com/mso/request/types/v1"
110                                                                                                         xmlns:sdncadapterworkflow="http://ecomp.att.com/mso/workflow/schema/v1"
111                                                                                                         xmlns:sdncadapter="http://domain2.att.com/workflow/sdnc/adapter/schema/v1">
112                                                       <sdncadapter:RequestHeader>
113                                                          <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
114                                                          <sdncadapter:SvcInstanceId>${vfModuleId}</sdncadapter:SvcInstanceId>
115                                                          <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
116                                                          <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
117                                                          <sdncadapter:CallbackUrl>${callbackUrl}</sdncadapter:CallbackUrl>
118                                                       </sdncadapter:RequestHeader>
119                                                       <sdncadapterworkflow:SDNCRequestData>
120                                                          <request-information>
121                                                             <request-id>${requestId}</request-id>
122                                                             <request-action>DisconnectVNFRequest</request-action>
123                                                             <source>${source}</source>
124                                                             <notification-url/>
125                                                             <order-number/>
126                                                             <order-version/>
127                                                          </request-information>
128                                                          <service-information>
129                                                             <service-id>${serviceId}</service-id>
130                                                                         <service-type>${serviceId}</service-type>
131                                                             <service-instance-id>${serviceInstanceIdToSdnc}</service-instance-id>
132                                                             <subscriber-name>notsurewecare</subscriber-name>
133                                                          </service-information>
134                                                          <vnf-request-information>
135                                                                 <vnf-id>${vfModuleId}</vnf-id>
136                                                                         <vnf-type>${vfModuleModelName}</vnf-type>
137                                     <vnf-name>${vfModuleName}</vnf-name>
138                                                                         <generic-vnf-id>${vnfId}</generic-vnf-id>
139                                     <generic-vnf-name>${vnfName}</generic-vnf-name>
140                                                                         <generic-vnf-type>${vnfType}</generic-vnf-type>
141                                                                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
142                                                                         <tenant>${tenantId}</tenant>
143                                                          </vnf-request-information>
144                                                       </sdncadapterworkflow:SDNCRequestData>
145                                                    </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
146
147                 utils.log("DEBUG", "sdncAdapterWorkflowRequest: " + request, isDebugEnabled)
148                 utils.logAudit("DoDeleteVfModule - SDNCAdapterWorkflowRequest: " + request)
149                 execution.setVariable("sdncAdapterWorkflowRequest", request)
150         }
151
152         // parse the incoming DELETE_VF_MODULE request
153         // and formulate the outgoing VnfAdapterDeleteV1 request
154         public void prepVNFAdapterRequest(Execution execution) {
155                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
156                 def xml = execution.getVariable("DoDeleteVfModuleRequest")
157                 def requestId = UUID.randomUUID().toString()
158                 def origRequestId = execution.getVariable('att-mso-request-id')
159                 def srvInstId = execution.getVariable('att-mso-service-instance-id')
160                 def aicCloudRegion = utils.getNodeText1(xml, "aic-cloud-region")
161                 def vnfId = utils.getNodeText1(xml, "vnf-id")
162                 def vnfName = utils.getNodeText1(xml, "vnf-name")
163                 def vfModuleId = utils.getNodeText1(xml, "vf-module-id")
164                 def vfModuleStackId = execution.getVariable('DoDVfMod_heatStackId')
165                 def tenantId = utils.getNodeText1(xml, "tenant-id")
166                 def messageId = execution.getVariable('att-mso-request-id') + '-' +
167                         System.currentTimeMillis()
168                 def notificationUrl = execution.getVariable("URN_mso_workflow_vnfadapter_rest_callback")
169                 def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")
170                 if ('true'.equals(useQualifiedHostName)) {
171                         notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
172                 }
173
174                 String request = """
175                         <deleteVfModuleRequest>
176                             <cloudSiteId>${aicCloudRegion}</cloudSiteId>
177                             <tenantId>${tenantId}</tenantId>
178                             <vnfId>${vnfId}</vnfId>
179                             <vfModuleId>${vfModuleId}</vfModuleId>
180                             <vfModuleStackId>${vfModuleStackId}</vfModuleStackId>
181                             <skipAAI>true</skipAAI>
182                             <msoRequest>
183                                 <requestId>${origRequestId}</requestId>
184                                 <serviceInstanceId>${srvInstId}</serviceInstanceId>
185                             </msoRequest>
186                             <messageId>${messageId}</messageId>
187                             <notificationUrl>${notificationUrl}</notificationUrl>
188                         </deleteVfModuleRequest>
189                         """ as String
190
191                 utils.log("DEBUG", "vnfAdapterRestV1Request: " + request, isDebugEnabled)
192                 utils.logAudit("deleteVfModuleRequest: " + request)
193                 execution.setVariable("vnfAdapterRestV1Request", request)
194         }
195
196         // parse the incoming DELETE_VF_MODULE request
197         // and formulate the outgoing UpdateAAIVfModuleRequest request
198         public void prepUpdateAAIVfModule(Execution execution) {
199                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
200                 def xml = execution.getVariable("DoDeleteVfModuleRequest")
201                 utils.log("DEBUG", "input request xml: " + xml, isDebugEnabled)
202                 initProcessVariables(execution)
203                 def vnfId = utils.getNodeText1(xml,"vnf-id")
204                 def vfModuleId = utils.getNodeText1(xml,"vf-module-id")
205                 // formulate the request for UpdateAAIVfModule
206                 String request = """<UpdateAAIVfModuleRequest>
207                                                                 <vnf-id>${vnfId}</vnf-id>
208                                                                 <vf-module-id>${vfModuleId}</vf-module-id>
209                                                                 <heat-stack-id>DELETE</heat-stack-id>
210                                                                 <orchestration-status>deleted</orchestration-status>
211                                                         </UpdateAAIVfModuleRequest>""" as String
212                 utils.log("DEBUG", "UpdateAAIVfModuleRequest :" + request, isDebugEnabled)
213                 utils.logAudit("UpdateAAIVfModuleRequest: " + request)
214                 execution.setVariable("UpdateAAIVfModuleRequest", request)
215         }
216
217         // parse the incoming DELETE_VF_MODULE request
218         // and formulate the outgoing DeleteAAIVfModuleRequest request
219         public void prepDeleteAAIVfModule(Execution execution) {
220                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
221                 def xml = execution.getVariable("DoDeleteVfModuleRequest")
222                 utils.log("DEBUG", "input request xml: " + xml, isDebugEnabled)
223                 initProcessVariables(execution)
224                 def vnfId = utils.getNodeText1(xml,"vnf-id")
225                 def vfModuleId = utils.getNodeText1(xml,"vf-module-id")
226                 // formulate the request for UpdateAAIVfModule
227                 String request = """<DeleteAAIVfModuleRequest>
228                                                                 <vnf-id>${vnfId}</vnf-id>
229                                                                 <vf-module-id>${vfModuleId}</vf-module-id>
230                                                         </DeleteAAIVfModuleRequest>""" as String
231                 utils.log("DEBUG", "DeleteAAIVfModuleRequest :" + request, isDebugEnabled)
232                 utils.logAudit("DeleteAAIVfModuleRequest: " + request)
233                 execution.setVariable("DeleteAAIVfModuleRequest", request)
234         }
235
236         // generates a WorkflowException if
237         //              -
238         public void handleDoDeleteVfModuleFailure(Execution execution) {
239                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
240                 utils.log("ERROR", "AAI error occurred deleting the Generic Vnf: "
241                         + execution.getVariable("DoDVfMod_deleteGenericVnfResponse"), isDebugEnabled)
242                 String processKey = getProcessKey(execution);
243                 WorkflowException exception = new WorkflowException(processKey, 5000,
244                         execution.getVariable("DoDVfMod_deleteGenericVnfResponse"))
245                 execution.setVariable("WorkflowException", exception)
246         }
247
248         public void sdncValidateResponse(Execution execution, String response){
249                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
250                 execution.setVariable("prefix",Prefix)
251
252                 WorkflowException workflowException = execution.getVariable("WorkflowException")
253                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
254
255                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
256                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
257
258                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
259                         utils.log("DEBUG", "Successfully Validated SDNC Response", isDebugEnabled)
260                 }else{
261                         throw new BpmnError("MSOWorkflowException")
262                 }
263         }
264 }