[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenerateVfModuleName.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.bpmn.common.scripts
21 import java.io.Serializable;
22
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.runtime.Execution
25 import org.springframework.web.util.UriUtils
26
27 import org.openecomp.mso.bpmn.core.json.JsonUtils
28 import org.openecomp.mso.bpmn.core.WorkflowException
29 import org.openecomp.mso.rest.APIResponse;
30 import org.openecomp.mso.rest.RESTClient
31 import org.openecomp.mso.rest.RESTConfig
32
33 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
34
35         def Prefix="GVFMN_"
36         ExceptionUtil exceptionUtil = new ExceptionUtil()
37
38         
39         
40         public void preProcessRequest(Execution execution) {
41                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
42                 
43                 try {
44                         def vnfId = execution.getVariable("vnfId")
45                         utils.log("DEBUG", "vnfId is " + vnfId, isDebugEnabled)
46                         def vnfName = execution.getVariable("vnfName")
47                         utils.log("DEBUG", "vnfName is " + vnfName, isDebugEnabled)
48                         def vfModuleLabel = execution.getVariable("vfModuleLabel")
49                         utils.log("DEBUG", "vfModuleLabel is " + vfModuleLabel, isDebugEnabled)
50                         def personaModelId = execution.getVariable("personaModelId")
51                         utils.log("DEBUG", "personaModelId is " + personaModelId, isDebugEnabled)
52                         execution.setVariable("GVFMN_vfModuleXml", "")
53                 }catch(BpmnError b){
54                         throw b
55                 }catch(Exception e){
56                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
57                 }
58         }
59
60
61         public void queryAAI(Execution execution) {
62                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
63                 def method = getClass().getSimpleName() + '.queryAAI(' +
64                         'execution=' + execution.getId() +
65                         ')'
66                 logDebug('Entered ' + method, isDebugLogEnabled)
67
68                 try {
69                         def vnfId = execution.getVariable('vnfId')
70                         def personaModelId = execution.getVariable('personaModelId')
71                         
72                         AaiUtil aaiUriUtil = new AaiUtil(this)
73                         String  aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
74                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
75
76                         String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
77                         utils.logAudit("AAI endPoint: " + endPoint)
78
79                         try {
80                                 RESTConfig config = new RESTConfig(endPoint);
81                                 def responseData = ''
82                                 def aaiRequestId = UUID.randomUUID().toString()
83                                 RESTClient client = new RESTClient(config).
84                                         addHeader('X-TransactionId', aaiRequestId).
85                                         addHeader('X-FromAppId', 'MSO').
86                                         addHeader('Content-Type', 'application/xml').
87                                         addHeader('Accept','application/xml');
88                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
89                                 APIResponse response = client.httpGet()
90                                 utils.logAudit("GenerateVfModuleName - invoking httpGet() to AAI")
91
92                                 responseData = response.getResponseBodyAsString()
93                                 if (responseData != null) {
94                                         logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
95
96                                 }
97
98                                 utils.logAudit("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
99                                 utils.logAudit("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatusCode())
100
101                                 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatusCode())
102                                 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
103                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
104                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
105                                 if (response.getStatusCode() == 200) {
106                                         // Set the VfModuleXML                                  
107                                         if (responseData != null) {                                             
108                                                 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
109                                                 if (vfModulesText == null || vfModulesText.isEmpty()) {
110                                                         logDebug("There are no VF modules in this VNF yet", isDebugLogEnabled)
111                                                         execution.setVariable("GVFMN_vfModuleXml", null)
112                                                 }
113                                                 else {
114                                                         def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
115                                                         def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
116                                                         int vfModulesSize = 0
117                                                         if (vfModules != null) {
118                                                                 vfModulesSize = vfModules.size()
119                                                         }
120                                                         String matchingVfModules = "<vfModules>"
121                                                         for (i in 0..vfModulesSize-1) {
122                                                                 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
123                                                                 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
124                                                                 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
125                                                                         matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
126                                                                 }                                                       
127                                                         }
128                                                         matchingVfModules = matchingVfModules + "</vfModules>"
129                                                         logDebug("Matching VF Modules: " + matchingVfModules, isDebugLogEnabled)                                        
130                                                         execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
131                                                 }
132                                         }
133                                 }       
134                         } catch (Exception ex) {
135                                 ex.printStackTrace()
136                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
137                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
138                         }
139                         logDebug('Exited ' + method, isDebugLogEnabled)
140                 } catch (BpmnError e) {
141                         throw e;
142                 } catch (Exception e) {
143                         logError('Caught exception in ' + method, e)
144                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
145                 }
146                 
147         }
148                                         
149         public void generateName (Execution execution) {
150                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
151                 def method = getClass().getSimpleName() + '.generateName() ' +
152                         'execution=' + execution.getId() +
153                         ')'
154                 logDebug('Entered ' + method, isDebugLogEnabled)
155         
156                 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")         
157                 
158                 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)                    
159                 logDebug("moduleIndex is: " + moduleIndex, isDebugLogEnabled)
160                 def vnfName = execution.getVariable("vnfName")
161                 def vfModuleLabel = execution.getVariable("vfModuleLabel")
162                 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
163                 logDebug("vfModuleName is: " + vfModuleName, isDebugLogEnabled)
164                 execution.setVariable("vfModuleName", vfModuleName)
165         }
166 }