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