Groovy scripts header correction
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / ConfirmVolumeGroupName.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 import org.camunda.bpm.engine.runtime.Execution\r
23 import org.openecomp.mso.rest.APIResponse\r
24 \r
25 \r
26 public class ConfirmVolumeGroupName extends AbstractServiceTaskProcessor{\r
27         \r
28         def Prefix="CVGN_"\r
29         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
30 \r
31         public void initProcessVariables(Execution execution) {\r
32                 execution.setVariable("prefix",Prefix)\r
33                 execution.setVariable("CVGN_volumeGroupId",null)\r
34                 execution.setVariable("CVGN_volumeGroupName",null)\r
35                 execution.setVariable("CVGN_aicCloudRegion", null)\r
36                 execution.setVariable("CVGN_volumeGroupGetEndpoint",null)\r
37                                                 \r
38                 // ConfirmVolumeGroupName workflow response variable placeholders\r
39                 execution.setVariable("CVGN_volumeGroupNameMatches", false)\r
40                 execution.setVariable("CVGN_queryVolumeGroupResponseCode",null)\r
41                 execution.setVariable("CVGN_queryVolumeGroupResponse","")\r
42                 execution.setVariable("CVGN_ResponseCode",null)\r
43 //              execution.setVariable("CVGN_ErrorResponse","")\r
44                 execution.setVariable("RollbackData", null)\r
45         }\r
46 \r
47         // store the incoming data in the flow Execution\r
48         public void preProcessRequest(Execution execution) {\r
49                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
50                 def volumeGroupId = execution.getVariable("ConfirmVolumeGroupName_volumeGroupId")\r
51                 def volumeGroupName= execution.getVariable("ConfirmVolumeGroupName_volumeGroupName")\r
52                 def aicCloudRegion = execution.getVariable("ConfirmVolumeGroupName_aicCloudRegion")\r
53                 \r
54                 initProcessVariables(execution)\r
55                 execution.setVariable("CVGN_volumeGroupId", volumeGroupId)\r
56                 execution.setVariable("CVGN_volumeGroupName", volumeGroupName)\r
57                 execution.setVariable("CVGN_aicCloudRegion", aicCloudRegion)\r
58                 \r
59                 AaiUtil aaiUriUtil = new AaiUtil(this)\r
60                 def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)\r
61                 logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)\r
62                 utils.logAudit("AAI URI: " + aai_uri)\r
63                 execution.setVariable("CVGN_volumeGroupGetEndpoint","${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" +\r
64                                 volumeGroupId)          \r
65         }\r
66         \r
67         // send a GET request to AA&I to retrieve the Volume information based on volume-group-id\r
68         // expect a 200 response with the information in the response body or a 404 if the volume group id does not exist\r
69         public void queryAAIForVolumeGroupId(Execution execution) {\r
70                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
71                 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("CVGN_volumeGroupGetEndpoint")\r
72                 \r
73                 try {\r
74                         logDebug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint,isDebugLogEnabled)\r
75                         utils.log("DEBUG","queryAAIForVolumeGroupId() endpoint-" + endPoint, isDebugLogEnabled)\r
76                         utils.logAudit("ConfirmVolumeGroup sending GET call to AAI Endpoint: " + endPoint)\r
77 \r
78                         AaiUtil aaiUtil = new AaiUtil(this)\r
79                         APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)\r
80                         def responseData = response.getResponseBodyAsString()\r
81                         def responseStatusCode = response.getStatusCode()\r
82                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", responseStatusCode)\r
83                         execution.setVariable("CVGN_queryVolumeGroupResponse", responseData)\r
84 \r
85                         utils.logAudit("Response code:" + responseStatusCode)\r
86                         utils.logAudit("Response:" + responseData)\r
87                         logDebug("Response code:" + responseStatusCode, isDebugLogEnabled)\r
88                         logDebug("Response:" + System.lineSeparator()+responseData,isDebugLogEnabled)\r
89                 } catch (Exception ex) {\r
90         //              ex.printStackTrace()\r
91                         logDebug("Exception occurred while executing AAI GET:" + ex.getMessage(),isDebugLogEnabled)\r
92                         execution.setVariable("CVGN_queryVolumeGroupResponseCode", 500)\r
93                         execution.setVariable("CVGN_queryVolumeGroupResponse", "AAI GET Failed:" + ex.getMessage())\r
94                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "AAI GET Failed")\r
95                 }\r
96         }\r
97 \r
98         // process the result from queryAAIVolumeGroupId()\r
99 \r
100         public void checkAAIQueryResult(Execution execution) {\r
101                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
102                 def result = execution.getVariable("CVGN_queryVolumeGroupResponse")\r
103 \r
104                 if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 404) {\r
105                         logDebug('volumeGroupId does not exist in AAI', isDebugLogEnabled)\r
106                 }\r
107                 else if (execution.getVariable("CVGN_queryVolumeGroupResponseCode") == 200) {\r
108                         logDebug("volumeGroupId exists in AAI", isDebugLogEnabled)\r
109                 }\r
110                 def xml = execution.getVariable("CVGN_queryVolumeGroupResponse")\r
111                 def actualVolumeGroupName = ""\r
112                 if (utils.nodeExists(xml, "volume-group-name")) {\r
113                         actualVolumeGroupName = utils.getNodeText(xml, "volume-group-name")\r
114                 }\r
115                 execution.setVariable("CVGN_volumeGroupNameMatches", false)\r
116                 def volumeGroupName = execution.getVariable("CVGN_volumeGroupName")\r
117 \r
118                 if (volumeGroupName.equals(actualVolumeGroupName)) {\r
119                         logDebug('Volume Group Name Matches AAI records', isDebugLogEnabled)\r
120                         execution.setVariable("CVGN_volumeGroupNameMatches", true)\r
121                 }\r
122         }\r
123 \r
124 \r
125         // generates a WorkflowException if the A&AI query returns a response code other than 200/404\r
126         public void handleAAIQueryFailure(Execution execution) {\r
127                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
128 \r
129                 logError("Error occurred attempting to query AAI, Response Code " +\r
130                         execution.getVariable("CVGN_queryVolumeGroupResponseCode") + ", Error Response " +\r
131                         execution.getVariable("CVGN_queryVolumeGroupResponse"))\r
132                 //String processKey = getProcessKey(execution);\r
133                 //WorkflowException exception = new WorkflowException(processKey, 5000,\r
134                         //execution.getVariable("CVGN_queryVolumeGroupResponse"))\r
135                 //execution.setVariable("WorkflowException", exception)\r
136         }\r
137 \r
138         // generates a WorkflowException if the volume group name does not match AAI record for this volume group\r
139         public void handleVolumeGroupNameNoMatch(Execution execution) {\r
140                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
141 \r
142                 def errorNotAssociated = "Error occurred - volume group id " + execution.getVariable("CVGN_volumeGroupId") +\r
143                         " is not associated with  " + execution.getVariable("CVGN_volumeGroupName")\r
144                 logError(errorNotAssociated)\r
145                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, errorNotAssociated)\r
146                 //String processKey = getProcessKey(execution);\r
147                 //WorkflowException exception = new WorkflowException(processKey, 1002,\r
148                 //      errorNotAssociated)\r
149                 //execution.setVariable("WorkflowException", exception)\r
150         }\r
151 \r
152         // sends a successful WorkflowResponse\r
153         public void reportSuccess(Execution execution) {\r
154                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
155                 logDebug("Sending 200 back to the caller", isDebugLogEnabled)\r
156                 def responseXML = ""\r
157                 execution.setVariable("WorkflowResponse", responseXML)\r
158         }\r
159 }