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.core.type.TypeReference
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.json.JSONArray
26 import com.google.gson.JsonArray
27 import com.google.gson.GsonBuilder
28 import com.google.gson.JsonObject
29 import com.google.gson.JsonParser
30 import groovy.json.JsonSlurper
31 import org.onap.so.bpmn.common.scripts.OofUtils
32 import com.google.gson.Gson
33 import org.camunda.bpm.engine.delegate.BpmnError
34 import org.camunda.bpm.engine.delegate.DelegateExecution
35 import org.onap.aai.domain.yang.ServiceInstance
36 import org.onap.aaiclient.client.aai.AAIResourcesClient
37 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
38 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
39 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
40 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
41 import org.onap.so.bpmn.common.scripts.ExceptionUtil
42 import org.onap.so.bpmn.common.scripts.RequestDBUtil
43 import org.onap.so.bpmn.core.UrnPropertiesReader
44 import org.onap.so.bpmn.core.domain.ServiceDecomposition
45 import org.onap.so.bpmn.core.domain.ServiceProxy
46 import org.onap.so.bpmn.core.json.JsonUtils
47 import org.onap.so.db.request.beans.ResourceOperationStatus
48 import org.slf4j.Logger
49 import org.slf4j.LoggerFactory
50 import org.springframework.web.util.UriUtils
52 import static org.apache.commons.lang3.StringUtils.isBlank
54 class TnAllocateNssi extends AbstractServiceTaskProcessor {
55 String Prefix = "TNALLOC_"
57 ExceptionUtil exceptionUtil = new ExceptionUtil()
58 JsonUtils jsonUtil = new JsonUtils()
59 RequestDBUtil requestDBUtil = new RequestDBUtil()
60 JsonSlurper jsonSlurper = new JsonSlurper()
61 ObjectMapper objectMapper = new ObjectMapper()
62 OofUtils oofUtils = new OofUtils()
63 TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
64 private static final Logger logger = LoggerFactory.getLogger(TnAllocateNssi.class)
66 void preProcessRequest(DelegateExecution execution) {
67 logger.debug("Start preProcessRequest")
68 execution.setVariable("prefix", Prefix)
72 execution.setVariable("startTime", System.currentTimeMillis())
74 msg = tnNssmfUtils.getExecutionInputParams(execution)
75 logger.debug("Allocate TN NSSI input parameters: " + msg)
77 tnNssmfUtils.setSdncCallbackUrl(execution, true)
78 logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
80 String additionalPropJsonStr = execution.getVariable("sliceParams")
82 String tnNssiId = jsonUtil.getJsonValue(additionalPropJsonStr, "serviceInstanceID") //for debug
83 if (isBlank(tnNssiId)) {
84 tnNssiId = UUID.randomUUID().toString()
87 String operationId = UUID.randomUUID().toString()
88 execution.setVariable("operationId", operationId)
90 logger.debug("Generate new TN NSSI ID:" + tnNssiId)
91 tnNssiId = UriUtils.encode(tnNssiId, "UTF-8")
92 execution.setVariable("sliceServiceInstanceId", tnNssiId)
94 String sliceServiceInstanceName = execution.getVariable("servicename")
95 execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
97 String sst = execution.getVariable("sst")
98 execution.setVariable("sst", sst)
100 //additional properties
101 String sliceProfile = jsonUtil.getJsonValue(additionalPropJsonStr, "sliceProfile")
102 if (isBlank(sliceProfile)) {
103 msg = "Input sliceProfile is null"
105 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
108 execution.setVariable("sliceProfile", sliceProfile)
111 String transportSliceNetworks = jsonUtil.getJsonValue(additionalPropJsonStr, "transportSliceNetworks")
112 if (isBlank(transportSliceNetworks)) {
113 msg = "Input transportSliceNetworks is null"
115 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
118 execution.setVariable("transportSliceNetworks", transportSliceNetworks)
120 logger.debug("transportSliceNetworks: " + transportSliceNetworks)
122 String nsiInfoStr = jsonUtil.getJsonValue(additionalPropJsonStr, "nsiInfo")
123 if (isBlank(nsiInfoStr)) {
124 msg = "Input nsiInfo is null"
126 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
129 execution.setVariable("nsiInfo", nsiInfoStr)
132 if (isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution, additionalPropJsonStr,
133 "enableSdnc", "enableSdnc"))) {
134 tnNssmfUtils.setEnableSdncConfig(execution)
137 if (isBlank(additionalPropJsonStr) ||
138 isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution,
139 additionalPropJsonStr,
140 "enableOof", "enableOof"))) {
141 tnNssmfUtils.setEnableOofConfig(execution)
144 } catch (BpmnError e) {
146 } catch (Exception ex) {
147 msg = "Exception in preProcessRequest " + ex.getMessage()
149 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
151 logger.debug("Finish preProcessRequest")
155 void prepareDecomposeService(DelegateExecution execution) {
156 logger.debug("Start prepareDecomposeService")
158 String modelUuid = execution.getVariable("modelUuid")
159 if (isBlank(modelUuid)) {
160 msg = "Input modelUuid is null"
162 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
165 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
166 if (isBlank(modelInvariantUuid)) {
167 msg = "Input modelInvariantUuid is null"
169 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
172 String serviceModelInfo = """{
173 "modelInvariantUuid":"${modelInvariantUuid}",
174 "modelUuid":"${modelUuid}",
177 execution.setVariable("serviceModelInfo", serviceModelInfo)
179 logger.debug("Finish prepareDecomposeService")
182 void processDecomposition(DelegateExecution execution) {
183 logger.debug("Start processDecomposition")
185 ServiceDecomposition tnNsstServiceDecomposition = execution.getVariable("tnNsstServiceDecomposition")
186 logger.debug("tnNsstServiceDecomposition : " + tnNsstServiceDecomposition.toString())
187 //TN NSST decomposition
188 String tnModelVersion = tnNsstServiceDecomposition.getModelInfo().getModelVersion()
189 String tnModelName = tnNsstServiceDecomposition.getModelInfo().getModelName()
191 List<ServiceProxy> serviceProxyList = tnNsstServiceDecomposition.getServiceProxy()
192 List<String> nsstInfoList = new ArrayList<>()
193 for (ServiceProxy serviceProxy : serviceProxyList) {
194 String nsstModelUuid = serviceProxy.getModelInfo().getModelUuid()
195 String nsstModelInvariantUuid = serviceProxy.getModelInfo().getModelInvariantUuid()
196 String name = serviceProxy.getModelInfo().getModelName()
197 String nsstServiceModelInfo = """{
198 "UUID":"${nsstModelUuid}",
199 "invariantUUID":"${nsstModelInvariantUuid}",
202 nsstInfoList.add(nsstServiceModelInfo)
205 int maxIndex = nsstInfoList.size()
207 String msg = "Exception in TN NSST processDecomposition. There is no NSST associated with TN NSST "
209 //exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
212 execution.setVariable("tnNsstInfoList", nsstInfoList)
213 execution.setVariable("tnModelVersion", tnModelVersion)
214 execution.setVariable("tnModelName", tnModelName)
215 execution.setVariable("currentIndex", currentIndex)
216 execution.setVariable("maxIndex", maxIndex)
219 execution.setVariable("tnNfSliceProfile", execution.getVariable("sliceProfile"))
220 execution.setVariable("tnModelName", tnModelName)
221 logger.debug("End processDecomposition")
224 void prepareOofSelection(DelegateExecution execution) {
225 logger.debug("Start prepareOofSelection")
227 String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
228 logger.debug("get NSSI option OOF Url: " + urlString)
229 //build oof request body
231 String requestId = execution.getVariable("msoRequestId")
232 String messageType = "NSSISelectionResponse"
233 Map<String, Object> profileInfo = (Map<String, Object>) objectMapper.readValue(execution.getVariable("tnNfSliceProfile"), Map.class)
234 String modelUuid = execution.getVariable("modelUuid")
235 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
236 String modelName = execution.getVariable("tnModelName")
237 String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution);
239 execution.setVariable("nssiSelection_Url", "/api/oof/selection/nssi/v1")
240 execution.setVariable("nssiSelection_messageType", messageType)
241 execution.setVariable("nssiSelection_correlator", requestId)
242 execution.setVariable("nssiSelection_timeout", timeout)
243 String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, messageType, modelUuid, modelInvariantUuid, modelName, profileInfo)
244 execution.setVariable("nssiSelection_oofRequest", oofRequest)
246 logger.debug("Finish prepareOofSelection")
249 void processOofSelection(DelegateExecution execution) {
250 logger.debug(Prefix + "processOofSelection method start")
251 String oofResponse = execution.getVariable("nssiSelection_asyncCallbackResponse")
252 String requestStatus = jsonUtil.getJsonValue(oofResponse, "requestStatus")
253 if (requestStatus.equals("completed")) {
254 String solutions = jsonUtil.getJsonValue(oofResponse, "solutions")
255 JsonParser parser = new JsonParser()
256 JsonArray solution = parser.parse(solutions) as JsonArray
257 if (solution.size() >= 1) {
258 JsonObject sol = solution.get(0) as JsonObject
259 String tnNfNssiId = sol.get("NSSIId").getAsString()
260 String invariantUuid = sol.get("invariantUUID").getAsString()
261 String uuid = sol.get("UUID").getAsString()
262 String nssiName = sol.get("NSSIName").getAsString()
263 execution.setVariable("TnServiceInstanceId", tnNfNssiId)
264 execution.setVariable("TNNFInvariantUUID", invariantUuid)
265 execution.setVariable("TNNFUUID", uuid)
266 execution.setVariable("servicename", nssiName)
267 logger.debug("TnServiceInstanceId from OOF " + tnNfNssiId)
268 execution.setVariable("isOofTnNssiSelected", true)
271 logger.debug("No solutions returned from OOF .. Create new TN NF NSSI")
272 execution.setVariable("isOofTnNssiSelected", false)
276 String statusMessage = jsonUtil.getJsonValue(oofResponse, "statusMessage")
277 logger.error("received failed status from oof " + statusMessage)
278 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a failed Async Response from OOF : " + statusMessage)
281 logger.debug(Prefix + "processOofSelection method finished")
284 void prepareModifyTnNssiInputs(DelegateExecution execution) {
285 logger.debug(Prefix + "prepareModifyTnNssiInputs method start")
286 String jobId = UUID.randomUUID().toString()
287 execution.setVariable("modifyTnNssiJobId", jobId)
288 String additionalPropJsonStr = execution.getVariable("sliceParams")
289 String sliceProfile = execution.getVariable("sliceProfile")
290 String snssaiList = jsonUtil.getJsonValue(sliceProfile, "snssaiList")
291 String sliceProfileId = jsonUtil.getJsonValue(sliceProfile, "sliceProfileId")
292 String nsiInfo = jsonUtil.getJsonValue(additionalPropJsonStr, "nsiInfo")
293 String scriptName = jsonUtil.getJsonValue(additionalPropJsonStr, "scriptName")
294 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
295 String modelUuid = execution.getVariable("modelUuid")
296 String serviceInstanceID = execution.getVariable("TnServiceInstanceId")
297 String servicename = execution.getVariable("servicename")
298 String transportSliceNetworks = jsonUtil.getJsonValue(additionalPropJsonStr, "transportSliceNetworks")
300 JsonObject modifySliceParams = new JsonObject()
301 modifySliceParams.addProperty("nsiInfo", nsiInfo)
302 modifySliceParams.addProperty("serviceInstanceID", serviceInstanceID)
303 modifySliceParams.addProperty("servicename", servicename)
304 modifySliceParams.addProperty("sliceProfile", sliceProfile)
305 modifySliceParams.addProperty("sliceProfileId", sliceProfileId)
306 modifySliceParams.addProperty("modelInvariantUuid", modelInvariantUuid)
307 modifySliceParams.addProperty("modelUuid", modelUuid)
308 modifySliceParams.addProperty("scriptName", scriptName)
309 modifySliceParams.addProperty("snssaiList", snssaiList)
310 modifySliceParams.addProperty("transportSliceNetworks", transportSliceNetworks)
312 execution.setVariable("modifySliceParams", modifySliceParams.toString())
314 logger.debug(Prefix + "prepareModifyTnNssiInputs method finished")
317 def createModifyNssiQueryJobStatus = { DelegateExecution execution ->
318 logger.debug(Prefix + "createModifyNssiQueryJobStatus method start")
319 JsonObject esrInfo = new JsonObject()
320 esrInfo.addProperty("networkType", "tn")
321 esrInfo.addProperty("vendor", "ONAP_internal")
323 execution.setVariable("esrInfo", esrInfo.toString())
325 JsonObject serviceInfo = new JsonObject()
326 serviceInfo.addProperty("nssiId", execution.getVariable("TnServiceInstanceId") as String)
327 serviceInfo.addProperty("nsiId", execution.getVariable("nsiId") as String)
328 serviceInfo.addProperty("nssiName", execution.getVariable("servicename") as String)
329 serviceInfo.addProperty("sST", execution.getVariable("sst") as String)
330 serviceInfo.addProperty("PLMNIdList", objectMapper.writeValueAsString(execution.getVariable("pLMNIdList")))
331 serviceInfo.addProperty("globalSubscriberId", execution.getVariable("globalSubscriberId") as String)
332 serviceInfo.addProperty("subscriptionServiceType", execution.getVariable("subscriptionServiceType") as String)
333 serviceInfo.addProperty("serviceInvariantUuid", execution.getVariable("modelInvariantUuid") as String)
334 serviceInfo.addProperty("serviceUuid", execution.getVariable("modelUuid") as String)
335 execution.setVariable("serviceInfo", serviceInfo.toString())
336 execution.setVariable("responseId", "")
339 def processModifyJobStatusRsp = { DelegateExecution execution ->
340 logger.debug(Prefix + "processJobStatusRsp method start")
341 String jobResponse = execution.getVariable("jobResponse")
342 logger.debug("Job status response " + jobResponse)
343 String status = jsonUtil.getJsonValue(jobResponse, "status")
344 String nssi = jsonUtil.getJsonValue(jobResponse, "nssiId")
345 if (status.equalsIgnoreCase("finished")) {
346 logger.debug("Job successfully completed ... proceeding with flow for nssi : " + nssi)
349 String statusDescription = jsonUtil.getJsonValue(jobResponse, "statusDescription")
350 logger.error("received failed status from job status query for nssi : " + nssi + " with status description : " + statusDescription)
351 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "received failed status from job status query for nssi : " + nssi + " with status description : " + statusDescription)
355 void updateAAIOrchStatus(DelegateExecution execution) {
356 logger.debug("Start updateAAIOrchStatus")
357 String sliceServiceInstanceId = execution.getVariable("sliceServiceInstanceId")
358 String orchStatus = execution.getVariable("orchestrationStatus")
361 ServiceInstance si = new ServiceInstance()
362 si.setOrchestrationStatus(orchStatus)
363 AAIResourcesClient client = new AAIResourcesClient()
364 AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceServiceInstanceId))
365 client.update(uri, si)
366 } catch (BpmnError e) {
368 } catch (Exception ex) {
369 String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
371 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
374 logger.debug("Finish updateAAIOrchStatus")
378 void prepareUpdateJobStatus(DelegateExecution execution,
381 String statusDescription) {
382 String modelUuid = execution.getVariable("modelUuid")
383 String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
384 String jobId = execution.getVariable("jobId")
385 String nsiId = execution.getVariable("nsiId")
387 ResourceOperationStatus roStatus = new ResourceOperationStatus()
388 roStatus.setServiceId(nsiId)
389 roStatus.setOperationId(jobId)
390 roStatus.setResourceTemplateUUID(modelUuid)
391 roStatus.setResourceInstanceID(ssInstanceId)
392 roStatus.setOperType("ALLOCATE")
393 roStatus.setProgress(progress)
394 roStatus.setStatus(status)
395 roStatus.setStatusDescription(statusDescription)
396 requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)