Dynamic Cloud Owner Support
[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
25 import java.io.Serializable;
26
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.springframework.web.util.UriUtils
30
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
42
43 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{
44         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenerateVfModuleName.class);
45
46         def Prefix="GVFMN_"
47         ExceptionUtil exceptionUtil = new ExceptionUtil()
48
49         
50         
51         public void preProcessRequest(DelegateExecution execution) {
52                 try {
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", "")
62                 }catch(BpmnError b){
63                         throw b
64                 }catch(Exception e){
65                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")
66                 }
67         }
68
69
70         public void queryAAI(DelegateExecution execution) {
71                 def method = getClass().getSimpleName() + '.queryAAI(' +
72                         'execution=' + execution.getId() +
73                         ')'
74                 msoLogger.trace('Entered ' + method)
75
76                 try {
77                         def vnfId = execution.getVariable('vnfId')
78                         def personaModelId = execution.getVariable('personaModelId')
79                         
80                         AaiUtil aaiUtil = new AaiUtil(this)
81                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
82                         uri.depth(Depth.ONE)
83                         String endPoint = aaiUtil.createAaiUri(uri)
84
85                         msoLogger.debug("AAI endPoint: " + endPoint)
86
87                         try {
88                                 RESTConfig config = new RESTConfig(endPoint);
89                                 def responseData = ''
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")
99
100                                 responseData = response.getResponseBodyAsString()
101                                 if (responseData != null) {
102                                         msoLogger.debug("Received generic VNF data: " + responseData)
103
104                                 }
105
106                                 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)
107                                 msoLogger.debug("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatusCode())
108
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)
120                                                 }
121                                                 else {
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()
127                                                         }
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")                                                             
135                                                                 }
136                                                                 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {
137                                                                         matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)
138                                                                 }                                                       
139                                                         }
140                                                         matchingVfModules = matchingVfModules + "</vfModules>"
141                                                         msoLogger.debug("Matching VF Modules: " + matchingVfModules)                                    
142                                                         execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)
143                                                 }
144                                         }
145                                 }       
146                         } catch (Exception ex) {
147                                 ex.printStackTrace()
148                                 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
149                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
150                         }
151                         msoLogger.trace('Exited ' + method)
152                 } catch (BpmnError e) {
153                         throw 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())
157                 }
158                 
159         }
160                                         
161         public void generateName (DelegateExecution execution) {
162                 def method = getClass().getSimpleName() + '.generateName() ' +
163                         'execution=' + execution.getId() +
164                         ')'
165                 msoLogger.trace('Entered ' + method)
166         
167                 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")         
168                 
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)
176         }
177 }