9b7fd6835c8a732b37b0d9c4173f65022561fbe9
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / CreateVnfInfra.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
21 package org.openecomp.mso.bpmn.infrastructure.scripts
22
23 import java.util.UUID;
24
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.runtime.Execution;
27
28 import static org.apache.commons.lang3.StringUtils.*;
29
30 import org.openecomp.mso.bpmn.common.scripts.AaiUtil;
31 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
32 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
33 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils;
34 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
35 import org.openecomp.mso.bpmn.core.WorkflowException
36 import org.openecomp.mso.bpmn.core.json.JsonUtils;
37
38
39 /**
40  * This class supports the CreateVnfInfra Flow
41  * with the creation of a generic vnf for
42  * infrastructure.
43  */
44 class CreateVnfInfra extends AbstractServiceTaskProcessor {
45
46         String Prefix="CREVI_"
47         ExceptionUtil exceptionUtil = new ExceptionUtil()
48         JsonUtils jsonUtil = new JsonUtils()
49         VidUtils vidUtils = new VidUtils(this)
50
51         /**
52          * This method gets and validates the incoming
53          * request.
54          *
55          * @param - execution
56          *
57          */
58         public void preProcessRequest(Execution execution) {
59                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
60                 execution.setVariable("prefix",Prefix)
61                 utils.log("DEBUG", " *** STARTED CreateVnfInfra PreProcessRequest Process*** ", isDebugEnabled)
62
63                 execution.setVariable("CREVI_sentSyncResponse", false)
64
65                 try{
66                         // Get Variables
67                         String createVnfRequest = execution.getVariable("bpmnRequest")
68                         execution.setVariable("CREVI_createVnfRequest", createVnfRequest)
69                         utils.logAudit("Incoming CreateVnfInfra Request is: \n" + createVnfRequest)
70
71                         if(createVnfRequest != null){
72
73                                 String requestId = execution.getVariable("mso-request-id")
74                                 execution.setVariable("CREVI_requestId", requestId)
75                                 utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
76
77                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
78                                 execution.setVariable("CREVI_serviceInstanceId", serviceInstanceId)
79                                 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
80
81                                 String vnfType = execution.getVariable("vnfType")
82                                 execution.setVariable("CREVI_vnfType", vnfType)
83                                 utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
84
85                                 String vnfName = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.instanceName")
86                                 execution.setVariable("CREVI_vnfName", vnfName)
87                                 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
88
89                                 String serviceId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.productFamilyId")
90                                 execution.setVariable("CREVI_serviceId", serviceId)
91                                 utils.log("DEBUG", "Incoming Service Id is: " + serviceId, isDebugEnabled)
92
93                                 String source = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.source")
94                                 execution.setVariable("CREVI_source", source)
95                                 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
96
97                                 String suppressRollback = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.suppressRollback")
98                                 execution.setVariable("CREVI_suppressRollback", suppressRollback)
99                                 utils.log("DEBUG", "Incoming Suppress Rollback is: " + suppressRollback, isDebugEnabled)
100                                 
101                                 def vnfModelInfo = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo")
102                                 execution.setVariable("CREVI_vnfModelInfo", vnfModelInfo)
103
104                                 String modelInvariantId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo.modelInvariantId")
105                                 execution.setVariable("CREVI_modelInvariantId", modelInvariantId)
106                                 utils.log("DEBUG", "Incoming Invariant Id is: " + modelInvariantId, isDebugEnabled)
107
108                                 String modelVersion = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo.modelVersion")
109                                 execution.setVariable("CREVI_modelVersion", modelVersion)
110                                 utils.log("DEBUG", "Incoming Model Version is: " + modelVersion, isDebugEnabled)
111                                 
112                                 def cloudConfiguration = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.cloudConfiguration")
113                                 execution.setVariable("CREVI_cloudConfiguration", cloudConfiguration)
114                                 
115                                 String cloudSiteId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.cloudConfiguration.lcpCloudRegionId")
116                                 execution.setVariable("CREVI_cloudSiteId", cloudSiteId)
117                                 utils.log("DEBUG", "Incoming Cloud Site Id is: " + cloudSiteId, isDebugEnabled)
118                                 
119                                 String tenantId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.cloudConfiguration.tenantId")
120                                 execution.setVariable("CREVI_tenantId", tenantId)
121                                 utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId, isDebugEnabled)
122
123                                 //For Completion Handler & Fallout Handler
124                                 String requestInfo =
125                                 """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
126                                         <request-id>${requestId}</request-id>
127                                         <action>CREATE</action>
128                                         <source>${source}</source>
129                                    </request-info>"""
130
131                                 execution.setVariable("CREVI_requestInfo", requestInfo)
132
133                                 //TODO: Orch Status - TBD, will come from SDN-C Response in 1702
134                                 String orchStatus = "Created"
135                                 execution.setVariable("CREVI_orchStatus", orchStatus)
136
137                                 //TODO: Equipment Role - Should come from SDN-C Response in 1702
138                                 String equipmentRole = " "
139                                 execution.setVariable("CREVI_equipmentRole", equipmentRole)
140
141                                 String vnfId = execution.getVariable("testVnfId") // for junits
142                                 if(isBlank(vnfId)){
143                                         vnfId = UUID.randomUUID().toString()
144                                         utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
145                                 }
146                                 execution.setVariable("CREVI_vnfId", vnfId)
147
148                                 // Setting for Sub Flow Calls
149                                 execution.setVariable("CREVI_type", "generic-vnf")
150                                 execution.setVariable("GENGS_type", "service-instance")
151                                 
152                                 String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
153                                 if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
154                                         def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
155                                         logError(msg)
156                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
157                                 }
158                                 execution.setVariable("CREVI_sdncCallbackUrl", sdncCallbackUrl)
159                                 
160                                 def vnfInputParameters = jsonUtil.getJsonValue(createVnfRequest, "requestParameters.userParams")
161                                 execution.setVariable("CREVI_vnfInputParameters", vnfInputParameters)
162                                 
163                                 
164                                 utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)
165                                 logDebug("SDNC Callback URL is: " + sdncCallbackUrl, isDebugEnabled)
166
167                         }else{
168                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
169                         }
170
171                 }catch(BpmnError b){
172                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
173                         throw b
174                 }catch(Exception e){
175                         utils.log("DEBUG", " Error Occured in CreateVnfInfra PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
176                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra PreProcessRequest")
177
178                 }
179                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PreProcessRequest Process ***", isDebugEnabled)
180         }
181
182         public void sendSyncResponse (Execution execution) {
183                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
184                 execution.setVariable("prefix",Prefix)
185
186                 utils.log("DEBUG", " *** STARTED CreateVnfInfra SendSyncResponse Process *** ", isDebugEnabled)
187
188                 try {
189                         String requestId = execution.getVariable("CREVI_requestId")
190                         String vnfId = execution.getVariable("CREVI_vnfId")
191
192                         String createVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
193
194                         utils.log("DEBUG", " CreateVnfInfra Sync Response is: \n"  + createVnfResponse, isDebugEnabled)
195
196                         sendWorkflowResponse(execution, 202, createVnfResponse)
197
198                         execution.setVariable("CREVI_sentSyncResponse", true)
199
200                 } catch (Exception ex) {
201                         utils.log("DEBUG", "Error Occured in CreateVnfInfra SendSyncResponse Process " + ex.getMessage(), isDebugEnabled)
202                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra SendSyncResponse Process")
203
204                 }
205                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra SendSyncResponse Process ***", isDebugEnabled)
206         }
207
208         
209         public void preProcessSDNCAssignRequest(Execution execution){
210                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
211                 execution.setVariable("prefix", Prefix)
212                 logDebug(" ======== STARTED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
213                 def vnfId = execution.getVariable("CREVI_vnfId")                
214                 def serviceInstanceId = execution.getVariable("CREVI_serviceInstanceId")
215                 logDebug("NEW VNF ID: " + vnfId, isDebugLogEnabled)
216                 utils.logAudit("NEW VNF ID: " + vnfId)
217
218                 try{
219                         //Build SDNC Request
220                         
221                         String assignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "assign")
222
223                         assignSDNCRequest = utils.formatXml(assignSDNCRequest)
224                         execution.setVariable("CREVI_assignSDNCRequest", assignSDNCRequest)
225                         logDebug("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest, isDebugLogEnabled)
226                         utils.logAudit("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest)
227
228                 }catch(Exception e){
229                         utils.log("ERROR", "Exception Occured Processing preProcessSDNCAssignRequest. Exception is:\n" + e, isDebugLogEnabled)
230                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareProvision Method:\n" + e.getMessage())
231                 }
232                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
233         }
234         
235         public void preProcessSDNCActivateRequest(Execution execution) {
236                 def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
237                         'execution=' + execution.getId() +
238                         ')'
239                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
240                 logDebug('Entered ' + method, isDebugLogEnabled)
241                 execution.setVariable("prefix", Prefix)
242                 logDebug(" ======== STARTED preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
243                 try{
244                         String vnfId = execution.getVariable("CREVI_vnfId")                     
245                         String serviceInstanceId = execution.getVariable("CREVI_serviceInstanceId")
246
247                         String activateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "activate")
248
249                         execution.setVariable("CREVI_activateSDNCRequest", activateSDNCRequest)
250                         logDebug("Outgoing CommitSDNCRequest is: \n" + activateSDNCRequest, isDebugLogEnabled)
251                         utils.logAudit("Outgoing CommitSDNCRequest is: \n"  + activateSDNCRequest)
252
253                 }catch(Exception e){
254                         log.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e, isDebugLogEnabled)
255                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
256                 }
257                 logDebug("======== COMPLETED  preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
258         }
259         
260         public String buildSDNCRequest(Execution execution, String svcInstId, String action){
261                 
262                                 String uuid = execution.getVariable('testReqId') // for junits
263                                 if(uuid==null){
264                                         uuid = execution.getVariable("mso-request-id") + "-" +          System.currentTimeMillis()
265                                 }
266                                 def callbackURL = execution.getVariable("CREVI_sdncCallbackUrl")
267                                 def requestId = execution.getVariable("CREVI_requestId")
268                                 def serviceId = execution.getVariable("CREVI_serviceId")
269                                 def vnfType = execution.getVariable("CREVI_vnfType")
270                                 def vnfName = execution.getVariable("CREVI_vnfName")
271                                 def tenantId = execution.getVariable("CREVI_tenantId")
272                                 def source = execution.getVariable("CREVI_source")                              
273                                 def vnfId = execution.getVariable("CREVI_vnfId")
274                                 def cloudSiteId = execution.getVariable("CREVI_cloudSiteId")
275                 
276                                 String sdncVNFParamsXml = ""
277                 
278                                 if(execution.getVariable("CREVI_vnfParamsExistFlag") == true){
279                                         sdncVNFParamsXml = buildSDNCParamsXml(execution)
280                                 }else{
281                                         sdncVNFParamsXml = ""
282                                 }
283                 
284                                 String sdncRequest =
285                                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
286                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
287                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
288            <sdncadapter:RequestHeader>
289                                 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
290                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
291                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
292                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
293                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
294                 </sdncadapter:RequestHeader>
295         <sdncadapterworkflow:SDNCRequestData>
296                 <request-information>
297                         <request-id>${requestId}</request-id>
298                         <request-action>VNFActivateRequest</request-action>
299                         <source>${source}</source>
300                         <notification-url/>
301                 </request-information>
302                 <service-information>
303                         <service-id>${serviceId}</service-id>
304                         <service-type>${serviceId}</service-type>
305                         <service-instance-id>${svcInstId}</service-instance-id>
306                         <subscriber-name>notsurewecare</subscriber-name>
307                 </service-information>
308                 <vnf-request-information>                       
309                         <vnf-id>${vnfId}</vnf-id>
310                         <vnf-name>${vnfName}</vnf-name>
311                         <vnf-type>${vnfType}</vnf-type>
312                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
313                         <tenant>${tenantId}</tenant>
314                 ${sdncVNFParamsXml}
315                 </vnf-request-information>
316         </sdncadapterworkflow:SDNCRequestData>
317         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
318                 
319                         utils.logAudit("sdncRequest:  " + sdncRequest)
320                         return sdncRequest              
321         }
322                 
323         public void validateSDNCResponse(Execution execution, String response, String method){
324                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
325                 execution.setVariable("prefix",Prefix)
326                 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
327
328                 WorkflowException workflowException = execution.getVariable("WorkflowException")
329                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
330
331                 utils.logAudit("workflowException: " + workflowException)
332
333                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
334                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
335
336                 utils.logAudit("SDNCResponse: " + response)
337
338                 String sdncResponse = response
339                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
340                         logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)
341                         
342                 }else{
343                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
344                         throw new BpmnError("MSOWorkflowException")
345                 }
346                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
347         }
348
349         public void prepareCompletionHandlerRequest(Execution execution){
350                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
351                 execution.setVariable("prefix",Prefix)
352
353                 utils.log("DEBUG", " *** STARTED CreateVnfInfra PrepareCompletionHandlerRequest Process *** ", isDebugEnabled)
354
355                 try {
356                         String requestInfo = execution.getVariable("CREVI_requestInfo")
357                         String vnfId = execution.getVariable("CREVI_vnfId")
358                         requestInfo = utils.removeXmlPreamble(requestInfo)
359
360                         String request =
361                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
362                                                                         xmlns:ns="http://org.openecomp/mso/request/types/v1">
363                                                         ${requestInfo}
364                                                         <status-message>Vnf has been created successfully.</status-message>
365                                                         <vnfId>${vnfId}</vnfId>
366                                                         <mso-bpel-name>CreateVnfInfra</mso-bpel-name>
367                                                 </aetgt:MsoCompletionRequest>"""
368
369                         execution.setVariable("CREVI_completionHandlerRequest", request)
370                         utils.log("DEBUG", "Completion Handler Request is: " + request, isDebugEnabled)
371
372                         execution.setVariable("WorkflowResponse", "Success") // for junits
373
374                 } catch (Exception ex) {
375                         utils.log("DEBUG", "Error Occured in CreateVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage(), isDebugEnabled)
376                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra PrepareCompletionHandlerRequest Process")
377
378                 }
379                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCompletionHandlerRequest Process ***", isDebugEnabled)
380         }
381
382         public void sendErrorResponse(Execution execution){
383                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
384                 execution.setVariable("prefix",Prefix)
385
386                 utils.log("DEBUG", " *** STARTED CreateVnfInfra sendErrorResponse Process *** ", isDebugEnabled)
387                 try {
388                         def sentSyncResponse = execution.getVariable("CREVI_sentSyncResponse")
389                         if(sentSyncResponse == false){
390                                 WorkflowException wfex = execution.getVariable("WorkflowException")
391                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
392
393                                 utils.logAudit(response)
394                                 sendWorkflowResponse(execution, 500, response)
395                         }else{
396                                 utils.log("DEBUG", "Not Sending Error Response.  Sync Response Already Sent", isDebugEnabled)
397                         }
398
399                 } catch (Exception ex) {
400                         utils.log("DEBUG", "Error Occured in CreateVnfInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
401                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra sendErrorResponse Process")
402
403                 }
404                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra sendErrorResponse Process ***", isDebugEnabled)
405         }
406
407         public void prepareFalloutRequest(Execution execution){
408                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
409                 execution.setVariable("prefix",Prefix)
410
411                 utils.log("DEBUG", " *** STARTED CreateVnfInfra prepareFalloutRequest Process *** ", isDebugEnabled)
412
413                 try {
414                         WorkflowException wfex = execution.getVariable("WorkflowException")
415                         utils.log("DEBUG", " Incoming Workflow Exception: " + wfex.toString(), isDebugEnabled)
416                         String requestInfo = execution.getVariable("CREVI_requestInfo")
417                         utils.log("DEBUG", " Incoming Request Info: " + requestInfo, isDebugEnabled)
418
419                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
420
421                         execution.setVariable("CREVI_falloutRequest", falloutRequest)
422
423
424                 } catch (Exception ex) {
425                         utils.log("DEBUG", "Error Occured in CreateVnfInfra prepareFalloutRequest Process " + ex.getMessage(), isDebugEnabled)
426                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVnfInfra prepareFalloutRequest Process")
427
428                 }
429                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra prepareFalloutRequest Process ***", isDebugEnabled)
430         }
431
432 }