Merge "Changed the object mapper to ignore unknown fields"
[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.aaiclient.client.aai.AAIObjectType
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
32 import org.slf4j.Logger
33 import org.slf4j.LoggerFactory
34
35 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
36     private static final Logger logger = LoggerFactory.getLogger( CreateAAIVfModuleVolumeGroup.class);
37
38         private XmlParser xmlParser = new XmlParser()
39         ExceptionUtil exceptionUtil = new ExceptionUtil()
40
41         /**
42          * Initialize the flow's variables.
43          *
44          * @param execution The flow's execution instance.
45          */
46         public void initProcessVariables(DelegateExecution execution) {
47                 execution.setVariable('prefix', 'CAAIVfModVG_')
48                 execution.setVariable('CAAIVfModVG_vnfId', null)
49                 execution.setVariable('CAAIVfModVG_vfModuleId', null)
50                 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
51                 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
52                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
53                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
54                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
55                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
56         }
57
58         /**
59          * Check for missing elements in the received request.
60          *
61          * @param execution The flow's execution instance.
62          */
63         public void preProcessRequest(DelegateExecution execution) {
64                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
65                         'execution=' + execution.getId() +
66                         ')'
67                 logger.trace('Entered ' + method)
68
69                 try {
70                         def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
71                         logger.debug('Received request xml:\n' + xml)
72                         logger.debug("CreateAAIVfModuleVolume Received Request XML: " + xml)
73                         initProcessVariables(execution)
74
75                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
76                         execution.setVariable('CAAIVfModVG_vnfId', vnfId)
77
78                         def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
79                         execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
80
81                         def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
82                         execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
83
84                         def cloudOwner = getRequiredNodeText(execution, xml,'cloud-owner')
85                         execution.setVariable('CAAIVfModVG_cloudOwner', cloudOwner)
86
87                         def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
88                         execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
89
90                         logger.trace('Exited ' + method)
91                 } catch (BpmnError e) {
92                         throw e;
93                 } catch (Exception e) {
94                         logger.error(e);
95                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
96
97                 }
98         }
99
100         /**
101          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
102          * A 200 response is expected with the VF Module in the response body.
103          *
104          * @param execution The flow's execution instance.
105          */
106         public void getVfModule(DelegateExecution execution) {
107                 def method = getClass().getSimpleName() + '.getVfModule(' +
108                         'execution=' + execution.getId() +
109                         ')'
110                 logger.trace('Entered ' + method)
111
112                 try {
113                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
114                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
115                         try {
116                                 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId).vfModule(vfModuleId));
117                                 Optional<org.onap.aai.domain.yang.VfModule> vfModule = getAAIClient().get(org.onap.aai.domain.yang.VfModule.class, resourceUri)
118                                 if(vfModule.isPresent()){
119                                         execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 200)
120                                         execution.setVariable('CAAIVfModVG_getVfModuleResponse', vfModule.get())
121                                 }else{
122                                         execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 404)
123                                         execution.setVariable('CAAIVfModVG_getVfModuleResponse', "VF-Module Not found!!")
124                                 }
125                         }catch (Exception ex) {
126                                 logger.debug('Exception occurred while executing AAI GET: {}', ex.getMessage(), ex)
127                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
128                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
129                         }
130                         logger.trace('Exited ' + method)
131                 } catch (BpmnError e) {
132                         throw e;
133                 } catch (Exception e) {
134                         logger.error(e);
135                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
136                 }
137         }
138
139         /**
140          * Construct and send a PUT request to AAI to update the VF Module with the
141          * created Volume Group relationship.
142          *
143          * @param execution The flow's execution instance.
144          */
145         public void updateVfModule(DelegateExecution execution) {
146                 def method = getClass().getSimpleName() + '.updateVfModule(' +
147                         'execution=' + execution.getId() +
148                         ')'
149                 logger.trace('Entered ' + method)
150
151                 try {
152                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
153                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
154                         org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
155
156                         // Confirm resource-version is in retrieved VF Module
157                         if (vfModule.getResourceVersion() == null) {
158                                 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
159                                 logger.error(msg);
160                                 throw new Exception(msg)
161                         }
162
163                         // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
164                         def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
165                         def cloudOwner = execution.getVariable('CAAIVfModVG_cloudOwner')
166                         def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
167
168                         try {
169                                 AAIResourceUri vfModuleUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId).vfModule(vfModuleId));
170                                 AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(cloudOwner, aicCloudRegion).volumeGroup(volumeGroupId));
171                                 logger.debug("Creating relationship between Vf Module: " + vfModuleUri.build().toString() + " and Volume Group: " + volumeGroupUri.build().toString())
172                                 getAAIClient().connect(vfModuleUri,volumeGroupUri)
173                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 200)
174                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', "Success")
175                                 logger.debug("CreateAAIVfModule Response code: " + 200)
176                                 logger.debug("CreateAAIVfModule Response: " + "Success")
177                         } catch (Exception ex) {
178                                 logger.debug('Exception occurred while executing AAI PUT: {}', ex.getMessage(), ex)
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 }