Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / CreateAAIVfModuleVolumeGroup.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
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.client.aai.AAIObjectType
26 import org.onap.so.client.aai.entities.uri.AAIResourceUri
27 import org.onap.so.client.aai.entities.uri.AAIUriFactory
28 import org.onap.so.logger.MsoLogger
29
30 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
31         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateAAIVfModuleVolumeGroup.class);
32
33         private XmlParser xmlParser = new XmlParser()
34         ExceptionUtil exceptionUtil = new ExceptionUtil()
35
36         /**
37          * Initialize the flow's variables.
38          *
39          * @param execution The flow's execution instance.
40          */
41         public void initProcessVariables(DelegateExecution execution) {
42                 execution.setVariable('prefix', 'CAAIVfModVG_')
43                 execution.setVariable('CAAIVfModVG_vnfId', null)
44                 execution.setVariable('CAAIVfModVG_vfModuleId', null)
45                 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
46                 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
47                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
48                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
49                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
50                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
51         }
52
53         /**
54          * Check for missing elements in the received request.
55          *
56          * @param execution The flow's execution instance.
57          */
58         public void preProcessRequest(DelegateExecution execution) {
59                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
60                         'execution=' + execution.getId() +
61                         ')'
62                 msoLogger.trace('Entered ' + method)
63
64                 try {
65                         def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
66                         msoLogger.debug('Received request xml:\n' + xml)
67                         msoLogger.debug("CreateAAIVfModuleVolume Received Request XML: " + xml)
68                         initProcessVariables(execution)
69
70                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
71                         execution.setVariable('CAAIVfModVG_vnfId', vnfId)
72
73                         def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
74                         execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
75                         
76                         def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
77                         execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
78                         
79                         def cloudOwner = getRequiredNodeText(execution, xml,'cloud-owner')
80                         execution.setVariable('CAAIVfModVG_cloudOwner', cloudOwner)
81                         
82                         def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
83                         execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
84
85                         msoLogger.trace('Exited ' + method)
86                 } catch (BpmnError e) {
87                         throw e;
88                 } catch (Exception e) {
89                         msoLogger.error(e);
90                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
91
92                 }
93         }
94
95         /**
96          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
97          * A 200 response is expected with the VF Module in the response body.
98          *
99          * @param execution The flow's execution instance.
100          */
101         public void getVfModule(DelegateExecution execution) {
102                 def method = getClass().getSimpleName() + '.getVfModule(' +
103                         'execution=' + execution.getId() +
104                         ')'
105                 msoLogger.trace('Entered ' + method)
106
107                 try {
108                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
109                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
110                         try {
111                                 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
112                                 Optional<org.onap.aai.domain.yang.VfModule> vfModule = getAAIClient().get(org.onap.aai.domain.yang.VfModule.class, resourceUri)
113                                 if(vfModule.isPresent()){
114                                         execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 200)
115                                         execution.setVariable('CAAIVfModVG_getVfModuleResponse', vfModule.get())
116                                 }else{
117                                         execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 404)
118                                         execution.setVariable('CAAIVfModVG_getVfModuleResponse', "VF-Module Not found!!")
119                                 }
120                         }catch (Exception ex) {
121                                 ex.printStackTrace()
122                                 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
123                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
124                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
125                         }
126                         msoLogger.trace('Exited ' + method)
127                 } catch (BpmnError e) {
128                         throw e;
129                 } catch (Exception e) {
130                         msoLogger.error(e);
131                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
132                 }
133         }
134
135         /**
136          * Construct and send a PUT request to AAI to update the VF Module with the
137          * created Volume Group relationship.
138          *
139          * @param execution The flow's execution instance.
140          */
141         public void updateVfModule(DelegateExecution execution) {
142                 def method = getClass().getSimpleName() + '.updateVfModule(' +
143                         'execution=' + execution.getId() +
144                         ')'
145                 msoLogger.trace('Entered ' + method)
146
147                 try {
148                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
149                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
150                         org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
151
152                         // Confirm resource-version is in retrieved VF Module
153                         if (vfModule.getResourceVersion() == null) {
154                                 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
155                                 msoLogger.error( msg);
156                                 throw new Exception(msg)
157                         }
158                                                 
159                         // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
160                         def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
161                         def cloudOwner = execution.getVariable('CAAIVfModVG_cloudOwner')
162                         def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
163
164                         try {
165                                 AAIResourceUri vfModuleUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId,vfModuleId);
166                                 AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwner, aicCloudRegion,volumeGroupId);
167                                 msoLogger.debug("Creating relationship between Vf Module: " + vfModuleUri.build().toString() + " and Volume Group: " + volumeGroupUri.build().toString())
168                                 getAAIClient().connect(vfModuleUri,volumeGroupUri)
169                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 200)
170                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', "Success")
171                                 msoLogger.debug("CreateAAIVfModule Response code: " + 200)
172                                 msoLogger.debug("CreateAAIVfModule Response: " + "Success")
173                         } catch (Exception ex) {
174                                 ex.printStackTrace()
175                                 msoLogger.debug('Exception occurred while executing AAI PUT:' + ex.getMessage())
176                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
177                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
178                         }
179                         msoLogger.trace('Exited ' + method)
180                 } catch (BpmnError e) {
181                         throw e;
182                 } catch (Exception e) {
183                         msoLogger.error(e);
184                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
185                 }
186         }
187                 
188         /**
189          * Find and return the value of the Volume Group ID for the specified VF Module.  If
190          * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
191          * 
192          * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
193          * @return the value of the Volume Group ID for the specified VF Module.  If the
194          * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
195          */
196         private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
197                 def Node currVolumeGroupRelationshipNode = null
198                 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
199                 for (Node relationshipNode in relationships) {
200                         def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
201                         if ((relatedTo != null) && relatedTo.equals('volume-group')) {
202                                 currVolumeGroupRelationshipNode = relationshipNode
203                         }
204                 }
205                 return currVolumeGroupRelationshipNode
206         }
207
208         /**
209          * Generates a WorkflowException if the AAI query returns a response code other than 200.
210          *
211          * @param execution The flow's execution instance.
212          */
213         public void handleAAIQueryFailure(DelegateExecution execution) {
214                 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
215                         'execution=' + execution.getId() +
216                         ')'
217                 msoLogger.trace('Entered ' + method)
218                 msoLogger.error( 'Error occurred attempting to query AAI, Response Code ' + execution.getVariable('CAAIVfModVG_getVfModuleResponseCode'));              
219                 ExceptionUtil exceptionUtil = new ExceptionUtil()
220                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
221
222                 msoLogger.trace('Exited ' + method)
223         }
224
225         /**
226          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
227          *
228          * @param execution The flow's execution instance.
229          */
230         public void handleUpdateVfModuleFailure(DelegateExecution execution) {
231                 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
232                         'execution=' + execution.getId() +
233                         ')'
234                 msoLogger.trace('Entered ' + method)
235
236                 msoLogger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode'));
237                 ExceptionUtil exceptionUtil = new ExceptionUtil()
238                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
239
240                 msoLogger.trace('Exited ' + method)
241         }
242 }