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 org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.Relationship
26 import org.onap.aai.domain.yang.ServiceInstance
27 import org.onap.aaiclient.client.aai.AAIResourcesClient
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.common.scripts.MsoUtils
33 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
34 import org.onap.so.bpmn.core.RollbackData
35 import org.onap.so.bpmn.core.UrnPropertiesReader
36 import org.onap.so.bpmn.core.WorkflowException
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.db.request.beans.ResourceOperationStatus
39 import org.slf4j.Logger
40 import org.slf4j.LoggerFactory
42 import static org.apache.commons.lang3.StringUtils.isBlank
45 private static final Logger logger = LoggerFactory.getLogger(TnNssmfUtils.class);
48 ExceptionUtil exceptionUtil = new ExceptionUtil()
49 JsonUtils jsonUtil = new JsonUtils()
50 MsoUtils msoUtils = new MsoUtils()
51 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
57 void setSdncCallbackUrl(DelegateExecution execution, boolean exceptionOnErr) {
58 setSdncCallbackUrl(execution, "sdncCallbackUrl", exceptionOnErr)
61 void setSdncCallbackUrl(DelegateExecution execution, String variableName, boolean exceptionOnErr) {
62 String sdncCallbackUrl = UrnPropertiesReader.getVariable('mso.workflow.sdncadapter.callback', execution)
64 if (isBlank(sdncCallbackUrl) && exceptionOnErr) {
65 String msg = "mso.workflow.sdncadapter.callback is null"
67 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
69 execution.setVariable(variableName, sdncCallbackUrl)
73 String buildSDNCRequest(DelegateExecution execution, String svcInstId, String svcAction) {
77 reqAction = "AllocateTransportSliceInstance"
80 reqAction = "DeleteTransportSliceInstance"
83 reqAction = "ActivateTransportSliceInstance"
86 reqAction = "DeactivateTransportSliceInstance"
92 buildSDNCRequest(execution, svcInstId, svcAction, reqAction)
95 String buildSDNCRequest(DelegateExecution execution, String svcInstId, String svcAction, String reqAction) {
97 String uuid = execution.getVariable('testReqId') // for junits
99 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
102 def callbackURL = execution.getVariable("sdncCallbackUrl")
103 def requestId = execution.getVariable("msoRequestId")
104 def serviceId = execution.getVariable("sliceServiceInstanceId")
105 def subServiceType = execution.getVariable("subscriptionServiceType")
106 def vnfType = execution.getVariable("serviceType")
107 def vnfName = execution.getVariable("sliceServiceInstanceName")
108 def tenantId = execution.getVariable("sliceServiceInstanceId")
109 def source = execution.getVariable("sliceServiceInstanceId")
110 def vnfId = execution.getVariable("sliceServiceInstanceId")
111 def cloudSiteId = execution.getVariable("sliceServiceInstanceId")
112 def serviceModelInfo = execution.getVariable("serviceModelInfo")
113 def vnfModelInfo = execution.getVariable("serviceModelInfo")
114 def globalSubscriberId = execution.getVariable("globalSubscriberId")
116 String vnfNameString = """<vnf-name>${MsoUtils.xmlEscape(vnfName)}</vnf-name>"""
117 String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
118 String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
120 String sdncVNFParamsXml = ""
122 if (execution.getVariable("vnfParamsExistFlag") == true) {
123 sdncVNFParamsXml = buildSDNCParamsXml(execution)
125 sdncVNFParamsXml = buildDefaultVnfInputParams(vnfId)
129 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
130 xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
131 xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
132 <sdncadapter:RequestHeader>
133 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
134 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
135 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(svcAction)}</sdncadapter:SvcAction>
136 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
137 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
138 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
139 </sdncadapter:RequestHeader>
140 <sdncadapterworkflow:SDNCRequestData>
141 <request-information>
142 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
143 <request-action>${MsoUtils.xmlEscape(reqAction)}</request-action>
144 <source>${MsoUtils.xmlEscape(source)}</source>
148 </request-information>
149 <service-information>
150 <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
151 <subscription-service-type>${MsoUtils.xmlEscape(subServiceType)}</subscription-service-type>
152 ${serviceEcompModelInformation}
153 <service-instance-id>${MsoUtils.xmlEscape(svcInstId)}</service-instance-id>
154 <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
155 </service-information>
157 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
158 <vnf-type>${MsoUtils.xmlEscape(vnfType)}</vnf-type>
159 ${vnfEcompModelInformation}
163 <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
164 <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
167 </sdncadapterworkflow:SDNCRequestData>
168 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
170 logger.debug("sdncRequest: " + sdncRequest)
175 String buildDefaultVnfInputParams(String vnfName) {
177 """<vnf-input-parameters>
179 <name>${MsoUtils.xmlEscape(vnfName)}</name>
181 </vnf-input-parameters>"""
186 String buildSDNCParamsXml(DelegateExecution execution) {
188 StringBuilder sb = new StringBuilder()
189 Map<String, String> paramsMap = execution.getVariable("TNNSSMF_vnfParamsMap")
191 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
193 String key = entry.getKey();
194 String value = entry.getValue()
195 paramsXml = """<${key}>$value</$key>"""
196 params = sb.append(paramsXml)
201 void validateSDNCResponse(DelegateExecution execution, String response, String method) {
202 validateSDNCResponse(execution, response, method, true)
205 void validateSDNCResponse(DelegateExecution execution, String response, String method, boolean exceptionOnErr) {
206 logger.debug("STARTED ValidateSDNCResponse Process")
210 String prefix = execution.getVariable("prefix")
211 if (isBlank(prefix)) {
212 if (exceptionOnErr) {
213 msg = "validateSDNCResponse: prefix is null"
215 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
220 WorkflowException workflowException = execution.getVariable("WorkflowException")
221 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
223 logger.debug("TnNssmfUtils.validateSDNCResponse: SDNCResponse: " + response)
224 logger.debug("TnNssmfUtils.validateSDNCResponse: workflowException: " + workflowException)
226 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
228 String sdncResponse = response
229 if (execution.getVariable(prefix + 'sdncResponseSuccess') == true) {
230 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse)
231 RollbackData rollbackData = execution.getVariable("rollbackData")
232 if (rollbackData == null) {
233 rollbackData = new RollbackData()
236 if (method.equals("allocate")) {
237 rollbackData.put("VNFMODULE", "rollbackSDNCRequestAllocate", "true")
238 } else if (method.equals("deallocate")) {
239 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeallocate", "true")
240 } else if (method.equals("activate")) {
241 rollbackData.put("VNFMODULE", "rollbackSDNCRequestActivate", "true")
242 } else if (method.equals("deactivate")) {
243 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeactivate", "true")
244 } else if (method.equals("modify")) {
245 rollbackData.put("VNFMODULE", "rollbackSDNCRequestModify", "true")
247 execution.setVariable("rollbackData", rollbackData)
249 if (exceptionOnErr) {
250 msg = "TnNssmfUtils.validateSDNCResponse: bad Response from SDNC Adapter for " + method + " SDNC Call."
252 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
256 logger.debug("COMPLETED ValidateSDNCResponse Process")
259 String getExecutionInputParams(DelegateExecution execution) {
260 String res = "\n msoRequestId=" + execution.getVariable("msoRequestId") +
261 "\n modelInvariantUuid=" + execution.getVariable("modelInvariantUuid") +
262 "\n modelUuid=" + execution.getVariable("modelUuid") +
263 "\n serviceInstanceID=" + execution.getVariable("serviceInstanceID") +
264 "\n operationType=" + execution.getVariable("operationType") +
265 "\n globalSubscriberId=" + execution.getVariable("globalSubscriberId") +
266 "\n dummyServiceId=" + execution.getVariable("dummyServiceId") +
267 "\n nsiId=" + execution.getVariable("nsiId") +
268 "\n networkType=" + execution.getVariable("networkType") +
269 "\n subscriptionServiceType=" + execution.getVariable("subscriptionServiceType") +
270 "\n jobId=" + execution.getVariable("jobId") +
271 "\n sliceParams=" + execution.getVariable("sliceParams") +
272 "\n servicename=" + execution.getVariable("servicename")
277 String getFirstSnssaiFromSliceProfile(String sliceProfileStr) {
278 String snssaiListStr = jsonUtil.getJsonValue(sliceProfileStr, "snssaiList")
279 String snssai = jsonUtil.StringArrayToList(snssaiListStr).get(0)
284 String getFirstPlmnIdFromSliceProfile(String sliceProfileStr) {
285 String plmnListStr = jsonUtil.getJsonValue(sliceProfileStr, "plmnIdList")
286 String res = jsonUtil.StringArrayToList(plmnListStr).get(0)
291 void createRelationShipInAAI(DelegateExecution execution, AAIResourceUri uri, Relationship relationship) {
292 logger.debug("createRelationShipInAAI Start")
294 AAIResourcesClient client = new AAIResourcesClient()
296 if (!client.exists(uri)) {
297 logger.info("ERROR: createRelationShipInAAI: not exist: uri={}", uri)
300 AAIResourceUri from = ((AAIResourceUri) (uri.clone())).relationshipAPI()
301 client.create(from, relationship)
303 } catch (BpmnError e) {
305 } catch (Exception ex) {
306 msg = "Exception in createRelationShipInAAI. " + ex.getMessage()
308 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
310 logger.debug("createRelationShipInAAI Exit")
313 void attachLogicalLinkToAllottedResource(DelegateExecution execution, String aaiVersion, AAIResourceUri arUri,
314 String logicalLinkId) {
316 String toLink = "aai/${aaiVersion}/network/logical-links/logical-link/${logicalLinkId}"
318 Relationship relationship = new Relationship()
319 relationship.setRelatedLink(toLink)
320 relationship.setRelatedTo("logical-link")
321 relationship.setRelationshipLabel("org.onap.relationships.inventory.ComposedOf")
323 createRelationShipInAAI(execution, arUri, relationship)
326 void attachNetworkPolicyToAllottedResource(DelegateExecution execution, String aaiVersion,
327 AAIResourceUri aaiResourceUri, String networkPolicyId) {
329 String toLink = "aai/${aaiVersion}/network/network-policies/network-policy/${networkPolicyId}"
331 Relationship relationship = new Relationship()
332 relationship.setRelatedLink(toLink)
333 relationship.setRelatedTo("network-policy")
334 relationship.setRelationshipLabel("org.onap.relationships.inventory.Uses")
336 createRelationShipInAAI(execution, aaiResourceUri, relationship)
340 ResourceOperationStatus buildRoStatus(String nsstId,
347 String statusDescription) {
348 ResourceOperationStatus roStatus = new ResourceOperationStatus()
349 roStatus.setResourceTemplateUUID(nsstId)
350 roStatus.setResourceInstanceID(nssiId)
351 roStatus.setServiceId(nsiId)
352 roStatus.setOperationId(jobId)
353 roStatus.setOperType(action)
354 roStatus.setProgress(progress)
355 roStatus.setStatus(status)
356 roStatus.setStatusDescription(statusDescription)
362 void setEnableSdncConfig(DelegateExecution execution) {
363 String enableSdnc = UrnPropertiesReader.getVariable(
364 "mso.workflow.TnNssmf.enableSDNCNetworkConfig")
365 if (isBlank(enableSdnc)) {
366 logger.debug("mso.workflow.TnNssmf.enableSDNCNetworkConfig is undefined, so use default value (true)")
370 logger.debug("setEnableSdncConfig: enableSdnc=" + enableSdnc)
372 execution.setVariable("enableSdnc", enableSdnc)
375 String setExecVarFromJsonIfExists(DelegateExecution execution,
376 String jsonStr, String jsonKey, String varName) {
377 return setExecVarFromJsonStr(execution, jsonStr, jsonKey, varName, false)
380 String setExecVarFromJsonStr(DelegateExecution execution,
381 String jsonStr, String jsonKey, String varName,
382 boolean exceptionOnErr) {
384 String valueStr = jsonUtil.getJsonValue(jsonStr, jsonKey)
385 if (isBlank(valueStr)) {
386 if (exceptionOnErr) {
387 msg = "cannot find " + jsonKey + " in " + jsonStr
389 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
392 execution.setVariable(varName, valueStr)
398 ServiceInstance getServiceInstanceFromAai(String serviceInstanceId) {
399 if (isBlank(serviceInstanceId)) {
400 logger.error("ERROR: getServiceInstanceFromAai: serviceInstanceId is blank")
404 ServiceInstance nssi = null
405 AAIResourcesClient client = new AAIResourcesClient()
406 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.Types.SERVICE_INSTANCE
407 .getFragment(serviceInstanceId))
408 Optional<ServiceInstance> nssiOpt = client.get(ServiceInstance.class, uri)
410 if (nssiOpt.isPresent()) {
414 String msg = String.format("ERROR: getServiceInstanceFromAai: NSSI %s not found in AAI", serviceInstanceId)
421 String getModelUuidFromServiceInstance(String serviceInstanceId) {
422 ServiceInstance si = getServiceInstanceFromAai(serviceInstanceId)
424 String msg = String.format("ERROR: getModelUuidFromServiceInstance: getServiceInstanceFromAai() failed. " +
425 "serviceInstanceId=%s", serviceInstanceId)
430 return si.modelVersionId()