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
25 import java.io.Serializable;
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.springframework.web.util.UriUtils
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.rest.APIResponse;
34 import org.onap.so.rest.RESTClient
35 import org.onap.so.rest.RESTConfig
36 import org.onap.so.logger.MessageEnum
37 import org.onap.so.logger.MsoLogger
39 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
40 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenerateVfModuleName.class);
43 ExceptionUtil exceptionUtil = new ExceptionUtil()
47 public void preProcessRequest(DelegateExecution execution) {
49 def vnfId = execution.getVariable("vnfId")
50 msoLogger.debug("vnfId is " + vnfId)
51 def vnfName = execution.getVariable("vnfName")
52 msoLogger.debug("vnfName is " + vnfName)
53 def vfModuleLabel = execution.getVariable("vfModuleLabel")
54 msoLogger.debug("vfModuleLabel is " + vfModuleLabel)
55 def personaModelId = execution.getVariable("personaModelId")
56 msoLogger.debug("personaModelId is " + personaModelId)
57 execution.setVariable("GVFMN_vfModuleXml", "")
61 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
66 public void queryAAI(DelegateExecution execution) {
67 def method = getClass().getSimpleName() + '.queryAAI(' +
68 'execution=' + execution.getId() +
70 msoLogger.trace('Entered ' + method)
73 def vnfId = execution.getVariable('vnfId')
74 def personaModelId = execution.getVariable('personaModelId')
76 AaiUtil aaiUriUtil = new AaiUtil(this)
77 String aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
78 msoLogger.debug('AAI URI is: ' + aai_uri)
80 String endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
81 msoLogger.debug("AAI endPoint: " + endPoint)
84 RESTConfig config = new RESTConfig(endPoint);
86 def aaiRequestId = UUID.randomUUID().toString()
87 RESTClient client = new RESTClient(config).
88 addHeader('X-TransactionId', aaiRequestId).
89 addHeader('X-FromAppId', 'MSO').
90 addHeader('Content-Type', 'application/xml').
91 addHeader('Accept','application/xml');
92 msoLogger.debug('sending GET to AAI endpoint \'' + endPoint + '\'')
93 APIResponse response = client.httpGet()
94 msoLogger.debug("GenerateVfModuleName - invoking httpGet() to AAI")
96 responseData = response.getResponseBodyAsString()
97 if (responseData != null) {
98 msoLogger.debug("Received generic VNF data: " + responseData)
102 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
103 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatusCode())
105 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatusCode())
106 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
107 msoLogger.debug('Response code:' + response.getStatusCode())
108 msoLogger.debug('Response:' + System.lineSeparator() + responseData)
109 if (response.getStatusCode() == 200) {
110 // Set the VfModuleXML
111 if (responseData != null) {
112 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
113 if (vfModulesText == null || vfModulesText.isEmpty()) {
114 msoLogger.debug("There are no VF modules in this VNF yet")
115 execution.setVariable("GVFMN_vfModuleXml", null)
118 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
119 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
120 int vfModulesSize = 0
121 if (vfModules != null) {
122 vfModulesSize = vfModules.size()
124 String matchingVfModules = "<vfModules>"
125 for (i in 0..vfModulesSize-1) {
126 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
127 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "model-invariant-id")
128 if (!personaModelIdFromAAI) {
129 // check old attribute name
130 personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
132 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
133 matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
136 matchingVfModules = matchingVfModules + "</vfModules>"
137 msoLogger.debug("Matching VF Modules: " + matchingVfModules)
138 execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
142 } catch (Exception ex) {
144 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
145 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
147 msoLogger.trace('Exited ' + method)
148 } catch (BpmnError e) {
150 } catch (Exception e) {
151 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
152 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
157 public void generateName (DelegateExecution execution) {
158 def method = getClass().getSimpleName() + '.generateName() ' +
159 'execution=' + execution.getId() +
161 msoLogger.trace('Entered ' + method)
163 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
165 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)
166 msoLogger.debug("moduleIndex is: " + moduleIndex)
167 def vnfName = execution.getVariable("vnfName")
168 def vfModuleLabel = execution.getVariable("vfModuleLabel")
169 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
170 msoLogger.debug("vfModuleName is: " + vfModuleName)
171 execution.setVariable("vfModuleName", vfModuleName)