2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.common.scripts
25 import javax.ws.rs.NotFoundException
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.onap.so.bpmn.core.WorkflowException
30 import org.onap.so.client.aai.AAIObjectType
31 import org.onap.so.client.aai.entities.uri.AAIResourceUri
32 import org.onap.so.client.aai.entities.uri.AAIUriFactory
33 import org.onap.so.logger.MsoLogger
34 import org.slf4j.Logger
35 import org.slf4j.LoggerFactory
38 public class UpdateAAIVfModule extends AbstractServiceTaskProcessor {
39 private static final Logger logger = LoggerFactory.getLogger( UpdateAAIVfModule.class);
42 private XmlParser xmlParser = new XmlParser()
43 ExceptionUtil exceptionUtil = new ExceptionUtil()
46 * Initialize the flow's variables.
48 * @param execution The flow's execution instance.
50 public void initProcessVariables(DelegateExecution execution) {
51 execution.setVariable('prefix', 'UAAIVfMod_')
52 execution.setVariable('UAAIVfMod_vnfId', null)
53 execution.setVariable('UAAIVfMod_vfModuleId', null)
54 execution.setVariable('UAAIVfMod_orchestrationStatus', null)
55 execution.setVariable('UAAIVfMod_heatStackId', null)
56 execution.setVariable('UAAIVfMod_volumeGroupId', null)
57 execution.setVariable('UAAIVfMod_getVfModuleResponseCode' ,null)
58 execution.setVariable('UAAIVfMod_getVfModuleResponse', '')
59 execution.setVariable('UAAIVfMod_updateVfModuleResponseCode', null)
60 execution.setVariable('UAAIVfMod_updateVfModuleResponse', '')
64 * Check for missing elements in the received request.
66 * @param execution The flow's execution instance.
68 public void preProcessRequest(DelegateExecution execution) {
69 def method = getClass().getSimpleName() + '.preProcessRequest(' +
70 'execution=' + execution.getId() +
72 logger.trace('Entered ' + method)
75 def xml = execution.getVariable('UpdateAAIVfModuleRequest')
76 logger.debug('Received request xml:\n' + xml)
77 initProcessVariables(execution)
79 def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
80 execution.setVariable('UAAIVfMod_vnfId', vnfId)
82 def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
83 execution.setVariable('UAAIVfMod_vfModuleId', vfModuleId)
85 logger.trace('Exited ' + method)
86 } catch (BpmnError e) {
88 } catch (Exception e) {
90 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
95 * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
96 * A 200 response is expected with the VF Module in the response body.
98 * @param execution The flow's execution instance.
100 public void getVfModule(DelegateExecution execution) {
101 def method = getClass().getSimpleName() + '.getVfModule(' +
102 'execution=' + execution.getId() +
104 logger.trace('Entered ' + method)
107 def vnfId = execution.getVariable('UAAIVfMod_vnfId')
108 def vfModuleId = execution.getVariable('UAAIVfMod_vfModuleId')
110 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
111 Optional<org.onap.aai.domain.yang.VfModule> vfModule = getAAIClient().get(org.onap.aai.domain.yang.VfModule.class, resourceUri)
112 if (vfModule.isPresent()) {
113 execution.setVariable('UAAIVfMod_getVfModuleResponseCode', 200)
114 execution.setVariable('UAAIVfMod_getVfModuleResponse', vfModule.get())
116 execution.setVariable('UAAIVfMod_getVfModuleResponseCode', 404)
117 execution.setVariable('UAAIVfMod_getVfModuleResponse', "VF Module not found in AAI")
119 } catch (Exception ex) {
120 logger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
121 execution.setVariable('UAAIVfMod_getVfModuleResponseCode', 500)
122 execution.setVariable('UAAIVfMod_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
124 } catch (BpmnError e) {
126 } catch (Exception e) {
128 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
133 * Construct and send a PATCH request to AAI to update the VF Module.
135 * @param execution The flow's execution instance.
137 public void updateVfModule(DelegateExecution execution) {
138 def method = getClass().getSimpleName() + '.updateVfModule(' +
139 'execution=' + execution.getId() +
141 logger.trace('Entered ' + method)
144 def vnfId = execution.getVariable('UAAIVfMod_vnfId')
145 def vfModuleId = execution.getVariable('UAAIVfMod_vfModuleId')
146 org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('UAAIVfMod_getVfModuleResponse')
147 def origRequest = execution.getVariable('UpdateAAIVfModuleRequest')
149 logger.debug("UpdateAAIVfModule request: " + origRequest)
150 // Handle persona-model-id/persona-model-version
151 def boolean doPersonaModelVersion = true
152 def String newPersonaModelId = utils.getNodeText(origRequest, 'persona-model-id')
153 def String newPersonaModelVersion = utils.getNodeText(origRequest, 'persona-model-version')
154 if ((newPersonaModelId == null) || (newPersonaModelVersion == null)) {
155 doPersonaModelVersion = false
157 // Confirm "new" persona-model-id is same as "current" persona-model-id
158 def String currPersonaModelId = vfModule.getModelInvariantId()
159 if (currPersonaModelId == null) {
160 // check the old attribute name
161 currPersonaModelId = vfModule.getModelVersionId()
163 if (currPersonaModelId == null) {
164 currPersonaModelId = ''
166 if (!newPersonaModelId.equals(currPersonaModelId)) {
167 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since there is \'persona-model-id\' mismatch between the current and new values'
169 throw new Exception(msg)
174 String orchestrationStatusEntry = updateVfModuleNode(origRequest , 'orchestration-status')
175 String heatStackIdEntry = updateVfModuleNode(origRequest, 'heat-stack-id')
176 String personaModelVersionEntry = ""
177 if (doPersonaModelVersion) {
178 personaModelVersionEntry = updateVfModuleNode(origRequest, 'persona-model-version')
180 String contrailServiceInstanceFqdnEntry = updateVfModuleNode(origRequest, 'contrail-service-instance-fqdn')
181 org.onap.aai.domain.yang.VfModule payload = new org.onap.aai.domain.yang.VfModule();
182 payload.setVfModuleId(vfModuleId)
183 payload.setOrchestrationStatus(orchestrationStatusEntry)
184 payload.setHeatStackId(heatStackIdEntry)
185 payload.setPersonaModelVersion(personaModelVersionEntry)
186 payload.setContrailServiceInstanceFqdn(contrailServiceInstanceFqdnEntry)
189 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
190 getAAIClient().update(resourceUri, payload)
191 execution.setVariable('UAAIVfMod_updateVfModuleResponseCode', 200)
192 execution.setVariable('UAAIVfMod_updateVfModuleResponse', "Success")
193 }catch(NotFoundException ignored){
194 logger.debug("VF-Module not found!!")
195 execution.setVariable('UAAIVfMod_updateVfModuleResponseCode', 404)
196 execution.setVariable('UAAIVfMod_updateVfModuleResponse', ignored.getMessage())
197 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "vf-module " + vfModuleId + " not found for under vnf " + vnfId + " in A&AI!")
200 execution.setVariable('UAAIVfMod_updateVfModuleResponseCode', 500)
201 execution.setVariable('UAAIVfMod_updateVfModuleResponse', 'AAI PATCH Failed:' + ex.getMessage())
202 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, 'Exception occurred while executing AAI PATCH:' + ex.getMessage())
204 } catch (BpmnError e) {
206 } catch (Exception e) {
208 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
213 * Sets up json attributes for PATCH request for Update
215 * @param origRequest Incoming update request with VF Module elements to be updated.
216 * @param element Name of element to be inserted.
218 private String updateVfModuleNode(String origRequest, String elementName) {
220 if (!utils.nodeExists(origRequest, elementName)) {
223 def elementValue = utils.getNodeText(origRequest, elementName)
225 if (elementValue.equals('DELETE')) {
226 // Set the element being deleted to empty string
235 * Generates a WorkflowException if the AAI query returns a response code other than 200.
237 * @param execution The flow's execution instance.
239 public void handleAAIQueryFailure(DelegateExecution execution) {
240 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
241 'execution=' + execution.getId() +
243 logger.trace('Entered ' + method)
245 logger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('UAAIVfMod_getVfModuleResponseCode'))
246 String processKey = getProcessKey(execution);
247 WorkflowException exception = new WorkflowException(processKey, 5000,
248 execution.getVariable('UAAIVfMod_getVfModuleResponse'))
249 execution.setVariable('WorkflowException', exception)
250 logger.debug("UpdateAAIVfModule query failure: " + exception.getErrorMessage())
251 logger.trace('Exited ' + method)