Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / ConfirmVolumeGroupTenant.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.common.scripts
22
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.VolumeGroup
26 import org.onap.so.client.aai.AAIObjectType
27 import org.onap.so.client.aai.entities.AAIResultWrapper
28 import org.onap.so.client.aai.entities.Relationships
29 import org.onap.so.client.aai.entities.uri.AAIResourceUri
30 import org.onap.so.client.aai.entities.uri.AAIUriFactory
31 import org.onap.so.constants.Defaults
32 import org.onap.so.logger.MessageEnum
33 import org.onap.so.logger.MsoLogger
34
35 /**
36  * Vnf Module Subflow for confirming the volume group belongs
37  * to the tenant
38  *
39  * @param tenantId
40  * @param volumeGroupId
41  *
42  */
43 class ConfirmVolumeGroupTenant extends AbstractServiceTaskProcessor{
44         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ConfirmVolumeGroupTenant.class);
45
46         String Prefix="CVGT_"
47         ExceptionUtil exceptionUtil = new ExceptionUtil()
48
49         public void preProcessRequest(DelegateExecution execution){
50                 execution.setVariable("prefix", Prefix)
51                 msoLogger.trace("STARTED Confirm Volume Group Tenant Subflow ")
52                 try{
53                         msoLogger.trace("Started QueryAAIForVolumeGroup Process ")
54
55                         String volumeGroupId = execution.getVariable("volumeGroupId")
56                         String incomingGroupName = execution.getVariable("volumeGroupName")
57                         String incomingTenantId = execution.getVariable("tenantId")
58                         String aicCloudRegion = execution.getVariable("aicCloudRegion")
59                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), aicCloudRegion, volumeGroupId)
60                         AAIResultWrapper wrapper = getAAIClient().get(uri);
61                         Optional<VolumeGroup> volumeGroup = wrapper.asBean(VolumeGroup.class)
62                         Optional<Relationships> relationships = wrapper.getRelationships()
63                         if(volumeGroup.isPresent()){
64                                 execution.setVariable("queryAAIVolumeGroupResponse", volumeGroup.get())
65                                 String volumeGroupTenantId = ""
66                                 if(relationships.isPresent()){
67                                         List<AAIResourceUri> tenantUris = relationships.get().getRelatedAAIUris(AAIObjectType.TENANT)
68                                         for (AAIResourceUri tenantURI: tenantUris){
69                                                         volumeGroupTenantId = tenantURI.getURIKeys().get("tenant-id")
70                                         }
71                                 }
72                                 //Determine if Tenant Ids match
73                                 if(incomingTenantId.equals(volumeGroupTenantId)){
74                                         msoLogger.debug("Tenant Ids Match")
75                                         execution.setVariable("tenantIdsMatch", true)
76                                 }else{
77                                         msoLogger.debug("Tenant Ids DO NOT Match")
78                                         execution.setVariable("tenantIdsMatch", false)
79                                 }
80
81                                 //Determine if Volume Group Names match
82                                 String volumeGroupName = volumeGroup.get().getVolumeGroupName()
83                                 if(incomingGroupName == null || incomingGroupName.length() < 1){
84                                         msoLogger.debug("Incoming Volume Group Name is NOT Provided.")
85                                         execution.setVariable("groupNamesMatch", true)
86                                 }else{
87                                         msoLogger.debug("Incoming Volume Group Name is: " + incomingGroupName)
88                                         if(volumeGroupName.equals(incomingGroupName)){
89                                                 msoLogger.debug("Volume Group Names Match.")
90                                                 execution.setVariable("groupNamesMatch", true)
91                                         }else{
92                                                 msoLogger.debug("Volume Group Names DO NOT Match.")
93                                                 execution.setVariable("groupNamesMatch", false)
94                                         }
95                                 }
96                         }else{
97                                 msoLogger.debug("QueryAAIForVolumeGroup Bad REST Response!")
98                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1, "Error Searching AAI for Volume Group. Received a Bad Response.")
99                         }
100
101                 }catch(BpmnError b){
102                         throw b
103                 }catch(Exception e){
104                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing queryAAIForVolumeGroup.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.getMessage());
105                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in preProcessRequest.")
106                 }
107                 msoLogger.trace("COMPLETED queryAAIForVolumeGroup Process ")
108         }
109
110         public void assignVolumeHeatId(DelegateExecution execution){
111                 execution.setVariable("prefix", Prefix)
112                 try{
113                         msoLogger.trace("Started assignVolumeHeatId Process ")
114
115                         VolumeGroup volumeGroup = execution.getVariable("queryAAIVolumeGroupResponse")
116                         String heatStackId = volumeGroup.getHeatStackId()
117                         execution.setVariable("volumeHeatStackId", heatStackId)
118                         execution.setVariable("ConfirmVolumeGroupTenantResponse", heatStackId)
119                         // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
120                         execution.setVariable("WorkflowResponse", heatStackId)
121                         msoLogger.debug("Volume Heat Stack Id is: " + heatStackId)
122
123                 }catch(Exception e){
124                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing assignVolumeHeatId.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e);
125                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in assignVolumeHeatId.")
126         }
127         msoLogger.trace("COMPLETED assignVolumeHeatId Process ")
128         msoLogger.trace("COMPLETED Confirm Volume Group Tenant Subflow ")
129 }
130
131         public void assignWorkflowException(DelegateExecution execution, String message){
132                 execution.setVariable("prefix", Prefix)
133                 String processKey = getProcessKey(execution);
134                 msoLogger.trace("STARTED Assign Workflow Exception ")
135                 try{
136                         String volumeGroupId = execution.getVariable("volumeGroupId")
137                         int errorCode = 1
138                         String errorMessage = "Volume Group " + volumeGroupId + " " + message
139
140                         exceptionUtil.buildWorkflowException(execution, errorCode, errorMessage)
141                 }catch(Exception e){
142                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing assignWorkflowException.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e);
143                 }
144                 msoLogger.trace("COMPLETED Assign Workflow Exception =")
145         }
146
147
148
149 }
150