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.logging.filter.base.ONAPComponents
25 import org.onap.so.beans.nsmf.DeAllocateNssi
26 import org.onap.so.beans.nsmf.EsrInfo
27 import org.onap.so.beans.nsmf.JobStatusRequest
28 import org.onap.so.beans.nsmf.JobStatusResponse
29 import org.onap.so.beans.nsmf.NetworkType
30 import org.onap.so.beans.nsmf.NssiDeAllocateRequest
31 import org.onap.so.beans.nsmf.NssiResponse
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.UrnPropertiesReader
37 import org.onap.so.bpmn.core.domain.ServiceArtifact
38 import org.onap.so.bpmn.core.domain.ServiceDecomposition
39 import org.onap.so.bpmn.core.json.JsonUtils
40 import org.onap.so.client.HttpClient
41 import org.onap.aaiclient.client.aai.AAIObjectType
42 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
43 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
44 import org.onap.so.db.request.beans.OperationStatus
45 import org.slf4j.Logger
46 import org.slf4j.LoggerFactory
48 import javax.ws.rs.core.Response
51 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
53 private final String PREFIX ="DoDeallocateNSSI"
55 private ExceptionUtil exceptionUtil = new ExceptionUtil()
56 private JsonUtils jsonUtil = new JsonUtils()
57 private RequestDBUtil requestDBUtil = new RequestDBUtil()
58 private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
60 private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateNSSI.class)
63 void preProcessRequest(DelegateExecution execution) {
64 LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")
66 def currentNSSI = execution.getVariable("currentNSSI")
68 String msg = "currentNSSI is null"
70 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
73 LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
80 void prepareDecomposeService(DelegateExecution execution)
82 LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
85 def currentNSSI = execution.getVariable("currentNSSI")
86 String modelInvariantUuid = currentNSSI['modelInvariantId']
87 String modelVersionId = currentNSSI['modelVersionId']
88 String serviceModelInfo = """{
89 "modelInvariantUuid":"${modelInvariantUuid}",
90 "modelUuid":"${modelVersionId}",
93 execution.setVariable("serviceModelInfo", serviceModelInfo)
97 String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
98 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
100 LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
107 void processDecomposition(DelegateExecution execution) {
108 LOGGER.debug("*****${PREFIX} start processDecomposition *****")
111 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
112 ServiceArtifact serviceArtifact = serviceDecomposition ?.getServiceInfo()?.getServiceArtifact()?.get(0)
113 String content = serviceArtifact.getContent()
114 String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
115 String domainType = jsonUtil.getJsonValue(content, "metadata.domainType")
117 def currentNSSI = execution.getVariable("currentNSSI")
118 currentNSSI['vendor'] = vendor
119 currentNSSI['domainType'] = domainType
120 LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType))
123 String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
124 LOGGER.debug(exceptionMessage)
125 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
127 LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
131 * send deallocate request to nssmf
134 void sendRequestToNSSMF(DelegateExecution execution)
136 LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
137 def currentNSSI = execution.getVariable("currentNSSI")
138 String snssai= currentNSSI['snssai']
139 String profileId = currentNSSI['profileId']
140 String nssiId = currentNSSI['nssiServiceInstanceId']
141 String nsiId = currentNSSI['nsiServiceInstanceId']
143 DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
144 deAllocateNssi.setNsiId(nsiId)
145 deAllocateNssi.setNssiId(nssiId)
146 deAllocateNssi.setTerminateNssiOption(0)
147 deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
149 NssiDeAllocateRequest deAllocateRequest = new NssiDeAllocateRequest()
150 deAllocateRequest.setDeAllocateNssi(deAllocateNssi)
151 deAllocateRequest.setEsrInfo(getEsrInfo(currentNSSI))
153 ObjectMapper mapper = new ObjectMapper()
154 String nssmfRequest = mapper.writeValueAsString(deAllocateRequest)
156 String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s",profileId)
158 NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
159 if (nssmfResponse != null) {
160 currentNSSI['jobId']= nssmfResponse.getJobId() ?: ""
161 currentNSSI['jobProgress'] = 0
162 execution.setVariable("currentNSSI", currentNSSI)
164 LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
166 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
172 * send to nssmf query progress
175 void getJobStatus(DelegateExecution execution)
177 def currentNSSI = execution.getVariable("currentNSSI")
178 String jobId = currentNSSI['jobId']
179 String nssiId = currentNSSI['nssiServiceInstanceId']
180 String nsiId = currentNSSI['nsiServiceInstanceId']
182 JobStatusRequest jobStatusRequest = new JobStatusRequest()
183 jobStatusRequest.setNssiId(nssiId)
184 jobStatusRequest.setNsiId(nsiId)
185 jobStatusRequest.setEsrInfo(getEsrInfo(currentNSSI))
187 ObjectMapper mapper = new ObjectMapper()
188 String nssmfRequest = mapper.writeValueAsString(jobStatusRequest)
190 String urlStr = String.format("/api/rest/provMns/v1/NSS/jobs/%s", jobId)
192 JobStatusResponse jobStatusResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, JobStatusResponse.class)
194 if (jobStatusResponse != null) {
195 def progress = jobStatusResponse?.getResponseDescriptor()?.getProgress()
198 LOGGER.error("job progress is null or empty!")
199 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
201 int oldProgress = currentNSSI['jobProgress']
202 int currentProgress = progress
204 execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
205 execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
206 currentNSSI['jobProgress'] = currentProgress
208 def statusDescription = jobStatusResponse?.getResponseDescriptor()?.getStatusDescription()
209 currentNSSI['statusDescription'] = statusDescription
211 LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
214 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
219 private EsrInfo getEsrInfo(def currentNSSI)
221 String domaintype = currentNSSI['domainType']
222 String vendor = currentNSSI['vendor']
224 EsrInfo info = new EsrInfo()
225 info.setNetworkType(NetworkType.fromString(domaintype))
226 info.setVendor(vendor)
232 * prepare update requestdb
235 void handleJobStatus(DelegateExecution execution)
237 def currentNSSI = execution.getVariable("currentNSSI")
238 int currentProgress = currentNSSI["jobProgress"]
239 def proportion = currentNSSI['proportion']
240 def statusDes = currentNSSI["statusDescription"]
241 int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
243 OperationStatus operationStatus = new OperationStatus()
244 operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
245 operationStatus.setOperationId(currentNSSI['operationId'] as String)
246 operationStatus.setOperation("DELETE")
247 operationStatus.setResult("processing")
248 operationStatus.setProgress(progress as String)
249 operationStatus.setOperationContent(statusDes as String)
250 requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
251 LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
254 void timeDelay(DelegateExecution execution) {
257 } catch(InterruptedException e) {
258 LOGGER.error("Time Delay exception" + e)
263 * delete slice profile from aai
266 void delSliceProfileFromAAI(DelegateExecution execution)
268 LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
269 def currentNSSI = execution.getVariable("currentNSSI")
270 String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
271 String profileId = currentNSSI['profileId']
272 String globalSubscriberId = currentNSSI["globalSubscriberId"]
273 String serviceType = currentNSSI["serviceType"]
277 LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
278 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, globalSubscriberId, serviceType, nssiServiceInstanceId, profileId)
279 if (!getAAIClient().exists(resourceUri)) {
280 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
282 getAAIClient().delete(resourceUri)
286 String msg = "delete slice profile from aai failed! cause-"+any.getCause()
287 LOGGER.error(any.printStackTrace())
288 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
290 LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")