Merge "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                                 logger.debug('Exception occurred while executing AAI GET: {}', ex.getMessage(), ex)
125                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
126                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
127                         }
128                         logger.trace('Exited ' + method)
129                 } catch (BpmnError e) {
130                         throw e;
131                 } catch (Exception e) {
132                         logger.error(e);
133                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
134                 }
135         }
136
137         /**
138          * Construct and send a PUT request to AAI to update the VF Module with the
139          * created Volume Group relationship.
140          *
141          * @param execution The flow's execution instance.
142          */
143         public void updateVfModule(DelegateExecution execution) {
144                 def method = getClass().getSimpleName() + '.updateVfModule(' +
145                         'execution=' + execution.getId() +
146                         ')'
147                 logger.trace('Entered ' + method)
148
149                 try {
150                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
151                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
152                         org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
153
154                         // Confirm resource-version is in retrieved VF Module
155                         if (vfModule.getResourceVersion() == null) {
156                                 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
157                                 logger.error(msg);
158                                 throw new Exception(msg)
159                         }
160
161                         // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
162                         def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
163                         def cloudOwner = execution.getVariable('CAAIVfModVG_cloudOwner')
164                         def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
165
166                         try {
167                                 AAIResourceUri vfModuleUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId,vfModuleId);
168                                 AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwner, aicCloudRegion,volumeGroupId);
169                                 logger.debug("Creating relationship between Vf Module: " + vfModuleUri.build().toString() + " and Volume Group: " + volumeGroupUri.build().toString())
170                                 getAAIClient().connect(vfModuleUri,volumeGroupUri)
171                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 200)
172                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', "Success")
173                                 logger.debug("CreateAAIVfModule Response code: " + 200)
174                                 logger.debug("CreateAAIVfModule Response: " + "Success")
175                         } catch (Exception ex) {
176                                 logger.debug('Exception occurred while executing AAI PUT: {}', ex.getMessage(), ex)
177                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
178                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
179                         }
180                         logger.trace('Exited ' + method)
181                 } catch (BpmnError e) {
182                         throw e;
183                 } catch (Exception e) {
184                         logger.error(e);
185                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
186                 }
187         }
188
189         /**
190          * Find and return the value of the Volume Group ID for the specified VF Module.  If
191          * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
192          *
193          * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
194          * @return the value of the Volume Group ID for the specified VF Module.  If the
195          * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
196          */
197         private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
198                 def Node currVolumeGroupRelationshipNode = null
199                 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
200                 for (Node relationshipNode in relationships) {
201                         def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
202                         if ((relatedTo != null) && relatedTo.equals('volume-group')) {
203                                 currVolumeGroupRelationshipNode = relationshipNode
204                         }
205                 }
206                 return currVolumeGroupRelationshipNode
207         }
208
209         /**
210          * Generates a WorkflowException if the AAI query returns a response code other than 200.
211          *
212          * @param execution The flow's execution instance.
213          */
214         public void handleAAIQueryFailure(DelegateExecution execution) {
215                 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
216                         'execution=' + execution.getId() +
217                         ')'
218                 logger.trace('Entered ' + method)
219                 logger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('CAAIVfModVG_getVfModuleResponseCode'));
220                 ExceptionUtil exceptionUtil = new ExceptionUtil()
221                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
222
223                 logger.trace('Exited ' + method)
224         }
225
226         /**
227          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
228          *
229          * @param execution The flow's execution instance.
230          */
231         public void handleUpdateVfModuleFailure(DelegateExecution execution) {
232                 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
233                         'execution=' + execution.getId() +
234                         ')'
235                 logger.trace('Entered ' + method)
236
237                 logger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode'));
238                 ExceptionUtil exceptionUtil = new ExceptionUtil()
239                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
240
241                 logger.trace('Exited ' + method)
242         }
243 }