Merge "Re-enable Actuator for Springboot 2.0"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / GenerateVfModuleName.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
21 package org.onap.so.bpmn.common.scripts
22
23 import org.onap.so.bpmn.core.UrnPropertiesReader
24 import org.onap.so.client.HttpClientFactory
25
26 import java.io.Serializable;
27
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
33
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
44
45 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
46         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenerateVfModuleName.class);
47
48         def Prefix="GVFMN_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50
51
52
53         public void preProcessRequest(DelegateExecution execution) {
54                 try {
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", "")
64                 }catch(BpmnError b){
65                         throw b
66                 }catch(Exception e){
67                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
68                 }
69         }
70
71
72         public void queryAAI(DelegateExecution execution) {
73                 def method = getClass().getSimpleName() + '.queryAAI(' +
74                         'execution=' + execution.getId() +
75                         ')'
76                 msoLogger.trace('Entered ' + method)
77
78                 try {
79                         def vnfId = execution.getVariable('vnfId')
80                         def personaModelId = execution.getVariable('personaModelId')
81
82                         AaiUtil aaiUtil = new AaiUtil(this)
83                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
84                         uri.depth(Depth.ONE)
85                         String endPoint = aaiUtil.createAaiUri(uri)
86
87                         msoLogger.debug("AAI endPoint: " + endPoint)
88
89                         try {
90                                 HttpClient client = new HttpClientFactory().newXmlClient(new URL(endPoint), TargetEntity.AAI)
91
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')
96
97                                 def responseData = ''
98
99                                 msoLogger.debug('sending GET to AAI endpoint \'' + endPoint + '\'')
100                                 Response response = client.get()
101                                 msoLogger.debug("GenerateVfModuleName - invoking httpGet() to AAI")
102
103                                 responseData = response.readEntity(String.class)
104                                 if (responseData != null) {
105                                         msoLogger.debug("Received generic VNF data: " + responseData)
106
107                                 }
108
109                                 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
110                                 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatus())
111
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)
123                                                 }
124                                                 else {
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()
130                                                         }
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")
138                                                                 }
139                                                                 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
140                                                                         matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
141                                                                 }
142                                                         }
143                                                         matchingVfModules = matchingVfModules + "</vfModules>"
144                                                         msoLogger.debug("Matching VF Modules: " + matchingVfModules)
145                                                         execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
146                                                 }
147                                         }
148                                 }
149                         } catch (Exception ex) {
150                                 ex.printStackTrace()
151                                 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
152                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
153                         }
154                         msoLogger.trace('Exited ' + method)
155                 } catch (BpmnError e) {
156                         throw 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())
160                 }
161
162         }
163
164         public void generateName (DelegateExecution execution) {
165                 def method = getClass().getSimpleName() + '.generateName() ' +
166                         'execution=' + execution.getId() +
167                         ')'
168                 msoLogger.trace('Entered ' + method)
169
170                 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
171
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)
179         }
180 }