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.client.aai.AAIObjectType
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory
36 import org.onap.so.client.graphinventory.entities.uri.Depth
37 import org.onap.so.rest.APIResponse;
38 import org.onap.so.rest.RESTClient
39 import org.onap.so.rest.RESTConfig
40 import org.onap.so.logger.MessageEnum
41 import org.onap.so.logger.MsoLogger
43 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
44 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenerateVfModuleName.class);
47 ExceptionUtil exceptionUtil = new ExceptionUtil()
51 public void preProcessRequest(DelegateExecution execution) {
53 def vnfId = execution.getVariable("vnfId")
54 msoLogger.debug("vnfId is " + vnfId)
55 def vnfName = execution.getVariable("vnfName")
56 msoLogger.debug("vnfName is " + vnfName)
57 def vfModuleLabel = execution.getVariable("vfModuleLabel")
58 msoLogger.debug("vfModuleLabel is " + vfModuleLabel)
59 def personaModelId = execution.getVariable("personaModelId")
60 msoLogger.debug("personaModelId is " + personaModelId)
61 execution.setVariable("GVFMN_vfModuleXml", "")
65 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
70 public void queryAAI(DelegateExecution execution) {
71 def method = getClass().getSimpleName() + '.queryAAI(' +
72 'execution=' + execution.getId() +
74 msoLogger.trace('Entered ' + method)
77 def vnfId = execution.getVariable('vnfId')
78 def personaModelId = execution.getVariable('personaModelId')
80 AaiUtil aaiUtil = new AaiUtil(this)
81 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
83 String endPoint = aaiUtil.createAaiUri(uri)
85 msoLogger.debug("AAI endPoint: " + endPoint)
88 RESTConfig config = new RESTConfig(endPoint);
90 def aaiRequestId = UUID.randomUUID().toString()
91 RESTClient client = new RESTClient(config).
92 addHeader('X-TransactionId', aaiRequestId).
93 addHeader('X-FromAppId', 'MSO').
94 addHeader('Content-Type', 'application/xml').
95 addHeader('Accept','application/xml');
96 msoLogger.debug('sending GET to AAI endpoint \'' + endPoint + '\'')
97 APIResponse response = client.httpGet()
98 msoLogger.debug("GenerateVfModuleName - invoking httpGet() to AAI")
100 responseData = response.getResponseBodyAsString()
101 if (responseData != null) {
102 msoLogger.debug("Received generic VNF data: " + responseData)
106 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
107 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatusCode())
109 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatusCode())
110 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
111 msoLogger.debug('Response code:' + response.getStatusCode())
112 msoLogger.debug('Response:' + System.lineSeparator() + responseData)
113 if (response.getStatusCode() == 200) {
114 // Set the VfModuleXML
115 if (responseData != null) {
116 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
117 if (vfModulesText == null || vfModulesText.isEmpty()) {
118 msoLogger.debug("There are no VF modules in this VNF yet")
119 execution.setVariable("GVFMN_vfModuleXml", null)
122 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
123 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
124 int vfModulesSize = 0
125 if (vfModules != null) {
126 vfModulesSize = vfModules.size()
128 String matchingVfModules = "<vfModules>"
129 for (i in 0..vfModulesSize-1) {
130 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
131 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "model-invariant-id")
132 if (!personaModelIdFromAAI) {
133 // check old attribute name
134 personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
136 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
137 matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
140 matchingVfModules = matchingVfModules + "</vfModules>"
141 msoLogger.debug("Matching VF Modules: " + matchingVfModules)
142 execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
146 } catch (Exception ex) {
148 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
149 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
151 msoLogger.trace('Exited ' + method)
152 } catch (BpmnError e) {
154 } catch (Exception e) {
155 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
156 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
161 public void generateName (DelegateExecution execution) {
162 def method = getClass().getSimpleName() + '.generateName() ' +
163 'execution=' + execution.getId() +
165 msoLogger.trace('Entered ' + method)
167 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
169 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)
170 msoLogger.debug("moduleIndex is: " + moduleIndex)
171 def vnfName = execution.getVariable("vnfName")
172 def vfModuleLabel = execution.getVariable("vfModuleLabel")
173 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
174 msoLogger.debug("vfModuleName is: " + vfModuleName)
175 execution.setVariable("vfModuleName", vfModuleName)