[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / ConfirmVolumeGroupName.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.openecomp.mso.bpmn.common.scripts
22 import org.camunda.bpm.engine.runtime.Execution
23 import org.openecomp.mso.rest.APIResponse
24
25
26 public class ConfirmVolumeGroupName extends AbstractServiceTaskProcessor{
27         
28         def Prefix="CVGN_"
29         
30         public void initProcessVariables(Execution execution) {
31                 execution.setVariable("prefix",Prefix)
32                 execution.setVariable("CVGN_volumeGroupId",null)
33                 execution.setVariable("CVGN_volumeGroupName",null)
34                 execution.setVariable("CVGN_aicCloudRegion", null)
35                 execution.setVariable("CVGN_volumeGroupGetEndpoint",null)
36                                                 
37                 // ConfirmVolumeGroupName workflow response variable placeholders
38                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
39                 execution.setVariable("CVGN_queryVolumeGroupResponseCode",null)
40                 execution.setVariable("CVGN_queryVolumeGroupResponse","")
41                 execution.setVariable("CVGN_ResponseCode",null)
42 //              execution.setVariable("CVGN_ErrorResponse","")
43                 execution.setVariable("RollbackData", null)
44         }       
45         
46         // store the incoming data in the flow Execution
47         public void preProcessRequest(Execution execution) {
48                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
49                 def volumeGroupId = execution.getVariable("ConfirmVolumeGroupName_volumeGroupId")
50                 def volumeGroupName= execution.getVariable("ConfirmVolumeGroupName_volumeGroupName")
51                 def aicCloudRegion = execution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")
52                 
53                 initProcessVariables(execution)
54                 execution.setVariable("CVGN_volumeGroupId", volumeGroupId)
55                 execution.setVariable("CVGN_volumeGroupName", volumeGroupName)
56                 execution.setVariable("CVGN_aicCloudRegion", aicCloudRegion)
57                 
58                 AaiUtil aaiUriUtil = new AaiUtil(this)
59                 def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)
60                 logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
61                 utils.logAudit("AAI URI: " + aai_uri)
62                 execution.setVariable("CVGN_volumeGroupGetEndpoint","${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" +
63                                 volumeGroupId)          
64         }
65         
66         // send a GET request to AA&I to retrieve the Volume information based on volume-group-id
67         // expect a 200 response with the information in the response body or a 404 if the volume group id does not exist
68         public void queryAAIForVolumeGroupId(Execution execution) {
69                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
70                 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("CVGN_volumeGroupGetEndpoint")
71                 
72                 try {
73                         logDebug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint,isDebugLogEnabled)
74                         utils.log("DEBUG","queryAAIForVolumeGroupId() endpoint-" + endPoint, isDebugLogEnabled)
75                         utils.logAudit("ConfirmVolumeGroup sending GET call to AAI Endpoint: " + endPoint)
76                         
77                         AaiUtil aaiUtil = new AaiUtil(this)
78                         APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
79                         def responseData = response.getResponseBodyAsString()
80                         def responseStatusCode = response.getStatusCode()
81                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", responseStatusCode)
82                         execution.setVariable("CVGN_queryVolumeGroupResponse", responseData)
83                         
84                         utils.logAudit("Response code:" + responseStatusCode)
85                         utils.logAudit("Response:" + responseData)
86                         logDebug("Response code:" + responseStatusCode, isDebugLogEnabled)
87                         logDebug("Response:" + System.lineSeparator()+responseData,isDebugLogEnabled)
88                 } catch (Exception ex) {
89                         ex.printStackTrace()
90                         logDebug("Exception occurred while executing AAI GET:" + ex.getMessage(),isDebugLogEnabled)
91                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", 500)
92                         execution.setVariable("CVGN_queryVolumeGroupResponse", "AAI GET Failed:" + ex.getMessage())
93                 }
94         }
95         
96         // process the result from queryAAIVolumeGroupId()
97         
98         public void checkAAIQueryResult(Execution execution) {
99                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
100                 def result = execution.getVariable("CVGN_queryVolumeGroupResponse")
101                 
102                 if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 404) {
103                         logDebug('volumeGroupId does not exist in AAI', isDebugLogEnabled)
104                 }
105                 else if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 200) {
106                         logDebug("volumeGroupId exists in AAI", isDebugLogEnabled)      
107                 }
108                 def xml = execution.getVariable("CVGN_queryVolumeGroupResponse")
109                 def actualVolumeGroupName = ""
110                 if (utils.nodeExists(xml, "volume-group-name")) {
111                         actualVolumeGroupName = utils.getNodeText(xml, "volume-group-name")
112                 }
113                 execution.setVariable("CVGN_volumeGroupNameMatches", false)
114                 def volumeGroupName = execution.getVariable("CVGN_volumeGroupName")
115                                 
116                 if (volumeGroupName.equals(actualVolumeGroupName)) {
117                         logDebug('Volume Group Name Matches AAI records', isDebugLogEnabled)                            
118                         execution.setVariable("CVGN_volumeGroupNameMatches", true)
119                 }                               
120         }
121         
122         
123         // generates a WorkflowException if the A&AI query returns a response code other than 200/404
124         public void handleAAIQueryFailure(Execution execution) {
125                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
126                 
127                 logError("Error occurred attempting to query AAI, Response Code " +
128                         execution.getVariable("CVGN_queryVolumeGroupResponseCode") + ", Error Response " +
129                         execution.getVariable("CVGN_queryVolumeGroupResponse"))
130                 //String processKey = getProcessKey(execution);
131                 //WorkflowException exception = new WorkflowException(processKey, 5000,
132                         //execution.getVariable("CVGN_queryVolumeGroupResponse"))
133                 //execution.setVariable("WorkflowException", exception)
134         }
135         
136         // generates a WorkflowException if the volume group name does not match AAI record for this volume group
137         public void handleVolumeGroupNameNoMatch(Execution execution) {
138                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
139                 
140                 def errorNotAssociated = "Error occurred - volume group id " + execution.getVariable("CVGN_volumeGroupId") +
141                         " is not associated with  " + execution.getVariable("CVGN_volumeGroupName")
142                 logError(errorNotAssociated)
143                 createWorkflowException(execution, 1002, errorNotAssociated)
144                 //String processKey = getProcessKey(execution);
145                 //WorkflowException exception = new WorkflowException(processKey, 1002,
146                 //      errorNotAssociated)
147                 //execution.setVariable("WorkflowException", exception)
148         }
149         
150         // sends a successful WorkflowResponse
151         public void reportSuccess(Execution execution) {
152                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
153                 logDebug("Sending 200 back to the caller", isDebugLogEnabled)
154                 def responseXML = ""
155                 execution.setVariable("WorkflowResponse", responseXML)
156         }
157 }