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.ServiceInstance
26 import org.onap.aaiclient.client.aai.AAIResourcesClient
27 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
28 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
29 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
30 import org.onap.logging.filter.base.ONAPComponents
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.common.scripts.MsoUtils
33 import org.onap.so.bpmn.common.scripts.RequestDBUtil
34 import org.onap.so.bpmn.core.UrnPropertiesReader
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.client.HttpClient
37 import org.onap.so.client.HttpClientFactory
38 import org.slf4j.Logger
39 import org.slf4j.LoggerFactory
41 import javax.ws.rs.core.Response
43 class DoDeallocateCoreNSSI extends DoCommonCoreNSSI {
44 private final String PREFIX ="DoDeallocateCoreNSSI"
45 private final String ACTION = "Deallocate"
47 private ExceptionUtil exceptionUtil = new ExceptionUtil()
48 private RequestDBUtil requestDBUtil = new RequestDBUtil()
49 private MsoUtils utils = new MsoUtils()
50 private JsonUtils jsonUtil = new JsonUtils()
52 private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateCoreNSSI.class)
55 * Queries OOF for NSSI termination
58 void executeTerminateNSSIQuery(DelegateExecution execution) {
59 LOGGER.trace("${PREFIX} Start executeTerminateNSSIQuery")
61 String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
63 //Prepare auth for OOF
65 String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
66 String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
68 String basicAuthValue = encryptBasicAuth(basicAuth, msokey)
69 if (basicAuthValue != null) {
70 String responseAuthHeader = getAuthHeader(execution, basicAuthValue, msokey)
71 String errorCode = jsonUtil.getJsonValue(responseAuthHeader, "errorCode")
72 if(errorCode == null || errorCode.isEmpty()) { // No error
73 authHeader = responseAuthHeader
76 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(responseAuthHeader, "errorMessage"))
79 LOGGER.error( "Unable to obtain BasicAuth - BasicAuth value null")
80 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
84 //Prepare send request to OOF
85 String oofRequest = buildOOFRequest(execution)
87 String callOOFResponse = callOOF(urlString, authHeader, oofRequest)
88 String errorCode = jsonUtil.getJsonValue(callOOFResponse, "errorCode")
89 if(errorCode == null || errorCode.isEmpty()) { // No error
90 String oofResponse = callOOFResponse
91 String isTerminateNSSI = jsonUtil.getJsonValue(oofResponse, "terminateResponse")
93 execution.setVariable("isTerminateNSSI", Boolean.parseBoolean(isTerminateNSSI))
96 LOGGER.error(jsonUtil.getJsonValue(callOOFResponse, "errorMessage"))
97 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(callOOFResponse, "errorMessage"))
101 LOGGER.trace("${PREFIX} Exit executeTerminateNSSIQuery")
106 * Executes sync call to OOF
107 * @return OOF response
109 String callOOF(String urlString, String authHeader, String oofRequest) {
110 String errorCode = ""
111 String errorMessage = ""
115 URL url = new URL(urlString + "/api/oof/terminate/nxi/v1")
116 HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.OOF)
117 httpClient.addAdditionalHeader("Authorization", authHeader)
118 httpClient.addAdditionalHeader("Accept", "application/json")
119 httpClient.addAdditionalHeader("Content-Type", "application/json")
121 Response httpResponse = httpClient.post(oofRequest)
123 int responseCode = httpResponse.getStatus()
124 LOGGER.debug("OOF sync response code is: " + responseCode)
126 if (responseCode != 202) { // Accepted
127 errorCode = responseCode
128 errorMessage = "Received a Bad Sync Response from OOF."
131 " \"errorCode\": \"${errorCode}\",\n" +
132 " \"errorMessage\": \"${errorMessage}\"\n" +
134 //exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
137 if (httpResponse.hasEntity()) {
138 response = httpResponse.readEntity(String.class)
142 errorMessage = "No response received from OOF."
145 " \"errorCode\": \"${errorCode}\",\n" +
146 " \"errorMessage\": \"${errorMessage}\"\n" +
152 errorMessage = e.getMessage()
155 " \"errorCode\": \"${errorCode}\",\n" +
156 " \"errorMessage\": \"${errorMessage}\"\n" +
170 private String buildOOFRequest(DelegateExecution execution) {
172 def currentNSSI = execution.getVariable("currentNSSI")
174 String nssiId = currentNSSI['nssiId']
175 String requestId = execution.getVariable("mso-request-id")
177 String request = "{\n" +
178 " \"type\": \"NSSI\",\n" +
179 " \"NxIId\": \"${nssiId}\",\n" +
180 " \"requestInfo\": {\n" +
181 " \"transactionId\": \"${requestId}\",\n" +
182 " \"requestId\": \"${requestId}\",\n" +
183 " \"sourceId\": \"so\",\n" +
193 * Invokes deleteServiceOrder external API
196 void deleteServiceOrder(DelegateExecution execution) {
197 LOGGER.trace("${PREFIX} Start deleteServiceOrder")
199 def currentNSSI = execution.getVariable("currentNSSI")
202 //url:/nbi/api/v4/serviceOrder/"
203 def nbiEndpointUrl = UrnPropertiesReader.getVariable("nbi.endpoint.url", execution)
205 ServiceInstance networkServiceInstance = (ServiceInstance)currentNSSI['networkServiceInstance']
207 String url = String.format("${nbiEndpointUrl}/api/v4/serviceOrder/%s", networkServiceInstance.getServiceInstanceId())
209 currentNSSI['deleteServiceOrderURL'] = url
211 String msoKey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
212 String basicAuth = UrnPropertiesReader.getVariable("mso.infra.endpoint.auth", execution)
214 String basicAuthValue = encryptBasicAuth(basicAuth, msoKey)
216 if (basicAuthValue != null) {
217 String responseAuthHeader = getAuthHeader(execution, basicAuthValue, msoKey)
218 String errorCode = jsonUtil.getJsonValue(responseAuthHeader, "errorCode")
219 if(errorCode == null || errorCode.isEmpty()) { // No error
220 authHeader = responseAuthHeader
223 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(responseAuthHeader, "errorMessage"))
226 LOGGER.error( "Unable to obtain BasicAuth - BasicAuth value null")
227 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
231 String callDeleteServiceOrderResponse = callDeleteServiceOrder(execution, url, authHeader)
232 String errorCode = jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorCode")
233 String deleteServcieResponse = ""
235 if(errorCode == null || errorCode.isEmpty()) { // No error
236 deleteServcieResponse = callDeleteServiceOrderResponse // check the response ???
239 LOGGER.error(jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorMessage"))
240 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(errorCode), jsonUtil.getJsonValue(callDeleteServiceOrderResponse, "errorMessage"))
243 String msg = "Exception in DoDeallocateCoreNSSI.deleteServiceOrder. " + any.getCause()
245 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
248 LOGGER.trace("${PREFIX} Exit deleteServiceOrder")
252 String callDeleteServiceOrder(DelegateExecution execution, String urlString, String authHeader) {
253 String errorCode = ""
254 String errorMessage = ""
258 HttpClient httpClient = getHttpClientFactory().newJsonClient(new URL(urlString), ONAPComponents.EXTERNAL)
259 httpClient.addAdditionalHeader("Authorization", authHeader)
260 httpClient.addAdditionalHeader("Accept", "application/json")
261 Response httpResponse = httpClient.delete()
263 if (httpResponse.hasEntity()) {
264 response = httpResponse.readEntity(String.class)
268 errorMessage = "No response received."
271 " \"errorCode\": \"${errorCode}\",\n" +
272 " \"errorMessage\": \"${errorMessage}\"\n" +
277 String msg = "Exception in DoDeallocateCoreNSSI.deleteServiceOrder. " + any.getCause()
280 " \"errorCode\": \"7000\",\n" +
281 " \"errorMessage\": \"${msg}\"\n" +
290 * Removes NSSI association with NSI
293 void removeNSSIAssociationWithNSI(DelegateExecution execution) {
294 LOGGER.trace("${PREFIX} Start removeNSSIAssociationWithNSI")
296 AAIResourcesClient client = getAAIClient()
298 def currentNSSI = execution.getVariable("currentNSSI")
300 String nssiId = currentNSSI['nssiId']
301 String nsiId = currentNSSI['nsiId']
303 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
304 AAIResourceUri nsiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nsiId))
307 client.disconnect(nssiUri, nsiUri)
309 exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI association with NSI disconnect call: " + e.getMessage())
312 LOGGER.trace("${PREFIX} Exit removeNSSIAssociationWithNSI")
317 * Delets NSSI Service Instance
320 void deleteNSSIServiceInstance(DelegateExecution execution) {
321 LOGGER.trace("${PREFIX} Start deleteNSSIServiceInstance")
323 AAIResourcesClient client = getAAIClient()
325 def currentNSSI = execution.getVariable("currentNSSI")
327 String nssiId = currentNSSI['nssiId']
328 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
331 getAAIClient().delete(nssiUri)
333 exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occurred while NSSI Service Instance delete call: " + e.getMessage())
336 LOGGER.trace("${PREFIX} Exit deleteNSSIServiceInstance")
341 * Gets Delete Service Order progress
344 void getDeleteServiceOrderProgress(DelegateExecution execution) {
345 LOGGER.trace("${getPrefix()} Start getDeleteServiceOrderProgress")
347 def currentNSSI = execution.getVariable("currentNSSI")
349 String url = currentNSSI['deleteServiceOrderURL']
351 getProgress(execution, url, "deleteStatus")