[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / CreateGenericVNFUtils.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
22 import org.openecomp.mso.bpmn.core.WorkflowException
23 import org.openecomp.mso.rest.APIResponse;
24
25 import org.camunda.bpm.engine.runtime.Execution;
26 import org.camunda.bpm.engine.delegate.BpmnError
27
28 /**
29  * Please describe the CreateGenericVNFUtils.groovy class
30  *
31  */
32 class CreateGenericVNFUtils{
33
34         String Prefix="CRTGVNF_"
35
36         def utils=new MsoUtils()
37
38         private AbstractServiceTaskProcessor taskProcessor
39         
40         public CreateGenericVNFUtils(AbstractServiceTaskProcessor taskProcessor) {
41                 this.taskProcessor = taskProcessor
42         }
43
44         /**
45          * This method is executed during the Initialization task of the process.
46          *
47          * @param execution
48          *
49          */
50
51         public APIResponse queryAAI(Execution execution, String path){
52
53         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
54         execution.setVariable("prefix", Prefix)
55         utils.log("DEBUG", " ======== STARTED queryAAI Process ======== ", isDebugEnabled)
56
57         def uuid = execution.getVariable("CRTGVNF_uuid")
58         utils.log("DEBUG", "UUID is: " + uuid, isDebugEnabled)
59
60         //Setting request path
61         String queryAAIRequestPath = execution.getVariable("URN_aai_endpoint")+path
62 //      execution.setVariable("CRTGVNF_queryAAIRequestPath", queryAAIRequestPath)
63         utils.log("DEBUG", "QueryAAIRequest Path is: " + "\n" + queryAAIRequestPath, isDebugEnabled)
64
65         try {
66                 AaiUtil aaiUtil = new AaiUtil(taskProcessor)
67                 return aaiUtil.executeAAIGetCall(execution, queryAAIRequestPath)
68         }catch(Exception e){
69                 utils.log("ERROR", "Exception Occured Processing queryAAI. Exception is:\n" + e, isDebugEnabled)
70                 execution.setVariable("CRTGVNF_dataValidationFlag", false)
71                 execution.setVariable("CRTGVNF_ErrorResponse", "Error Occured during queryAAI Method:\n" + e.getMessage())
72         }
73         utils.log("DEBUG", "======== COMPLETED queryAAI Process ======== ", isDebugEnabled)
74 }
75
76         public String buildSDNCRequest(Execution execution, String svcInstId, String action){
77
78                 String uuid = execution.getVariable('testReqId') // for junits
79                 if(uuid==null){
80                         uuid = execution.getVariable("att-mso-request-id") + "-" +      System.currentTimeMillis()
81                 }
82                 def callbackURL = execution.getVariable("CRTGVNF_sdncCallbackUrl")
83                 def requestId = execution.getVariable("CRTGVNF_requestId")
84                 def serviceType = execution.getVariable("CRTGVNF_serviceType")
85                 def vnfType = execution.getVariable("CRTGVNF_vnfType")
86                 def vnfName = execution.getVariable("CRTGVNF_vnfName")
87                 def tenantId = execution.getVariable("CRTGVNF_tenantId")
88                 def source = execution.getVariable("CRTGVNF_source")
89                 String vnfId = svcInstId
90                 String sdncVNFParamsXml = ""
91
92                 if(execution.getVariable("CRTGVNF_vnfParamsExistFlag") == true){
93                         sdncVNFParamsXml = buildSDNCParamsXml(execution)
94                 }else{
95                         sdncVNFParamsXml = ""
96                 }
97
98                 String sdncRequest =
99                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://ecomp.att.com/mso/request/types/v1"
100                                                                                                         xmlns:sdncadapterworkflow="http://ecomp.att.com/mso/workflow/schema/v1"
101                                                                                                         xmlns:sdncadapter="http://domain2.att.com/workflow/sdnc/adapter/schema/v1">
102            <sdncadapter:RequestHeader>
103                                 <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>
104                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
105                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
106                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
107                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
108                 </sdncadapter:RequestHeader>
109         <sdncadapterworkflow:SDNCRequestData>
110                 <request-information>
111                         <request-id>${requestId}</request-id>
112                         <request-action>VNFActivateRequest</request-action>
113                         <source>${source}</source>
114                 </request-information>
115                 <service-information>
116                         <service-type>${serviceType}</service-type>
117                         <service-instance-id>${vnfId}</service-instance-id>
118                         <subscriber-name>notsurewecare</subscriber-name>
119                 </service-information>
120                 <vnf-request-information>
121                         <vnf-id>${vnfId}</vnf-id>
122                         <vnf-type>${vnfType}</vnf-type>
123                         <vnf-name>${vnfName}</vnf-name>
124                         <tenant>${tenantId}</tenant>
125 ${sdncVNFParamsXml}
126                 </vnf-request-information>
127         </sdncadapterworkflow:SDNCRequestData>
128         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
129
130         return sdncRequest
131
132         }
133
134         public String buildSDNCParamsXml(Execution execution){
135
136                 String params = ""
137                 StringBuilder sb = new StringBuilder()
138                 Map<String, String> paramsMap = execution.getVariable("CRTGVNF_vnfParamsMap")
139
140                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
141                         String paramsXml
142                         String key = entry.getKey();
143                         if(key.endsWith("_network")){
144                                 String requestKey = key.substring(0, key.indexOf("_network"))
145                                 String requestValue = entry.getValue()
146                                 paramsXml =
147 """<vnf-networks>
148         <network-role>{ functx:substring-before-match(data($param/@name), '_network') }</network-role>
149         <network-name>{ $param/text() }</network-name>
150 </vnf-networks>"""
151                         }else{
152                         paramsXml = ""
153                         }
154                         params = sb.append(paramsXml)
155                 }
156                 return params
157         }
158
159         /**
160          * Builds a "CompletionHandler" request and stores it in the specified
161          * execution variable.
162          * @param execution the execution
163          */
164         public void buildCompletionHandlerRequest(Execution execution, String flowName) {
165                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
166                         'execution=' + execution.getId() +
167                         ')'
168                 def prefix = execution.getVariable('prefix')
169                 def resultVar = prefix + "CompletionHandlerRequest"
170                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
171                 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
172
173                 try {
174                         def request = taskProcessor.getVariable(execution, prefix+'Request')
175                         def requestInformation = taskProcessor.utils.getNodeXml(request, 'request-information', false)
176                         if (requestInformation == null || requestInformation == ""){
177                                 requestInformation = taskProcessor.utils.getNodeXml(request, 'request-info', false)
178                         }
179
180                         String content = """
181                 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://ecomp.att.com/mso/workflow/schema/v1"
182                                 xmlns:reqtype="http://ecomp.att.com/mso/request/types/v1">
183                         ${requestInformation}
184                         <sdncadapterworkflow:mso-bpel-name>${flowName}</sdncadapterworkflow:mso-bpel-name>
185                 </sdncadapterworkflow:MsoCompletionRequest>
186         """
187
188                         content = taskProcessor.utils.removeXmlPreamble(taskProcessor.utils.formatXML(content))
189                         taskProcessor.logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
190                         execution.setVariable(resultVar, content)
191
192                         taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
193                 } catch (BpmnError e) {
194                         throw e;
195                 } catch (Exception e) {
196                         taskProcessor.logError('Caught exception in ' + method, e)
197                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error Occured during completion handler request")
198                 }
199         }
200
201         /**
202          * Builds a "FalloutHandler" request and stores it in the specified
203          * execution variable.
204          * @param execution the execution
205          */
206         public void buildfalloutHandlerRequest(Execution execution) {
207                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
208                         'execution=' + execution.getId() +
209                         ')'
210                 def prefix = execution.getVariable('prefix')
211                 def resultVar = prefix + "FalloutHandlerRequest"
212                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
213                 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)
214                 try {
215                         def request = taskProcessor.getVariable(execution, prefix+'Request')
216
217                         def requestInformation = ""
218                         if (request != null){
219                          requestInformation = taskProcessor.utils.getNodeXml(request, 'request-information', false)
220                          if (requestInformation == null || requestInformation == ""){
221                                  requestInformation = taskProcessor.utils.getNodeXml(request, 'request-info', false)
222                          }
223                         }
224                         def  errorInformation = ""
225                         def encErrorResponseMsg = ""
226                         def errorResponseCode = ""
227                         String content = ""
228                         
229                         def WorkflowException workflowException
230                         def exception =  execution.getVariable("WorkflowException")
231                                                 
232                         if (exception instanceof WorkflowException)
233                          {
234                                  workflowException = execution.getVariable("WorkflowException")
235                          }
236                          
237                         if (workflowException != null){
238                                 errorResponseCode = workflowException.getErrorCode()
239                                 def errorResponseMsg = workflowException.getErrorMessage()
240
241                                 if (errorResponseMsg != null) {
242                                         encErrorResponseMsg = errorResponseMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
243                                 }
244
245                                 errorInformation = """<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
246                                                 <aetgt:ErrorMessage>${encErrorResponseMsg}</aetgt:ErrorMessage>
247                                                 <aetgt:ErrorCode>${errorResponseCode}</aetgt:ErrorCode>
248                                 </aetgt:WorkflowException>"""
249                         }
250                         else {
251                                 errorInformation = execution.getVariable(prefix+'ErrorResponse')
252
253                                 if (errorInformation == null) errorInformation = ""
254                         }
255                         
256                         content = """
257                                 <wfsch:FalloutHandlerRequest xmlns:wfsch="http://ecomp.att.com/mso/workflow/schema/v1"
258                              xmlns:reqtype="http://ecomp.att.com/mso/request/types/v1">
259                                 ${requestInformation}
260                 ${errorInformation}
261                                 </wfsch:FalloutHandlerRequest>
262                         """
263                          
264                         content = taskProcessor.utils.removeXmlPreamble(taskProcessor.utils.formatXML(content))
265                 
266                         taskProcessor.logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
267                         execution.setVariable(resultVar, content)
268                         taskProcessor.logDebug('Exited ' + method, isDebugLogEnabled)
269                 } catch (Exception e) {
270                         taskProcessor.logError('Caught exception in ' + method, e)
271                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error in buildfalloutHandlerRequest")
272                 }
273         }
274         
275 }
276
277
278
279
280