3c08779513eaba5eb855f60e97555cf7b6900957
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / vcpe / scripts / DoCreateAllottedResourceBRG.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.vcpe.scripts;
22
23 import org.onap.so.bpmn.common.scripts.*;
24 import org.onap.so.bpmn.common.scripts.AaiUtil
25 import org.onap.so.bpmn.core.RollbackData
26 import org.onap.so.bpmn.core.WorkflowException
27 import org.onap.so.bpmn.core.UrnPropertiesReader
28 import org.onap.so.bpmn.core.json.JsonUtils
29 import org.onap.so.rest.APIResponse
30
31 import java.util.UUID;
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34 import org.apache.commons.lang3.*
35 import org.springframework.web.util.UriUtils;
36 import static org.apache.commons.lang3.StringUtils.*
37
38 import org.onap.so.logger.MessageEnum
39 import org.onap.so.logger.MsoLogger
40 import org.onap.so.client.aai.AAIResourcesClient
41 import org.onap.so.client.aai.AAIObjectType
42 import org.onap.so.client.aai.entities.AAIResultWrapper
43 import org.onap.so.client.aai.entities.Relationships
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri
45 import org.onap.so.client.aai.entities.uri.AAIUriFactory
46 import org.json.JSONObject
47 import javax.ws.rs.NotFoundException
48 /**
49  * This groovy class supports the <class>DoCreateAllottedResourceBRG.bpmn</class> process.
50  *
51  * @author
52  *
53  * Inputs:
54  * @param - msoRequestId
55  * @param - isDEbugLogEnabled
56  * @param - disableRollback
57  * @param - failExists  - O
58  * @param - serviceInstanceId
59  * @param - globalCustomerId - O
60  * @param - subscriptionServiceType - O
61  * @param - parentServiceInstanceId
62  * @param - allottedReourceId - O
63  * @param - allottedResourceModelInfo
64  * @param - allottedResourceRole
65  * @param - allottedResourceType
66  * @param - brgWanMacAddress
67  * @param - vni
68  * @param - vgmuxBearerIP
69  *
70  * Outputs:
71  * @param - rollbackData (localRB->null)
72  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
73  * @param - WorkflowException - O
74  * @param - allottedResourceId
75  * @param - allottedResourceName
76  *
77  */
78 public class DoCreateAllottedResourceBRG extends AbstractServiceTaskProcessor{
79         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateAllottedResourceBRG.class);
80
81         String Prefix="DCARBRG_"
82         ExceptionUtil exceptionUtil = new ExceptionUtil()
83         JsonUtils jsonUtil = new JsonUtils()
84
85         public void preProcessRequest (DelegateExecution execution) {
86
87
88                 String msg = ""
89                 msoLogger.trace("start preProcessRequest")
90
91                 try {
92                         execution.setVariable("prefix", Prefix)
93
94                         //Config Inputs
95                         String sdncCallbackUrl = UrnPropertiesReader.getVariable("mso.workflow.sdncadapter.callback",execution)
96                         if (isBlank(sdncCallbackUrl)) {
97                                 msg = "mso.workflow.sdncadapter.callback is null"
98                                 msoLogger.debug(msg)
99                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100                         }
101                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
102                         msoLogger.debug("SDNC Callback URL: " + sdncCallbackUrl)
103
104                         String sdncReplDelay = UrnPropertiesReader.getVariable("mso.workflow.sdnc.replication.delay",execution)
105                         if (isBlank(sdncReplDelay)) {
106                                 msg = "mso.workflow.sdnc.replication.delay is null"
107                                 msoLogger.debug(msg)
108                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109                         }
110                         execution.setVariable("sdncReplDelay", sdncReplDelay)
111                         msoLogger.debug("SDNC replication delay: " + sdncReplDelay)
112
113                         //Request Inputs
114                         if (isBlank(execution.getVariable("serviceInstanceId"))){
115                                 msg = "Input serviceInstanceId is null"
116                                 msoLogger.debug(msg)
117                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
118                         }
119                         if (isBlank(execution.getVariable("parentServiceInstanceId"))) {
120                                 msg = "Input parentServiceInstanceId is null"
121                                 msoLogger.debug(msg)
122                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
123                         }
124                         if (isBlank(execution.getVariable("allottedResourceModelInfo"))) {
125                                 msg = "Input allottedResourceModelInfo is null"
126                                 msoLogger.debug(msg)
127                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
128                         }
129                         if (isBlank(execution.getVariable("vni"))) {
130                                 msg = "Input vni is null"
131                                 msoLogger.debug(msg)
132                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
133                         }
134                         if (isBlank(execution.getVariable("vgmuxBearerIP"))) {
135                                 msg = "Input vgmuxBearerIP is null"
136                                 msoLogger.debug(msg)
137                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
138                         }
139                         if (isBlank(execution.getVariable("brgWanMacAddress"))) {
140                                 msg = "Input brgWanMacAddress is null"
141                                 msoLogger.debug(msg)
142                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
143                         }
144                         if (isBlank(execution.getVariable("allottedResourceRole"))) {
145                                 msg = "Input allottedResourceRole is null"
146                                 msoLogger.debug(msg)
147                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
148                         }
149                         if (isBlank(execution.getVariable("allottedResourceType"))) {
150                                 msg = "Input allottedResourceType is null"
151                                 msoLogger.debug(msg)
152                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
153                         }
154                 }catch(BpmnError b){
155                         msoLogger.debug("Rethrowing MSOWorkflowException")
156                         throw b
157                 } catch (Exception ex){
158                         msg = "Exception in preProcessRequest " + ex.getMessage()
159                         msoLogger.debug(msg)
160                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
161                 }
162                 msoLogger.trace("end preProcessRequest")
163         }
164
165         /**
166          * Gets the service instance uri from aai
167          */
168         public void getServiceInstance(DelegateExecution execution) {
169                 msoLogger.trace("getServiceInstance ")
170                 try {
171                         String serviceInstanceId = execution.getVariable('serviceInstanceId')
172
173                         AAIResourcesClient resourceClient = new AAIResourcesClient()
174                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
175
176                         if(resourceClient.exists(uri)){
177                                 execution.setVariable("CSI_resourceLink", uri.build().toString())
178                         }else{
179                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai")
180                         }
181
182                 }catch(BpmnError e) {
183                         throw e;
184                 }catch (Exception ex){
185                         String msg = "Exception in getServiceInstance. " + ex.getMessage()
186                         msoLogger.debug(msg)
187                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
188                 }
189                 msoLogger.trace("Exit getServiceInstance ")
190         }
191
192         public void getAaiAR (DelegateExecution execution) {
193
194
195                 msoLogger.trace("start getAaiAR")
196
197                 String arType = execution.getVariable("allottedResourceType")
198                 String arRole = execution.getVariable("allottedResourceRole")
199
200                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
201                 String orchStatus = arUtils.getAROrchStatus(execution)
202
203                 String errorMsg = ""
204
205                 if (orchStatus != null) // AR was found
206                 {
207                         if ("true".equals(execution.getVariable("failExists")))
208                         {
209                                 errorMsg = "Allotted resource " + arType + " with Role " + arRole + " already exists"
210                         }
211                         else
212                         {
213                                 if ("Active".equals(orchStatus))
214                                 {
215                                         execution.setVariable("foundActiveAR", true)
216                                 }
217                                 else // blanks included
218                                 {
219                                         errorMsg = "Allotted Resource " + arType + " with Role " + arRole + " already exists in an incomplete state -"  +  orchStatus
220                                 }
221                         }
222                 }
223                 if (!isBlank(errorMsg)) {
224                         msoLogger.debug(errorMsg)
225                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, errorMsg)
226                 }
227                 msoLogger.trace("end getAaiAR")
228         }
229
230         public void getParentServiceInstance(DelegateExecution execution) {
231                 msoLogger.trace("getParentServiceInstance ")
232                 try {
233                         String serviceInstanceId = execution.getVariable('parentServiceInstanceId')
234
235                         AAIResourcesClient resourceClient = new AAIResourcesClient()
236                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.NODES_QUERY, "").queryParam("search-node-type", "service-instance").queryParam("filter", "service-instance-id:EQUALS:" + serviceInstanceId)
237                         String json = resourceClient.get(uri).getJson()
238
239                         JSONObject obj = new JSONObject(json)
240                         if(obj.has("result-data")){
241                                 JSONObject ob = obj.getJSONArray("result-data").getJSONObject(0)
242                                 String resourceLink = ob.getString("resource-link")
243
244                                 String[] split = resourceLink.split("/aai/")
245                                 String siRelatedLink = "/aai/" + split[1]
246
247                                 execution.setVariable("PSI_resourceLink", resourceLink)
248                         }else{
249                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai")
250                         }
251
252                 }catch(BpmnError e) {
253                         throw e;
254                 }catch (Exception ex){
255                         String msg = "Exception in getParentServiceInstance. " + ex.getMessage()
256                         msoLogger.debug(msg)
257                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
258                 }
259                 msoLogger.trace("Exit getParentServiceInstance ")
260         }
261
262
263         public void createAaiAR(DelegateExecution execution) {
264
265
266                 msoLogger.trace("start createAaiAR")
267                 String msg = ""
268
269                 String allottedResourceId = execution.getVariable("allottedResourceId")
270                 if (isBlank(allottedResourceId))
271                 {
272                         allottedResourceId = UUID.randomUUID().toString()
273                         execution.setVariable("allottedResourceId", allottedResourceId)
274                 }
275                 String arUrl = ""
276                 try {
277
278                         //AAI PUT
279                         AaiUtil aaiUriUtil = new AaiUtil(this)
280                         String aaiEndpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
281                         String siResourceLink= execution.getVariable("PSI_resourceLink")
282
283                         String siUri = ""
284                         msoLogger.debug("PSI_resourceLink:" + siResourceLink)
285
286                         if(!isBlank(siResourceLink)) {
287                                 msoLogger.debug("Incoming PSI Resource Link is: " + siResourceLink)
288                                 String[] split = siResourceLink.split("/aai/")
289                                 siUri = "/aai/" + split[1]
290                         }
291                         else
292                         {
293                                 msg = "Parent Service Link in AAI is null"
294                                 msoLogger.debug(msg)
295                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
296                         }
297
298                         arUrl = "${aaiEndpoint}${siUri}"  + "/allotted-resources/allotted-resource/" + UriUtils.encode(allottedResourceId,"UTF-8")
299                         execution.setVariable("aaiARPath", arUrl)
300                         msoLogger.debug("GET AllottedResource AAI URL is:\n" + arUrl)
301
302                         String namespace = aaiUriUtil.getNamespaceFromUri(execution, arUrl)
303
304                         String arType = execution.getVariable("allottedResourceType")
305                         String arRole = execution.getVariable("allottedResourceRole")
306                         String CSI_resourceLink = execution.getVariable("CSI_resourceLink")
307                         String arModelInfo = execution.getVariable("allottedResourceModelInfo")
308                         String modelInvariantId = jsonUtil.getJsonValue(arModelInfo, "modelInvariantUuid")
309                         String modelVersionId = jsonUtil.getJsonValue(arModelInfo, "modelUuid")
310                         String modelCustomizationId = jsonUtil.getJsonValue(arModelInfo, "modelCustomizationUuid")
311
312                         if (modelInvariantId == null) {
313                                 modelInvariantId = ""
314                         }
315                         if (modelVersionId == null) {
316                                 modelVersionId = ""
317                         }
318                         if (modelCustomizationId == null) {
319                                 modelCustomizationId = ""
320                         }
321
322                         String payload =
323                         """<allotted-resource xmlns="${namespace}">
324                                 <id>${MsoUtils.xmlEscape(allottedResourceId)}</id>
325                                 <description></description>
326                                 <type>${MsoUtils.xmlEscape(arType)}</type>
327                                 <role>${MsoUtils.xmlEscape(arRole)}</role>
328                                 <selflink></selflink>
329                                 <model-invariant-id>${MsoUtils.xmlEscape(modelInvariantId)}</model-invariant-id>
330                                 <model-version-id>${MsoUtils.xmlEscape(modelVersionId)}</model-version-id>
331                                 <model-customization-id>${MsoUtils.xmlEscape(modelCustomizationId)}</model-customization-id>
332                                 <orchestration-status>PendingCreate</orchestration-status>
333                                 <operation-status></operation-status>
334                                 <relationship-list>
335                                         <relationship>
336                                 <related-to>service-instance</related-to>
337                                 <related-link>${MsoUtils.xmlEscape(CSI_resourceLink)}</related-link>
338                                         </relationship>
339                                 </relationship-list>
340                         </allotted-resource>""".trim()
341
342                         execution.setVariable("AaiARPayload", payload)
343                         msoLogger.debug(" payload to create AllottedResource in AAI:" + "\n" + payload)
344
345                         APIResponse response = aaiUriUtil.executeAAIPutCall(execution, arUrl, payload)
346                         int responseCode = response.getStatusCode()
347                         msoLogger.debug("AllottedResource AAI PUT responseCode:" + responseCode)
348
349                         String aaiResponse = response.getResponseBodyAsString()
350                         msoLogger.debug("AllottedResource AAI PUT responseStr:" + aaiResponse)
351
352                         //200 OK 201 CREATED 202 ACCEPTED
353                         if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
354                         {
355                                 msoLogger.debug("AAI PUT AllottedResource received a Good Response")
356                         }
357                         else{
358                                 msoLogger.debug("AAI Put AllottedResouce received a Bad Response Code: " + responseCode)
359                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
360                                 throw new BpmnError("MSOWorkflowException")
361                         }
362                 }catch(BpmnError b){
363                         msoLogger.debug("Rethrowing MSOWorkflowException")
364                         throw b
365                 } catch (Exception ex) {
366                         msg = "Exception in createAaiAR " + ex.getMessage()
367                         msoLogger.debug(msg)
368                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
369                 }
370
371                 //start rollback set up
372                 RollbackData rollbackData = new RollbackData()
373                 def disableRollback = execution.getVariable("disableRollback")
374                 rollbackData.put(Prefix, "disableRollback", disableRollback.toString())
375                 rollbackData.put(Prefix, "rollbackAAI", "true")
376                 rollbackData.put(Prefix, "allottedResourceId", allottedResourceId)
377                 rollbackData.put(Prefix, "serviceInstanceId", execution.getVariable("serviceInstanceId"))
378                 rollbackData.put(Prefix, "parentServiceInstanceId", execution.getVariable("parentServiceInstanceId"))
379                 rollbackData.put(Prefix, "aaiARPath", arUrl)
380                 execution.setVariable("rollbackData", rollbackData)
381                 msoLogger.trace("end createAaiAR")
382         }
383
384         public String buildSDNCRequest(DelegateExecution execution, String action, String sdncRequestId) {
385
386
387                 String msg = ""
388                 msoLogger.trace("start buildSDNCRequest")
389                 String sdncReq = null
390
391                 try {
392
393                         String allottedResourceId = execution.getVariable("allottedResourceId")
394                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
395                         String globalCustomerId = execution.getVariable("globalCustomerId")
396                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
397                         String parentServiceInstanceId = execution.getVariable("parentServiceInstanceId")
398                         String callbackUrl = execution.getVariable("sdncCallbackUrl")
399                         String requestId = execution.getVariable("msoRequestId")
400
401                         String brgWanMacAddress = execution.getVariable("brgWanMacAddress")
402                         String vni = execution.getVariable("vni")
403                         String vgmuxBearerIP = execution.getVariable("vgmuxBearerIP")
404
405                         String arModelInfo = execution.getVariable("allottedResourceModelInfo")
406                         String modelInvariantId = jsonUtil.getJsonValue(arModelInfo, "modelInvariantUuid")
407                         String modelVersion = jsonUtil.getJsonValue(arModelInfo, "modelVersion")
408                         String modelUUId = jsonUtil.getJsonValue(arModelInfo, "modelUuid")
409                         String modelCustomizationId = jsonUtil.getJsonValue(arModelInfo, "modelCustomizationUuid")
410                         String modelName = jsonUtil.getJsonValue(arModelInfo, "modelName")
411
412                         if (modelInvariantId == null) {
413                                 modelInvariantId = ""
414                         }
415                         if (modelVersion == null) {
416                                 modelVersion = ""
417                         }
418                         if (modelUUId == null) {
419                                 modelUUId = ""
420                         }
421                         if (modelName == null) {
422                                 modelName = ""
423                         }
424                         if (modelCustomizationId == null) {
425                                 modelCustomizationId = ""
426                         }
427
428                         sdncReq =
429                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
430                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
431                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
432                                    <sdncadapter:RequestHeader>
433                                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(sdncRequestId)}</sdncadapter:RequestId>
434                                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
435                                                         <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
436                                                         <sdncadapter:SvcOperation>brg-topology-operation</sdncadapter:SvcOperation>
437                                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackUrl)}</sdncadapter:CallbackUrl>
438                                         </sdncadapter:RequestHeader>
439                                 <sdncadapterworkflow:SDNCRequestData>
440                                         <request-information>
441                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
442                                                 <request-action>CreateBRGInstance</request-action>
443                                                 <source>MSO</source>
444                                                 <notification-url/>
445                                                 <order-number/>
446                                                 <order-version/>
447                                         </request-information>
448                                         <service-information>
449                                                 <service-id></service-id>
450                                                 <subscription-service-type>${MsoUtils.xmlEscape(subscriptionServiceType)}</subscription-service-type>
451                                                 <onap-model-information></onap-model-information>
452                                                 <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
453                                                 <subscriber-name/>
454                                                 <global-customer-id>${MsoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
455                                         </service-information>
456                                         <allotted-resource-information>
457                                                 <allotted-resource-id>${MsoUtils.xmlEscape(allottedResourceId)}</allotted-resource-id>
458                                                 <allotted-resource-type>brg</allotted-resource-type>
459                                                 <parent-service-instance-id>${MsoUtils.xmlEscape(parentServiceInstanceId)}</parent-service-instance-id>
460                                                 <onap-model-information>
461                                                         <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantId)}</model-invariant-uuid>
462                                                         <model-uuid>${MsoUtils.xmlEscape(modelUUId)}</model-uuid>
463                                                         <model-customization-uuid>${MsoUtils.xmlEscape(modelCustomizationId)}</model-customization-uuid>
464                                                         <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
465                                                         <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
466                                                 </onap-model-information>
467                                         </allotted-resource-information>
468                                         <brg-request-input>
469                                                         <brg-wan-mac-address>${MsoUtils.xmlEscape(brgWanMacAddress)}</brg-wan-mac-address>
470                                                         <vni>${MsoUtils.xmlEscape(vni)}</vni>
471                                                         <vgmux-bearer-ip>${MsoUtils.xmlEscape(vgmuxBearerIP)}</vgmux-bearer-ip>
472                                         </brg-request-input>
473                                 </sdncadapterworkflow:SDNCRequestData>
474                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
475
476                         msoLogger.debug("sdncRequest:\n" + sdncReq)
477                         sdncReq = utils.formatXml(sdncReq)
478
479                 } catch(Exception ex) {
480                         msg = "Exception in buildSDNCRequest. " + ex.getMessage()
481                         msoLogger.debug(msg)
482                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
483                 }
484                 msoLogger.trace("end buildSDNCRequest")
485                 return sdncReq
486         }
487
488         public void preProcessSDNCAssign(DelegateExecution execution) {
489
490
491                 String msg = ""
492                 msoLogger.trace("start preProcessSDNCAssign")
493
494                 try {
495                         String sdncRequestId = UUID.randomUUID().toString()
496                         String sdncAssignReq = buildSDNCRequest(execution, "assign", sdncRequestId)
497                         execution.setVariable("sdncAssignRequest", sdncAssignReq)
498                         msoLogger.debug("sdncAssignRequest:  " + sdncAssignReq)
499                         def sdncRequestId2 = UUID.randomUUID().toString()
500                         String sdncAssignRollbackReq = sdncAssignReq.replace(">assign<", ">unassign<").replace(">CreateBRGInstance<", ">DeleteBRGInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
501                         def rollbackData = execution.getVariable("rollbackData")
502                         rollbackData.put(Prefix, "sdncAssignRollbackReq", sdncAssignRollbackReq)
503                         execution.setVariable("rollbackData", rollbackData)
504
505                         msoLogger.debug("sdncAssignRollbackReq:\n" + sdncAssignRollbackReq)
506                         msoLogger.debug("rollbackData:\n" + rollbackData.toString())
507
508                 } catch (BpmnError e) {
509                         throw e;
510                 } catch(Exception ex) {
511                         msg = "Exception in preProcessSDNCAssign. " + ex.getMessage()
512                         msoLogger.debug(msg)
513                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
514                 }
515                 msoLogger.trace("end preProcessSDNCAssign")
516         }
517
518         public void preProcessSDNCCreate(DelegateExecution execution) {
519
520
521                 String msg = ""
522                 msoLogger.trace("start preProcessSDNCCreate")
523
524                 try {
525                         String sdncRequestId = UUID.randomUUID().toString()
526                         String sdncCreateReq = buildSDNCRequest(execution, "create", sdncRequestId)
527                         execution.setVariable("sdncCreateRequest", sdncCreateReq)
528                         msoLogger.debug("sdncCreateReq:  " + sdncCreateReq)
529                         def sdncRequestId2 = UUID.randomUUID().toString()
530                         String sdncCreateRollbackReq = sdncCreateReq.replace(">create<", ">delete<").replace(">CreateBRGInstance<", ">DeleteBRGInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
531                         def rollbackData = execution.getVariable("rollbackData")
532                         rollbackData.put(Prefix, "sdncCreateRollbackReq", sdncCreateRollbackReq)
533                         execution.setVariable("rollbackData", rollbackData)
534
535                         msoLogger.debug("sdncCreateRollbackReq:\n" + sdncCreateRollbackReq)
536                         msoLogger.debug("rollbackData:\n" + rollbackData.toString())
537
538                 } catch (BpmnError e) {
539                         throw e;
540                 } catch(Exception ex) {
541                         msg = "Exception in preProcessSDNCCreate. " + ex.getMessage()
542                         msoLogger.debug(msg)
543                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
544                 }
545                 msoLogger.trace("end preProcessSDNCCreate")
546         }
547
548         public void preProcessSDNCActivate(DelegateExecution execution) {
549
550
551                 String msg = ""
552                 msoLogger.trace("start preProcessSDNCActivate")
553
554                 try {
555                         String sdncRequestId = UUID.randomUUID().toString()
556                         String sdncActivateReq = buildSDNCRequest(execution, "activate", sdncRequestId)
557                         execution.setVariable("sdncActivateRequest", sdncActivateReq)
558                         msoLogger.debug("sdncActivateReq:  " + sdncActivateReq)
559                         def sdncRequestId2 = UUID.randomUUID().toString()
560                         String sdncActivateRollbackReq = sdncActivateReq.replace(">activate<", ">deactivate<").replace(">CreateBRGInstance<", ">DeleteBRGInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
561                         def rollbackData = execution.getVariable("rollbackData")
562                         rollbackData.put(Prefix, "sdncActivateRollbackReq", sdncActivateRollbackReq)
563                         execution.setVariable("rollbackData", rollbackData)
564
565                         msoLogger.debug("sdncActivateRollbackReq:\n" + sdncActivateRollbackReq)
566                         msoLogger.debug("rollbackData:\n" + rollbackData.toString())
567
568                 } catch (BpmnError e) {
569                         throw e;
570                 } catch(Exception ex) {
571                         msg = "Exception in preProcessSDNCActivate. " + ex.getMessage()
572                         msoLogger.debug(msg)
573                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
574                 }
575                 msoLogger.trace("end preProcessSDNCActivate")
576         }
577
578         public void validateSDNCResp(DelegateExecution execution, String response, String method){
579
580
581                 msoLogger.trace("ValidateSDNCResponse Process")
582                 String msg = ""
583
584                 try {
585                         WorkflowException workflowException = execution.getVariable("WorkflowException")
586                         msoLogger.debug("workflowException: " + workflowException)
587
588                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
589                         msoLogger.debug("SDNCResponse: " + response)
590
591                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
592                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
593
594                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
595                                 msoLogger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response)
596
597                                 if (!"get".equals(method))
598                                 {
599                                         def rollbackData = execution.getVariable("rollbackData")
600                                         rollbackData.put(Prefix, "rollback" +  "SDNC" + method, "true")
601                                         execution.setVariable("rollbackData", rollbackData)
602                                 }
603
604                         }else{
605                                 msoLogger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
606                                 throw new BpmnError("MSOWorkflowException")
607                         }
608                 } catch (BpmnError e) {
609                         throw e;
610                 } catch(Exception ex) {
611                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
612                         msoLogger.debug(msg)
613                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
614                 }
615                 msoLogger.trace("End ValidateSDNCResp Process")
616         }
617
618         public void preProcessSDNCGet(DelegateExecution execution){
619
620                 msoLogger.trace("start preProcessSDNCGet")
621                 try{
622
623                         def callbackUrl = execution.getVariable("sdncCallbackUrl")
624                         // serviceOperation (URI for topology GET) will be retrieved from "selflink" from AAI if active AR exists in AAI
625                         // or from  "object-path" in SDNC response for assign when AR does not exist in AA
626
627                         String serviceOperation = ""
628
629                         if (execution.getVariable("foundActiveAR")) {
630                                 def aaiQueryResponse = execution.getVariable("aaiARGetResponse")
631                                 serviceOperation = utils.getNodeText(aaiQueryResponse, "selflink")
632                                 msoLogger.debug("AR service operation/aaiARSelfLink: " + serviceOperation)
633                         }
634                         else
635                         {
636                                 String response = execution.getVariable("sdncAssignResponse")
637                                 String data = utils.getNodeXml(response, "response-data")
638                                 msoLogger.debug("Assign responseData: " + data)
639                                 serviceOperation = utils.getNodeText(data, "object-path")
640                                 msoLogger.debug("AR service operation:" + serviceOperation)
641                         }
642
643                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
644                         String sdncRequestId = UUID.randomUUID().toString()
645
646                         //neeed the same url as used by vfmodules
647                         String SDNCGetRequest =
648                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
649                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
650                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
651                                         <sdncadapter:RequestHeader>
652                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(sdncRequestId)}</sdncadapter:RequestId>
653                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
654                                         <sdncadapter:SvcAction>query</sdncadapter:SvcAction>
655                                         <sdncadapter:SvcOperation>${MsoUtils.xmlEscape(serviceOperation)}</sdncadapter:SvcOperation>
656                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackUrl)}</sdncadapter:CallbackUrl>
657                                         <sdncadapter:MsoAction>vfmodule</sdncadapter:MsoAction>
658                                 </sdncadapter:RequestHeader>
659                                         <sdncadapterworkflow:SDNCRequestData></sdncadapterworkflow:SDNCRequestData>
660                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
661
662                         execution.setVariable("sdncGetRequest", SDNCGetRequest)
663
664                 }catch(Exception e){
665                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occurred Processing preProcessSDNCGetRequest.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
666                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + e.getMessage())
667                 }
668                 msoLogger.trace("end preProcessSDNCGet")
669         }
670
671         public void updateAaiAROrchStatus(DelegateExecution execution, String status){
672
673                 msoLogger.trace("start updateAaiAROrchStatus")
674                 String aaiARPath = execution.getVariable("aaiARPath") //set during query (existing AR) or create
675                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
676                 String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
677                 msoLogger.trace("end updateAaiAROrchStatus")
678         }
679
680         public void generateOutputs(DelegateExecution execution)
681         {
682
683                 msoLogger.trace("start generateOutputs")
684                 try {
685                         String sdncGetResponse = execution.getVariable("enhancedCallbackRequestData") //unescaped
686                         msoLogger.debug("resp:" + sdncGetResponse)
687                         String arData = utils.getNodeXml(sdncGetResponse, "brg-topology")
688                         arData = utils.removeXmlNamespaces(arData)
689
690                         String brga = utils.getNodeXml(arData, "brg-assignments")
691                         String ari = utils.getNodeXml(arData, "allotted-resource-identifiers")
692                         execution.setVariable("allotedResourceName", utils.getNodeText(ari, "allotted-resource-name"))
693                 } catch (BpmnError e) {
694                         msoLogger.debug("BPMN Error in generateOutputs ")
695                 } catch(Exception ex) {
696                         String msg = "Exception in generateOutputs " + ex.getMessage()
697                         msoLogger.debug(msg)
698                 }
699                 msoLogger.trace("end generateOutputs")
700
701         }
702
703         public void preProcessRollback (DelegateExecution execution) {
704
705                 msoLogger.trace("start preProcessRollback")
706                 try {
707
708                         Object workflowException = execution.getVariable("WorkflowException");
709
710                         if (workflowException instanceof WorkflowException) {
711                                 msoLogger.debug("Prev workflowException: " + workflowException.getErrorMessage())
712                                 execution.setVariable("prevWorkflowException", workflowException);
713                                 //execution.setVariable("WorkflowException", null);
714                         }
715                 } catch (BpmnError e) {
716                         msoLogger.debug("BPMN Error during preProcessRollback")
717                 } catch(Exception ex) {
718                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
719                         msoLogger.debug(msg)
720                 }
721                 msoLogger.trace("end preProcessRollback")
722         }
723
724         public void postProcessRollback (DelegateExecution execution) {
725
726                 msoLogger.trace("start postProcessRollback")
727                 String msg = ""
728                 try {
729                         Object workflowException = execution.getVariable("prevWorkflowException");
730                         if (workflowException instanceof WorkflowException) {
731                                 msoLogger.debug("Setting prevException to WorkflowException: ")
732                                 execution.setVariable("WorkflowException", workflowException);
733                         }
734                         execution.setVariable("rollbackData", null)
735                 } catch (BpmnError b) {
736                         msoLogger.debug("BPMN Error during postProcessRollback")
737                         throw b;
738                 } catch(Exception ex) {
739                         msg = "Exception in postProcessRollback. " + ex.getMessage()
740                         msoLogger.debug(msg)
741                 }
742                 msoLogger.trace("end postProcessRollback")
743         }
744
745 }