Containerization feature of SO
[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("CVGN_ErrorResponse","")
47                 execution.setVariable("RollbackData", null)
48         }
49
50         // store the incoming data in the flow DelegateExecution
51         public void preProcessRequest(DelegateExecution execution) {
52                 def volumeGroupId = execution.getVariable("ConfirmVolumeGroupName_volumeGroupId")
53                 def volumeGroupName= execution.getVariable("ConfirmVolumeGroupName_volumeGroupName")
54                 def aicCloudRegion = execution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")
55                 
56                 initProcessVariables(execution)
57                 execution.setVariable("CVGN_volumeGroupId", volumeGroupId)
58                 execution.setVariable("CVGN_volumeGroupName", volumeGroupName)
59                 execution.setVariable("CVGN_aicCloudRegion", aicCloudRegion)
60                 
61                 AaiUtil aaiUriUtil = new AaiUtil(this)
62                 def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)
63                 msoLogger.debug('AAI URI is: ' + aai_uri)
64                 msoLogger.debug("AAI URI: " + aai_uri)
65                 execution.setVariable("CVGN_volumeGroupGetEndpoint","${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" +
66                                 volumeGroupId)          
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                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("CVGN_volumeGroupGetEndpoint")
73                 
74                 try {
75                         msoLogger.debug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint)
76                         msoLogger.debug("queryAAIForVolumeGroupId() endpoint-" + endPoint)
77                         msoLogger.debug("ConfirmVolumeGroup sending GET call to AAI Endpoint: " + endPoint)
78
79                         AaiUtil aaiUtil = new AaiUtil(this)
80                         APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
81                         def responseData = response.getResponseBodyAsString()
82                         def responseStatusCode = response.getStatusCode()
83                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", responseStatusCode)
84                         execution.setVariable("CVGN_queryVolumeGroupResponse", responseData)
85
86                         msoLogger.debug("Response code:" + responseStatusCode)
87                         msoLogger.debug("Response:" + responseData)
88                         msoLogger.debug("Response code:" + responseStatusCode)
89                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
90                 } catch (Exception ex) {
91         //              ex.printStackTrace()
92                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
93                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", 500)
94                         execution.setVariable("CVGN_queryVolumeGroupResponse", "AAI GET Failed:" + ex.getMessage())
95                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "AAI GET Failed")
96                 }
97         }
98
99         // process the result from queryAAIVolumeGroupId()
100
101         public void checkAAIQueryResult(DelegateExecution execution) {
102                 def result = execution.getVariable("CVGN_queryVolumeGroupResponse")
103
104                 if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 404) {
105                         msoLogger.debug('volumeGroupId does not exist in AAI')
106                 }
107                 else if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 200) {
108                         msoLogger.debug("volumeGroupId exists in AAI")
109                 }
110                 def xml = execution.getVariable("CVGN_queryVolumeGroupResponse")
111                 def actualVolumeGroupName = ""
112                 if (utils.nodeExists(xml, "volume-group-name")) {
113                         actualVolumeGroupName = utils.getNodeText(xml, "volume-group-name")
114                 }
115                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
116                 def volumeGroupName = execution.getVariable("CVGN_volumeGroupName")
117
118                 if (volumeGroupName.equals(actualVolumeGroupName)) {
119                         msoLogger.debug('Volume Group Name Matches AAI records')
120                         execution.setVariable("CVGN_volumeGroupNameMatches", true)
121                 }
122         }
123
124
125         // generates a WorkflowException if the A&AI query returns a response code other than 200/404
126         public void handleAAIQueryFailure(DelegateExecution execution) {
127                 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"));
128                 //String processKey = getProcessKey(execution);
129                 //WorkflowException exception = new WorkflowException(processKey, 5000,
130                         //execution.getVariable("CVGN_queryVolumeGroupResponse"))
131                 //execution.setVariable("WorkflowException", exception)
132         }
133
134         // generates a WorkflowException if the volume group name does not match AAI record for this volume group
135         public void handleVolumeGroupNameNoMatch(DelegateExecution execution) {
136                 def errorNotAssociated = "Error occurred - volume group id " + execution.getVariable("CVGN_volumeGroupId") +
137                         " is not associated with  " + execution.getVariable("CVGN_volumeGroupName")
138                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, errorNotAssociated, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
139                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, errorNotAssociated)
140                 //String processKey = getProcessKey(execution);
141                 //WorkflowException exception = new WorkflowException(processKey, 1002,
142                 //      errorNotAssociated)
143                 //execution.setVariable("WorkflowException", exception)
144         }
145
146         // sends a successful WorkflowResponse
147         public void reportSuccess(DelegateExecution execution) {
148                 msoLogger.debug("Sending 200 back to the caller")
149                 def responseXML = ""
150                 execution.setVariable("WorkflowResponse", responseXML)
151         }
152 }