2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Telecom Italia
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
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.v19.AllottedResource
26 import org.onap.aai.domain.yang.v19.ServiceInstance
27 import org.onap.aaiclient.client.aai.AAIResourcesClient
28 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
29 import org.onap.aaiclient.client.aai.entities.Relationships
30 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
31 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
34 import org.onap.logging.filter.base.ONAPComponents
35 import org.onap.so.bpmn.common.scripts.ExceptionUtil
36 import org.onap.so.bpmn.common.scripts.MsoUtils
37 import org.onap.so.bpmn.common.scripts.RequestDBUtil
38 import org.onap.so.bpmn.core.UrnPropertiesReader
39 import org.onap.so.bpmn.core.json.JsonUtils
40 import org.onap.so.client.HttpClient
41 import org.onap.so.client.HttpClientFactory
42 import org.slf4j.Logger
43 import org.slf4j.LoggerFactory
45 import javax.ws.rs.core.Response
47 class DoDeallocateCoreNSSI extends DoCommonCoreNSSI {
48 private final String PREFIX ="DoDeallocateCoreNSSI"
49 private final String ACTION = "Deallocate"
51 private ExceptionUtil exceptionUtil = new ExceptionUtil()
52 private RequestDBUtil requestDBUtil = new RequestDBUtil()
53 private MsoUtils utils = new MsoUtils()
54 private JsonUtils jsonUtil = new JsonUtils()
56 private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateCoreNSSI.class)
59 * Queries OOF for NSSI termination
62 void executeTerminateNSSIQuery(DelegateExecution execution) {
63 LOGGER.trace("${PREFIX} Start executeTerminateNSSIQuery")
65 String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
67 //Prepare auth for OOF
69 String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
70 String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
72 String basicAuthValue = encryptBasicAuth(basicAuth, msokey)
73 if (basicAuthValue != null) {
74 String responseAuthHeader = getAuthHeader(execution, basicAuthValue, msokey)
75 String errorCode = jsonUtil.getJsonValue(responseAuthHeader, "errorCode")
76 if(errorCode == null || errorCode.isEmpty()) { // No error
77 authHeader = responseAuthHeader
80 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(responseAuthHeader, "errorMessage"))
83 LOGGER.error( "Unable to obtain BasicAuth - BasicAuth value null")
84 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
88 //Prepare send request to OOF
89 String oofRequest = buildOOFRequest(execution)
91 String callOOFResponse = callOOF(urlString, authHeader, oofRequest)
92 String errorCode = jsonUtil.getJsonValue(callOOFResponse, "errorCode")
93 if(errorCode == null || errorCode.isEmpty()) { // No error
94 String oofResponse = callOOFResponse
95 String isTerminateNSSI = jsonUtil.getJsonValue(oofResponse, "terminateResponse")
97 execution.setVariable("isTerminateNSSI", Boolean.parseBoolean(isTerminateNSSI))
100 LOGGER.error(jsonUtil.getJsonValue(callOOFResponse, "errorMessage"))
101 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(callOOFResponse, "errorMessage"))
105 LOGGER.trace("${PREFIX} Exit executeTerminateNSSIQuery")
110 * Executes sync call to OOF
111 * @return OOF response
113 String callOOF(String urlString, String authHeader, String oofRequest) {
114 String errorCode = ""
115 String errorMessage = ""
119 URL url = new URL(urlString + "/api/oof/terminate/nxi/v1")
120 HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.OOF)
121 httpClient.addAdditionalHeader("Authorization", authHeader)
122 httpClient.addAdditionalHeader("Accept", "application/json")
123 httpClient.addAdditionalHeader("Content-Type", "application/json")
125 Response httpResponse = httpClient.post(oofRequest)
127 int responseCode = httpResponse.getStatus()
128 LOGGER.debug("OOF sync response code is: " + responseCode)
130 if (responseCode != 202) { // Accepted
131 errorCode = responseCode
132 errorMessage = "Received a Bad Sync Response from OOF."
135 " \"errorCode\": \"${errorCode}\",\n" +
136 " \"errorMessage\": \"${errorMessage}\"\n" +
138 //exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
141 if (httpResponse.hasEntity()) {
142 response = httpResponse.readEntity(String.class)
146 errorMessage = "No response received from OOF."
149 " \"errorCode\": \"${errorCode}\",\n" +
150 " \"errorMessage\": \"${errorMessage}\"\n" +
156 errorMessage = e.getMessage()
159 " \"errorCode\": \"${errorCode}\",\n" +
160 " \"errorMessage\": \"${errorMessage}\"\n" +
174 private String buildOOFRequest(DelegateExecution execution) {
176 def currentNSSI = execution.getVariable("currentNSSI")
178 String nssiId = currentNSSI['nssiId']
179 String requestId = execution.getVariable("mso-request-id")
181 String request = "{\n" +
182 " \"type\": \"NSSI\",\n" +
183 " \"NxIId\": \"${nssiId}\",\n" +
184 " \"requestInfo\": {\n" +
185 " \"transactionId\": \"${requestId}\",\n" +
186 " \"requestId\": \"${requestId}\",\n" +
187 " \"sourceId\": \"so\",\n" +
197 * Invokes deleteServiceOrder external API
200 void deleteServiceOrder(DelegateExecution execution) {
201 LOGGER.trace("${PREFIX} Start deleteServiceOrder")
203 def currentNSSI = execution.getVariable("currentNSSI")
206 //url:/nbi/api/v4/serviceOrder/"
207 def nbiEndpointUrl = UrnPropertiesReader.getVariable("nbi.endpoint.url", execution)
209 ServiceInstance networkServiceInstance = (ServiceInstance)currentNSSI['networkServiceInstance']
211 String url = String.format("${nbiEndpointUrl}/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
213 currentNSSI['deleteServiceOrderURL'] = url
215 String msoKey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
216 String basicAuth = UrnPropertiesReader.getVariable("mso.infra.endpoint.auth", execution)
218 String basicAuthValue = encryptBasicAuth(basicAuth, msoKey)
220 if (basicAuthValue != null) {
221 String responseAuthHeader = getAuthHeader(execution, basicAuthValue, msoKey)
222 String errorCode = jsonUtil.getJsonValue(responseAuthHeader, "errorCode")
223 if(errorCode == null || errorCode.isEmpty()) { // No error
224 authHeader = responseAuthHeader
227 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(responseAuthHeader, "errorMessage"))
230 LOGGER.error( "Unable to obtain BasicAuth - BasicAuth value null")
231 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
235 String callDeleteServiceOrderResponse = callDeleteServiceOrder(execution, url, authHeader)
236 String errorCode = jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorCode")
237 String deleteServcieResponse = ""
239 if(errorCode == null || errorCode.isEmpty()) { // No error
240 deleteServcieResponse = callDeleteServiceOrderResponse // check the response ???
243 LOGGER.error(jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorMessage"))
244 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorMessage"))
247 String msg = "Exception in DoDeallocateCoreNSSI.deleteServiceOrder. " + any.getCause()
249 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
252 LOGGER.trace("${PREFIX} Exit deleteServiceOrder")
256 String callDeleteServiceOrder(DelegateExecution execution, String urlString, String authHeader) {
257 String errorCode = ""
258 String errorMessage = ""
262 HttpClient httpClient = getHttpClientFactory().newJsonClient(new URL(urlString), ONAPComponents.EXTERNAL)
263 httpClient.addAdditionalHeader("Authorization", authHeader)
264 httpClient.addAdditionalHeader("Accept", "application/json")
265 Response httpResponse = httpClient.delete()
267 if (httpResponse.hasEntity()) {
268 response = httpResponse.readEntity(String.class)
272 errorMessage = "No response received."
275 " \"errorCode\": \"${errorCode}\",\n" +
276 " \"errorMessage\": \"${errorMessage}\"\n" +
281 String msg = "Exception in DoDeallocateCoreNSSI.deleteServiceOrder. " + any.getCause()
284 " \"errorCode\": \"7000\",\n" +
285 " \"errorMessage\": \"${msg}\"\n" +
294 * Removes NSSI association with NSI
297 void removeNSSIAssociationWithNSI(DelegateExecution execution) {
298 LOGGER.trace("${PREFIX} Start removeNSSIAssociationWithNSI")
300 AAIResourcesClient client = getAAIClient()
302 def currentNSSI = execution.getVariable("currentNSSI")
304 String nssiId = currentNSSI['nssiId']
305 String nsiId = currentNSSI['nsiId']
306 String globalSubscriberId = execution.getVariable("globalSubscriberId")
307 String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
310 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
311 ServiceInstance nssi = currentNSSI['nssi']
313 String allottedResourceId = null
315 // Removes Allotted resource
316 List<AllottedResource> allottedResources = nssi.getAllottedResources()?.getAllottedResource()
317 if(allottedResources != null && allottedResources.size() == 1) { // Shouldn contain one allotted resource
318 allottedResourceId = allottedResources.get(0).getId()
319 allottedResources.remove(0)
322 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "No allotted resource found for NSSI id = " + nssiId)
326 client.update(nssiUri, nssi)
328 exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI association with NSI disconnect call: " + e.getMessage())
332 // Remove association between NSI and Allotted Resource
333 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
334 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiId).allottedResource(allottedResourceId))
337 client.disconnect(nsiUri, allottedResourceUri)
339 exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI association with NSI disconnect call: " + e.getMessage())
342 LOGGER.trace("${PREFIX} Exit removeNSSIAssociationWithNSI")
347 * Delets NSSI Service Instance
350 void deleteNSSIServiceInstance(DelegateExecution execution) {
351 LOGGER.trace("${PREFIX} Start deleteNSSIServiceInstance")
353 AAIResourcesClient client = getAAIClient()
355 def currentNSSI = execution.getVariable("currentNSSI")
357 String nssiId = currentNSSI['nssiId']
358 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
361 getAAIClient().delete(nssiUri)
363 exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occurred while NSSI Service Instance delete call: " + e.getMessage())
366 LOGGER.trace("${PREFIX} Exit deleteNSSIServiceInstance")
371 * Gets Delete Service Order progress
374 void getDeleteServiceOrderProgress(DelegateExecution execution) {
375 LOGGER.trace("${getPrefix()} Start getDeleteServiceOrderProgress")
377 def currentNSSI = execution.getVariable("currentNSSI")
379 String url = currentNSSI['deleteServiceOrderURL']
381 getProgress(execution, url, "deleteStatus")