Remove unnecessary use of Calendar.getInstance()
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / CreateAAIVfModuleVolumeGroup.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
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.runtime.Execution
25 import org.openecomp.mso.rest.APIResponse
26
27
28 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
29
30         private XmlParser xmlParser = new XmlParser()
31
32         /**
33          * Initialize the flow's variables.
34          *
35          * @param execution The flow's execution instance.
36          */
37         public void initProcessVariables(Execution execution) {
38                 execution.setVariable('prefix', 'CAAIVfModVG_')
39                 execution.setVariable('CAAIVfModVG_vnfId', null)
40                 execution.setVariable('CAAIVfModVG_vfModuleId', null)
41                 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
42                 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
43                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
44                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
45                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
46                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
47         }
48
49         /**
50          * Check for missing elements in the received request.
51          *
52          * @param execution The flow's execution instance.
53          */
54         public void preProcessRequest(Execution execution) {
55                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
56                         'execution=' + execution.getId() +
57                         ')'
58                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
59                 logDebug('Entered ' + method, isDebugLogEnabled)
60
61                 try {
62                         def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
63                         logDebug('Received request xml:\n' + xml, isDebugLogEnabled)
64                         utils.logAudit("CreateAAIVfModuleVolume Received Request XML: " + xml)
65                         initProcessVariables(execution)
66
67                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
68                         execution.setVariable('CAAIVfModVG_vnfId', vnfId)
69
70                         def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
71                         execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
72                         
73                         def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
74                         execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
75                         
76                         def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
77                         execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
78
79                         logDebug('Exited ' + method, isDebugLogEnabled)
80                 } catch (BpmnError e) {
81                         throw e;
82                 } catch (Exception e) {
83                         logError('Caught exception in ' + method, e)
84                         createWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
85                 }
86         }
87
88         /**
89          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
90          * A 200 response is expected with the VF Module in the response body.
91          *
92          * @param execution The flow's execution instance.
93          */
94         public void getVfModule(Execution execution) {
95                 def method = getClass().getSimpleName() + '.getVfModule(' +
96                         'execution=' + execution.getId() +
97                         ')'
98                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
99                 logDebug('Entered ' + method, isDebugLogEnabled)
100
101                 try {
102                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
103                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
104
105                         // Construct endpoint
106                         AaiUtil aaiUtil = new AaiUtil(this)
107                         def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
108                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
109                         String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + URLEncoder.encode(vnfId, "UTF-8") + '/vf-modules/vf-module/' + URLEncoder.encode(vfModuleId, "UTF-8")
110
111                         try {
112                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
113                                 utils.logAudit("aaiResponse GET TO AAI Endpoint: " + endPoint)
114                                 APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
115                                 def responseData = response.getResponseBodyAsString()
116                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', response.getStatusCode())
117                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', responseData)
118                                 
119                                 utils.logAudit("CreateAAIVfModule Response Code: " + response.getStatusCode())
120                                 utils.logAudit("CreateAAIVfModule Response: " + response)
121                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
122                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
123                         } catch (Exception ex) {
124                                 ex.printStackTrace()
125                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
126                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
127                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
128                         }
129                         logDebug('Exited ' + method, isDebugLogEnabled)
130                 } catch (BpmnError e) {
131                         throw e;
132                 } catch (Exception e) {
133                         logError('Caught exception in ' + method, e)
134                         createWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
135                 }
136         }
137
138         /**
139          * Construct and send a PUT request to AAI to update the VF Module with the
140          * created Volume Group relationship.
141          *
142          * @param execution The flow's execution instance.
143          */
144         public void updateVfModule(Execution execution) {
145                 def method = getClass().getSimpleName() + '.updateVfModule(' +
146                         'execution=' + execution.getId() +
147                         ')'
148                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
149                 logDebug('Entered ' + method, isDebugLogEnabled)
150
151                 try {
152                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
153                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
154                         def vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
155                         def origRequest = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
156                         def Node vfModuleNode = xmlParser.parseText(vfModule)
157                         
158                         // Confirm resource-version is in retrieved VF Module
159                         if (utils.getChildNode(vfModuleNode, 'resource-version') == null) {
160                                 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
161                                 logError(msg)
162                                 throw new Exception(msg)
163                         }
164                                                 
165                         // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
166                         def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
167                         def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
168                         def Node vgRelationshipNode = createVolumeGroupRelationshipNode(aicCloudRegion, volumeGroupId)
169                         insertVolumeGroupRelationshipNode(vfModuleNode, vgRelationshipNode)
170                         def payload = utils.nodeToString(vfModuleNode)
171
172                         // Construct endpoint
173                         AaiUtil aaiUtil = new AaiUtil(this)
174                         def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
175                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
176                         String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + URLEncoder.encode(vnfId, "UTF-8") + '/vf-modules/vf-module/' + URLEncoder.encode(vfModuleId, "UTF-8")
177
178                         try {
179                                 utils.logAudit("CreateAAIVfModuleVolume Sendind PUT to AAI Endpoint \n " + endPoint + " with payload \n " + payload)
180                                 logDebug('sending PUT to AAI endpoint \'' + endPoint + '\'' + 'with payload \n' + payload, isDebugLogEnabled)
181                                 APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload)
182                                 def responseData = response.getResponseBodyAsString()
183                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', response.getStatusCode())
184                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', responseData)
185                                 
186                                 utils.logAudit("CreateAAIVfModule Response code: " + response.getStatusCode())
187                                 utils.logAudit("CreateAAIVfModule Response: " + responseData)
188                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
189                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
190                         } catch (Exception ex) {
191                                 ex.printStackTrace()
192                                 logDebug('Exception occurred while executing AAI PUT:' + ex.getMessage(),isDebugLogEnabled)
193                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
194                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
195                         }
196                         logDebug('Exited ' + method, isDebugLogEnabled)
197                 } catch (BpmnError e) {
198                         throw e;
199                 } catch (Exception e) {
200                         logError('Caught exception in ' + method, e)
201                         createWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
202                 }
203         }
204
205         /**
206          * Construct a Volume Group relationship Node with the given AIC Cloud Region and
207          * Volume Group ID for insertion into a VF Module.
208          * 
209          * @param aicCloudRegion Cloud Region ID to use in the Volume Group relationship
210          * @param volumeGroupId Volume Group ID to use in the Volume Group relationship
211          * @return a Node representing the new Volume Group relationship
212          */
213         private Node createVolumeGroupRelationshipNode(String aicCloudRegion, String volumeGroupId) {
214                 
215                 def Node relatedTo = new Node(null, 'related-to', 'volume-group')
216                 
217                 def Node relationshipKeyCO = new Node(null, 'relationship-key', 'cloud-region.cloud-owner')
218                 def Node relationshipValueCO = new Node(null, 'relationship-value', 'att-aic')
219                 def Node relationshipDataCO = new Node(null, 'relationship-data')
220                 relationshipDataCO.append(relationshipKeyCO)
221                 relationshipDataCO.append(relationshipValueCO)
222                 
223                 def Node relationshipKeyCRI = new Node(null, 'relationship-key', 'cloud-region.cloud-region-id')
224                 def Node relationshipValueCRI = new Node(null, 'relationship-value', aicCloudRegion)
225                 def Node relationshipDataCRI = new Node(null, 'relationship-data')
226                 relationshipDataCRI.append(relationshipKeyCRI)
227                 relationshipDataCRI.append(relationshipValueCRI)
228
229                 def Node relationshipKeyVGI = new Node(null, 'relationship-key', 'volume-group.volume-group-id')
230                 def Node relationshipValueVGI = new Node(null, 'relationship-value', volumeGroupId)
231                 def Node relationshipDataVGI = new Node(null, 'relationship-data')
232                 relationshipDataVGI.append(relationshipKeyVGI)
233                 relationshipDataVGI.append(relationshipValueVGI)
234                 
235                 def Node volumeGroupRelationship = new Node(null, 'relationship')
236                 volumeGroupRelationship.append(relatedTo)
237                 volumeGroupRelationship.append(relationshipDataCO)
238                 volumeGroupRelationship.append(relationshipDataCRI)
239                 volumeGroupRelationship.append(relationshipDataVGI)
240                 
241                 return volumeGroupRelationship;
242         }
243         
244         /**
245          * Insert the given Volume Group relationship Node into the given VF Module.
246          * If the VF Module does NOT contain a relationship list:
247          *      - Create a relationship list containing the Volume Group relationship and insert it into the VF Module
248          * If the VF Module contains a relationship list but not a Volume Group relationship:
249          *      - Insert the the Volume Group relationship into the relationship lsit
250          * If the VF Module contains a relationship list and has a Volume Group relationship:
251          *      - Replace the existing Volume Group relationship with the new one
252          * @param vfModuleNode
253          * @param volumeGroupRelationshipNode
254          */
255         private void insertVolumeGroupRelationshipNode(Node vfModuleNode, Node volumeGroupRelationshipNode) {
256                 def Node relationshipList = utils.getChildNode(vfModuleNode, 'relationship-list')
257                 if (relationshipList == null) {
258                         relationshipList = new Node(null, 'relationship-list')
259                         relationshipList.append(volumeGroupRelationshipNode)
260                         vfModuleNode.append(relationshipList)
261                 } else {
262                         def Node currVolumeGroupRelationshipNode = getCurrVolumeGroupRelationshipNode(relationshipList)
263                         if (currVolumeGroupRelationshipNode == null) {
264                                 relationshipList.append(volumeGroupRelationshipNode)
265                         } else {
266                                 currVolumeGroupRelationshipNode.replaceNode(volumeGroupRelationshipNode)
267                         }
268                 }
269         }
270                 
271         /**
272          * Find and return the value of the Volume Group ID for the specified VF Module.  If
273          * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
274          * 
275          * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
276          * @return the value of the Volume Group ID for the specified VF Module.  If the
277          * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
278          */
279         private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
280                 def Node currVolumeGroupRelationshipNode = null
281                 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
282                 for (Node relationshipNode in relationships) {
283                         def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
284                         if ((relatedTo != null) && relatedTo.equals('volume-group')) {
285                                 currVolumeGroupRelationshipNode = relationshipNode
286                         }
287                 }
288                 return currVolumeGroupRelationshipNode
289         }
290
291         /**
292          * Generates a WorkflowException if the AAI query returns a response code other than 200.
293          *
294          * @param execution The flow's execution instance.
295          */
296         public void handleAAIQueryFailure(Execution execution) {
297                 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
298                         'execution=' + execution.getId() +
299                         ')'
300                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
301                 logDebug('Entered ' + method, isDebugLogEnabled)
302
303                 logError('Error occurred attempting to query AAI, Response Code ' +
304                         execution.getVariable('CAAIVfModVG_getVfModuleResponseCode') + ', Error Response ' +
305                         execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
306                 ExceptionUtil exceptionUtil = new ExceptionUtil()
307                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
308
309                 logDebug('Exited ' + method, isDebugLogEnabled)
310         }
311
312         /**
313          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
314          *
315          * @param execution The flow's execution instance.
316          */
317         public void handleUpdateVfModuleFailure(Execution execution) {
318                 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
319                         'execution=' + execution.getId() +
320                         ')'
321                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
322                 logDebug('Entered ' + method, isDebugLogEnabled)
323
324                 logError('Error occurred attempting to update VF Module in AAI, Response Code ' +
325                         execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode') + ', Error Response ' +
326                         execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
327                 ExceptionUtil exceptionUtil = new ExceptionUtil()
328                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
329
330                 logDebug('Exited ' + method, isDebugLogEnabled)
331         }
332 }