2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
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
47 class DoDeleteVfModuleVolumeV2 extends AbstractServiceTaskProcessor{
48 private static final Logger logger = LoggerFactory.getLogger( DoDeleteVfModuleVolumeV2.class);
50 String prefix="DDVMV_"
51 ExceptionUtil exceptionUtil = new ExceptionUtil()
52 XmlParser xmlParser = new XmlParser()
53 JsonUtils jsonUtil = new JsonUtils()
56 public void preProcessRequest(DelegateExecution execution) {
57 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
58 preProcessRequest(execution, isDebugEnabled)
62 * Set default variable values
64 * @param isDebugLogEnabled
66 public void preProcessRequest (DelegateExecution execution, isDebugEnabled) {
71 // failIfNotFound (Optional)
72 // serviceInstanceId (Optional)
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
81 // workflowException @TODO: actual variable name is WorkflowException
85 execution.setVariable('prefix', prefix)
86 execution.setVariable('wasDeleted', 'false')
88 def tenantId = execution.getVariable("tenantId")
89 def cloudSiteId = execution.getVariable("lcpCloudRegionId")
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)
106 * Set out 'wasDeleted' variable to 'true'
108 * @param isDebugLogEnabled
110 public void postProcess(DelegateExecution execution, isDebugLogEnabled) {
111 execution.setVariable('wasDeleted', 'true')
116 * Query and set cloud region to use for AAI calls
117 * Output variables: prefix+'aicCloudRegion', prefix+'cloudRegion'
119 * @param isDebugEnabled
121 public void callRESTQueryAAICloudRegion(DelegateExecution execution, isDebugEnabled) {
123 String cloudRegion = execution.getVariable('lcpCloudRegionId')
124 AaiUtil aaiUtil = new AaiUtil(this)
126 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), cloudRegion))
127 def queryCloudRegionRequest = aaiUtil.createAaiUri(uri)
129 cloudRegion = aaiUtil.getAAICloudReqion(execution, queryCloudRegionRequest, "PO", cloudRegion)
131 if ((cloudRegion != "ERROR")) {
132 if(execution.getVariable(prefix+"queryCloudRegionReturnCode") == "404") {
133 execution.setVariable(prefix+"aicCloudRegion", "AAIAIC25")
136 execution.setVariable(prefix+"aicCloudRegion", cloudRegion)
140 logger.debug("AAI Query Cloud Region Unsuccessful.")
141 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode"))
147 * Query AAI Volume Group
148 * Output variables: prefix+'queryAAIVolGrpResponse'; prefix+'volumeGroupHeatStackId'
150 * @param isDebugLogEnabled
152 public void callRESTQueryAAIForVolumeGroup(DelegateExecution execution, isDebugLogEnabled) {
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')
160 String cloudRegion = execution.getVariable(prefix+'aicCloudRegion')
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)
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
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.")
181 for(AAIResourceUri aaiResourceUri: relationships.get().getRelatedUris(Types.TENANT)){
182 volumeGroupTenantId = aaiResourceUri.getURIKeys().get(AAIFluentTypeBuilder.Types.TENANT.getUriParams().tenantId)
186 logger.debug('Tenant ID from AAI response: ' + volumeGroupTenantId)
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}")
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)
198 logger.debug('Received Tenant Id ' + volumeGroupTenantId + ' from AAI for Volume Group with Volume Group Id ' + volumeGroupId )
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")
204 }catch (Exception ex) {
205 execution.setVariable(prefix+"queryAAIVolGrpResponse", ex.getMessage())
206 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(ex.getMessage(), execution)
207 throw new BpmnError("MSOWorkflowException")
212 * Format VNF Adapter subflow request XML
213 * Variables: prefix+'deleteVnfARequest'
215 * @param isDebugLogEnabled
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)
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)
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>
242 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
243 <serviceInstanceId>${MsoUtils.xmlEscape(serviceId)}</serviceInstanceId>
245 <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
246 <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
247 </deleteVolumeGroupRequest>
249 vnfAdapterRestRequest = utils.formatXml(vnfAdapterRestRequest)
250 execution.setVariable(prefix+'deleteVnfARequest', vnfAdapterRestRequest)
251 logger.debug('Request for VNFAdapter Rest:\n' + vnfAdapterRestRequest)
256 * Delete volume group in AAI
258 * @param isDebugEnabled
260 public void callRESTDeleteAAIVolumeGroup(DelegateExecution execution, isDebugEnabled) {
263 VolumeGroup volumeGroupResponse = execution.getVariable(prefix+"queryAAIVolGrpResponse")
264 String volumeGroupId = volumeGroupResponse.getVolumeGroupId()
265 String cloudRegion = execution.getVariable(prefix+'aicCloudRegion')
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")