fba5cbe1b059ad2c00b9379b2eefd864bedad77a
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\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
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
19  */\r
20 package org.openecomp.mso.bpmn.common.scripts\r
21 import java.io.Serializable;\r
22 \r
23 import org.camunda.bpm.engine.delegate.BpmnError\r
24 import org.camunda.bpm.engine.runtime.Execution\r
25 import org.springframework.web.util.UriUtils\r
26 \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
32 \r
33 public class GenerateVfModuleName extends AbstractServiceTaskProcessor{\r
34 \r
35         def Prefix="GVFMN_"\r
36         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
37 \r
38         \r
39         \r
40         public void preProcessRequest(Execution execution) {\r
41                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
42                 \r
43                 try {\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
54                         throw b\r
55                 }catch(Exception e){\r
56                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in initVariables method!")\r
57                 }\r
58         }\r
59 \r
60 \r
61         public void queryAAI(Execution execution) {\r
62                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
63                 def method = getClass().getSimpleName() + '.queryAAI(' +\r
64                         'execution=' + execution.getId() +\r
65                         ')'\r
66                 logDebug('Entered ' + method, isDebugLogEnabled)\r
67 \r
68                 try {\r
69                         def vnfId = execution.getVariable('vnfId')\r
70                         def personaModelId = execution.getVariable('personaModelId')\r
71                         \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
75 \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
78 \r
79                         try {\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
91 \r
92                                 responseData = response.getResponseBodyAsString()\r
93                                 if (responseData != null) {\r
94                                         logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)\r
95 \r
96                                 }\r
97 \r
98                                 utils.logAudit("GenerateVfModuleName - queryAAIVfModule Response: " + responseData)\r
99                                 utils.logAudit("GenerateVfModuleName - queryAAIVfModule ResponseCode: " + response.getStatusCode())\r
100 \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
112                                                 }\r
113                                                 else {\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
119                                                         }\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, "persona-model-id")\r
124                                                                 if (personaModelIdFromAAI != null && personaModelIdFromAAI.equals(personaModelId)) {\r
125                                                                         matchingVfModules = matchingVfModules + utils.removeXmlPreamble(vfModuleXml)\r
126                                                                 }                                                       \r
127                                                         }\r
128                                                         matchingVfModules = matchingVfModules + "</vfModules>"\r
129                                                         logDebug("Matching VF Modules: " + matchingVfModules, isDebugLogEnabled)                                        \r
130                                                         execution.setVariable("GVFMN_vfModuleXml", matchingVfModules)\r
131                                                 }\r
132                                         }\r
133                                 }       \r
134                         } catch (Exception ex) {\r
135                                 ex.printStackTrace()\r
136                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)\r
137                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())\r
138                         }\r
139                         logDebug('Exited ' + method, isDebugLogEnabled)\r
140                 } catch (BpmnError e) {\r
141                         throw e;\r
142                 } catch (Exception e) {\r
143                         logError('Caught exception in ' + method, e)\r
144                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAI(): ' + e.getMessage())\r
145                 }\r
146                 \r
147         }\r
148                                         \r
149         public void generateName (Execution execution) {\r
150                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
151                 def method = getClass().getSimpleName() + '.generateName() ' +\r
152                         'execution=' + execution.getId() +\r
153                         ')'\r
154                 logDebug('Entered ' + method, isDebugLogEnabled)\r
155         \r
156                 String vfModuleXml = execution.getVariable("GVFMN_vfModuleXml")         \r
157                 \r
158                 String moduleIndex = utils.getLowestUnusedIndex(vfModuleXml)                    \r
159                 logDebug("moduleIndex is: " + moduleIndex, isDebugLogEnabled)\r
160                 def vnfName = execution.getVariable("vnfName")\r
161                 def vfModuleLabel = execution.getVariable("vfModuleLabel")\r
162                 def vfModuleName = vnfName + "_" + vfModuleLabel + "_" + moduleIndex\r
163                 logDebug("vfModuleName is: " + vfModuleName, isDebugLogEnabled)\r
164                 execution.setVariable("vfModuleName", vfModuleName)\r
165         }\r
166 }\r