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