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