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.aai.domain.yang.SliceProfiles
25 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
26 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
27 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
28 import org.onap.aaiclient.client.aai.entities.uri.AAISimpleUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.so.beans.nsmf.*
32 import org.onap.so.beans.nsmf.oof.SubnetType
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
36 import org.onap.so.bpmn.common.scripts.RequestDBUtil
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
43 import javax.ws.rs.NotFoundException
45 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
47 private final String PREFIX ="DoDeallocateNSSI"
49 private ExceptionUtil exceptionUtil = new ExceptionUtil()
50 private JsonUtils jsonUtil = new JsonUtils()
51 ObjectMapper objectMapper = new ObjectMapper()
52 private RequestDBUtil requestDBUtil = new RequestDBUtil()
53 private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
55 private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateNSSI.class)
58 void preProcessRequest(DelegateExecution execution) {
59 LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")
61 def currentNSSI = execution.getVariable("currentNSSI")
63 String msg = "currentNSSI is null"
65 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
68 LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
75 void prepareDecomposeService(DelegateExecution execution)
77 LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
80 def currentNSSI = execution.getVariable("currentNSSI")
81 String modelInvariantUuid = currentNSSI['modelInvariantId']
82 String modelVersionId = currentNSSI['modelVersionId']
83 String serviceModelInfo = """{
84 "modelInvariantUuid":"${modelInvariantUuid}",
85 "modelUuid":"${modelVersionId}",
88 execution.setVariable("serviceModelInfo", serviceModelInfo)
92 String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
93 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
95 LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
102 void processDecomposition(DelegateExecution execution) {
103 LOGGER.debug("*****${PREFIX} start processDecomposition *****")
106 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
107 String vendor = serviceDecomposition ?.getServiceRole()
108 NetworkType domainType = convertServiceCategory(serviceDecomposition.getServiceCategory())
110 def currentNSSI = execution.getVariable("currentNSSI")
111 currentNSSI['vendor'] = vendor
112 currentNSSI['domainType'] = domainType
113 LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType.toString()))
116 String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
117 LOGGER.debug(exceptionMessage)
118 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
120 LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
125 * get subnetType from serviceCategory
128 private NetworkType convertServiceCategory(String serviceCategory){
129 if(serviceCategory ==~ /CN.*/){
130 return SubnetType.CN.getNetworkType()
132 if (serviceCategory ==~ /AN.*/){
133 return SubnetType.AN.getNetworkType()
135 if (serviceCategory ==~ /TN.*BH.*/){
136 return SubnetType.TN_BH.getNetworkType()
138 if(serviceCategory ==~ /TN.*MH.*/){
139 return SubnetType.TN_MH.getNetworkType()
145 * send deallocate request to nssmf
148 void sendRequestToNSSMF(DelegateExecution execution)
150 LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
151 def currentNSSI = execution.getVariable("currentNSSI")
152 String snssai= currentNSSI['snssai']
153 String profileId = currentNSSI['profileId']
154 String nssiId = currentNSSI['nssiServiceInstanceId']
155 String nsiId = currentNSSI['nsiServiceInstanceId']
156 String scriptName = execution.getVariable("scriptName")
158 String serviceInvariantUuid = currentNSSI['modelInvariantId']
159 String serviceUuid = currentNSSI['modelVersionId']
160 String globalSubscriberId = currentNSSI['globalSubscriberId']
161 String subscriptionServiceType = currentNSSI['serviceType']
163 DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
164 deAllocateNssi.setNsiId(nsiId)
165 deAllocateNssi.setNssiId(nssiId)
166 deAllocateNssi.setTerminateNssiOption(0)
167 deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
168 deAllocateNssi.setScriptName(scriptName)
169 deAllocateNssi.setSliceProfileId(profileId)
171 ServiceInfo serviceInfo = new ServiceInfo()
172 serviceInfo.setServiceInvariantUuid(serviceInvariantUuid)
173 serviceInfo.setServiceUuid(serviceUuid)
174 serviceInfo.setNsiId(nsiId)
175 serviceInfo.setNssiId(nssiId)
176 serviceInfo.setGlobalSubscriberId(globalSubscriberId)
177 serviceInfo.setSubscriptionServiceType(subscriptionServiceType)
178 String serviceInfoString = objectMapper.writeValueAsString(serviceInfo)
180 EsrInfo esrInfo = getEsrInfo(currentNSSI)
181 String esrInfoString = objectMapper.writeValueAsString(esrInfo)
183 execution.setVariable("deAllocateNssi",deAllocateNssi)
184 execution.setVariable("esrInfo", esrInfoString)
185 execution.setVariable("serviceInfo", serviceInfoString)
186 String nssmfRequest = """
188 "deAllocateNssi": ${objectMapper.writeValueAsString(deAllocateNssi)},
189 "esrInfo": ${esrInfoString},
190 "serviceInfo": ${serviceInfoString}
194 String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s", profileId)
196 NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
197 if (nssmfResponse != null) {
198 currentNSSI['jobId']= nssmfResponse.getJobId() ?: ""
199 currentNSSI['jobProgress'] = 0
200 execution.setVariable("currentNSSI", currentNSSI)
202 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
204 LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
208 * send to nssmf query progress
211 void prepareJobStatusRequest(DelegateExecution execution)
213 def currentNSSI = execution.getVariable("currentNSSI")
214 String jobId = currentNSSI['jobId']
215 execution.setVariable("jobId", jobId)
220 * send to nssmf query progress
223 void handleJobStatus(DelegateExecution execution)
227 String jobStatusResponse = execution.getVariable("responseDescriptor")
228 String status = jsonUtil.getJsonValue(jobStatusResponse,"status")
229 def statusDescription = jsonUtil.getJsonValue(jobStatusResponse,"statusDescription")
230 def progress = jsonUtil.getJsonValue(jobStatusResponse,"progress")
231 if(!status.equalsIgnoreCase("failed"))
235 LOGGER.error("job progress is null or empty!")
236 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
238 def currentNSSI = execution.getVariable("currentNSSI")
239 int oldProgress = currentNSSI['jobProgress']
240 int currentProgress = Integer.parseInt(progress)
242 execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
243 execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
244 currentNSSI['jobProgress'] = currentProgress
245 currentNSSI['status'] = status
246 currentNSSI['statusDescription'] = statusDescription
248 String nssiId = currentNSSI['nssiServiceInstanceId']
249 String nsiId = currentNSSI['nsiServiceInstanceId']
250 LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
253 execution.setVariable("isNeedUpdateDB", "true")
254 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
259 String msg = "Received a Bad Response from NSSMF. cause-"+any.getCause()
260 LOGGER.error(any.printStackTrace())
261 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
265 private EsrInfo getEsrInfo(def currentNSSI)
267 NetworkType domainType = currentNSSI['domainType']
268 String vendor = currentNSSI['vendor']
270 EsrInfo info = new EsrInfo()
271 info.setNetworkType(domainType)
272 info.setVendor(vendor)
278 * prepare update requestdb
281 void prepareUpdateOperationStatus(DelegateExecution execution)
283 def currentNSSI = execution.getVariable("currentNSSI")
284 int currentProgress = currentNSSI["jobProgress"]
285 def proportion = currentNSSI['proportion']
286 int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
287 def status = currentNSSI['status']
290 OperationStatus operationStatus = new OperationStatus()
291 operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
292 operationStatus.setOperationId(currentNSSI['operationId'] as String)
293 operationStatus.setOperation("DELETE")
294 operationStatus.setResult("processing")
295 operationStatus.setProgress(progress as String)
296 operationStatus.setOperationContent(currentNSSI['domainType'].toString() + " " + status.toString())
297 requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
298 LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
302 * delete slice profile from aai
305 void delSliceProfileServiceFromAAI(DelegateExecution execution)
307 LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
308 def currentNSSI = execution.getVariable("currentNSSI")
309 String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
310 String profileId = currentNSSI['profileId']
311 String globalSubscriberId = currentNSSI["globalSubscriberId"]
312 String serviceType = currentNSSI['serviceType']
316 LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
317 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(
318 AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(profileId))
319 if (!getAAIClient().exists(resourceUri)) {
320 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
322 getAAIClient().delete(resourceUri)
326 String msg = "delete slice profile from aai failed! cause-"+any.getCause()
327 LOGGER.error(any.printStackTrace())
328 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
330 LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")
333 void delSliceProfileFromAAI(DelegateExecution execution){
335 LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
336 def currentNSSI = execution.getVariable("currentNSSI")
337 String globalSubscriberId = currentNSSI["globalSubscriberId"]
338 String serviceType = currentNSSI['serviceType']
339 String sliceProfileInstId = currentNSSI['profileId']
343 AAIPluralResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(sliceProfileInstId).sliceProfiles())
344 AAIResultWrapper wrapper = getAAIClient().get(resourceUri, NotFoundException.class)
345 Optional<SliceProfiles> sliceProfilesOpt =wrapper.asBean(SliceProfiles.class)
346 SliceProfiles sliceProfiles
348 if(sliceProfilesOpt.isPresent()){
349 sliceProfiles = sliceProfilesOpt.get()
350 org.onap.aai.domain.yang.SliceProfile sliceProfile = sliceProfiles.getSliceProfile().get(0)
351 profileId = sliceProfile ? sliceProfile.getProfileId() : ""
354 AAISimpleUri profileUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(sliceProfileInstId).sliceProfile(profileId))
355 if (!getAAIClient().exists(profileUri)) {
356 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
358 getAAIClient().delete(profileUri)
364 String msg = "delete service profile from aai failed! cause-"+any.getCause()
365 LOGGER.error(any.printStackTrace())
366 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);