Merge "add queryparam to catalog db api to filter"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVfModuleVolumeV2.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
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 javax.ws.rs.NotFoundException
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.aai.domain.yang.VolumeGroup
29 import org.onap.aaiclient.client.aai.AAIObjectType
30 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
31 import org.onap.aaiclient.client.aai.entities.Relationships
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
36 import org.onap.so.bpmn.common.scripts.AaiUtil
37 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
38 import org.onap.so.bpmn.common.scripts.ExceptionUtil
39 import org.onap.so.bpmn.common.scripts.MsoUtils
40 import org.onap.so.bpmn.core.UrnPropertiesReader;
41 import org.onap.so.bpmn.core.WorkflowException
42 import org.onap.so.bpmn.core.json.JsonUtils
43 import org.onap.so.constants.Defaults
44 import org.slf4j.Logger
45 import org.slf4j.LoggerFactory
46
47 class DoDeleteVfModuleVolumeV2 extends AbstractServiceTaskProcessor{
48     private static final Logger logger = LoggerFactory.getLogger( DoDeleteVfModuleVolumeV2.class);
49
50         String prefix="DDVMV_"
51         ExceptionUtil exceptionUtil = new ExceptionUtil()
52         XmlParser xmlParser = new XmlParser()
53         JsonUtils jsonUtil = new JsonUtils()
54
55         @Override
56         public void preProcessRequest(DelegateExecution execution) {
57                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
58                 preProcessRequest(execution, isDebugEnabled)
59         }
60
61         /**
62          * Set default variable values
63          * @param execution
64          * @param isDebugLogEnabled
65          */
66         public void preProcessRequest (DelegateExecution execution, isDebugEnabled) {
67
68                 //Input:
69                 //  msoRequestId
70                 //  isDebugLogEnabled
71                 //  failIfNotFound (Optional)
72                 //  serviceInstanceId (Optional)
73                 //  vnfId (Optional)
74                 //  volumeGroupId
75                 //  vfModuleModelInfo (Optional)
76                 //  lcpCloudRegionId (Optional)                 @TODO: this is actually required
77                 //  tenantId (Optional)                                 @TODO: this is actually required
78                 //  cloudConfiguration                                  @TODO: temporary solution? this contains lcpCloudregion and tenantId
79                 //
80                 //Output:
81                 //  workflowException                                   @TODO: actual variable name is WorkflowException
82                 //  rolledBack
83                 //  wasDeleted
84
85                 execution.setVariable('prefix', prefix)
86                 execution.setVariable('wasDeleted', 'false')
87
88                 def tenantId = execution.getVariable("tenantId")
89                 def cloudSiteId = execution.getVariable("lcpCloudRegionId")
90
91                 // if tenantId or lcpCloudregionId is not passed, get it from cloudRegionConfiguration variable
92                 if(!tenantId || !cloudSiteId) {
93                         def cloudConfiguration = execution.getVariable("cloudConfiguration")
94                         logger.debug("Using cloudConfiguration variable to get tenantId and lcpCloudRegionId - " + cloudConfiguration)
95                         tenantId = jsonUtil.getJsonValue(cloudConfiguration, "tenantId")
96                         execution.setVariable("tenantId", tenantId)
97                         cloudSiteId = jsonUtil.getJsonValue(cloudConfiguration, "lcpCloudRegionId")
98                         execution.setVariable("lcpCloudRegionId", cloudSiteId)
99                         cloudOwner = jsonUtil.getJsonValue(cloudConfiguration, "cloudOwner")
100                         execution.setVariable("cloudOwner", cloudOwner)
101                 }
102         }
103
104
105         /**
106          * Set out 'wasDeleted' variable to 'true'
107          * @param execution
108          * @param isDebugLogEnabled
109          */
110         public void postProcess(DelegateExecution execution, isDebugLogEnabled) {
111                 execution.setVariable('wasDeleted', 'true')
112         }
113
114
115         /**
116          * Query and set cloud region to use for AAI calls
117          * Output variables: prefix+'aicCloudRegion', prefix+'cloudRegion'
118          * @param execution
119          * @param isDebugEnabled
120          */
121         public void callRESTQueryAAICloudRegion(DelegateExecution execution, isDebugEnabled) {
122
123                 String cloudRegion = execution.getVariable('lcpCloudRegionId')
124                 AaiUtil aaiUtil = new AaiUtil(this)
125
126                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), cloudRegion))
127                 def queryCloudRegionRequest = aaiUtil.createAaiUri(uri)
128
129                 cloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
130
131                 if ((cloudRegion != "ERROR")) {
132                         if(execution.getVariable(prefix+"queryCloudRegionReturnCode") == "404") {
133                                 execution.setVariable(prefix+"aicCloudRegion", "AAIAIC25")
134                         }
135                         else{
136                                 execution.setVariable(prefix+"aicCloudRegion", cloudRegion)
137                         }
138                 }
139                 else {
140                         logger.debug("AAI Query Cloud Region Unsuccessful.")
141                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode"))
142                 }
143         }
144
145
146         /**
147          * Query AAI Volume Group
148          * Output variables: prefix+'queryAAIVolGrpResponse'; prefix+'volumeGroupHeatStackId'
149          * @param execution
150          * @param isDebugLogEnabled
151          */
152         public void callRESTQueryAAIForVolumeGroup(DelegateExecution execution, isDebugLogEnabled) {
153
154                 def tenantId = execution.getVariable('tenantId')
155                 def volumeGroupId = execution.getVariable('volumeGroupId')
156                 if(volumeGroupId == null) {
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, 'volumeGroupId is not provided in the request')
158                         throw new Exception('volume-group-id is not provided in the request')
159                 }
160                 String cloudRegion = execution.getVariable(prefix+'aicCloudRegion')
161
162                 try {
163                         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), cloudRegion).volumeGroup(volumeGroupId))
164                         Optional<VolumeGroup> volumeGroupOps = getAAIClient().get(VolumeGroup.class,resourceUri)
165             if(volumeGroupOps.present) {
166                 VolumeGroup volumeGroup = volumeGroupOps.get()
167                 execution.setVariable(prefix + "queryAAIVolGrpResponse", volumeGroup)
168                 def heatStackId = volumeGroup.getHeatStackId()==null ? '' : volumeGroup.getHeatStackId()
169                 execution.setVariable(prefix+'volumeGroupHeatStackId', heatStackId)
170
171                 logger.debug('Heat stack id from AAI response: ' + heatStackId)
172                                 AAIResultWrapper wrapper = getAAIClient().get(resourceUri);
173                                 Optional<Relationships> relationships = wrapper.getRelationships()
174                                 String volumeGroupTenantId = null
175
176                                 if(relationships.isPresent()){
177                                         if(relationships.get().getRelatedUris(Types.VF_MODULE)){
178                                                 logger.debug('Volume Group ' + volumeGroupId + ' currently in use')
179                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume Group ${volumeGroupId} currently in use - found vf-module relationship.")
180                                         }
181                                         for(AAIResourceUri aaiResourceUri: relationships.get().getRelatedUris(Types.TENANT)){
182                                                 volumeGroupTenantId = aaiResourceUri.getURIKeys().get(AAIFluentTypeBuilder.Types.TENANT.getUriParams().tenantId)
183                                         }
184                                 }
185
186                 logger.debug('Tenant ID from AAI response: ' + volumeGroupTenantId)
187
188                 if (volumeGroupTenantId == null) {
189                     logger.debug("Could not find Tenant Id element in Volume Group with Volume Group Id ${volumeGroupId}")
190                     exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Could not find Tenant Id element in Volume Group with Volume Group Id ${volumeGroupId}")
191                 }
192
193                 if (volumeGroupTenantId != tenantId) {
194                     def String errorMessage = 'TenantId ' + tenantId + ' in incoming request does not match Tenant Id ' + volumeGroupTenantId + ' retrieved from AAI for Volume Group Id ' + volumeGroupId
195                     logger.debug("Error in DeleteVfModuleVolume: " + errorMessage)
196                     exceptionUtil.buildAndThrowWorkflowException(execution, 5000, errorMessage)
197                 }
198                 logger.debug('Received Tenant Id ' + volumeGroupTenantId + ' from AAI for Volume Group with Volume Group Id ' + volumeGroupId )
199             }else{
200                 execution.setVariable(prefix + "queryAAIVolGrpResponse", "Volume Group ${volumeGroupId} not found in AAI. Response code: 404")
201                 logger.debug("Volume Group ${volumeGroupId} not found in AAI")
202                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume Group ${volumeGroupId} not found in AAI. Response code: 404")
203             }
204                 }catch (Exception ex) {
205             execution.setVariable(prefix+"queryAAIVolGrpResponse", ex.getMessage())
206             WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(ex.getMessage(), execution)
207             throw new BpmnError("MSOWorkflowException")
208                 }
209         }
210
211         /**
212          * Format VNF Adapter subflow request XML
213          * Variables: prefix+'deleteVnfARequest'
214          * @param execution
215          * @param isDebugLogEnabled
216          */
217         public void prepareVnfAdapterDeleteRequest(DelegateExecution execution, isDebugLogEnabled) {
218                 def cloudRegion = execution.getVariable(prefix+'aicCloudRegion')
219                 def cloudOwner = execution.getVariable(prefix+'cloudOwner')
220                 def tenantId = execution.getVariable('tenantId')                                                                                // input parameter (optional) - see preProcessRequest
221                 def volumeGroupId = execution.getVariable('volumeGroupId')                                                              // input parameter (required)
222                 def volumeGroupHeatStackId = execution.getVariable(prefix+'volumeGroupHeatStackId')             // from AAI query volume group
223                 def requestId = execution.getVariable('msoRequestId')                                                                   // input parameter (required)
224                 def serviceId = execution.getVariable('serviceInstanceId')                                                              // imput parameter (optional)
225
226                 def messageId = UUID.randomUUID().toString()
227                 def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId)
228                 def useQualifiedHostName = UrnPropertiesReader.getVariable("mso.use.qualified.host",execution)
229                 if ('true'.equals(useQualifiedHostName)) {
230                                 notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
231                 }
232
233                 String vnfAdapterRestRequest = """
234                         <deleteVolumeGroupRequest>
235                                 <cloudSiteId>${MsoUtils.xmlEscape(cloudRegion)}</cloudSiteId>
236                                 <cloudOwner>${MsoUtils.xmlEscape(cloudOwner)}</cloudOwner>
237                                 <tenantId>${MsoUtils.xmlEscape(tenantId)}</tenantId>
238                                 <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
239                                 <volumeGroupStackId>${MsoUtils.xmlEscape(volumeGroupHeatStackId)}</volumeGroupStackId>
240                                 <skipAAI>true</skipAAI>
241                             <msoRequest>
242                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
243                                 <serviceInstanceId>${MsoUtils.xmlEscape(serviceId)}</serviceInstanceId>
244                             </msoRequest>
245                             <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
246                             <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
247                         </deleteVolumeGroupRequest>
248                 """
249                 vnfAdapterRestRequest = utils.formatXml(vnfAdapterRestRequest)
250                 execution.setVariable(prefix+'deleteVnfARequest', vnfAdapterRestRequest)
251                 logger.debug('Request for VNFAdapter Rest:\n' + vnfAdapterRestRequest)
252         }
253
254
255         /**
256          * Delete volume group in AAI
257          * @param execution
258          * @param isDebugEnabled
259          */
260         public void callRESTDeleteAAIVolumeGroup(DelegateExecution execution, isDebugEnabled) {
261
262                 // get variables
263                 VolumeGroup volumeGroupResponse = execution.getVariable(prefix+"queryAAIVolGrpResponse")
264                 String volumeGroupId = volumeGroupResponse.getVolumeGroupId()
265                 String cloudRegion = execution.getVariable(prefix+'aicCloudRegion')
266
267         try {
268             AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), cloudRegion).volumeGroup(volumeGroupId))
269             getAAIClient().delete(resourceUri)
270             logger.debug("Volume group $volumeGroupId deleted.")
271         }catch (NotFoundException ex) {
272             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupId not found for delete in AAI Response code: 404")
273         }catch (Exception ex) {
274             WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(ex.getMessage(), execution)
275             throw new BpmnError("MSOWorkflowException")
276         }
277         }
278
279 }