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