Implemented OOF call in TN NSSMF for deallocate flow
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / TnNssmfUtils.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. 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.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.AllottedResources
26 import org.onap.aai.domain.yang.LogicalLink
27 import org.onap.aai.domain.yang.NetworkPolicy
28 import org.onap.aai.domain.yang.Relationship
29 import org.onap.aai.domain.yang.ServiceInstance
30 import org.onap.aaiclient.client.aai.AAIResourcesClient
31 import org.onap.aaiclient.client.aai.AAIVersion
32 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
33 import org.onap.aaiclient.client.aai.entities.Relationships
34 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
35 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
36 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
37 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
38 import org.onap.so.bpmn.common.scripts.ExceptionUtil
39 import org.onap.so.bpmn.common.scripts.MsoUtils
40 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
41 import org.onap.so.bpmn.core.RollbackData
42 import org.onap.so.bpmn.core.UrnPropertiesReader
43 import org.onap.so.bpmn.core.WorkflowException
44 import org.onap.so.bpmn.core.json.JsonUtils
45 import org.onap.so.db.request.beans.ResourceOperationStatus
46 import org.slf4j.Logger
47 import org.slf4j.LoggerFactory
48
49 import static org.apache.commons.lang3.StringUtils.isBlank
50
51 class TnNssmfUtils {
52     static final String AAI_VERSION = AAIVersion.LATEST
53     private static final Logger logger = LoggerFactory.getLogger(TnNssmfUtils.class);
54
55
56     ExceptionUtil exceptionUtil = new ExceptionUtil()
57     JsonUtils jsonUtil = new JsonUtils()
58     MsoUtils msoUtils = new MsoUtils()
59     SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
60
61     TnNssmfUtils() {
62     }
63
64
65     void setSdncCallbackUrl(DelegateExecution execution, boolean exceptionOnErr) {
66         setSdncCallbackUrl(execution, "sdncCallbackUrl", exceptionOnErr)
67     }
68
69     void setSdncCallbackUrl(DelegateExecution execution, String variableName, boolean exceptionOnErr) {
70         String sdncCallbackUrl = UrnPropertiesReader.getVariable('mso.workflow.sdncadapter.callback', execution)
71
72         if (isBlank(sdncCallbackUrl) && exceptionOnErr) {
73             String msg = "mso.workflow.sdncadapter.callback is null"
74             logger.debug(msg)
75             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
76         } else {
77             execution.setVariable(variableName, sdncCallbackUrl)
78         }
79     }
80
81     String buildSDNCRequest(DelegateExecution execution, String svcInstId, String svcAction) {
82         String reqAction
83         switch (svcAction) {
84             case "create":
85                 reqAction = "AllocateTransportSliceInstance"
86                 break
87             case "delete":
88                 reqAction = "DeleteTransportSliceInstance"
89                 break
90             case "activate":
91                 reqAction = "ActivateTransportSliceInstance"
92                 break
93             case "deactivate":
94                 reqAction = "DeactivateTransportSliceInstance"
95                 break
96             case "update":
97                 reqAction = "ModifyTransportSliceInstance"
98                 break
99             default:
100                 reqAction = svcAction
101         }
102
103         buildSDNCRequest(execution, svcInstId, svcAction, reqAction)
104     }
105
106     String buildSDNCRequest(DelegateExecution execution, String svcInstId, String svcAction, String reqAction) {
107
108         String uuid = execution.getVariable('testReqId') // for junits
109         if (uuid == null) {
110             uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
111         }
112
113         def callbackURL = execution.getVariable("sdncCallbackUrl")
114         def requestId = execution.getVariable("msoRequestId")
115         def serviceId = execution.getVariable("sliceServiceInstanceId")
116         def subServiceType = execution.getVariable("subscriptionServiceType")
117         def vnfType = execution.getVariable("serviceType")
118         def vnfName = execution.getVariable("sliceServiceInstanceName")
119         def tenantId = execution.getVariable("sliceServiceInstanceId")
120         def source = execution.getVariable("sliceServiceInstanceId")
121         def vnfId = execution.getVariable("sliceServiceInstanceId")
122         def cloudSiteId = execution.getVariable("sliceServiceInstanceId")
123         def serviceModelInfo = execution.getVariable("serviceModelInfo")
124         def vnfModelInfo = execution.getVariable("serviceModelInfo")
125         def globalSubscriberId = execution.getVariable("globalSubscriberId")
126
127         String vnfNameString = """<vnf-name>${MsoUtils.xmlEscape(vnfName)}</vnf-name>"""
128         String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
129         String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
130
131         String sdncVNFParamsXml = ""
132
133         if (execution.getVariable("vnfParamsExistFlag") == true) {
134             sdncVNFParamsXml = buildSDNCParamsXml(execution)
135         } else {
136             sdncVNFParamsXml = buildDefaultVnfInputParams(vnfId)
137         }
138
139         String sdncRequest =
140                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
141                                                     xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
142                                                     xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
143          <sdncadapter:RequestHeader>
144             <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
145             <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
146             <sdncadapter:SvcAction>${MsoUtils.xmlEscape(svcAction)}</sdncadapter:SvcAction>
147             <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
148             <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
149             <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
150          </sdncadapter:RequestHeader>
151     <sdncadapterworkflow:SDNCRequestData>
152         <request-information>
153             <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
154             <request-action>${MsoUtils.xmlEscape(reqAction)}</request-action>
155             <source>${MsoUtils.xmlEscape(source)}</source>
156             <notification-url/>
157             <order-number/>
158             <order-version/>
159         </request-information>
160         <service-information>
161             <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
162             <subscription-service-type>${MsoUtils.xmlEscape(subServiceType)}</subscription-service-type>
163             ${serviceEcompModelInformation}
164             <service-instance-id>${MsoUtils.xmlEscape(svcInstId)}</service-instance-id>
165             <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
166         </service-information>
167         <vnf-information>
168             <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
169             <vnf-type>${MsoUtils.xmlEscape(vnfType)}</vnf-type>
170             ${vnfEcompModelInformation}
171         </vnf-information>
172         <vnf-request-input>
173             ${vnfNameString}
174             <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
175             <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
176             ${sdncVNFParamsXml}
177         </vnf-request-input>
178     </sdncadapterworkflow:SDNCRequestData>
179     </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
180
181         logger.debug("sdncRequest:  " + sdncRequest)
182         return sdncRequest
183     }
184
185
186     String buildDefaultVnfInputParams(String vnfName) {
187         String res =
188                 """<vnf-input-parameters>
189                       <param>
190                           <name>${MsoUtils.xmlEscape(vnfName)}</name>
191                       </param>
192                    </vnf-input-parameters>"""
193
194         return res
195     }
196
197     String buildSDNCParamsXml(DelegateExecution execution) {
198         String params = ""
199         StringBuilder sb = new StringBuilder()
200         Map<String, String> paramsMap = execution.getVariable("TNNSSMF_vnfParamsMap")
201
202         for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
203             String paramsXml
204             String key = entry.getKey();
205             String value = entry.getValue()
206             paramsXml = """<${key}>$value</$key>"""
207             params = sb.append(paramsXml)
208         }
209         return params
210     }
211
212     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
213         validateSDNCResponse(execution, response, method, true)
214     }
215
216     void validateSDNCResponse(DelegateExecution execution, String response, String method, boolean exceptionOnErr) {
217         logger.debug("STARTED ValidateSDNCResponse Process")
218
219         String msg
220
221         String prefix = execution.getVariable("prefix")
222         if (isBlank(prefix)) {
223             if (exceptionOnErr) {
224                 msg = "validateSDNCResponse: prefix is null"
225                 logger.error(msg)
226                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
227             }
228             return
229         }
230
231         WorkflowException workflowException = execution.getVariable("WorkflowException")
232         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
233
234         logger.debug("TnNssmfUtils.validateSDNCResponse: SDNCResponse: " + response)
235         logger.debug("TnNssmfUtils.validateSDNCResponse: workflowException: " + workflowException)
236
237         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
238
239         String sdncResponse = response
240         if (execution.getVariable(prefix + 'sdncResponseSuccess') == true) {
241             logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse)
242             RollbackData rollbackData = execution.getVariable("rollbackData")
243             if (rollbackData == null) {
244                 rollbackData = new RollbackData()
245             }
246
247             if (method.equals("allocate")) {
248                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestAllocate", "true")
249             } else if (method.equals("deallocate")) {
250                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeallocate", "true")
251             } else if (method.equals("activate")) {
252                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestActivate", "true")
253             } else if (method.equals("deactivate")) {
254                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeactivate", "true")
255             } else if (method.equals("modify")) {
256                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestModify", "true")
257             }
258             execution.setVariable("rollbackData", rollbackData)
259         } else {
260             if (exceptionOnErr) {
261                 msg = "TnNssmfUtils.validateSDNCResponse: bad Response from SDNC Adapter for " + method + " SDNC Call."
262                 logger.error(msg)
263                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
264             }
265         }
266
267         logger.debug("COMPLETED ValidateSDNCResponse Process")
268     }
269
270     String getExecutionInputParams(DelegateExecution execution) {
271         String res = "\n msoRequestId=" + execution.getVariable("msoRequestId") +
272                 "\n modelInvariantUuid=" + execution.getVariable("modelInvariantUuid") +
273                 "\n modelUuid=" + execution.getVariable("modelUuid") +
274                 "\n serviceInstanceID=" + execution.getVariable("serviceInstanceID") +
275                 "\n operationType=" + execution.getVariable("operationType") +
276                 "\n globalSubscriberId=" + execution.getVariable("globalSubscriberId") +
277                 "\n dummyServiceId=" + execution.getVariable("dummyServiceId") +
278                 "\n nsiId=" + execution.getVariable("nsiId") +
279                 "\n networkType=" + execution.getVariable("networkType") +
280                 "\n subscriptionServiceType=" + execution.getVariable("subscriptionServiceType") +
281                 "\n jobId=" + execution.getVariable("jobId") +
282                 "\n sliceParams=" + execution.getVariable("sliceParams") +
283                 "\n servicename=" + execution.getVariable("servicename")
284
285         return res
286     }
287
288     String getFirstSnssaiFromSliceProfile(String sliceProfileStr) {
289         String snssaiListStr = jsonUtil.getJsonValue(sliceProfileStr, "snssaiList")
290         String snssai = jsonUtil.StringArrayToList(snssaiListStr).get(0)
291
292         return snssai
293     }
294
295     String getFirstPlmnIdFromSliceProfile(String sliceProfileStr) {
296         String plmnListStr = jsonUtil.getJsonValue(sliceProfileStr, "pLMNIdList")
297         String res = jsonUtil.StringArrayToList(plmnListStr).get(0)
298
299         return res
300     }
301
302     void createRelationShipInAAI(DelegateExecution execution, AAIResourceUri uri, Relationship relationship) {
303         logger.debug("createRelationShipInAAI Start")
304         String msg
305         AAIResourcesClient client = new AAIResourcesClient()
306         try {
307             if (!client.exists(uri)) {
308                 logger.info("ERROR: createRelationShipInAAI: not exist: uri={}", uri)
309                 return
310             }
311             AAIResourceUri from = ((AAIResourceUri) (uri.clone())).relationshipAPI()
312             client.create(from, relationship)
313
314         } catch (BpmnError e) {
315             throw e
316         } catch (Exception ex) {
317             msg = "Exception in createRelationShipInAAI. " + ex.getMessage()
318             logger.info(msg)
319             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
320         }
321         logger.debug("createRelationShipInAAI Exit")
322     }
323
324     void attachLogicalLinkToAllottedResource(DelegateExecution execution, String aaiVersion, AAIResourceUri arUri,
325                                              String logicalLinkId) {
326
327         String toLink = "aai/${aaiVersion}/network/logical-links/logical-link/${logicalLinkId}"
328
329         Relationship relationship = new Relationship()
330         relationship.setRelatedLink(toLink)
331         relationship.setRelatedTo("logical-link")
332         relationship.setRelationshipLabel("org.onap.relationships.inventory.ComposedOf")
333
334         createRelationShipInAAI(execution, arUri, relationship)
335     }
336
337     void attachNetworkPolicyToAllottedResource(DelegateExecution execution, String aaiVersion,
338                                                AAIResourceUri aaiResourceUri, String networkPolicyId) {
339
340         String toLink = "aai/${aaiVersion}/network/network-policies/network-policy/${networkPolicyId}"
341
342         Relationship relationship = new Relationship()
343         relationship.setRelatedLink(toLink)
344         relationship.setRelatedTo("network-policy")
345         relationship.setRelationshipLabel("org.onap.relationships.inventory.Uses")
346
347         createRelationShipInAAI(execution, aaiResourceUri, relationship)
348
349     }
350
351     ResourceOperationStatus buildRoStatus(String nsstId,
352                                           String nssiId,
353                                           String jobId,
354                                           String nsiId,
355                                           String action,
356                                           String status,
357                                           String progress,
358                                           String statusDescription) {
359         ResourceOperationStatus roStatus = new ResourceOperationStatus()
360         roStatus.setResourceTemplateUUID(nsstId)
361         roStatus.setResourceInstanceID(nssiId)
362         roStatus.setServiceId(nsiId)
363         roStatus.setOperationId(jobId)
364         roStatus.setOperType(action)
365         roStatus.setProgress(progress)
366         roStatus.setStatus(status)
367         roStatus.setStatusDescription(statusDescription)
368
369         return roStatus
370     }
371
372
373     void setEnableSdncConfig(DelegateExecution execution) {
374         String enableSdnc = UrnPropertiesReader.getVariable(
375                 "mso.workflow.TnNssmf.enableSDNCNetworkConfig")
376         if (isBlank(enableSdnc)) {
377             logger.debug("mso.workflow.TnNssmf.enableSDNCNetworkConfig is undefined, so use default value (true)")
378             enableSdnc = "true"
379         }
380         logger.debug("setEnableSdncConfig: enableSdnc=" + enableSdnc)
381
382         execution.setVariable("enableSdnc", enableSdnc)
383     }
384
385     void setEnableOofConfig(DelegateExecution execution) {
386         String enableOof = UrnPropertiesReader.getVariable(
387                 "mso.workflow.TnNssmf.enableOOFNetworkConfig")
388         if (isBlank(enableOof)) {
389             logger.debug("mso.workflow.TnNssmf.enableOOFNetworkConfig is undefined, so use default value (true)")
390             enableOof = "true"
391         }
392         logger.debug("setEnableOofConfig: enableOof=" + enableOof)
393
394         execution.setVariable("enableOof", enableOof)
395     }
396
397     String setExecVarFromJsonIfExists(DelegateExecution execution,
398                                       String jsonStr, String jsonKey, String varName) {
399         return setExecVarFromJsonStr(execution, jsonStr, jsonKey, varName, false)
400     }
401
402     String setExecVarFromJsonStr(DelegateExecution execution,
403                                  String jsonStr, String jsonKey, String varName,
404                                  boolean exceptionOnErr) {
405         String msg = ""
406         String valueStr = jsonUtil.getJsonValue(jsonStr, jsonKey)
407         if (isBlank(valueStr)) {
408             if (exceptionOnErr) {
409                 msg = "cannot find " + jsonKey + " in " + jsonStr
410                 logger.debug(msg)
411                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
412             }
413         } else {
414             execution.setVariable(varName, valueStr)
415         }
416
417         return valueStr
418     }
419
420     ServiceInstance getServiceInstanceFromAai(String serviceInstanceId) {
421         if (isBlank(serviceInstanceId)) {
422             logger.error("ERROR: getServiceInstanceFromAai: serviceInstanceId is blank")
423             return null
424         }
425
426         ServiceInstance nssi = null
427         AAIResourcesClient client = new AAIResourcesClient()
428         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.Types.SERVICE_INSTANCE
429                 .getFragment(serviceInstanceId))
430         Optional<ServiceInstance> nssiOpt = client.get(ServiceInstance.class, uri)
431
432         if (nssiOpt.isPresent()) {
433             nssi = nssiOpt.get()
434             return nssi
435         } else {
436             String msg = String.format("ERROR: getServiceInstanceFromAai: NSSI %s not found in AAI", serviceInstanceId)
437             logger.error(msg)
438         }
439
440         return nssi;
441     }
442
443     String getModelUuidFromServiceInstance(String serviceInstanceId) {
444         ServiceInstance si = getServiceInstanceFromAai(serviceInstanceId)
445         if (si == null) {
446             String msg = String.format("ERROR: getModelUuidFromServiceInstance: getServiceInstanceFromAai() failed. " +
447                     "serviceInstanceId=%s", serviceInstanceId)
448             logger.error(msg)
449             return null
450         }
451
452         return si.getModelVersionId()
453     }
454
455     AAIResourceUri buildNetworkPolicyUri(String networkPolicyId) {
456         AAIResourceUri networkPolicyUri =
457                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
458
459         return networkPolicyUri
460     }
461
462     AAIResourceUri buildAllottedResourceUri(DelegateExecution execution, String serviceInstanceId,
463                                             String allottedResourceId) {
464
465         AAIResourceUri allottedResourceUri =
466                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
467                         .customer(execution.getVariable("globalSubscriberId"))
468                         .serviceSubscription(execution.getVariable("subscriptionServiceType"))
469                         .serviceInstance(serviceInstanceId)
470                         .allottedResource(allottedResourceId))
471
472         return allottedResourceUri
473     }
474
475     AAIPluralResourceUri buildAllottedResourcesUri(DelegateExecution execution, String serviceInstanceId) {
476
477         AAIPluralResourceUri arsUri =
478                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
479                         .customer(execution.getVariable("globalSubscriberId"))
480                         .serviceSubscription(execution.getVariable("subscriptionServiceType"))
481                         .serviceInstance(serviceInstanceId)
482                         .allottedResources())
483
484         return arsUri
485     }
486
487     AllottedResources getAllottedResourcesFromAai(DelegateExecution execution, String serviceInstanceId, boolean exceptionOnErr) {
488         AllottedResources res
489         try {
490             AAIResourcesClient client = new AAIResourcesClient()
491
492             AAIPluralResourceUri arsUri = buildAllottedResourcesUri(execution, serviceInstanceId)
493
494             //AAIResultWrapper wrapperAllotted = client.get(arsUri, NotFoundException.class)
495             //Optional<AllottedResources> allAllotted = wrapperAllotted.asBean(AllottedResources.class)
496             //AllottedResources allottedResources = allAllotted.get()
497
498             Optional<AllottedResources> arsOpt = client.get(AllottedResources.class, arsUri)
499             if (arsOpt.isPresent()) {
500                 res = arsOpt.get()
501                 return res
502             } else {
503                 String msg = String.format("ERROR: getAllottedResourcesFromAai: ars not found. nssiId=%s", serviceInstanceId)
504                 logger.error(msg)
505                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
506             }
507         } catch (BpmnError e) {
508             if (exceptionOnErr) {
509                 throw e;
510             }
511         } catch (Exception ex) {
512             if (exceptionOnErr) {
513                 String msg = String.format("ERROR: getAllottedResourcesFromAai: %s", ex.getMessage())
514                 logger.error(msg)
515                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
516             }
517         }
518
519         return res
520     }
521
522     String getPolicyIdFromAr(DelegateExecution execution, String serviceInstanceId,
523                              String arId, boolean exceptionOnErr) {
524         String res
525         try {
526             AAIResourcesClient client = new AAIResourcesClient()
527
528             AAIResourceUri arUri = buildAllottedResourceUri(execution, serviceInstanceId, arId)
529             List<AAIResourceUri> policyUriList = getRelationshipUriListInAai(execution, arUri,
530                     AAIFluentTypeBuilder.Types.NETWORK_POLICY, exceptionOnErr)
531             for (AAIResourceUri policyUri : policyUriList) {
532                 Optional<NetworkPolicy> policyOpt = client.get(NetworkPolicy.class, policyUri)
533                 if (policyOpt.isPresent()) {
534                     NetworkPolicy policy = policyOpt.get()
535                     return policy.getNetworkPolicyId()
536                 } else {
537                     String msg = String.format("ERROR: getPolicyIdFromAr: arUri=%s", policyUri)
538                     logger.error(msg)
539                     exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
540                 }
541             }
542         } catch (BpmnError e) {
543             if (exceptionOnErr) {
544                 throw e;
545             }
546         } catch (Exception ex) {
547             if (exceptionOnErr) {
548                 String msg = String.format("ERROR: getPolicyIdFromAr: %s", ex.getMessage())
549                 logger.error(msg)
550                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
551             }
552         }
553
554         return res
555     }
556
557
558     List<AAIResourceUri> getRelationshipUriListInAai(DelegateExecution execution,
559                                                      AAIResourceUri uri,
560                                                      Object info,
561                                                      boolean exceptionOnErr) {
562         AAIResourcesClient client = new AAIResourcesClient()
563         AAIResultWrapper wrapper = client.get(uri);
564         Optional<Relationships> relationships = wrapper.getRelationships()
565         if (relationships.isPresent()) {
566             return relationships.get().getRelatedUris(info)
567         } else {
568             if (exceptionOnErr) {
569                 String msg = "ERROR: getRelationshipUriListInAai: No relationship found"
570                 logger.error(msg)
571                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
572             }
573         }
574
575         return null
576     }
577
578     List<String> getLogicalLinkNamesFromAr(DelegateExecution execution, String serviceInstanceId,
579                                            String arId, boolean exceptionOnErr) {
580         List<String> res = new ArrayList<>()
581         try {
582             AAIResourcesClient client = new AAIResourcesClient()
583
584             AAIResourceUri arUri = buildAllottedResourceUri(execution, serviceInstanceId, arId)
585             List<AAIResourceUri> logicalLinkUriList = getRelationshipUriListInAai(execution, arUri,
586                     AAIFluentTypeBuilder.Types.LOGICAL_LINK, exceptionOnErr)
587             for (AAIResourceUri logicalLinkUri : logicalLinkUriList) {
588                 Optional<LogicalLink> logicalLinkOpt = client.get(LogicalLink.class, logicalLinkUri)
589                 if (logicalLinkOpt.isPresent()) {
590                     LogicalLink logicalLink = logicalLinkOpt.get()
591                     res.add(logicalLink.getLinkName())
592                 } else {
593                     String msg = String.format("ERROR: getLogicalLinkNamesFromAr: logicalLinkUri=%s", logicalLinkUri)
594                     logger.error(msg)
595                     exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
596                 }
597             }
598         } catch (BpmnError e) {
599             if (exceptionOnErr) {
600                 throw e;
601             }
602         } catch (Exception ex) {
603             if (exceptionOnErr) {
604                 String msg = String.format("ERROR: getLogicalLinkNamesFromAr: %s", ex.getMessage())
605                 logger.error(msg)
606                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
607             }
608         }
609
610         return res
611     }
612 }