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.RequestDBUtil
35 import org.onap.so.bpmn.core.UrnPropertiesReader
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.client.HttpClient
40 import org.onap.so.client.HttpClientFactory
41 import org.onap.so.client.aai.AAIObjectType
42 import org.onap.so.client.aai.AAIResourcesClient
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory
45 import org.onap.so.db.request.beans.OperationStatus
46 import org.slf4j.Logger
47 import org.slf4j.LoggerFactory
49 import javax.ws.rs.core.Response
52 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
54 private final String PREFIX ="DoDeallocateNSSI"
56 private ExceptionUtil exceptionUtil = new ExceptionUtil()
57 private JsonUtils jsonUtil = new JsonUtils()
58 private RequestDBUtil requestDBUtil = new RequestDBUtil()
59 private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateNSSI.class)
62 void preProcessRequest(DelegateExecution execution) {
63 LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")
65 def currentNSSI = execution.getVariable("currentNSSI")
67 String msg = "currentNSSI is null"
69 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
72 LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
79 void prepareDecomposeService(DelegateExecution execution)
81 LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
84 def currentNSSI = execution.getVariable("currentNSSI")
85 String modelInvariantUuid = currentNSSI['modelInvariantId']
86 String modelVersionId = currentNSSI['modelVersionId']
87 String serviceModelInfo = """{
88 "modelInvariantUuid":"${modelInvariantUuid}",
89 "modelUuid":"${modelVersionId}",
92 execution.setVariable("serviceModelInfo", serviceModelInfo)
96 String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
97 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
99 LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
106 void processDecomposition(DelegateExecution execution) {
107 LOGGER.debug("*****${PREFIX} start processDecomposition *****")
110 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
111 ServiceArtifact serviceArtifact = serviceDecomposition ?.getServiceInfo()?.getServiceArtifact()?.get(0)
112 String content = serviceArtifact.getContent()
113 String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
114 String domainType = jsonUtil.getJsonValue(content, "metadata.domainType")
116 def currentNSSI = execution.getVariable("currentNSSI")
117 currentNSSI['vendor'] = vendor
118 currentNSSI['domainType'] = domainType
119 LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType))
122 String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
123 LOGGER.debug(exceptionMessage)
124 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
126 LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
130 * send deallocate request to nssmf
133 void sendRequestToNSSMF(DelegateExecution execution)
135 LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
136 def currentNSSI = execution.getVariable("currentNSSI")
137 String snssai= currentNSSI['snssai']
138 String profileId = currentNSSI['profileId']
139 String nssiId = currentNSSI['nssiServiceInstanceId']
140 String nsiId = currentNSSI['nsiServiceInstanceId']
141 String scriptName = execution.getVariable("scriptName")
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 NssiDeAllocateRequest deAllocateRequest = new NssiDeAllocateRequest()
151 deAllocateRequest.setDeAllocateNssi(deAllocateNssi)
152 deAllocateRequest.setEsrInfo(getEsrInfo(currentNSSI))
154 ObjectMapper mapper = new ObjectMapper()
155 String json = mapper.writeValueAsString(deAllocateRequest)
157 //Prepare auth for NSSMF - Begin
158 String nssmfRequest = UrnPropertiesReader.getVariable("mso.adapters.nssmf.endpoint", execution)
159 nssmfRequest = nssmfRequest + String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s",profileId)
160 //nssmfRequest = nssmfRequest + String.format(NssmfAdapterUtil.NSSMI_DEALLOCATE_URL,profileId)
161 //send request to active NSSI TN option
162 URL url = new URL(nssmfRequest)
163 LOGGER.info("deallocate nssmfRequest:${nssmfRequest}, reqBody: ${json}")
165 HttpClient httpClient = getHttpClientFactory().newJsonClient(url, ONAPComponents.EXTERNAL)
166 Response httpResponse = httpClient.post(json)
167 checkNssmfResponse(httpResponse, execution)
169 NssiResponse nssmfResponse = httpResponse.readEntity(NssiResponse.class)
170 currentNSSI['jobId']= nssmfResponse.getJobId() ?: ""
171 currentNSSI['jobProgress'] = 0
172 execution.setVariable("currentNSSI", currentNSSI)
174 LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
178 * send to nssmf query progress
181 void getJobStatus(DelegateExecution execution)
183 def currentNSSI = execution.getVariable("currentNSSI")
184 String jobId = currentNSSI['jobId']
185 String nssiId = currentNSSI['nssiServiceInstanceId']
186 String nsiId = currentNSSI['nsiServiceInstanceId']
188 JobStatusRequest jobStatusRequest = new JobStatusRequest()
189 jobStatusRequest.setNssiId(nssiId)
190 jobStatusRequest.setNsiId(nsiId)
191 jobStatusRequest.setEsrInfo(getEsrInfo(currentNSSI))
193 ObjectMapper mapper = new ObjectMapper()
194 String json = mapper.writeValueAsString(jobStatusRequest)
196 //Prepare auth for NSSMF - Begin
197 String nssmfRequest = UrnPropertiesReader.getVariable("mso.adapters.nssmf.endpoint", execution)
198 nssmfRequest = nssmfRequest + String.format("/api/rest/provMns/v1/NSS/jobs/%s",jobId)
199 //send request to active NSSI TN option
200 URL url = new URL(nssmfRequest)
201 LOGGER.info("get deallocate job status, nssmfRequest:${nssmfRequest}, requestBody: ${json}")
203 HttpClient httpClient = getHttpClientFactory().newJsonClient(url, ONAPComponents.EXTERNAL)
204 Response httpResponse = httpClient.post(json)
205 checkNssmfResponse(httpResponse, execution)
207 JobStatusResponse jobStatusResponse = httpResponse.readEntity(JobStatusResponse.class)
208 def progress = jobStatusResponse?.getResponseDescriptor()?.getProgress()
211 LOGGER.error("job progress is null or empty!")
212 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
214 int oldProgress = currentNSSI['jobProgress']
215 int currentProgress = progress
217 execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
218 execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
219 currentNSSI['jobProgress'] = currentProgress
221 def statusDescription = jobStatusResponse?.getResponseDescriptor()?.getStatusDescription()
222 currentNSSI['statusDescription'] = statusDescription
224 LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
227 private void checkNssmfResponse(Response httpResponse, DelegateExecution execution) {
228 int responseCode = httpResponse.getStatus()
229 LOGGER.debug("NSSMF response code is: " + responseCode)
231 if ( responseCode < 200 || responseCode > 204 || !httpResponse.hasEntity()) {
232 exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Response from NSSMF.")
237 private EsrInfo getEsrInfo(def currentNSSI)
239 String domaintype = currentNSSI['domainType']
240 String vendor = currentNSSI['vendor']
242 EsrInfo info = new EsrInfo()
243 info.setNetworkType(NetworkType.fromString(domaintype))
244 info.setVendor(vendor)
250 * prepare update requestdb
253 void handleJobStatus(DelegateExecution execution)
255 def currentNSSI = execution.getVariable("currentNSSI")
256 int currentProgress = currentNSSI["jobProgress"]
257 def proportion = currentNSSI['proportion']
258 def statusDes = currentNSSI["statusDescription"]
259 int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
261 OperationStatus operationStatus = new OperationStatus()
262 operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
263 operationStatus.setOperationId(currentNSSI['operationId'] as String)
264 operationStatus.setOperation("DELETE")
265 operationStatus.setResult("processing")
266 operationStatus.setProgress(progress as String)
267 operationStatus.setOperationContent(statusDes as String)
268 requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
269 LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
272 void timeDelay(DelegateExecution execution) {
275 } catch(InterruptedException e) {
276 LOGGER.error("Time Delay exception" + e)
281 * delete slice profile from aai
284 void delSliceProfileFromAAI(DelegateExecution execution)
286 LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
287 def currentNSSI = execution.getVariable("currentNSSI")
288 String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
289 String profileId = currentNSSI['profileId']
290 String globalSubscriberId = currentNSSI["globalSubscriberId"]
291 String serviceType = currentNSSI["serviceType"]
295 LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
296 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, globalSubscriberId, serviceType, nssiServiceInstanceId, profileId)
297 if (!getAAIClient().exists(resourceUri)) {
298 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
300 getAAIClient().delete(resourceUri)
304 String msg = "delete slice profile from aai failed! cause-"+any.getCause()
305 LOGGER.error(any.printStackTrace())
306 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
308 LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")