4d7557bfcd95c90ad57cc3f190a53d59847e2245
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.common.scripts
24
25 import org.onap.so.bpmn.core.UrnPropertiesReader
26 import org.onap.so.client.HttpClientFactory
27
28 import java.io.Serializable;
29
30 import javax.ws.rs.core.MediaType
31 import javax.ws.rs.core.Response
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34 import org.springframework.web.util.UriUtils
35
36 import org.onap.so.bpmn.core.json.JsonUtils
37 import org.onap.so.bpmn.core.WorkflowException
38 import org.onap.so.client.HttpClient
39 import org.onap.so.client.aai.AAIObjectType
40 import org.onap.so.client.aai.entities.uri.AAIResourceUri
41 import org.onap.so.client.aai.entities.uri.AAIUriFactory
42 import org.onap.so.client.graphinventory.entities.uri.Depth
43 import org.onap.so.utils.TargetEntity
44 import org.onap.so.logger.MessageEnum
45 import org.onap.so.logger.MsoLogger
46 import org.slf4j.Logger
47 import org.slf4j.LoggerFactory
48
49 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
50     private static final Logger logger = LoggerFactory.getLogger( GenerateVfModuleName.class);
51
52         def Prefix="GVFMN_"
53         ExceptionUtil exceptionUtil = new ExceptionUtil()
54
55
56
57         public void preProcessRequest(DelegateExecution execution) {
58                 try {
59                         def vnfId = execution.getVariable("vnfId")
60                         logger.debug("vnfId is " + vnfId)
61                         def vnfName = execution.getVariable("vnfName")
62                         logger.debug("vnfName is " + vnfName)
63                         def vfModuleLabel = execution.getVariable("vfModuleLabel")
64                         logger.debug("vfModuleLabel is " + vfModuleLabel)
65                         def personaModelId = execution.getVariable("personaModelId")
66                         logger.debug("personaModelId is " + personaModelId)
67                         execution.setVariable("GVFMN_vfModuleXml", "")
68                 }catch(BpmnError b){
69                         throw b
70                 }catch(Exception e){
71                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
72                 }
73         }
74
75
76         public void queryAAI(DelegateExecution execution) {
77                 def method = getClass().getSimpleName() + '.queryAAI(' +
78                         'execution=' + execution.getId() +
79                         ')'
80                 logger.trace('Entered ' + method)
81
82                 try {
83                         def vnfId = execution.getVariable('vnfId')
84                         def personaModelId = execution.getVariable('personaModelId')
85
86                         AaiUtil aaiUtil = new AaiUtil(this)
87                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
88                         uri.depth(Depth.ONE)
89                         String endPoint = aaiUtil.createAaiUri(uri)
90
91                         logger.debug("AAI endPoint: " + endPoint)
92
93                         try {
94                                 HttpClient client = new HttpClientFactory().newXmlClient(new URL(endPoint), TargetEntity.AAI)
95
96                                 client.addAdditionalHeader('X-TransactionId', UUID.randomUUID().toString())
97                                 client.addAdditionalHeader('X-FromAppId', 'MSO')
98                                 client.addAdditionalHeader('Content-Type', 'application/xml')
99                                 client.addAdditionalHeader('Accept','application/xml')
100
101                                 def responseData = ''
102
103                                 logger.debug('sending GET to AAI endpoint \'' + endPoint + '\'')
104                                 Response response = client.get()
105                                 logger.debug("GenerateVfModuleName - invoking httpGet() to AAI")
106
107                                 responseData = response.readEntity(String.class)
108                                 if (responseData != null) {
109                                         logger.debug("Received generic VNF data: " + responseData)
110
111                                 }
112
113                                 logger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
114                                 logger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatus())
115
116                                 execution.setVariable('GVFMN_queryAAIVfModuleResponseCode', response.getStatus())
117                                 execution.setVariable('GVFMN_queryAAIVfModuleResponse', responseData)
118                                 logger.debug('Response code:' + response.getStatus())
119                                 logger.debug('Response:' + System.lineSeparator() + responseData)
120                                 if (response.getStatus() == 200) {
121                                         // Set the VfModuleXML
122                                         if (responseData != null) {
123                                                 String vfModulesText = utils.getNodeXml(responseData, "vf-modules")
124                                                 if (vfModulesText == null || vfModulesText.isEmpty()) {
125                                                         logger.debug("There are no VF modules in this VNF yet")
126                                                         execution.setVariable("GVFMN_vfModuleXml", null)
127                                                 }
128                                                 else {
129                                                         def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
130                                                         def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
131                                                         int vfModulesSize = 0
132                                                         if (vfModules != null) {
133                                                                 vfModulesSize = vfModules.size()
134                                                         }
135                                                         String matchingVfModules = "<vfModules>"
136                                                         for (i in 0..vfModulesSize-1) {
137                                                                 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
138                                                                 def personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "model-invariant-id")
139                                                                 if (!personaModelIdFromAAI) {
140                                                                         // check old attribute name
141                                                                    personaModelIdFromAAI = utils.getNodeText(vfModuleXml, "persona-model-id")
142                                                                 }
143                                                                 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
144                                                                         matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
145                                                                 }
146                                                         }
147                                                         matchingVfModules = matchingVfModules + "</vfModules>"
148                                                         logger.debug("Matching VF Modules: " + matchingVfModules)
149                                                         execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
150                                                 }
151                                         }
152                                 }
153                         } catch (Exception ex) {
154                                 ex.printStackTrace()
155                                 logger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
156                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
157                         }
158                         logger.trace('Exited ' + method)
159                 } catch (BpmnError e) {
160                         throw e;
161                 } catch (Exception e) {
162                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
163                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
164                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
165                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())
166                 }
167
168         }
169
170         public void generateName (DelegateExecution execution) {
171                 def method = getClass().getSimpleName() + '.generateName() ' +
172                         'execution=' + execution.getId() +
173                         ')'
174                 logger.trace('Entered ' + method)
175
176                 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")
177
178                 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)
179                 logger.debug("moduleIndex is: " + moduleIndex)
180                 def vnfName = execution.getVariable("vnfName")
181                 def vfModuleLabel = execution.getVariable("vfModuleLabel")
182                 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex
183                 logger.debug("vfModuleName is: " + vfModuleName)
184                 execution.setVariable("vfModuleName", vfModuleName)
185         }
186 }