2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.common.scripts
23 import org.onap.so.bpmn.core.UrnPropertiesReader
24 import org.onap.so.client.HttpClientFactory
26 import java.io.Serializable;
28 import javax.ws.rs.core.MediaType
29 import javax.ws.rs.core.Response
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.delegate.DelegateExecution
32 import org.springframework.web.util.UriUtils
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.bpmn.core.WorkflowException
36 import org.onap.so.client.HttpClient
37 import org.onap.so.client.aai.AAIObjectType
38 import org.onap.so.client.aai.entities.uri.AAIResourceUri
39 import org.onap.so.client.aai.entities.uri.AAIUriFactory
40 import org.onap.so.client.graphinventory.entities.uri.Depth
41 import org.onap.so.utils.TargetEntity
42 import org.onap.so.logger.MessageEnum
43 import org.onap.so.logger.MsoLogger
45 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
46 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenerateVfModuleName.class);
49 ExceptionUtil exceptionUtil = new ExceptionUtil()
53 public void preProcessRequest(DelegateExecution execution) {
55 def vnfId = execution.getVariable("vnfId")
56 msoLogger.debug("vnfId is " + vnfId)
57 def vnfName = execution.getVariable("vnfName")
58 msoLogger.debug("vnfName is " + vnfName)
59 def vfModuleLabel = execution.getVariable("vfModuleLabel")
60 msoLogger.debug("vfModuleLabel is " + vfModuleLabel)
61 def personaModelId = execution.getVariable("personaModelId")
62 msoLogger.debug("personaModelId is " + personaModelId)
63 execution.setVariable("GVFMN_vfModuleXml", "")
67 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
72 public void queryAAI(DelegateExecution execution) {
73 def method = getClass().getSimpleName() + '.queryAAI(' +
74 'execution=' + execution.getId() +
76 msoLogger.trace('Entered ' + method)
79 def vnfId = execution.getVariable('vnfId')
80 def personaModelId = execution.getVariable('personaModelId')
82 AaiUtil aaiUtil = new AaiUtil(this)
83 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
85 String endPoint = aaiUtil.createAaiUri(uri)
87 msoLogger.debug("AAI endPoint: " + endPoint)
90 HttpClient client = new HttpClientFactory().newXmlClient(new URL(endPoint), TargetEntity.AAI)
92 client.addAdditionalHeader('X-TransactionId', UUID.randomUUID().toString())
93 client.addAdditionalHeader('X-FromAppId', 'MSO')
94 client.addAdditionalHeader('Content-Type', 'application/xml')
95 client.addAdditionalHeader('Accept','application/xml')
99 msoLogger.debug('sending GET to AAI endpoint \'' + endPoint + '\'')
100 Response response = client.get()
101 msoLogger.debug("GenerateVfModuleName - invoking httpGet() to AAI")
103 responseData = response.readEntity(String.class)
104 if (responseData != null) {
105 msoLogger.debug("Received generic VNF data: " + responseData)
109 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
110 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatus())
112 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatus())
113 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
114 msoLogger.debug('Response code:' + response.getStatus())
115 msoLogger.debug('Response:' + System.lineSeparator() + responseData)
116 if (response.getStatus() == 200) {
117 // Set the VfModuleXML
118 if (responseData != null) {
119 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
120 if (vfModulesText == null || vfModulesText.isEmpty()) {
121 msoLogger.debug("There are no VF modules in this VNF yet")
122 execution.setVariable("GVFMN_vfModuleXml", null)
125 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
126 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
127 int vfModulesSize = 0
128 if (vfModules != null) {
129 vfModulesSize = vfModules.size()
131 String matchingVfModules = "<vfModules>"
132 for (i in 0..vfModulesSize-1) {
133 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
134 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "model-invariant-id")
135 if (!personaModelIdFromAAI) {
136 // check old attribute name
137 personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
139 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
140 matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
143 matchingVfModules = matchingVfModules + "</vfModules>"
144 msoLogger.debug("Matching VF Modules: " + matchingVfModules)
145 execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
149 } catch (Exception ex) {
151 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
152 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
154 msoLogger.trace('Exited ' + method)
155 } catch (BpmnError e) {
157 } catch (Exception e) {
158 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
159 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
164 public void generateName (DelegateExecution execution) {
165 def method = getClass().getSimpleName() + '.generateName() ' +
166 'execution=' + execution.getId() +
168 msoLogger.trace('Entered ' + method)
170 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
172 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)
173 msoLogger.debug("moduleIndex is: " + moduleIndex)
174 def vnfName = execution.getVariable("vnfName")
175 def vfModuleLabel = execution.getVariable("vfModuleLabel")
176 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
177 msoLogger.debug("vfModuleName is: " + vfModuleName)
178 execution.setVariable("vfModuleName", vfModuleName)