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 org.onap.so.bpmn.core.UrnPropertiesReader
26 import org.onap.so.client.HttpClientFactory
27 import org.onap.so.logger.ErrorCode
29 import java.io.Serializable;
31 import javax.ws.rs.core.MediaType
32 import javax.ws.rs.core.Response
33 import org.camunda.bpm.engine.delegate.BpmnError
34 import org.camunda.bpm.engine.delegate.DelegateExecution
35 import org.springframework.web.util.UriUtils
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.bpmn.core.WorkflowException
39 import org.onap.so.client.HttpClient
40 import org.onap.so.client.aai.AAIObjectType
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory
43 import org.onap.so.client.graphinventory.entities.uri.Depth
44 import org.onap.so.utils.TargetEntity
45 import org.onap.so.logger.MessageEnum
46 import org.slf4j.Logger
47 import org.slf4j.LoggerFactory
49 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
50 private static final Logger logger = LoggerFactory.getLogger( GenerateVfModuleName.class);
53 ExceptionUtil exceptionUtil = new ExceptionUtil()
57 public void preProcessRequest(DelegateExecution execution) {
59 def vnfId = execution.getVariable("vnfId")
60 logger.debug("vnfId is " + vnfId)
61 def vnfName = execution.getVariable("vnfName")
62 logger.debug("vnfName is " + vnfName)
63 def vfModuleLabel = execution.getVariable("vfModuleLabel")
64 logger.debug("vfModuleLabel is " + vfModuleLabel)
65 def personaModelId = execution.getVariable("personaModelId")
66 logger.debug("personaModelId is " + personaModelId)
67 execution.setVariable("GVFMN_vfModuleXml", "")
71 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
76 public void queryAAI(DelegateExecution execution) {
77 def method = getClass().getSimpleName() + '.queryAAI(' +
78 'execution=' + execution.getId() +
80 logger.trace('Entered ' + method)
83 def vnfId = execution.getVariable('vnfId')
84 def personaModelId = execution.getVariable('personaModelId')
86 AaiUtil aaiUtil = new AaiUtil(this)
87 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
89 String endPoint = aaiUtil.createAaiUri(uri)
91 logger.debug("AAI endPoint: " + endPoint)
94 HttpClient client = new HttpClientFactory().newXmlClient(new URL(endPoint), TargetEntity.AAI)
96 client.addAdditionalHeader('X-TransactionId', UUID.randomUUID().toString())
97 client.addAdditionalHeader('X-FromAppId', 'MSO')
98 client.addAdditionalHeader('Content-Type', 'application/xml')
99 client.addAdditionalHeader('Accept','application/xml')
101 def responseData = ''
103 logger.debug('sending GET to AAI endpoint \'' + endPoint + '\'')
104 Response response = client.get()
105 logger.debug("GenerateVfModuleName - invoking httpGet() to AAI")
107 responseData = response.readEntity(String.class)
108 if (responseData != null) {
109 logger.debug("Received generic VNF data: " + responseData)
113 logger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
114 logger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatus())
116 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatus())
117 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
118 logger.debug('Response code:' + response.getStatus())
119 logger.debug('Response:' + System.lineSeparator() + responseData)
120 if (response.getStatus() == 200) {
121 // Set the VfModuleXML
122 if (responseData != null) {
123 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
124 if (vfModulesText == null || vfModulesText.isEmpty()) {
125 logger.debug("There are no VF modules in this VNF yet")
126 execution.setVariable("GVFMN_vfModuleXml", null)
129 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
130 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
131 int vfModulesSize = 0
132 if (vfModules != null) {
133 vfModulesSize = vfModules.size()
135 String matchingVfModules = "<vfModules>"
136 for (i in 0..vfModulesSize-1) {
137 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
138 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "model-invariant-id")
139 if (!personaModelIdFromAAI) {
140 // check old attribute name
141 personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
143 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
144 matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
147 matchingVfModules = matchingVfModules + "</vfModules>"
148 logger.debug("Matching VF Modules: " + matchingVfModules)
149 execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
153 } catch (Exception ex) {
155 logger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
156 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
158 logger.trace('Exited ' + method)
159 } catch (BpmnError e) {
161 } catch (Exception e) {
162 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
163 'Caught exception in ' + method, "BPMN",
164 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
165 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
170 public void generateName (DelegateExecution execution) {
171 def method = getClass().getSimpleName() + '.generateName() ' +
172 'execution=' + execution.getId() +
174 logger.trace('Entered ' + method)
176 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
178 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)
179 logger.debug("moduleIndex is: " + moduleIndex)
180 def vnfName = execution.getVariable("vnfName")
181 def vfModuleLabel = execution.getVariable("vfModuleLabel")
182 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
183 logger.debug("vfModuleName is: " + vfModuleName)
184 execution.setVariable("vfModuleName", vfModuleName)