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