Dynamic Cloud Owner Support
[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  * 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 import javax.ws.rs.core.UriBuilder
23
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.uri.AAIResourceUri
28 import org.onap.so.client.aai.entities.uri.AAIUriFactory
29 import org.onap.so.constants.Defaults
30 import org.onap.so.logger.MessageEnum
31 import org.onap.so.logger.MsoLogger
32
33 public class ConfirmVolumeGroupName extends AbstractServiceTaskProcessor{
34         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ConfirmVolumeGroupName.class);
35
36         def Prefix="CVGN_"
37         ExceptionUtil exceptionUtil = new ExceptionUtil()
38
39         public void initProcessVariables(DelegateExecution execution) {
40                 execution.setVariable("prefix",Prefix)
41                 execution.setVariable("CVGN_volumeGroupId",null)
42                 execution.setVariable("CVGN_volumeGroupName",null)
43                 execution.setVariable("CVGN_aicCloudRegion", null)
44                 execution.setVariable("CVGN_volumeGroupGetEndpoint",null)
45
46                 // ConfirmVolumeGroupName workflow response variable placeholders
47                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
48                 execution.setVariable("CVGN_queryVolumeGroupResponseCode",null)
49                 execution.setVariable("CVGN_queryVolumeGroupResponse","")
50                 execution.setVariable("CVGN_ResponseCode",null)
51                 execution.setVariable("RollbackData", null)
52         }
53
54         // store the incoming data in the flow DelegateExecution
55         public void preProcessRequest(DelegateExecution execution) {
56                 def volumeGroupId = execution.getVariable("ConfirmVolumeGroupName_volumeGroupId")
57                 def volumeGroupName= execution.getVariable("ConfirmVolumeGroupName_volumeGroupName")
58                 def aicCloudRegion = execution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")
59
60                 initProcessVariables(execution)
61                 execution.setVariable("CVGN_volumeGroupId", volumeGroupId)
62                 execution.setVariable("CVGN_volumeGroupName", volumeGroupName)
63                 execution.setVariable("CVGN_aicCloudRegion", aicCloudRegion)
64
65                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), aicCloudRegion, volumeGroupId)
66                 execution.setVariable("CVGN_volumeGroupGetEndpoint", uri)
67         }
68
69         // send a GET request to AA&I to retrieve the Volume information based on volume-group-id
70         // expect a 200 response with the information in the response body or a 404 if the volume group id does not exist
71         public void queryAAIForVolumeGroupId(DelegateExecution execution) {
72                 AAIResourceUri resourceUri = execution.getVariable("CVGN_volumeGroupGetEndpoint")
73
74                 try {
75                         Optional<VolumeGroup> volumeGroupOp = getAAIClient().get(VolumeGroup.class,  resourceUri)
76             if(volumeGroupOp.isPresent()){
77                 execution.setVariable("CVGN_queryVolumeGroupResponseCode", 200)
78                 execution.setVariable("CVGN_queryVolumeGroupResponse", volumeGroupOp.get())
79             }else{
80                 execution.setVariable("CVGN_queryVolumeGroupResponseCode", 404)
81                 execution.setVariable("CVGN_queryVolumeGroupResponse", "Volume Group not Found!")
82             }
83                 } catch (Exception ex) {
84                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
85                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", 500)
86                         execution.setVariable("CVGN_queryVolumeGroupResponse", "AAI GET Failed:" + ex.getMessage())
87                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "AAI GET Failed")
88                 }
89         }
90
91         // process the result from queryAAIVolumeGroupId()
92
93         public void checkAAIQueryResult(DelegateExecution execution) {
94                 def result = execution.getVariable("CVGN_queryVolumeGroupResponse")
95
96         def actualVolumeGroupName = ""
97         if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 404) {
98                         msoLogger.debug('volumeGroupId does not exist in AAI')
99                 }
100                 else if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 200) {
101             VolumeGroup volumeGroup = execution.getVariable("CVGN_queryVolumeGroupResponse")
102             if(volumeGroup.getVolumeGroupName()!=null){
103                 actualVolumeGroupName =  volumeGroup.getVolumeGroupName()
104             }
105                         msoLogger.debug("volumeGroupId exists in AAI")
106                 }
107                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
108                 def volumeGroupName = execution.getVariable("CVGN_volumeGroupName")
109
110                 if (volumeGroupName.equals(actualVolumeGroupName)) {
111                         msoLogger.debug('Volume Group Name Matches AAI records')
112                         execution.setVariable("CVGN_volumeGroupNameMatches", true)
113                 }
114         }
115
116
117         // generates a WorkflowException if the A&AI query returns a response code other than 200/404
118         public void handleAAIQueryFailure(DelegateExecution execution) {
119                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred attempting to query AAI, Response Code " + execution.getVariable("CVGN_queryVolumeGroupResponseCode"), "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "ErrorResponse is:\n" + execution.getVariable("CVGN_queryVolumeGroupResponse"));
120         }
121
122         // generates a WorkflowException if the volume group name does not match AAI record for this volume group
123         public void handleVolumeGroupNameNoMatch(DelegateExecution execution) {
124                 def errorNotAssociated = "Error occurred - volume group id " + execution.getVariable("CVGN_volumeGroupId") +
125                         " is not associated with  " + execution.getVariable("CVGN_volumeGroupName")
126                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, errorNotAssociated, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
127                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, errorNotAssociated)
128         }
129
130         // sends a successful WorkflowResponse
131         public void reportSuccess(DelegateExecution execution) {
132                 msoLogger.debug("Sending 200 back to the caller")
133                 def responseXML = ""
134                 execution.setVariable("WorkflowResponse", responseXML)
135         }
136 }