Merge "Remove Swagger UI"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / PrepareUpdateAAIVfModule.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 javax.ws.rs.NotFoundException
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution
26 import org.camunda.bpm.model.dmn.instance.OrganizationUnit
27 import org.hibernate.engine.jdbc.Size.LobMultiplier
28 import org.onap.so.bpmn.core.WorkflowException
29 import org.onap.aai.domain.yang.GenericVnf
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.onap.so.client.aai.AAIObjectType
32 import org.onap.so.client.aai.AAIResourcesClient
33 import org.onap.so.client.aai.entities.AAIResultWrapper
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory
36 import org.onap.so.client.graphinventory.entities.uri.Depth
37 import org.springframework.web.util.UriUtils
38 import org.onap.so.logger.MessageEnum
39 import org.onap.so.logger.MsoLogger
40
41
42
43 public class PrepareUpdateAAIVfModule extends VfModuleBase {
44         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, PrepareUpdateAAIVfModule.class);
45
46
47         ExceptionUtil exceptionUtil = new ExceptionUtil()
48         private MsoUtils utils = new MsoUtils()
49         /**
50          * Initialize the flow's variables.
51          *
52          * @param execution The flow's execution instance.
53          */
54         public void initProcessVariables(DelegateExecution execution) {
55                 execution.setVariable('prefix', 'PUAAIVfMod_')
56                 execution.setVariable('PUAAIVfMod_vnfId', null)
57                 execution.setVariable('PUAAIVfMod_vfModuleId', null)
58                 execution.setVariable('PUAAIVfMod_vnfName', null)
59                 execution.setVariable('PUAAIVfMod_orchestrationStatus', null)
60                 execution.setVariable('PUAAIVfMod_vfModule', null)
61                 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
62                 execution.setVariable('PUAAIVfMod_vfModuleValidationError', null)
63                 execution.setVariable('PUAAIVfMod_getVnfResponseCode' ,null)
64                 execution.setVariable('PUAAIVfMod_getVnfResponse', '')
65                 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', null)
66                 execution.setVariable('PUAAIVfMod_updateVfModuleResponse', '')
67                 execution.setVariable('PUAAIVfMod_outVfModule', null)
68         }
69
70         /**
71          * Check for missing elements in the received request.
72          *
73          * @param execution The flow's execution instance.
74          */
75         public void preProcessRequest(DelegateExecution execution) {
76                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
77                         'execution=' + execution.getId() +
78                         ')'
79                 msoLogger.trace('Entered ' + method)
80
81                 try {
82                         def xml = execution.getVariable('PrepareUpdateAAIVfModuleRequest')
83                         msoLogger.debug('Received request xml:\n' + xml)
84                         msoLogger.debug("PrepareUpdateAAIVfModule Request  : " + xml)
85
86                         initProcessVariables(execution)
87
88                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
89                         execution.setVariable('PUAAIVfMod_vnfId', vnfId)
90
91                         def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
92                         execution.setVariable('PUAAIVfMod_vfModuleId', vfModuleId)
93
94                         def orchestrationStatus = getRequiredNodeText(execution, xml,'orchestration-status')
95                         execution.setVariable('PUAAIVfMod_orchestrationStatus', orchestrationStatus)
96
97                         msoLogger.trace('Exited ' + method)
98                 } catch (BpmnError e) {
99                         throw e;
100                 } catch (Exception e) {
101                         msoLogger.error(e)
102                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
103                 }
104         }
105
106         /**
107          * Using the received vnfId, query AAI to get the corresponding Generic VNF.
108          * A 200 response is expected with the Generic VNF in the response body.
109          *
110          * @param execution The flow's execution instance.
111          */
112         public void getGenericVnf(DelegateExecution execution) {
113                 def method = getClass().getSimpleName() + '.getGenericVnf(' +
114                         'execution=' + execution.getId() +
115                         ')'
116                 msoLogger.trace('Entered ' + method)
117
118                 try {
119                         def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
120
121
122                         try {
123                                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
124                                 AAIResourcesClient resourceClient = new AAIResourcesClient()
125                                 AAIResultWrapper wrapper = resourceClient.get(uri.depth(Depth.ONE), NotFoundException.class)
126                                 GenericVnf responseData = wrapper.asBean(GenericVnf.class).get()
127
128                                 execution.setVariable('PUAAIVfMod_getVnfResponse', responseData)
129                                 execution.setVariable('PUAAIVfMod_getVnfResponseCode', 200)
130
131                         } catch (Exception ex) {
132                                 msoLogger.error(ex);
133                                 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
134                                 execution.setVariable('PUAAIVfMod_getVnfResponseCode', 500)
135                                 execution.setVariable('PUAAIVfMod_getVnfResponse', 'AAI GET Failed:' + ex.getMessage())
136                         }
137                         msoLogger.trace('Exited ' + method)
138                 } catch (BpmnError e) {
139                         throw e;
140                 } catch (Exception e) {
141                         msoLogger.error(e)
142                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getGenericVnf(): ' + e.getMessage())
143                 }
144         }
145
146         /**
147          * Validate the VF Module.  That is, confirm that a VF Module with the input VF Module ID
148          * exists in the retrieved Generic VNF.  Then, check to make sure that if that VF Module
149          * is the base VF Module and it's not the only VF Module for this Generic VNF, that we're not
150          * attempting to delete it.
151          *
152          * @param execution The flow's execution instance.
153          */
154         public void validateVfModule(DelegateExecution execution) {
155                 def method = getClass().getSimpleName() + '.validateVfModule(' +
156                         'execution=' + execution.getId() +
157                         ')'
158                 msoLogger.trace('Entered ' + method)
159
160                 try {
161                         GenericVnf genericVnf = execution.getVariable('PUAAIVfMod_getVnfResponse')
162                         def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
163                         def vfModuleId = execution.getVariable('PUAAIVfMod_vfModuleId')
164                         def vnfName = genericVnf.getVnfName()
165                         execution.setVariable('PUAAIVfMod_vnfName', vnfName)
166
167                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
168                         AAIResourcesClient resourceClient = new AAIResourcesClient()
169
170
171
172                 //      def VfModule vfModule = findVfModule(genericVnf, vfModuleId)
173                         if (!resourceClient.exists(uri)) {
174                                 def String msg = 'VF Module \'' + vfModuleId + '\' does not exist in Generic VNF \'' + vnfId + '\''
175                                 execution.setVariable('PUAAIVfMod_vfModuleValidationError', msg)
176                                 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
177                         } else {
178                                 AAIResultWrapper wrapper = resourceClient.get(uri, NotFoundException.class)
179                                 org.onap.aai.domain.yang.VfModule vfModule = wrapper.asBean(org.onap.aai.domain.yang.VfModule.class).get()
180
181                                 def orchestrationStatus = execution.getVariable('PUAAIVfMod_orchestrationStatus')
182                                 if (vfModule.isBaseVfModule && genericVnf.getVfModules().getVfModule().size() > 1 && vfModule.getOrchestrationStatus().equals('pending-delete')) {
183                                         def String msg = 'Orchestration status for VF Module \'' + vfModuleId +
184                                                 '\' cannot be set to \'pending-delete\' since it is the base VF Module and it\'s not the only VF Module in Generic VNF \'' + vnfId + '\''
185                                         execution.setVariable('PUAAIVfMod_vfModuleValidationError', msg)
186                                         execution.setVariable('PUAAIVfMod_vfModuleOK', false)
187                                 } else {
188                                         execution.setVariable('PUAAIVfMod_vfModule', vfModule)
189                                         execution.setVariable('PUAAIVfMod_vfModuleOK', true)
190                                 }
191                         }
192
193                         msoLogger.trace('Exited ' + method)
194                 } catch (BpmnError e) {
195                         throw e;
196                 } catch (Exception e) {
197                         msoLogger.error(e)
198                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in validateVfModule(): ' + e.getMessage())
199                 }
200         }
201
202         /**
203          * Construct and send a PATCH request to AAI to update the VF Module.
204          *
205          * @param execution The flow's execution instance.
206          */
207         public void updateVfModule(DelegateExecution execution) {
208                 def method = getClass().getSimpleName() + '.updateVfModule(' +
209                         'execution=' + execution.getId() +
210                         ')'
211                 msoLogger.trace('Entered ' + method)
212
213                 try {
214                         def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
215                         def vfModuleId = execution.getVariable('PUAAIVfMod_vfModuleId')
216                         def orchestrationStatus = execution.getVariable('PUAAIVfMod_orchestrationStatus')
217
218                         org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('PUAAIVfMod_vfModule')
219
220                         vfModule.setVfModuleId(vfModuleId)
221                         vfModule.setOrchestrationStatus(orchestrationStatus)
222
223                         AAIResourcesClient client = new AAIResourcesClient()
224                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
225                         client.update(uri, vfModule)
226                         execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 200)
227                         // Set the output for this flow.  The updated VfModule is an output, the generic VNF name, and for
228                         // backward compatibilty, the heat-stack-id is an output
229                         execution.setVariable('PUAAIVfMod_outVfModule', vfModule)
230                         def vnfName = execution.getVariable('PUAAIVfMod_vnfName')
231                         msoLogger.debug('Output PUAAIVfMod_vnfName set to ' + vnfName)
232                         // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
233                         execution.setVariable('WorkflowResponse', vfModule)
234
235                         def heatStackId = vfModule.getHeatStackId()
236                         execution.setVariable('PUAAIVfMod_heatStackId', heatStackId)
237                         msoLogger.debug('Output PUAAIVfMod_heatStackId set to \'' + heatStackId + '\'')
238
239                         msoLogger.trace('Exited ' + method)
240                 } catch (BpmnError e) {
241                         execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 500)
242                         throw e;
243                 } catch (Exception e) {
244                         msoLogger.error(e)
245                         execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 500)
246                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
247                 }
248         }
249
250         /**
251          * Generates a WorkflowException if the AAI query returns a response code other than 200.
252          *
253          * @param execution The flow's execution instance.
254          */
255         public void handleVnfNotFound(DelegateExecution execution) {
256                 def method = getClass().getSimpleName() + '.handleVnfNotFound(' +
257                         'execution=' + execution.getId() +
258                         ')'
259                 msoLogger.trace('Entered ' + method)
260
261                 msoLogger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('PUAAIVfMod_getVnfResponseCode'));
262                 String processKey = getProcessKey(execution);
263                 WorkflowException exception = new WorkflowException(processKey, 5000,
264                         execution.getVariable('PUAAIVfMod_getVnfResponse'))
265                 execution.setVariable('WorkflowException', exception)
266
267                 msoLogger.trace('Exited ' + method)
268         }
269
270         /**
271          * Generates a WorkflowException if the VF Module does not pass validation.
272          *
273          * @param execution The flow's execution instance.
274          */
275         public void handleVfModuleValidationError(DelegateExecution execution) {
276                 def method = getClass().getSimpleName() + '.handleVfModuleValidationError(' +
277                         'execution=' + execution.getId() +
278                         ')'
279                 msoLogger.trace('Entered ' + method)
280
281                 def String errorMsg = 'VF Module validation error: ' + execution.getVariable('PUAAIVfMod_vfModuleValidationError')
282                 msoLogger.error(errorMsg);
283                 msoLogger.debug("PrepareUpdateAAIVfModule: Error Message : " + errorMsg)
284
285                 String processKey = getProcessKey(execution);
286                 WorkflowException exception = new WorkflowException(processKey, 5000, errorMsg)
287                 execution.setVariable('WorkflowException', exception)
288
289                 msoLogger.trace('Exited ' + method)
290         }
291
292         /**
293          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
294          *
295          * @param execution The flow's execution instance.
296          */
297         public void handleUpdateVfModuleFailure(DelegateExecution execution) {
298                 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
299                         'execution=' + execution.getId() +
300                         ')'
301                 msoLogger.trace('Entered ' + method)
302
303                 msoLogger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('PUAAIVfMod_updateVfModuleResponseCode'));
304                 String processKey = getProcessKey(execution);
305                 WorkflowException exception = new WorkflowException(processKey, 5000,
306                         execution.getVariable('PUAAIVfMod_updateVfModuleResponse'))
307                 execution.setVariable('WorkflowException', exception)
308
309                 msoLogger.trace('Exited ' + method)
310         }
311 }