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