7d18a8b1cebe3f6c6515cf5c715df083ede376f2
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / ConfirmVolumeGroupName.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2018 Nokia
8  * ================================================================================
9  * Modifications Copyright (c) 2019 Samsung
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.so.bpmn.common.scripts
26
27 import joptsimple.internal.Strings
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.springframework.http.HttpStatus
30
31 import javax.ws.rs.core.UriBuilder
32
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34 import org.onap.aai.domain.yang.VolumeGroup
35 import org.onap.so.client.aai.AAIObjectType
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory
38 import org.onap.so.constants.Defaults
39 import org.onap.so.logger.MessageEnum
40 import org.onap.so.logger.MsoLogger
41 import org.slf4j.Logger
42 import org.slf4j.LoggerFactory
43
44 public class ConfirmVolumeGroupName extends AbstractServiceTaskProcessor{
45     private static final Logger logger = LoggerFactory.getLogger( ConfirmVolumeGroupName.class);
46
47         def static final Prefix = "CVGN_"
48         private final ExceptionUtil exceptionUtil
49
50         ConfirmVolumeGroupName(ExceptionUtil exceptionUtil) {
51                 this.exceptionUtil = exceptionUtil
52         }
53
54         public void initProcessVariables(DelegateExecution execution) {
55                 execution.setVariable("prefix",Prefix)
56                 execution.setVariable("CVGN_volumeGroupId",null)
57                 execution.setVariable("CVGN_volumeGroupName",null)
58                 execution.setVariable("CVGN_aicCloudRegion", null)
59                 execution.setVariable("CVGN_volumeGroupGetEndpoint",null)
60
61                 // ConfirmVolumeGroupName workflow response variable placeholders
62                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
63                 execution.setVariable("CVGN_queryVolumeGroupResponseCode",null)
64                 execution.setVariable("CVGN_queryVolumeGroupResponse","")
65                 execution.setVariable("CVGN_ResponseCode",null)
66                 execution.setVariable("RollbackData", null)
67         }
68
69         // store the incoming data in the flow DelegateExecution
70         public void preProcessRequest(DelegateExecution execution) {
71                 def volumeGroupId = execution.getVariable("ConfirmVolumeGroupName_volumeGroupId")
72                 def volumeGroupName= execution.getVariable("ConfirmVolumeGroupName_volumeGroupName")
73                 def aicCloudRegion = execution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")
74
75                 initProcessVariables(execution)
76                 execution.setVariable("CVGN_volumeGroupId", volumeGroupId)
77                 execution.setVariable("CVGN_volumeGroupName", volumeGroupName)
78                 execution.setVariable("CVGN_aicCloudRegion", aicCloudRegion)
79
80                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), aicCloudRegion, volumeGroupId)
81                 execution.setVariable("CVGN_volumeGroupGetEndpoint", uri)
82         }
83
84         // send a GET request to AA&I to retrieve the Volume information based on volume-group-id
85         // expect a 200 response with the information in the response body or a 404 if the volume group id does not exist
86         public void queryAAIForVolumeGroupId(DelegateExecution execution) {
87                 AAIResourceUri resourceUri = execution.getVariable("CVGN_volumeGroupGetEndpoint")
88
89                 try {
90                         Optional<VolumeGroup> volumeGroupOp = getAAIClient().get(VolumeGroup.class,  resourceUri)
91             if(volumeGroupOp.isPresent()){
92                 execution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.OK.value())
93                 execution.setVariable("CVGN_queryVolumeGroupResponse", volumeGroupOp.get())
94             }else{
95                 execution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.NOT_FOUND.value())
96                 execution.setVariable("CVGN_queryVolumeGroupResponse", "Volume Group not Found!")
97             }
98                 } catch (Exception ex) {
99                         logger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
100                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.INTERNAL_SERVER_ERROR.value())
101                         execution.setVariable("CVGN_queryVolumeGroupResponse", "AAI GET Failed:" + ex.getMessage())
102                         exceptionUtil.buildAndThrowWorkflowException(execution, HttpStatus.INTERNAL_SERVER_ERROR.value(), "AAI GET Failed")
103                 }
104         }
105
106         // process the result from queryAAIVolumeGroupId()
107
108         public void checkAAIQueryResult(DelegateExecution execution) {
109         def actualVolumeGroupName = ""
110         if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == HttpStatus.NOT_FOUND.value()) {
111                         logger.debug('volumeGroupId does not exist in AAI')
112                 }
113                 else if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == HttpStatus.OK.value()) {
114             VolumeGroup volumeGroup = execution.getVariable("CVGN_queryVolumeGroupResponse")
115
116             if (!Strings.isNullOrEmpty(volumeGroup.getVolumeGroupName())) {
117                 actualVolumeGroupName =  volumeGroup.getVolumeGroupName()
118                                 logger.debug("volumeGroupId exists in AAI")
119                         }
120                 }
121                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
122                 def volumeGroupName = execution.getVariable("CVGN_volumeGroupName")
123
124                 if (!actualVolumeGroupName.isEmpty() && volumeGroupName.equals(actualVolumeGroupName)) {
125                         logger.debug('Volume Group Name Matches AAI records')
126                         execution.setVariable("CVGN_volumeGroupNameMatches", true)
127                 }
128         }
129
130
131         // generates a WorkflowException if the A&AI query returns a response code other than 200/404
132         public void handleAAIQueryFailure(DelegateExecution execution) {
133                 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
134                                 "Error occurred attempting to query AAI, Response Code " + execution.getVariable("CVGN_queryVolumeGroupResponseCode"),
135                                 "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError.getValue(),
136                                 "ErrorResponse is:\n" + execution.getVariable("CVGN_queryVolumeGroupResponse"));
137         }
138
139         // generates a WorkflowException if the volume group name does not match AAI record for this volume group
140         public void handleVolumeGroupNameNoMatch(DelegateExecution execution) {
141                 def errorNotAssociated = "Error occurred - volume group id ${execution.getVariable('CVGN_volumeGroupId')} " +
142                                 "is not associated with ${execution.getVariable('CVGN_volumeGroupName')}"
143                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), errorNotAssociated, "BPMN",
144                                 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError.getValue());
145                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, errorNotAssociated)
146         }
147
148         // sends a successful WorkflowResponse
149         public void reportSuccess(DelegateExecution execution) {
150                 logger.debug("Sending 200 back to the caller")
151                 def responseXML = ""
152                 execution.setVariable("WorkflowResponse", responseXML)
153         }
154 }