2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import groovy.json.JsonSlurper
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.*
28 import org.onap.aaiclient.client.aai.AAIResourcesClient
29 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.common.scripts.RequestDBUtil
36 import org.onap.so.bpmn.core.json.JsonUtils
37 import org.onap.so.db.request.beans.ResourceOperationStatus
38 import org.slf4j.Logger
39 import org.slf4j.LoggerFactory
41 import static org.apache.commons.lang3.StringUtils.*
43 public class DoModifyTnNssi extends AbstractServiceTaskProcessor {
44 String Prefix = "TNMOD_"
46 ExceptionUtil exceptionUtil = new ExceptionUtil()
47 JsonUtils jsonUtil = new JsonUtils()
48 RequestDBUtil requestDBUtil = new RequestDBUtil()
49 TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
50 JsonSlurper jsonSlurper = new JsonSlurper()
51 ObjectMapper objectMapper = new ObjectMapper()
52 private static final Logger logger = LoggerFactory.getLogger(DoModifyTnNssi.class)
55 void preProcessRequest(DelegateExecution execution) {
56 logger.debug("Start preProcessRequest")
57 execution.setVariable("prefix", Prefix)
61 execution.setVariable("startTime", System.currentTimeMillis())
62 msg = tnNssmfUtils.getExecutionInputParams(execution)
63 logger.debug("Modify TN NSSI input parameters: " + msg)
65 execution.setVariable("prefix", Prefix)
67 tnNssmfUtils.setSdncCallbackUrl(execution, true)
68 logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
70 String additionalPropJsonStr = execution.getVariable("sliceParams")
71 if (isBlank(additionalPropJsonStr)) {
72 msg = "ERROR: additionalPropJsonStr is null"
74 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
77 String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
78 if (isBlank(sliceServiceInstanceId)) {
79 msg = "ERROR: sliceServiceInstanceId is null"
81 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
83 execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
85 String sliceServiceInstanceName = execution.getVariable("servicename")
86 execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
88 String operationId = UUID.randomUUID().toString()
89 execution.setVariable("operationId", operationId)
91 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
92 String modelUuid = execution.getVariable("modelUuid")
93 if (isEmpty(modelUuid)) {
94 modelUuid = tnNssmfUtils.getModelUuidFromServiceInstance(execution.getVariable("serviceInstanceID"))
97 def isDebugLogEnabled = true
98 execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
99 String serviceModelInfo = """{
100 "modelInvariantUuid":"${modelInvariantUuid}",
101 "modelUuid":"${modelUuid}",
104 execution.setVariable("serviceModelInfo", serviceModelInfo)
106 tnNssmfUtils.setExecVarFromJsonStr(execution, additionalPropJsonStr,
107 "sliceProfile", "sliceProfile", true)
109 tnNssmfUtils.setExecVarFromJsonStr(execution, additionalPropJsonStr,
110 "transportSliceNetworks", "transportSliceNetworks", true)
111 logger.debug("transportSliceNetworks: " + execution.getVariable("transportSliceNetworks"))
113 tnNssmfUtils.setExecVarFromJsonStr(execution, additionalPropJsonStr,
114 "nsiInfo", "nsiInfo", true)
116 if (isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution, additionalPropJsonStr,
117 "enableSdnc", "enableSdnc"))) {
118 tnNssmfUtils.setEnableSdncConfig(execution)
120 } catch (BpmnError e) {
122 } catch (Exception ex) {
123 msg = "Exception in preProcessRequest " + ex.getMessage()
125 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
127 logger.debug("Finish preProcessRequest")
131 void deleteServiceInstance(DelegateExecution execution) {
133 AAIResourcesClient client = getAAIClient()
134 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceID")))
136 } catch (BpmnError e) {
138 } catch (Exception ex) {
139 String msg = "Exception in DoDeallocateTnNssi.deleteServiceInstance. " + ex.getMessage()
141 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
146 void getExistingServiceInstance(DelegateExecution execution) {
147 String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
149 AAIResourcesClient resourceClient = getAAIClient()
150 AAIResourceUri ssServiceuri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstanceId))
153 if (resourceClient.exists(ssServiceuri)) {
154 ServiceInstance ss = resourceClient.get(ServiceInstance.class, ssServiceuri)
156 AllottedResources ars = ss.getAllottedResources()
157 List<AllottedResource> arList = ars.getAllottedResource()
158 List<String> arIdList = new ArrayList<>()
159 Map<String, String> policyMap = new HashMap<>()
160 Map<String, List<String>> logicalLinksMap = new HashMap<>()
161 for (AllottedResource ar : arList) {
162 String arId = ar.getId()
164 String policyId = tnNssmfUtils.getPolicyIdFromAr(execution, serviceInstanceId, arId, true)
165 policyMap.put(arId, policyId)
166 List<String> logicalLinkList = tnNssmfUtils.getLogicalLinkNamesFromAr(execution,
167 serviceInstanceId, arId, true)
168 logicalLinksMap.put(arId, logicalLinkList)
170 execution.setVariable("arIdList", arIdList)
171 execution.setVariable("arPolicyMap", policyMap)
172 execution.setVariable("arLogicalLinkMap", logicalLinksMap)
174 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
175 "associate allotted resource for service :" + serviceInstanceId)
177 } catch (BpmnError e) {
179 } catch (Exception ex) {
180 String msg = "Exception in getServiceInstance. " + ex.getMessage()
182 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
186 void updateTnNssiInAAI(DelegateExecution execution) {
187 getExistingServiceInstance(execution)
188 updateTsciNetworks(execution)
191 void updateServiceInstance(DelegateExecution execution) {
192 String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
194 ServiceInstance ss = new ServiceInstance()
195 //ss.setServiceInstanceId(ssInstanceId)
196 String serviceStatus = "modified"
197 ss.setOrchestrationStatus(serviceStatus)
198 AAIResourcesClient client = getAAIClient()
199 AAIResourceUri uri = AAIUriFactory.createResourceUri(
200 AAIFluentTypeBuilder.business()
201 .customer(execution.getVariable("globalSubscriberId"))
202 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
203 .serviceInstance(ssInstanceId))
204 client.update(uri, ss)
205 } catch (BpmnError e) {
207 } catch (Exception ex) {
208 String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance. " + ex.getMessage()
210 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
214 void updateSliceProfile(DelegateExecution execution) {
216 String sliceserviceInstanceId = execution.getVariable("sliceServiceInstanceId")
217 String sliceProfileStr = execution.getVariable("sliceProfile")
218 String sliceProfileId = execution.getVariable("sliceProfileId")
219 SliceProfile sliceProfile = new SliceProfile();
220 sliceProfile.setProfileId(sliceProfileId)
221 sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
222 sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
223 sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)) //TODO: should be list
225 sliceProfile.setE2ELatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
226 sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
229 sliceProfile.setReliability(new Object())
231 AAIResourcesClient client = getAAIClient()
232 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(sliceserviceInstanceId).sliceProfile(sliceProfileId))
233 client.update(uri, sliceProfile)
235 } catch (BpmnError e) {
237 } catch (Exception ex) {
238 String msg = "Exception in updateSliceProfile. " + ex.getMessage()
240 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
244 String getValidArId(DelegateExecution execution, String arIdStr) {
245 List<String> arIdList = execution.getVariable("arIdList")
247 * If arId is not specified by the caller, then we assume the caller
248 * wants to modify the first network (i.e., allotted resource) in the TSCi tree.
250 String arId = isBlank(arIdStr) ? arIdList.get(0) : arIdStr
255 void updateLogicalLinksInAr(DelegateExecution execution, String arId, String linkArrayJsonStr) {
257 String serviceInstanceId = execution.getVariable('sliceServiceInstanceId')
260 * Each TSCi connection-link in linkArrayJsonStr is considered as an "ADD" new
261 * link to allotted-resource. So, if the link already exists under AR, then do
262 * nothing. Otherwise, create logical-link.
264 List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayJsonStr)
265 for (String linkStr : linkStrList) {
266 if (logicalLinkExists(execution, arId, linkStr)) {
270 createLogicalLinkForAllocatedResource(execution, linkStr, serviceInstanceId, arId)
272 } catch (BpmnError e) {
274 } catch (Exception ex) {
275 exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
276 "Exception in updateLogicalLinksInAr" + ex.getMessage())
280 void updateLogicalLinksInNetwork(DelegateExecution execution, String networkJsonStr) {
282 String arId = getValidArId(jsonUtil.getJsonValue(networkJsonStr, "id"))
283 String linkArrayStr = jsonUtil.getJsonValue(networkJsonStr, "connectionLinks")
284 updateLogicalLinksInAr(execution, arId, linkArrayStr)
285 } catch (BpmnError e) {
287 } catch (Exception ex) {
288 exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
289 "Exception in updateLogicalLinksInNetwork" + ex.getMessage())
293 void updateTsciNetworks(DelegateExecution execution) {
295 List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
296 for (String networkStr : networkStrList) {
297 updateLogicalLinksInNetwork(execution, networkStr)
298 updateNetworkPolicy(execution, networkStr)
301 } catch (BpmnError e) {
303 } catch (Exception ex) {
304 exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
305 "Exception in updateTsciNetworks" + ex.getMessage())
309 int getMaxBw(DelegateExecution execution) {
312 String sliceProfileStr = execution.getVariable("sliceProfile")
313 if (isBlank(sliceProfileStr)) {
314 String msg = "ERROR: getMaxBw: sliceProfile is null"
316 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
318 String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
319 if (isNotBlank(bwStr)) {
320 maxBw = Integer.parseInt(bwStr)
322 logger.error("ERROR: getMaxBw: maxBandwidth is null")
324 } catch (BpmnError e) {
326 } catch (Exception ex) {
327 exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
328 "Exception in getMaxBw" + ex.getMessage())
334 void updatePolicyMaxBandwidthInAAI(DelegateExecution execution, String policyId, int maxBw) {
336 NetworkPolicy networkPolicy = new NetworkPolicy()
337 networkPolicy.setMaxBandwidth(maxBw)
338 AAIResourceUri networkPolicyUri =
339 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(policyId))
340 getAAIClient().update(networkPolicyUri, networkPolicy)
341 } catch (BpmnError e) {
343 } catch (Exception ex) {
344 String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance. " + ex.getMessage()
346 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
350 void updateNetworkPolicy(DelegateExecution execution, String networkJsonStr) {
352 int maxBw = getMaxBw(execution)
354 String arId = getValidArId(jsonUtil.getJsonValue(networkJsonStr, "id"))
355 Map<String, String> policyMap = execution.getVariable("arPolicyMap")
356 String policyId = policyMap.get(arId)
357 if (isBlank(policyId)) {
358 String msg = String.format("ERROR: updateNetworkPolicy: policyId not found. arId=%s", arId)
360 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
363 updatePolicyMaxBandwidthInAAI(execution, policyId, maxBw)
365 } catch (BpmnError e) {
367 } catch (Exception ex) {
368 exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
369 "Exception in updateNetworkPolicy" + ex.getMessage())
374 void createLogicalLinkForAllocatedResource(DelegateExecution execution,
375 String linkJsonStr, String ssInstanceId,
376 String allottedResourceId) {
378 AAIResourceUri allottedResourceUri = tnNssmfUtils.buildAllottedResourceUri(execution,
379 ssInstanceId, allottedResourceId)
381 if (!getAAIClient().exists(allottedResourceUri)) {
382 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
387 String linkId = jsonUtil.getJsonValue(linkJsonStr, "id")
388 if (isBlank(linkId)) {
389 linkId = "tn-nssmf-" + UUID.randomUUID().toString()
391 logger.debug("createLogicalLinkForAllocatedResource: linkId=" + linkId)
393 String epA = jsonUtil.getJsonValue(linkJsonStr, "transportEndpointA")
394 String epB = jsonUtil.getJsonValue(linkJsonStr, "transportEndpointB")
395 String modelInvariantId = execution.getVariable("modelInvariantUuid")
396 String modelVersionId = execution.getVariable("modelUuid")
398 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
399 resource.setLinkId(linkId)
400 resource.setLinkName(epA)
401 resource.setLinkName2(epB)
402 resource.setLinkType("TsciConnectionLink")
403 resource.setInMaint(false)
406 AAIResourceUri logicalLinkUri =
407 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
408 getAAIClient().create(logicalLinkUri, resource)
410 tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
411 allottedResourceUri, epA);
412 } catch (BpmnError e) {
414 } catch (Exception ex) {
415 String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
417 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
422 void preprocessSdncModifyTnNssiRequest(DelegateExecution execution) {
423 def method = getClass().getSimpleName() + '.preprocessSdncModifyTnNssiRequest(' +
424 'execution=' + execution.getId() + ')'
425 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
426 logger.trace('Entered ' + method)
429 String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
431 String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "modify")
433 execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
434 logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
436 } catch (BpmnError e) {
438 } catch (Exception e) {
439 logger.debug("Exception Occured Processing preprocessSdncModifyTnNssiRequest. Exception is:\n" + e)
440 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessSDNCActivateRequest Method:\n" + e.getMessage())
442 logger.trace("COMPLETED preprocessSdncModifyTnNssiRequest Process")
446 void validateSDNCResponse(DelegateExecution execution, String response, String method) {
447 tnNssmfUtils.validateSDNCResponse(execution, response, method)
451 void updateAAIOrchStatus(DelegateExecution execution) {
452 logger.debug("Start updateAAIOrchStatus")
453 String sliceServiceInstanceId = execution.getVariable("sliceServiceInstanceId")
454 String orchStatus = execution.getVariable("orchestrationStatus")
457 ServiceInstance si = new ServiceInstance()
458 si.setOrchestrationStatus(orchStatus)
459 AAIResourcesClient client = getAAIClient()
460 AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceServiceInstanceId))
461 client.update(uri, si)
462 } catch (BpmnError e) {
464 } catch (Exception ex) {
465 String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
467 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
470 logger.debug("Finish updateAAIOrchStatus")
473 void prepareUpdateJobStatus(DelegateExecution execution,
476 String statusDescription) {
477 String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
478 String modelUuid = execution.getVariable("modelUuid")
479 String jobId = execution.getVariable("jobId")
480 String nsiId = execution.getVariable("nsiId")
481 String operType = "MODIFY"
483 ResourceOperationStatus roStatus = tnNssmfUtils.buildRoStatus(modelUuid, ssInstanceId,
484 jobId, nsiId, operType, status, progress, statusDescription)
486 requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
489 boolean logicalLinkExists(DelegateExecution execution, String arIdStr, String linkJsonStr) {
490 if (isBlank(arIdStr)) {
491 logger.error("ERROR: logicalLinkExists: arIdStr is empty")
494 if (isBlank(linkJsonStr)) {
495 logger.error("ERROR: logicalLinkExists: linkJsonStr is empty")
499 Map<String, List<String>> logicalLinksMap = execution.getVariable("arLogicalLinkMap")
500 if (logicalLinksMap == null) {
501 logger.error("ERROR: logicalLinkExists: logicalLinksMap is null")
505 List<String> logicalLinkNameList = logicalLinksMap.get(arIdStr)
506 if (logicalLinksMap == null) {
507 logger.error("ERROR: logicalLinkExists: logicalLinkNameList is null. arIdStr=" + arIdStr)
511 String linkName = jsonUtil.getJsonValue(linkJsonStr, "transportEndpointA")
512 if (isBlank(linkName)) {
513 logger.error("ERROR: logicalLinkExists: epA is empty")
517 return logicalLinkNameList.contains(linkName)