bcd740eae9c64dc1c2ee18ed89e224ca3870a3e4
[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 org.camunda.bpm.engine.delegate.DelegateExecution
23 import org.onap.so.bpmn.core.UrnPropertiesReader
24 import org.onap.so.rest.APIResponse
25 import org.onap.so.logger.MessageEnum
26 import org.onap.so.logger.MsoLogger
27
28 public class ConfirmVolumeGroupName extends AbstractServiceTaskProcessor{
29         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ConfirmVolumeGroupName.class);
30         
31         def Prefix="CVGN_"
32         ExceptionUtil exceptionUtil = new ExceptionUtil()
33
34         public void initProcessVariables(DelegateExecution execution) {
35                 execution.setVariable("prefix",Prefix)
36                 execution.setVariable("CVGN_volumeGroupId",null)
37                 execution.setVariable("CVGN_volumeGroupName",null)
38                 execution.setVariable("CVGN_aicCloudRegion", null)
39                 execution.setVariable("CVGN_volumeGroupGetEndpoint",null)
40                                                 
41                 // ConfirmVolumeGroupName workflow response variable placeholders
42                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
43                 execution.setVariable("CVGN_queryVolumeGroupResponseCode",null)
44                 execution.setVariable("CVGN_queryVolumeGroupResponse","")
45                 execution.setVariable("CVGN_ResponseCode",null)
46                 execution.setVariable("RollbackData", null)
47         }
48
49         // store the incoming data in the flow DelegateExecution
50         public void preProcessRequest(DelegateExecution execution) {
51                 def volumeGroupId = execution.getVariable("ConfirmVolumeGroupName_volumeGroupId")
52                 def volumeGroupName= execution.getVariable("ConfirmVolumeGroupName_volumeGroupName")
53                 def aicCloudRegion = execution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")
54                 
55                 initProcessVariables(execution)
56                 execution.setVariable("CVGN_volumeGroupId", volumeGroupId)
57                 execution.setVariable("CVGN_volumeGroupName", volumeGroupName)
58                 execution.setVariable("CVGN_aicCloudRegion", aicCloudRegion)
59                 
60                 AaiUtil aaiUriUtil = new AaiUtil(this)
61                 def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)
62                 msoLogger.debug('AAI URI is: ' + aai_uri)
63                 msoLogger.debug("AAI URI: " + aai_uri)
64                 execution.setVariable("CVGN_volumeGroupGetEndpoint","${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" +
65                                 volumeGroupId)          
66         }
67         
68         // send a GET request to AA&I to retrieve the Volume information based on volume-group-id
69         // expect a 200 response with the information in the response body or a 404 if the volume group id does not exist
70         public void queryAAIForVolumeGroupId(DelegateExecution execution) {
71                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("CVGN_volumeGroupGetEndpoint")
72                 
73                 try {
74                         msoLogger.debug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint)
75                         msoLogger.debug("queryAAIForVolumeGroupId() endpoint-" + endPoint)
76                         msoLogger.debug("ConfirmVolumeGroup sending GET call to AAI Endpoint: " + endPoint)
77
78                         AaiUtil aaiUtil = new AaiUtil(this)
79                         APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
80                         def responseData = response.getResponseBodyAsString()
81                         def responseStatusCode = response.getStatusCode()
82                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", responseStatusCode)
83                         execution.setVariable("CVGN_queryVolumeGroupResponse", responseData)
84
85                         msoLogger.debug("Response code:" + responseStatusCode)
86                         msoLogger.debug("Response:" + responseData)
87                         msoLogger.debug("Response code:" + responseStatusCode)
88                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
89                 } catch (Exception ex) {
90         //              ex.printStackTrace()
91                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
92                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", 500)
93                         execution.setVariable("CVGN_queryVolumeGroupResponse", "AAI GET Failed:" + ex.getMessage())
94                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "AAI GET Failed")
95                 }
96         }
97
98         // process the result from queryAAIVolumeGroupId()
99
100         public void checkAAIQueryResult(DelegateExecution execution) {
101                 def result = execution.getVariable("CVGN_queryVolumeGroupResponse")
102
103                 if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 404) {
104                         msoLogger.debug('volumeGroupId does not exist in AAI')
105                 }
106                 else if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 200) {
107                         msoLogger.debug("volumeGroupId exists in AAI")
108                 }
109                 def xml = execution.getVariable("CVGN_queryVolumeGroupResponse")
110                 def actualVolumeGroupName = ""
111                 if (utils.nodeExists(xml, "volume-group-name")) {
112                         actualVolumeGroupName = utils.getNodeText(xml, "volume-group-name")
113                 }
114                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
115                 def volumeGroupName = execution.getVariable("CVGN_volumeGroupName")
116
117                 if (volumeGroupName.equals(actualVolumeGroupName)) {
118                         msoLogger.debug('Volume Group Name Matches AAI records')
119                         execution.setVariable("CVGN_volumeGroupNameMatches", true)
120                 }
121         }
122
123
124         // generates a WorkflowException if the A&AI query returns a response code other than 200/404
125         public void handleAAIQueryFailure(DelegateExecution execution) {
126                 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"));
127         }
128
129         // generates a WorkflowException if the volume group name does not match AAI record for this volume group
130         public void handleVolumeGroupNameNoMatch(DelegateExecution execution) {
131                 def errorNotAssociated = "Error occurred - volume group id " + execution.getVariable("CVGN_volumeGroupId") +
132                         " is not associated with  " + execution.getVariable("CVGN_volumeGroupName")
133                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, errorNotAssociated, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
134                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, errorNotAssociated)
135         }
136
137 }