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 action) {
71 String uuid = execution.getVariable('testReqId') // for junits
73 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
76 def callbackURL = execution.getVariable("sdncCallbackUrl")
77 def requestId = execution.getVariable("msoRequestId")
78 def serviceId = execution.getVariable("sliceServiceInstanceId")
79 def vnfType = execution.getVariable("serviceType")
80 def vnfName = execution.getVariable("sliceServiceInstanceName")
81 def tenantId = execution.getVariable("sliceServiceInstanceId")
82 def source = execution.getVariable("sliceServiceInstanceId")
83 def vnfId = execution.getVariable("sliceServiceInstanceId")
84 def cloudSiteId = execution.getVariable("sliceServiceInstanceId")
85 def serviceModelInfo = execution.getVariable("serviceModelInfo")
86 def vnfModelInfo = execution.getVariable("serviceModelInfo")
87 def globalSubscriberId = execution.getVariable("globalSubscriberId")
89 String vnfNameString = """<vnf-name>${MsoUtils.xmlEscape(vnfName)}</vnf-name>"""
90 String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
91 String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
93 String sdncVNFParamsXml = ""
95 if (execution.getVariable("vnfParamsExistFlag") == true) {
96 sdncVNFParamsXml = buildSDNCParamsXml(execution)
102 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
103 xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
104 xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
105 <sdncadapter:RequestHeader>
106 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
107 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
108 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
109 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
110 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
111 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
112 </sdncadapter:RequestHeader>
113 <sdncadapterworkflow:SDNCRequestData>
114 <request-information>
115 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
116 <request-action>AllocateTnNssi</request-action>
117 <source>${MsoUtils.xmlEscape(source)}</source>
121 </request-information>
122 <service-information>
123 <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
124 <subscription-service-type>${MsoUtils.xmlEscape(serviceId)}</subscription-service-type>
125 ${serviceEcompModelInformation}
126 <service-instance-id>${MsoUtils.xmlEscape(svcInstId)}</service-instance-id>
127 <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
128 </service-information>
130 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
131 <vnf-type>${MsoUtils.xmlEscape(vnfType)}</vnf-type>
132 ${vnfEcompModelInformation}
136 <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
137 <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
140 </sdncadapterworkflow:SDNCRequestData>
141 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
143 logger.debug("sdncRequest: " + sdncRequest)
147 String buildSDNCParamsXml(DelegateExecution execution) {
149 StringBuilder sb = new StringBuilder()
150 Map<String, String> paramsMap = execution.getVariable("TNNSSMF_vnfParamsMap")
152 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
154 String key = entry.getKey();
155 String value = entry.getValue()
156 paramsXml = """<${key}>$value</$key>"""
157 params = sb.append(paramsXml)
162 void validateSDNCResponse(DelegateExecution execution, String response, String method) {
163 validateSDNCResponse(execution, response, method, true)
166 void validateSDNCResponse(DelegateExecution execution, String response, String method, boolean exceptionOnErr) {
167 logger.debug("STARTED ValidateSDNCResponse Process")
171 String prefix = execution.setVariable("prefix")
172 if (isBlank(prefix)) {
173 if (exceptionOnErr) {
174 msg = "validateSDNCResponse: prefix is null"
176 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
181 WorkflowException workflowException = execution.getVariable("WorkflowException")
182 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
184 logger.debug("workflowException: " + workflowException)
186 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
188 String sdncResponse = response
189 if (execution.getVariable(prefix + 'sdncResponseSuccess') == true) {
190 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse)
191 RollbackData rollbackData = execution.getVariable("rollbackData")
193 if (method.equals("allocate")) {
194 rollbackData.put("VNFMODULE", "rollbackSDNCRequestAllocate", "true")
195 } else if (method.equals("deallocate")) {
196 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeallocate", "true")
197 } else if (method.equals("activate")) {
198 rollbackData.put("VNFMODULE", "rollbackSDNCRequestActivate", "true")
199 } else if (method.equals("deactivate")) {
200 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeactivate", "true")
201 } else if (method.equals("modify")) {
202 rollbackData.put("VNFMODULE", "rollbackSDNCRequestModify", "true")
204 execution.setVariable("rollbackData", rollbackData)
206 if (exceptionOnErr) {
207 msg = "validateSDNCResponse: bad Response from SDNC Adapter for " + method + " SDNC Call."
209 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
213 logger.debug("COMPLETED ValidateSDNCResponse Process")
216 String getExecutionInputParams(DelegateExecution execution) {
217 String res = "msoRequestId=" + execution.getVariable("msoRequestId") +
218 ", modelInvariantUuid=" + execution.getVariable("modelInvariantUuid") +
219 ", modelUuid=" + execution.getVariable("modelUuid") +
220 ", serviceInstanceID=" + execution.getVariable("serviceInstanceID") +
221 ", operationType=" + execution.getVariable("operationType") +
222 ", globalSubscriberId=" + execution.getVariable("globalSubscriberId") +
223 ", dummyServiceId=" + execution.getVariable("dummyServiceId") +
224 ", nsiId=" + execution.getVariable("nsiId") +
225 ", networkType=" + execution.getVariable("networkType") +
226 ", subscriptionServiceType=" + execution.getVariable("subscriptionServiceType") +
227 ", jobId=" + execution.getVariable("jobId") +
228 ", sliceParams=" + execution.getVariable("sliceParams") +
229 ", servicename=" + execution.getVariable("servicename")
234 String getFirstSnssaiFromSliceProfile(String sliceProfileStr) {
235 String snssaiListStr = jsonUtil.getJsonValue(sliceProfileStr, "snssaiList")
236 String snssai = jsonUtil.StringArrayToList(snssaiListStr).get(0)
241 String getFirstPlmnIdFromSliceProfile(String sliceProfileStr) {
242 String plmnListStr = jsonUtil.getJsonValue(sliceProfileStr, "plmnIdList")
243 String res = jsonUtil.StringArrayToList(plmnListStr).get(0)
248 void createRelationShipInAAI(DelegateExecution execution, AAIResourceUri uri, Relationship relationship) {
249 logger.debug("createRelationShipInAAI Start")
251 AAIResourcesClient client = new AAIResourcesClient()
253 if (!client.exists(uri)) {
254 logger.info("ERROR: createRelationShipInAAI: not exist: uri={}", uri)
257 AAIResourceUri from = ((AAIResourceUri) (uri.clone())).relationshipAPI()
258 client.create(from, relationship)
260 } catch (BpmnError e) {
262 } catch (Exception ex) {
263 msg = "Exception in createRelationShipInAAI. " + ex.getMessage()
265 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
267 logger.debug("createRelationShipInAAI Exit")
270 void attachLogicalLinkToAllottedResource(DelegateExecution execution, String aaiVersion, AAIResourceUri arUri,
271 String logicalLinkId) {
274 String toLink = "aai/${aaiVersion}/network/logical-links/logical-link/${logicalLinkId}"
276 Relationship relationship = new Relationship()
277 relationship.setRelatedLink(toLink)
278 relationship.setRelatedTo("logical-link")
279 relationship.setRelationshipLabel("org.onap.relationships.inventory.ComposedOf")
281 createRelationShipInAAI(execution, arUri, relationship)