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.aaiclient.client.aai.AAIResourcesClient
27 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.common.scripts.MsoUtils
30 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
31 import org.onap.so.bpmn.core.RollbackData
32 import org.onap.so.bpmn.core.UrnPropertiesReader
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.slf4j.Logger
36 import org.slf4j.LoggerFactory
38 import static org.apache.commons.lang3.StringUtils.isBlank
41 private static final Logger logger = LoggerFactory.getLogger(TnNssmfUtils.class);
44 ExceptionUtil exceptionUtil = new ExceptionUtil()
45 JsonUtils jsonUtil = new JsonUtils()
46 MsoUtils msoUtils = new MsoUtils()
47 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
53 void setSdncCallbackUrl(DelegateExecution execution, boolean exceptionOnErr) {
54 setSdncCallbackUrl(execution, "sdncCallbackUrl", exceptionOnErr)
57 void setSdncCallbackUrl(DelegateExecution execution, String variableName, boolean exceptionOnErr) {
58 String sdncCallbackUrl = UrnPropertiesReader.getVariable('mso.workflow.sdncadapter.callback', execution)
60 if (isBlank(sdncCallbackUrl) && exceptionOnErr) {
61 String msg = "mso.workflow.sdncadapter.callback is null"
63 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
65 execution.setVariable(variableName, sdncCallbackUrl)
69 String buildSDNCRequest(DelegateExecution execution, String svcInstId, String svcAction) {
73 reqAction = "AllocateTransportSliceInstance"
76 reqAction = "DeleteTransportSliceInstance"
79 reqAction = "ActivateTransportSliceInstance"
82 reqAction = "DeactivateTransportSliceInstance"
88 buildSDNCRequest(execution, svcInstId, svcAction, reqAction)
91 String buildSDNCRequest(DelegateExecution execution, String svcInstId, String svcAction, String reqAction) {
93 String uuid = execution.getVariable('testReqId') // for junits
95 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
98 def callbackURL = execution.getVariable("sdncCallbackUrl")
99 def requestId = execution.getVariable("msoRequestId")
100 def serviceId = execution.getVariable("sliceServiceInstanceId")
101 def subServiceType = execution.getVariable("subscriptionServiceType")
102 def vnfType = execution.getVariable("serviceType")
103 def vnfName = execution.getVariable("sliceServiceInstanceName")
104 def tenantId = execution.getVariable("sliceServiceInstanceId")
105 def source = execution.getVariable("sliceServiceInstanceId")
106 def vnfId = execution.getVariable("sliceServiceInstanceId")
107 def cloudSiteId = execution.getVariable("sliceServiceInstanceId")
108 def serviceModelInfo = execution.getVariable("serviceModelInfo")
109 def vnfModelInfo = execution.getVariable("serviceModelInfo")
110 def globalSubscriberId = execution.getVariable("globalSubscriberId")
112 String vnfNameString = """<vnf-name>${MsoUtils.xmlEscape(vnfName)}</vnf-name>"""
113 String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
114 String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
116 String sdncVNFParamsXml = ""
118 if (execution.getVariable("vnfParamsExistFlag") == true) {
119 sdncVNFParamsXml = buildSDNCParamsXml(execution)
121 sdncVNFParamsXml = buildDefaultVnfInputParams(vnfId)
125 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
126 xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
127 xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
128 <sdncadapter:RequestHeader>
129 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
130 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
131 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(svcAction)}</sdncadapter:SvcAction>
132 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
133 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
134 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
135 </sdncadapter:RequestHeader>
136 <sdncadapterworkflow:SDNCRequestData>
137 <request-information>
138 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
139 <request-action>${MsoUtils.xmlEscape(reqAction)}</request-action>
140 <source>${MsoUtils.xmlEscape(source)}</source>
144 </request-information>
145 <service-information>
146 <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
147 <subscription-service-type>${MsoUtils.xmlEscape(subServiceType)}</subscription-service-type>
148 ${serviceEcompModelInformation}
149 <service-instance-id>${MsoUtils.xmlEscape(svcInstId)}</service-instance-id>
150 <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
151 </service-information>
153 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
154 <vnf-type>${MsoUtils.xmlEscape(vnfType)}</vnf-type>
155 ${vnfEcompModelInformation}
159 <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
160 <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
163 </sdncadapterworkflow:SDNCRequestData>
164 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
166 logger.debug("sdncRequest: " + sdncRequest)
171 String buildDefaultVnfInputParams(String vnfName) {
173 """<vnf-input-parameters>
175 <name>${MsoUtils.xmlEscape(vnfName)}</name>
177 </vnf-input-parameters>"""
182 String buildSDNCParamsXml(DelegateExecution execution) {
184 StringBuilder sb = new StringBuilder()
185 Map<String, String> paramsMap = execution.getVariable("TNNSSMF_vnfParamsMap")
187 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
189 String key = entry.getKey();
190 String value = entry.getValue()
191 paramsXml = """<${key}>$value</$key>"""
192 params = sb.append(paramsXml)
197 void validateSDNCResponse(DelegateExecution execution, String response, String method) {
198 validateSDNCResponse(execution, response, method, true)
201 void validateSDNCResponse(DelegateExecution execution, String response, String method, boolean exceptionOnErr) {
202 logger.debug("STARTED ValidateSDNCResponse Process")
206 String prefix = execution.getVariable("prefix")
207 if (isBlank(prefix)) {
208 if (exceptionOnErr) {
209 msg = "validateSDNCResponse: prefix is null"
211 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
216 WorkflowException workflowException = execution.getVariable("WorkflowException")
217 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
219 logger.debug("TnNssmfUtils.validateSDNCResponse: SDNCResponse: " + response)
220 logger.debug("TnNssmfUtils.validateSDNCResponse: workflowException: " + workflowException)
222 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
224 String sdncResponse = response
225 if (execution.getVariable(prefix + 'sdncResponseSuccess') == true) {
226 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse)
227 RollbackData rollbackData = execution.getVariable("rollbackData")
228 if (rollbackData == null) {
229 rollbackData = new RollbackData()
232 if (method.equals("allocate")) {
233 rollbackData.put("VNFMODULE", "rollbackSDNCRequestAllocate", "true")
234 } else if (method.equals("deallocate")) {
235 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeallocate", "true")
236 } else if (method.equals("activate")) {
237 rollbackData.put("VNFMODULE", "rollbackSDNCRequestActivate", "true")
238 } else if (method.equals("deactivate")) {
239 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeactivate", "true")
240 } else if (method.equals("modify")) {
241 rollbackData.put("VNFMODULE", "rollbackSDNCRequestModify", "true")
243 execution.setVariable("rollbackData", rollbackData)
245 if (exceptionOnErr) {
246 msg = "TnNssmfUtils.validateSDNCResponse: bad Response from SDNC Adapter for " + method + " SDNC Call."
248 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
252 logger.debug("COMPLETED ValidateSDNCResponse Process")
255 String getExecutionInputParams(DelegateExecution execution) {
256 String res = "\n msoRequestId=" + execution.getVariable("msoRequestId") +
257 "\n modelInvariantUuid=" + execution.getVariable("modelInvariantUuid") +
258 "\n modelUuid=" + execution.getVariable("modelUuid") +
259 "\n serviceInstanceID=" + execution.getVariable("serviceInstanceID") +
260 "\n operationType=" + execution.getVariable("operationType") +
261 "\n globalSubscriberId=" + execution.getVariable("globalSubscriberId") +
262 "\n dummyServiceId=" + execution.getVariable("dummyServiceId") +
263 "\n nsiId=" + execution.getVariable("nsiId") +
264 "\n networkType=" + execution.getVariable("networkType") +
265 "\n subscriptionServiceType=" + execution.getVariable("subscriptionServiceType") +
266 "\n jobId=" + execution.getVariable("jobId") +
267 "\n sliceParams=" + execution.getVariable("sliceParams") +
268 "\n servicename=" + execution.getVariable("servicename")
273 String getFirstSnssaiFromSliceProfile(String sliceProfileStr) {
274 String snssaiListStr = jsonUtil.getJsonValue(sliceProfileStr, "snssaiList")
275 String snssai = jsonUtil.StringArrayToList(snssaiListStr).get(0)
280 String getFirstPlmnIdFromSliceProfile(String sliceProfileStr) {
281 String plmnListStr = jsonUtil.getJsonValue(sliceProfileStr, "plmnIdList")
282 String res = jsonUtil.StringArrayToList(plmnListStr).get(0)
287 void createRelationShipInAAI(DelegateExecution execution, AAIResourceUri uri, Relationship relationship) {
288 logger.debug("createRelationShipInAAI Start")
290 AAIResourcesClient client = new AAIResourcesClient()
292 if (!client.exists(uri)) {
293 logger.info("ERROR: createRelationShipInAAI: not exist: uri={}", uri)
296 AAIResourceUri from = ((AAIResourceUri) (uri.clone())).relationshipAPI()
297 client.create(from, relationship)
299 } catch (BpmnError e) {
301 } catch (Exception ex) {
302 msg = "Exception in createRelationShipInAAI. " + ex.getMessage()
304 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
306 logger.debug("createRelationShipInAAI Exit")
309 void attachLogicalLinkToAllottedResource(DelegateExecution execution, String aaiVersion, AAIResourceUri arUri,
310 String logicalLinkId) {
312 String toLink = "aai/${aaiVersion}/network/logical-links/logical-link/${logicalLinkId}"
314 Relationship relationship = new Relationship()
315 relationship.setRelatedLink(toLink)
316 relationship.setRelatedTo("logical-link")
317 relationship.setRelationshipLabel("org.onap.relationships.inventory.ComposedOf")
319 createRelationShipInAAI(execution, arUri, relationship)
322 void attachNetworkPolicyToAllottedResource(DelegateExecution execution, String aaiVersion,
323 AAIResourceUri aaiResourceUri, String networkPolicyId) {
325 String toLink = "aai/${aaiVersion}/network/network-policies/network-policy/${networkPolicyId}"
327 Relationship relationship = new Relationship()
328 relationship.setRelatedLink(toLink)
329 relationship.setRelatedTo("network-policy")
330 relationship.setRelationshipLabel("org.onap.relationships.inventory.Uses")
332 createRelationShipInAAI(execution, aaiResourceUri, relationship)