2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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=========================================================
20 package org.onap.so.bpmn.infrastructure.scripts
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
25 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
26 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
27 import org.onap.so.beans.nsmf.DeAllocateNssi
28 import org.onap.so.beans.nsmf.EsrInfo
29 import org.onap.so.beans.nsmf.NetworkType
30 import org.onap.so.beans.nsmf.NssiResponse
31 import org.onap.so.beans.nsmf.ServiceInfo
32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
34 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
35 import org.onap.so.bpmn.common.scripts.RequestDBUtil
36 import org.onap.so.bpmn.core.domain.ServiceArtifact
37 import org.onap.so.bpmn.core.domain.ServiceDecomposition
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.onap.so.db.request.beans.OperationStatus
40 import org.slf4j.Logger
41 import org.slf4j.LoggerFactory
44 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
46 private final String PREFIX ="DoDeallocateNSSI"
48 private ExceptionUtil exceptionUtil = new ExceptionUtil()
49 private JsonUtils jsonUtil = new JsonUtils()
50 ObjectMapper objectMapper = new ObjectMapper()
51 private RequestDBUtil requestDBUtil = new RequestDBUtil()
52 private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
54 private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateNSSI.class)
57 void preProcessRequest(DelegateExecution execution) {
58 LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")
60 def currentNSSI = execution.getVariable("currentNSSI")
62 String msg = "currentNSSI is null"
64 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
67 LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
74 void prepareDecomposeService(DelegateExecution execution)
76 LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
79 def currentNSSI = execution.getVariable("currentNSSI")
80 String modelInvariantUuid = currentNSSI['modelInvariantId']
81 String modelVersionId = currentNSSI['modelVersionId']
82 String serviceModelInfo = """{
83 "modelInvariantUuid":"${modelInvariantUuid}",
84 "modelUuid":"${modelVersionId}",
87 execution.setVariable("serviceModelInfo", serviceModelInfo)
91 String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
92 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
94 LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
101 void processDecomposition(DelegateExecution execution) {
102 LOGGER.debug("*****${PREFIX} start processDecomposition *****")
105 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
106 ServiceArtifact serviceArtifact = serviceDecomposition ?.getServiceInfo()?.getServiceArtifact()?.get(0)
107 String content = serviceArtifact.getContent()
108 String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
109 String domainType = jsonUtil.getJsonValue(content, "metadata.domainType")
111 def currentNSSI = execution.getVariable("currentNSSI")
112 currentNSSI['vendor'] = vendor
113 currentNSSI['domainType'] = domainType
114 LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType))
117 String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
118 LOGGER.debug(exceptionMessage)
119 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
121 LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
125 * send deallocate request to nssmf
128 void sendRequestToNSSMF(DelegateExecution execution)
130 LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
131 def currentNSSI = execution.getVariable("currentNSSI")
132 String snssai= currentNSSI['snssai']
133 String profileId = currentNSSI['profileId']
134 String nssiId = currentNSSI['nssiServiceInstanceId']
135 String nsiId = currentNSSI['nsiServiceInstanceId']
136 String scriptName = execution.getVariable("scriptName")
138 String serviceInvariantUuid = currentNSSI['modelInvariantId']
139 String serviceUuid = currentNSSI['modelId']
140 String globalSubscriberId = currentNSSI['globalSubscriberId']
141 String subscriptionServiceType = execution.getVariable("serviceType")
143 DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
144 deAllocateNssi.setNsiId(nsiId)
145 deAllocateNssi.setNssiId(nssiId)
146 deAllocateNssi.setTerminateNssiOption(0)
147 deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
148 deAllocateNssi.setScriptName(scriptName)
150 ServiceInfo serviceInfo = new ServiceInfo()
151 serviceInfo.setServiceInvariantUuid(serviceInvariantUuid)
152 serviceInfo.setServiceUuid(serviceUuid)
153 serviceInfo.setNsiId(nsiId)
154 serviceInfo.setGlobalSubscriberId(globalSubscriberId)
155 serviceInfo.setSubscriptionServiceType(subscriptionServiceType)
156 String serviceInfoString = objectMapper.writeValueAsString(serviceInfo)
158 EsrInfo esrInfo = getEsrInfo(currentNSSI)
159 String esrInfoString = objectMapper.writeValueAsString(esrInfo)
161 execution.setVariable("deAllocateNssi",deAllocateNssi)
162 execution.setVariable("esrInfo", esrInfoString)
163 execution.setVariable("serviceInfo", serviceInfoString)
164 String nssmfRequest = """
166 "deAllocateNssi": ${objectMapper.writeValueAsString(deAllocateNssi)},
167 "esrInfo": ${esrInfoString},
168 "serviceInfo": ${serviceInfoString}
172 String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s", profileId)
174 NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
175 if (nssmfResponse != null) {
176 currentNSSI['jobId']= nssmfResponse.getJobId() ?: ""
177 currentNSSI['jobProgress'] = 0
178 execution.setVariable("currentNSSI", currentNSSI)
180 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
182 LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
186 * send to nssmf query progress
189 void prepareJobStatusRequest(DelegateExecution execution)
191 def currentNSSI = execution.getVariable("currentNSSI")
192 String jobId = currentNSSI['jobId']
194 execution.setVariable("responseId", "3")
195 execution.setVariable("jobId", jobId)
200 * send to nssmf query progress
203 void handleJobStatus(DelegateExecution execution)
207 String jobStatusResponse = execution.getVariable("responseDescriptor")
208 String status = jsonUtil.getJsonValue(jobStatusResponse,"status")
209 def statusDescription = jsonUtil.getJsonValue(jobStatusResponse,"statusDescription")
210 def progress = jsonUtil.getJsonValue(jobStatusResponse,"progress")
211 if(!status.equalsIgnoreCase("failed"))
215 LOGGER.error("job progress is null or empty!")
216 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
218 def currentNSSI = execution.getVariable("currentNSSI")
219 int oldProgress = currentNSSI['jobProgress']
220 int currentProgress = Integer.parseInt(progress)
222 execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
223 execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
224 currentNSSI['jobProgress'] = currentProgress
225 currentNSSI['status'] = status
226 currentNSSI['statusDescription'] = statusDescription
228 String nssiId = currentNSSI['nssiServiceInstanceId']
229 String nsiId = currentNSSI['nsiServiceInstanceId']
230 LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
233 execution.setVariable("isNeedUpdateDB", "true")
234 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
239 String msg = "Received a Bad Response from NSSMF. cause-"+any.getCause()
240 LOGGER.error(any.printStackTrace())
241 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
245 private EsrInfo getEsrInfo(def currentNSSI)
247 String domaintype = currentNSSI['domainType']
248 String vendor = currentNSSI['vendor']
250 EsrInfo info = new EsrInfo()
251 info.setNetworkType(NetworkType.fromString(domaintype))
252 info.setVendor(vendor)
258 * prepare update requestdb
261 void prepareUpdateOperationStatus(DelegateExecution execution)
263 def currentNSSI = execution.getVariable("currentNSSI")
264 int currentProgress = currentNSSI["jobProgress"]
265 def proportion = currentNSSI['proportion']
266 def statusDes = currentNSSI["statusDescription"]
267 int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
268 def status = currentNSSI['status']
270 OperationStatus operationStatus = new OperationStatus()
271 operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
272 operationStatus.setOperationId(currentNSSI['operationId'] as String)
273 operationStatus.setOperation("DELETE")
274 operationStatus.setResult(status as String)
275 operationStatus.setProgress(progress as String)
276 operationStatus.setOperationContent(statusDes as String)
277 requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
278 LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
282 * delete slice profile from aai
285 void delSliceProfileFromAAI(DelegateExecution execution)
287 LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
288 def currentNSSI = execution.getVariable("currentNSSI")
289 String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
290 String profileId = currentNSSI['profileId']
291 String globalSubscriberId = currentNSSI["globalSubscriberId"]
292 String serviceType = execution.getVariable("serviceType")
296 LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
297 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(
298 AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(nssiServiceInstanceId).sliceProfile(profileId))
299 if (!getAAIClient().exists(resourceUri)) {
300 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
302 getAAIClient().delete(resourceUri)
306 String msg = "delete slice profile from aai failed! cause-"+any.getCause()
307 LOGGER.error(any.printStackTrace())
308 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
310 LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")