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