2c433863aa82e267f01dbf8807ce9d73105813f9
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVnf.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.infrastructure.scripts
24
25 import org.onap.so.db.catalog.beans.HomingInstance
26
27 import static org.apache.commons.lang3.StringUtils.*
28
29 import org.camunda.bpm.engine.delegate.BpmnError
30 import org.camunda.bpm.engine.delegate.DelegateExecution
31 import org.onap.aai.domain.yang.GenericVnf
32 import org.onap.so.bpmn.common.scripts.AaiUtil
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.common.scripts.MsoUtils
36 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
37 import org.onap.so.bpmn.common.scripts.VidUtils
38 import org.onap.so.bpmn.common.util.OofInfraUtils
39 import org.onap.so.bpmn.core.RollbackData
40 import org.onap.so.bpmn.core.UrnPropertiesReader
41 import org.onap.so.bpmn.core.WorkflowException
42 import org.onap.so.bpmn.core.domain.VnfResource
43 import org.onap.so.bpmn.core.json.JsonUtils
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 import org.onap.so.client.aai.AAIObjectType;
49 import org.onap.so.client.aai.AAIResourcesClient
50 import org.onap.so.client.aai.entities.AAIResultWrapper
51 import org.onap.so.client.aai.entities.uri.AAIResourceUri
52 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
53 import org.springframework.web.util.UriUtils
54 import org.json.JSONObject
55
56
57 /**
58  * This class supports the DoCreateVnf building block subflow
59  * with the creation of a generic vnf for
60  * infrastructure.
61  *
62  */
63 class DoCreateVnf extends AbstractServiceTaskProcessor {
64
65     private static final Logger logger = LoggerFactory.getLogger( DoCreateVnf.class);
66         String Prefix="DoCVNF_"
67         ExceptionUtil exceptionUtil = new ExceptionUtil()
68         JsonUtils jsonUtil = new JsonUtils()
69         VidUtils vidUtils = new VidUtils(this)
70         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
71         OofInfraUtils oofInfraUtils = new OofInfraUtils()
72
73         /**
74          * This method gets and validates the incoming
75          * request.
76          *
77          * @param - execution
78          *
79          */
80         public void preProcessRequest(DelegateExecution execution) {
81                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
82                 execution.setVariable("prefix",Prefix)
83                 logger.debug("STARTED DoCreateVnf PreProcessRequest Process")
84
85                 // DISABLE SDNC INTERACTION FOR NOW
86                 execution.setVariable("SDNCInteractionEnabled", false)
87
88
89                 /*******************/
90                 try{
91                         // Get Variables
92
93                         def rollbackData = execution.getVariable("rollbackData")
94                         if (rollbackData == null) {
95                                 rollbackData = new RollbackData()
96                         }
97
98                         String vnfModelInfo = execution.getVariable("vnfModelInfo")
99                         String serviceModelInfo = execution.getVariable("serviceModelInfo")
100
101                         String requestId = execution.getVariable("msoRequestId")
102                         execution.setVariable("DoCVNF_requestId", requestId)
103                         execution.setVariable("mso-request-id", requestId)
104                         logger.debug("Incoming Request Id is: " + requestId)
105
106                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
107                         execution.setVariable("DoCVNF_serviceInstanceId", serviceInstanceId)
108                         rollbackData.put("VNF", "serviceInstanceId", serviceInstanceId)
109                         logger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
110
111                         String vnfType = execution.getVariable("vnfType")
112                         execution.setVariable("DoCVNF_vnfType", vnfType)
113                         logger.debug("Incoming Vnf Type is: " + vnfType)
114
115                         String vnfName = execution.getVariable("vnfName")
116                         if (vnfName.equals("") || vnfName.equals("null")) {
117                                 AAIResourcesClient resourceClient = new AAIResourcesClient()
118                                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, execution.getVariable("vnfId"))
119                                 if(resourceClient.exists(uri)){
120                                         exceptionUtil.buildWorkflowException(execution, 5000, "Generic Vnf Already Exist.")
121                                 }
122
123
124                         }
125                         execution.setVariable("DoCVNF_vnfName", vnfName)
126                         logger.debug("Incoming Vnf Name is: " + vnfName)
127
128                         String serviceId = execution.getVariable("productFamilyId")
129                         execution.setVariable("DoCVNF_serviceId", serviceId)
130                         logger.debug("Incoming Service Id is: " + serviceId)
131
132                         String source = "VID"
133                         execution.setVariable("DoCVNF_source", source)
134                         rollbackData.put("VNF", "source", source)
135                         logger.debug("Incoming Source is: " + source)
136
137                         String suppressRollback = execution.getVariable("disableRollback")
138                         execution.setVariable("DoCVNF_suppressRollback", suppressRollback)
139                         logger.debug("Incoming Suppress Rollback is: " + suppressRollback)
140
141                         String modelInvariantId = jsonUtil.getJsonValue(vnfModelInfo, "modelInvariantUuid")
142                         execution.setVariable("DoCVNF_modelInvariantId", modelInvariantId)
143                         logger.debug("Incoming Invariant Id is: " + modelInvariantId)
144
145                         String modelVersionId = jsonUtil.getJsonValue(vnfModelInfo, "modelUuid")
146                         if (modelVersionId == null) {
147                                 modelVersionId = ""
148                         }
149                         execution.setVariable("DoCVNF_modelVersionId", modelVersionId)
150                         logger.debug("Incoming Version Id is: " + modelVersionId)
151
152                         String modelVersion = jsonUtil.getJsonValue(vnfModelInfo, "modelVersion")
153                         execution.setVariable("DoCVNF_modelVersion", modelVersion)
154                         logger.debug("Incoming Model Version is: " + modelVersion)
155
156                         String modelName = jsonUtil.getJsonValue(vnfModelInfo, "modelName")
157                         execution.setVariable("DoCVNF_modelName", modelName)
158                         logger.debug("Incoming Model Name is: " + modelName)
159
160                         String modelCustomizationId = jsonUtil.getJsonValue(vnfModelInfo, "modelCustomizationUuid")
161                         if (modelCustomizationId == null) {
162                                 modelCustomizationId = ""
163                         }
164                         execution.setVariable("DoCVNF_modelCustomizationId", modelCustomizationId)
165                         logger.debug("Incoming Model Customization Id is: " + modelCustomizationId)
166
167                         String cloudSiteId = execution.getVariable("lcpCloudRegionId")
168                         execution.setVariable("DoCVNF_cloudSiteId", cloudSiteId)
169                         rollbackData.put("VNF", "cloudSiteId", cloudSiteId)
170                         logger.debug("Incoming Cloud Site Id is: " + cloudSiteId)
171
172                         String tenantId = execution.getVariable("tenantId")
173                         execution.setVariable("DoCVNF_tenantId", tenantId)
174                         rollbackData.put("VNF", "tenantId", tenantId)
175                         logger.debug("Incoming Tenant Id is: " + tenantId)
176
177                         String globalSubscriberId = execution.getVariable("globalSubscriberId")
178                         if (globalSubscriberId == null) {
179                                 globalSubscriberId = ""
180                         }
181                         execution.setVariable("DoCVNF_globalSubscriberId", globalSubscriberId)
182                         logger.debug("Incoming Global Subscriber Id is: " + globalSubscriberId)
183
184                         String sdncVersion = execution.getVariable("sdncVersion")
185                         if (sdncVersion == null) {
186                                 sdncVersion = "1702"
187                         }
188                         execution.setVariable("DoCVNF_sdncVersion", sdncVersion)
189                         logger.debug("Incoming Sdnc Version is: " + sdncVersion)
190
191                         //For Completion Handler & Fallout Handler
192                         String requestInfo =
193                                 """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
194                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
195                                         <action>CREATE</action>
196                                         <source>${MsoUtils.xmlEscape(source)}</source>
197                                    </request-info>"""
198
199                         execution.setVariable("DoCVNF_requestInfo", requestInfo)
200                         //TODO: Orch Status - TBD, will come from SDN-C Response in 1702
201                         String orchStatus = "Created"
202                         execution.setVariable("DoCVNF_orchStatus", orchStatus)
203
204                         //TODO: Equipment Role - Should come from SDN-C Response in 1702
205                         String equipmentRole = " "
206                         execution.setVariable("DoCVNF_equipmentRole", equipmentRole)
207                         String vnfId = execution.getVariable("testVnfId") // for junits
208                         if(isBlank(vnfId)){
209                                 vnfId = execution.getVariable("vnfId")
210                                 if (isBlank(vnfId)) {
211                                         vnfId = UUID.randomUUID().toString()
212                                         logger.debug("Generated Vnf Id is: " + vnfId)
213                                 }
214                         }
215                         execution.setVariable("DoCVNF_vnfId", vnfId)
216
217                         // Setting for Sub Flow Calls
218                         execution.setVariable("DoCVNF_type", "generic-vnf")
219                         execution.setVariable("GENGS_type", "service-instance")
220
221                         String sdncCallbackUrl = (String) UrnPropertiesReader.getVariable("mso.workflow.sdncadapter.callback",execution)
222                         if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
223                                 def msg = 'Required variable \'mso.workflow.sdncadapter.callback\' is missing'
224                                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
225                                                 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError.getValue());
226                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
227                         }
228                         execution.setVariable("DoCVNF_sdncCallbackUrl", sdncCallbackUrl)
229                         rollbackData.put("VNF", "sdncCallbackUrl", sdncCallbackUrl)
230                         logger.debug("SDNC Callback URL is: " + sdncCallbackUrl)
231
232                         VnfResource vnfResource = (VnfResource) execution.getVariable((String)"vnfResourceDecomposition")
233                         String nfRole = vnfResource.getNfRole()
234
235                         execution.setVariable("DoCVNF_nfRole", nfRole)
236                         logger.debug("NF Role is: " + nfRole)
237
238                         String nfNamingCode = vnfResource.getNfNamingCode()
239                         execution.setVariable("DoCVNF_nfNamingCode", nfNamingCode)
240                         logger.debug("NF Naming Code is: " + nfNamingCode)
241
242                         String nfType = vnfResource.getNfType()
243                         execution.setVariable("DoCVNF_nfType", nfType)
244                         logger.debug("NF Type is: " + nfType)
245
246                         String nfFunction = vnfResource.getNfFunction()
247                         execution.setVariable("DoCVNF_nfFunction", nfFunction)
248                         logger.debug("NF Function is: " + nfFunction)
249
250                         // Set Homing Info
251                         try {
252                                 HomingInstance homingInstance = oofInfraUtils.getHomingInstance(serviceInstanceId, execution)
253                                 if (homingInstance != null) {
254                                         execution.setVariable("DoCVNF_cloudSiteId", homingInstance.getCloudRegionId())
255                                         rollbackData.put("VNF", "cloudSiteId", homingInstance.getCloudRegionId())
256                                         logger.debug("Overwriting cloudSiteId with homing cloudSiteId: " +
257                                                         homingInstance.getCloudRegionId())
258                                 }
259                         } catch (Exception exception) {
260                                 logger.debug("Could not find homing information for service instance: " + serviceInstanceId +
261                                                 "... continuing")
262                                 logger.debug("Could not find homing information for service instance error: " + exception)
263                         }
264
265                         rollbackData.put("VNF", "rollbackSDNCAssign", "false")
266                         rollbackData.put("VNF", "rollbackSDNCActivate", "false")
267                         rollbackData.put("VNF", "rollbackVnfCreate", "false")
268
269                         execution.setVariable("rollbackData", rollbackData)
270
271                 }catch(BpmnError b){
272                         logger.debug("Rethrowing MSOWorkflowException")
273                         throw b
274                 }catch(Exception e){
275                         logger.debug(" Error Occured in DoCreateVnf PreProcessRequest method!" + e.getMessage())
276                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
277
278                 }
279                 logger.trace("COMPLETED DoCreateVnf PreProcessRequest Process")
280         }
281
282         /**
283          * Gets the service instance from aai
284          */
285         public void getServiceInstance(DelegateExecution execution) {
286                 try {
287                         String serviceInstanceId = execution.getVariable('DoCVNF_serviceInstanceId')
288
289                         AAIResourcesClient resourceClient = new AAIResourcesClient()
290                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
291
292                         if(resourceClient.exists(uri)){
293                                 Map<String, String> keys = uri.getURIKeys()
294                                 execution.setVariable("globalCustomerId", keys.get("global-customer-id"))
295                                 execution.setVariable("serviceType", keys.get("service-type"))
296                                 execution.setVariable("GENGS_siResourceLink", uri.build().toString())
297
298                         }else{
299                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai")
300                         }
301
302                 }catch(BpmnError e) {
303                         throw e;
304                 }catch(Exception ex) {
305                         String msg = "Exception in getServiceInstance. " + ex.getMessage()
306                         logger.debug(msg)
307                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
308                 }
309         }
310
311         private Object getVariableEnforced(DelegateExecution execution, String name){
312                 Object enforced = execution.getVariable(name)
313                 if(!enforced){
314                 return "";
315                 }
316                 return enforced;
317         }
318
319         public void createGenericVnf (DelegateExecution execution) {
320                 execution.setVariable("prefix",Prefix)
321                 logger.trace("STARTED DoCreateVnf CreateGenericVnf Process")
322                 try {
323                         //Get Vnf Info
324                         String vnfId = getVariableEnforced(execution, "DoCVNF_vnfId")
325                         String vnfName = getVariableEnforced(execution, "DoCVNF_vnfName")
326                         if (vnfName == null) {
327                                 vnfName = "sdncGenerated"
328                                 logger.debug("Sending a dummy VNF name to AAI - the name will be generated by SDNC: " + vnfName)
329                         }
330                         String vnfType = getVariableEnforced(execution, "DoCVNF_vnfType")
331                         String serviceId = getVariableEnforced(execution, "DoCVNF_serviceId")
332                         String orchStatus = getVariableEnforced(execution, "DoCVNF_orchStatus")
333                         String modelInvariantId = getVariableEnforced(execution, "DoCVNF_modelInvariantId")
334                         String modelVersionId = getVariableEnforced(execution, "DoCVNF_modelVersionId")
335                         String modelCustomizationId = getVariableEnforced(execution, "DoCVNF_modelCustomizationId")
336                         String equipmentRole = getVariableEnforced(execution, "DoCVNF_equipmentRole")
337                         String nfType = getVariableEnforced(execution, "DoCVNF_nfType")
338                         String nfRole = getVariableEnforced(execution, "DoCVNF_nfRole")
339                         String nfFunction = getVariableEnforced(execution, "DoCVNF_nfFunction")
340                         String nfNamingCode = getVariableEnforced(execution, "DoCVNF_nfNamingCode")
341
342                         //Get Service Instance Info
343                         String serviceInstanceId = getVariableEnforced(execution, "DoCVNF_serviceInstanceId")
344
345                         String globalCustId = execution.getVariable("globalCustomerId")
346                         String serviceType = execution.getVariable("serviceType")
347
348                         Map<String, String> payload = new LinkedHashMap<>();
349                         payload.put("vnf-id", vnfId);
350                         payload.put("vnf-name", vnfName);
351                         payload.put("service-id", serviceId);
352                         payload.put("vnf-type", vnfType);
353                         payload.put("prov-status", "PREPROV");
354                         payload.put("orchestration-status", orchStatus);
355                         payload.put("model-invariant-id", modelInvariantId);
356                         payload.put("model-version-id", modelVersionId);
357                         payload.put("model-customization-id", modelCustomizationId);
358                         payload.put("nf-type", nfType);
359                         payload.put("nf-role", nfRole);
360                         payload.put("nf-function", nfFunction);
361                         payload.put("nf-naming-code", nfNamingCode);
362
363                         AAIResourcesClient resourceClient = new AAIResourcesClient();
364                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
365                         resourceClient.create(uri, payload)
366
367                         AAIResourceUri siUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustId, serviceType, serviceInstanceId)
368                         resourceClient.connect(uri, siUri)
369
370                 }catch(Exception ex) {
371                         logger.debug("Error Occured in DoCreateVnf CreateGenericVnf Process ", ex)
372                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf CreateGenericVnf Process")
373                 }
374                 logger.trace("COMPLETED DoCreateVnf CreateGenericVnf Process")
375         }
376
377         public void postProcessCreateGenericVnf (DelegateExecution execution) {
378                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
379                 execution.setVariable("prefix",Prefix)
380
381                 logger.trace("STARTED DoCreateVnf PostProcessCreateGenericVnf Process")
382                 try {
383                         //Get Vnf Info
384                         String vnfId = execution.getVariable("DoCVNF_vnfId")
385                         def rollbackData = execution.getVariable("rollbackData")
386                         rollbackData.put("VNF", "vnfId", vnfId)
387                         rollbackData.put("VNF", "rollbackVnfCreate", "true")
388                         execution.setVariable("rollbackData", rollbackData)
389                 }catch(Exception ex) {
390                         logger.debug("Error Occured in DoCreateVnf PostProcessCreateGenericVnf Process " + ex.getMessage())
391                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PostProcessCreateGenericVnf Process")
392                 }
393                 logger.trace("COMPLETED DoCreateVnf PostProcessCreateGenericVnf Process")
394         }
395
396
397         public void preProcessSDNCAssignRequest(DelegateExecution execution){
398                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
399                 execution.setVariable("prefix", Prefix)
400                 logger.trace("STARTED preProcessSDNCAssignRequest")
401                 def vnfId = execution.getVariable("DoCVNF_vnfId")
402                 def serviceInstanceId = execution.getVariable("DoCVNF_serviceInstanceId")
403                 logger.debug("NEW VNF ID: " + vnfId)
404
405                 try{
406                         //Build SDNC Request
407
408                         String assignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "assign")
409
410                         assignSDNCRequest = utils.formatXml(assignSDNCRequest)
411                         execution.setVariable("DoCVNF_assignSDNCRequest", assignSDNCRequest)
412                         logger.debug("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest)
413
414                 }catch(Exception e){
415                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
416                                         "Exception Occured Processing preProcessSDNCAssignRequest", "BPMN", MsoLogger.getServiceName(),
417                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e)
418                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCAssignRequest Method:\n" + e.getMessage())
419                 }
420                 logger.trace("COMPLETED preProcessSDNCAssignRequest")
421         }
422
423         public void preProcessSDNCActivateRequest(DelegateExecution execution) {
424                 def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
425                         'execution=' + execution.getId() +
426                         ')'
427                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
428                 logger.trace('Entered ' + method)
429                 execution.setVariable("prefix", Prefix)
430                 logger.trace("STARTED preProcessSDNCActivateRequest Process")
431                 try{
432                         String vnfId = execution.getVariable("DoCVNF_vnfId")
433                         String serviceInstanceId = execution.getVariable("DoCVNF_serviceInstanceId")
434
435                         String activateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "activate")
436
437                         execution.setVariable("DoCVNF_activateSDNCRequest", activateSDNCRequest)
438                         logger.debug("Outgoing CommitSDNCRequest is: \n" + activateSDNCRequest)
439
440                 }catch(Exception e){
441                         logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
442                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
443                 }
444                 logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
445         }
446
447         public String buildSDNCRequest(DelegateExecution execution, String svcInstId, String action){
448
449                                 String uuid = execution.getVariable('testReqId') // for junits
450                                 if(uuid==null){
451                                         uuid = execution.getVariable("DoCVNF_requestId") + "-" +        System.currentTimeMillis()
452                                 }
453                                 def callbackURL = execution.getVariable("DoCVNF_sdncCallbackUrl")
454                                 def requestId = execution.getVariable("DoCVNF_requestId")
455                                 def serviceId = execution.getVariable("DoCVNF_serviceId")
456                                 def vnfType = execution.getVariable("DoCVNF_vnfType")
457                                 def vnfName = execution.getVariable("DoCVNF_vnfName")
458                                 // Only send vnfName to SDNC if it is not null, otherwise it will get generated by SDNC on Assign
459                                 String vnfNameString = ""
460                                 if (vnfName != null) {
461                                         vnfNameString = """<vnf-name>${MsoUtils.xmlEscape(vnfName)}</vnf-name>"""
462                                 }
463                                 def tenantId = execution.getVariable("DoCVNF_tenantId")
464                                 def source = execution.getVariable("DoCVNF_source")
465                                 def vnfId = execution.getVariable("DoCVNF_vnfId")
466                                 def cloudSiteId = execution.getVariable("DoCVNF_cloudSiteId")
467                                 def modelCustomizationId = execution.getVariable("DoCVNF_modelCustomizationId")
468                                 def serviceModelInfo = execution.getVariable("serviceModelInfo")
469                                 def vnfModelInfo = execution.getVariable("vnfModelInfo")
470                                 String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
471                                 String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
472                                 def globalSubscriberId = execution.getVariable("DoCVNF_globalSubscriberId")
473                                 def sdncVersion = execution.getVariable("DoCVNF_sdncVersion")
474
475                                 String sdncVNFParamsXml = ""
476
477                                 if(execution.getVariable("DoCVNF_vnfParamsExistFlag") == true){
478                                         sdncVNFParamsXml = buildSDNCParamsXml(execution)
479                                 }else{
480                                         sdncVNFParamsXml = ""
481                                 }
482
483                                 String sdncRequest =
484                                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
485                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
486                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
487            <sdncadapter:RequestHeader>
488                                 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
489                                 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
490                                 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
491                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
492                                 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
493                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
494                 </sdncadapter:RequestHeader>
495         <sdncadapterworkflow:SDNCRequestData>
496                 <request-information>
497                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
498                         <request-action>CreateVnfInstance</request-action>
499                         <source>${MsoUtils.xmlEscape(source)}</source>
500                         <notification-url/>
501                         <order-number/>
502                         <order-version/>
503                 </request-information>
504                 <service-information>
505                         <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
506                         <subscription-service-type>${MsoUtils.xmlEscape(serviceId)}</subscription-service-type>
507                         ${serviceEcompModelInformation}
508                         <service-instance-id>${MsoUtils.xmlEscape(svcInstId)}</service-instance-id>
509                         <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
510                 </service-information>
511                 <vnf-information>
512                         <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
513                         <vnf-type>${MsoUtils.xmlEscape(vnfType)}</vnf-type>
514                         ${vnfEcompModelInformation}
515                 </vnf-information>
516                 <vnf-request-input>
517                         ${vnfNameString}
518                         <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
519                         <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
520                         ${sdncVNFParamsXml}
521                 </vnf-request-input>
522         </sdncadapterworkflow:SDNCRequestData>
523         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
524
525                         logger.debug("sdncRequest:  " + sdncRequest)
526                         return sdncRequest
527         }
528
529         public void validateSDNCResponse(DelegateExecution execution, String response, String method){
530                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
531                 execution.setVariable("prefix",Prefix)
532                 logger.debug("STARTED ValidateSDNCResponse Process")
533
534                 WorkflowException workflowException = execution.getVariable("WorkflowException")
535                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
536
537                 logger.debug("workflowException: " + workflowException)
538
539                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
540                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
541
542                 logger.debug("SDNCResponse: " + response)
543
544                 String sdncResponse = response
545                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
546                         logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse)
547                         if(method.equals("get")){
548                                 String topologyGetResponse = execution.getVariable("DoCVNF_getSDNCAdapterResponse")
549                                 String data = utils.getNodeXml(topologyGetResponse, "response-data")
550                                 logger.debug("topologyGetResponseData: " + data)
551                                 String vnfName = utils.getNodeText(data, "vnf-name")
552                                 logger.debug("vnfName received from SDNC: " + vnfName)
553                                 execution.setVariable("vnfName", vnfName)
554                                 execution.setVariable("DoCVNF_vnfName", vnfName)
555                         }
556                         def rollbackData = execution.getVariable("rollbackData")
557                         if (method.equals("assign")) {
558                                 rollbackData.put("VNF", "rollbackSDNCAssign", "true")
559                         }
560                         else if (method.equals("activate")) {
561                                 rollbackData.put("VNF", "rollbackSDNCActivate", "true")
562                         }
563                         execution.setVariable("rollbackData", rollbackData)
564
565
566                 }else{
567                         logger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
568                         throw new BpmnError("MSOWorkflowException")
569                 }
570                 logger.debug("COMPLETED ValidateSDNCResponse Process")
571         }
572
573         public void preProcessSDNCGetRequest(DelegateExecution execution){
574                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
575                 execution.setVariable("prefix", Prefix)
576                 logger.trace("STARTED preProcessSDNCGetRequest Process")
577                 try{
578                         def serviceInstanceId = execution.getVariable('DoCVNF_serviceInstanceId')
579
580                         String uuid = execution.getVariable('testReqId') // for junits
581                         if(uuid==null){
582                                 uuid = execution.getVariable("mso-request-id") + "-" +  System.currentTimeMillis()
583                         }
584
585                         def callbackUrl = execution.getVariable("DoCVFM_sdncCallbackUrl")
586                         logger.debug("callbackUrl:" + callbackUrl)
587
588                         def vnfId = execution.getVariable('DCVFM_vnfId')
589
590                         def svcInstId = ""
591                         if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
592                                 svcInstId = vnfId
593                         }
594                         else {
595                                 svcInstId = serviceInstanceId
596                         }
597                         // serviceOperation will be retrieved from "object-path" element
598                         // in SDNC Assign Response for VNF
599                         String response = execution.getVariable("DoCVNF_assignSDNCAdapterResponse")
600                         logger.debug("DoCVNF_assignSDNCAdapterResponse is: \n" + response)
601
602                         String serviceOperation = ""
603
604                         String data = utils.getNodeXml(response, "response-data")
605                         logger.debug("responseData: " + data)
606                         serviceOperation = utils.getNodeText(data, "object-path")
607                         logger.debug("VNF with sdncVersion of 1707 or later - service operation: " + serviceOperation)
608
609
610                         //!!!! TEMPORARY WORKAROUND FOR SDNC REPLICATION ISSUE
611                         sleep(5000)
612
613                         String SDNCGetRequest =
614                                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
615                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
616                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
617                                         <sdncadapter:RequestHeader>
618                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
619                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
620                                         <sdncadapter:SvcAction>query</sdncadapter:SvcAction>
621                                         <sdncadapter:SvcOperation>${MsoUtils.xmlEscape(serviceOperation)}</sdncadapter:SvcOperation>
622                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackUrl)}</sdncadapter:CallbackUrl>
623                                         <sdncadapter:MsoAction>vfmodule</sdncadapter:MsoAction>
624                                 </sdncadapter:RequestHeader>
625                                         <sdncadapterworkflow:SDNCRequestData></sdncadapterworkflow:SDNCRequestData>
626                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
627
628
629                         execution.setVariable("DoCVNF_getSDNCRequest", SDNCGetRequest)
630                         logger.debug("Outgoing GetSDNCRequest is: \n" + SDNCGetRequest)
631
632                 }catch(Exception e){
633                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
634                                         "Exception Occurred Processing preProcessSDNCGetRequest. ", "BPMN", MsoLogger.getServiceName(),
635                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
636                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during prepareProvision Method:\n" + e.getMessage())
637                 }
638                 logger.trace("COMPLETED preProcessSDNCGetRequest Process")
639         }
640
641         /**
642          * Prepare a Request for invoking the UpdateAAIGenericVnf subflow.
643          *
644          * @param execution The flow's execution instance.
645          */
646         public void prepUpdateAAIGenericVnf(DelegateExecution execution) {
647                 def method = getClass().getSimpleName() + '.prepUpdateAAIGenericVnf(' +
648                         'execution=' + execution.getId() +
649                         ')'
650                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
651                 logger.trace('Entered ' + method)
652
653                 try {
654                         def vnfId = execution.getVariable('DoCVNF_vnfId')
655                         logger.debug("VNF ID: " + vnfId)
656
657                         String updateAAIGenericVnfRequest = """
658                                         <UpdateAAIGenericVnfRequest>
659                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
660                                                 <orchestration-status>Active</orchestration-status>
661                                         </UpdateAAIGenericVnfRequest>
662                                 """
663                                 updateAAIGenericVnfRequest = utils.formatXml(updateAAIGenericVnfRequest)
664                                 execution.setVariable('DoCVNF_updateAAIGenericVnfRequest', updateAAIGenericVnfRequest)
665                                 logger.debug('Request for UpdateAAIGenericVnf:\n' + updateAAIGenericVnfRequest)
666
667
668                         logger.trace('Exited ' + method)
669                 } catch (BpmnError e) {
670                         throw e;
671                 } catch (Exception e) {
672                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
673                                         "Caught exception in " + method, "BPMN", MsoLogger.getServiceName(),
674                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
675                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateAAIGenericVnf(): ' + e.getMessage())
676                 }
677         }
678
679
680 }