2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
20 package org.openecomp.mso.bpmn.common.scripts
\r
21 import java.io.Serializable;
\r
23 import org.camunda.bpm.engine.delegate.BpmnError
\r
24 import org.camunda.bpm.engine.delegate.DelegateExecution
\r
25 import org.springframework.web.util.UriUtils
\r
27 import org.openecomp.mso.bpmn.core.json.JsonUtils
\r
28 import org.openecomp.mso.bpmn.core.WorkflowException
\r
29 import org.openecomp.mso.rest.APIResponse;
\r
30 import org.openecomp.mso.rest.RESTClient
\r
31 import org.openecomp.mso.rest.RESTConfig
\r
33 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
\r
36 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
40 public void preProcessRequest(DelegateExecution execution) {
\r
41 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
44 def vnfId = execution.getVariable("vnfId")
\r
45 utils.log("DEBUG", "vnfId is " + vnfId, isDebugEnabled)
\r
46 def vnfName = execution.getVariable("vnfName")
\r
47 utils.log("DEBUG", "vnfName is " + vnfName, isDebugEnabled)
\r
48 def vfModuleLabel = execution.getVariable("vfModuleLabel")
\r
49 utils.log("DEBUG", "vfModuleLabel is " + vfModuleLabel, isDebugEnabled)
\r
50 def personaModelId = execution.getVariable("personaModelId")
\r
51 utils.log("DEBUG", "personaModelId is " + personaModelId, isDebugEnabled)
\r
52 execution.setVariable("GVFMN_vfModuleXml", "")
\r
53 }catch(BpmnError b){
\r
55 }catch(Exception e){
\r
56 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
\r
61 public void queryAAI(DelegateExecution execution) {
\r
62 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
\r
63 def method = getClass().getSimpleName() + '.queryAAI(' +
\r
64 'execution=' + execution.getId() +
\r
66 logDebug('Entered ' + method, isDebugLogEnabled)
\r
69 def vnfId = execution.getVariable('vnfId')
\r
70 def personaModelId = execution.getVariable('personaModelId')
\r
72 AaiUtil aaiUriUtil = new AaiUtil(this)
\r
73 String aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
\r
74 logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
\r
76 String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
\r
77 utils.logAudit("AAI endPoint: " + endPoint)
\r
80 RESTConfig config = new RESTConfig(endPoint);
\r
81 def responseData = ''
\r
82 def aaiRequestId = UUID.randomUUID().toString()
\r
83 RESTClient client = new RESTClient(config).
\r
84 addHeader('X-TransactionId', aaiRequestId).
\r
85 addHeader('X-FromAppId', 'MSO').
\r
86 addHeader('Content-Type', 'application/xml').
\r
87 addHeader('Accept','application/xml');
\r
88 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
\r
89 APIResponse response = client.httpGet()
\r
90 utils.logAudit("GenerateVfModuleName - invoking httpGet() to AAI")
\r
92 responseData = response.getResponseBodyAsString()
\r
93 if (responseData != null) {
\r
94 logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
\r
98 utils.logAudit("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
\r
99 utils.logAudit("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatusCode())
\r
101 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatusCode())
\r
102 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
\r
103 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
\r
104 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
\r
105 if (response.getStatusCode() == 200) {
\r
106 // Set the VfModuleXML
\r
107 if (responseData != null) {
\r
108 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
\r
109 if (vfModulesText == null || vfModulesText.isEmpty()) {
\r
110 logDebug("There are no VF modules in this VNF yet", isDebugLogEnabled)
\r
111 execution.setVariable("GVFMN_vfModuleXml", null)
\r
114 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
\r
115 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
\r
116 int vfModulesSize = 0
\r
117 if (vfModules != null) {
\r
118 vfModulesSize = vfModules.size()
\r
120 String matchingVfModules = "<vfModules>"
\r
121 for (i in 0..vfModulesSize-1) {
\r
122 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
\r
123 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "model-invariant-id")
\r
124 if (!personaModelIdFromAAI) {
\r
125 // check old attribute name
\r
126 personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
\r
128 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
\r
129 matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
\r
132 matchingVfModules = matchingVfModules + "</vfModules>"
\r
133 logDebug("Matching VF Modules: " + matchingVfModules, isDebugLogEnabled)
\r
134 execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
\r
138 } catch (Exception ex) {
\r
139 ex.printStackTrace()
\r
140 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
\r
141 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
\r
143 logDebug('Exited ' + method, isDebugLogEnabled)
\r
144 } catch (BpmnError e) {
\r
146 } catch (Exception e) {
\r
147 logError('Caught exception in ' + method, e)
\r
148 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
\r
153 public void generateName (DelegateExecution execution) {
\r
154 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
\r
155 def method = getClass().getSimpleName() + '.generateName() ' +
\r
156 'execution=' + execution.getId() +
\r
158 logDebug('Entered ' + method, isDebugLogEnabled)
\r
160 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
\r
162 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)
\r
163 logDebug("moduleIndex is: " + moduleIndex, isDebugLogEnabled)
\r
164 def vnfName = execution.getVariable("vnfName")
\r
165 def vfModuleLabel = execution.getVariable("vfModuleLabel")
\r
166 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
\r
167 logDebug("vfModuleName is: " + vfModuleName, isDebugLogEnabled)
\r
168 execution.setVariable("vfModuleName", vfModuleName)
\r