diff options
Diffstat (limited to 'src/main')
4 files changed, 226 insertions, 87 deletions
diff --git a/src/main/java/org/onap/aai/rest/util/AaiGraphChecker.java b/src/main/java/org/onap/aai/rest/util/AaiGraphChecker.java new file mode 100644 index 0000000..dab5cc3 --- /dev/null +++ b/src/main/java/org/onap/aai/rest/util/AaiGraphChecker.java @@ -0,0 +1,88 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Bell Canada + * Modification Copyright (C) 2022 Deutsche Telekom SA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest.util; + +import java.util.Iterator; + +import org.janusgraph.core.JanusGraphException; +import org.janusgraph.core.JanusGraphTransaction; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.logging.ErrorLogHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +/** + * Singleton class responsible to check that AAI service is able to connect to its back-end + * database. + */ +@Component +public class AaiGraphChecker { + private static final Logger LOGGER = LoggerFactory.getLogger(AaiGraphChecker.class); + + private AaiGraphChecker() { + } + + /** + * Checks whether a connection to the graph database can be made. + * + * @return + * <li>true, if database is available</li> + * <li>false, if database is NOT available</li> + */ + public Boolean isAaiGraphDbAvailable() { + Boolean dbAvailable; + JanusGraphTransaction transaction = null; + try { + transaction = AAIGraph.getInstance().getGraph().newTransaction(); + final Iterator<?> vertexIterator = transaction.query().limit(1).vertices().iterator(); + vertexIterator.hasNext(); + dbAvailable = Boolean.TRUE; + } catch (JanusGraphException e) { + String message = "Database is not available (after JanusGraph exception)"; + ErrorLogHelper.logError("500", message + ": " + e.getMessage()); + LOGGER.error(message, e); + dbAvailable = Boolean.FALSE; + } catch (Error e) { + String message = "Database is not available (after error)"; + ErrorLogHelper.logError("500", message + ": " + e.getMessage()); + LOGGER.error(message, e); + dbAvailable = Boolean.FALSE; + } catch (Exception e) { + String message = "Database availability can not be determined"; + ErrorLogHelper.logError("500", message + ": " + e.getMessage()); + LOGGER.error(message, e); + dbAvailable = null; + } finally { + if (transaction != null && !transaction.isClosed()) { + // check if transaction is open then closed instead of flag + try { + transaction.rollback(); + } catch (Exception e) { + String message = "Exception occurred while closing transaction"; + LOGGER.error(message, e); + ErrorLogHelper.logError("500", message + ": " + e.getMessage()); + } + } + } + return dbAvailable; + } +} diff --git a/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java b/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java new file mode 100644 index 0000000..781c7a9 --- /dev/null +++ b/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java @@ -0,0 +1,48 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.rest.util; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +@ConditionalOnProperty(name = "aai.actuator.echo.enabled", havingValue = "true") +public class EchoHealthIndicator implements HealthIndicator { + + private final AaiGraphChecker aaiGraphChecker; + + @Override + public Health health() { + return healthy() + ? Health.up().build() + : Health.down().build(); + } + + private boolean healthy() { + return aaiGraphChecker.isAaiGraphDbAvailable(); + } + +} diff --git a/src/main/java/org/onap/aai/rest/util/EchoResponse.java b/src/main/java/org/onap/aai/rest/util/EchoResponse.java index b033473..ebe2356 100644 --- a/src/main/java/org/onap/aai/rest/util/EchoResponse.java +++ b/src/main/java/org/onap/aai/rest/util/EchoResponse.java @@ -1,14 +1,13 @@ -/** +/* * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2022 Bell Canada + * Modification Copyright (C) 2022 Deutsche Telekom SA * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -19,10 +18,8 @@ */ package org.onap.aai.rest.util; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.logging.ErrorLogHelper; -import org.onap.aai.restcore.RESTAPI; -import org.springframework.stereotype.Component; +import java.util.ArrayList; +import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; @@ -34,89 +31,87 @@ import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import java.util.ArrayList; -import java.util.HashMap; -/** - * The Class EchoResponse. - */ +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.logging.ErrorLogHelper; +import org.onap.aai.restcore.RESTAPI; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + @Component @Path("/util") public class EchoResponse extends RESTAPI { - - protected static String authPolicyFunctionName = "util"; - - public static final String echoPath = "/util/echo"; + private static final Logger LOGGER = LoggerFactory.getLogger(EchoResponse.class); + + public static final String echoPath = "/util/echo"; + private final AaiGraphChecker aaiGraphChecker; + + private static final String UP_RESPONSE="{\"status\":\"UP\",\"groups\":[\"liveness\",\"readiness\"]}"; + + + @Autowired + public EchoResponse(AaiGraphChecker aaiGraphChecker ) { + this.aaiGraphChecker = aaiGraphChecker; + } + + @GET + @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) + @Path("/echo") + public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req, + @QueryParam("action") String myAction) { + Response response; + + String fromAppId; + String transId; + try { + fromAppId = getFromAppId(headers); + transId = getTransId(headers); + } catch (AAIException e) { + ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add("PUT uebProvider"); + templateVars.add("addTopic"); + return Response + .status(e.getErrorObject().getHTTPResponseCode()) + .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars)) + .build(); + } - /** - * Simple health-check API that echos back the X-FromAppId and X-TransactionId to clients. - * If there is a query string, a transaction gets logged into hbase, proving the application is connected to the data store. - * If there is no query string, no transacction logging is done to hbase. - * - * @param headers the headers - * @param req the req - * @param myAction if exists will cause transaction to be logged to hbase - * @return the response - */ - @GET - @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) - @Path("/echo") - public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req, - @QueryParam("action") String myAction) { - Response response = null; - - AAIException ex = null; - String fromAppId = null; - String transId = null; - - try { - fromAppId = getFromAppId(headers ); - transId = getTransId(headers); - } catch (AAIException e) { - ArrayList<String> templateVars = new ArrayList<String>(); - templateVars.add("PUT uebProvider"); - templateVars.add("addTopic"); - return Response - .status(e.getErrorObject().getHTTPResponseCode()) - .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars)) - .build(); - } - - try { - - HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<AAIException, ArrayList<String>>(); - - ArrayList<String> templateVars = new ArrayList<String>(); - templateVars.add(fromAppId); - templateVars.add(transId); - - exceptionList.put(new AAIException("AAI_0002", "OK"), templateVars); - - response = Response.status(Status.OK) - .entity(ErrorLogHelper.getRESTAPIInfoResponse(new ArrayList<>(headers.getAcceptableMediaTypes()) - , exceptionList)) - .build(); - - } catch (Exception e) { - ex = new AAIException("AAI_4000", e); - ArrayList<String> templateVars = new ArrayList<String>(); - templateVars.add(Action.GET.name()); - templateVars.add(fromAppId +" "+transId); + ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(fromAppId); + templateVars.add(transId); - response = Response - .status(Status.INTERNAL_SERVER_ERROR) - .entity(ErrorLogHelper.getRESTAPIErrorResponse( - new ArrayList<>(headers.getAcceptableMediaTypes()), ex, - templateVars)).build(); + try { + if (myAction != null) { + LOGGER.info("Checking Database Connectivity...!"); + if (!aaiGraphChecker.isAaiGraphDbAvailable()) { + throw new AAIException("AAI_5105", "Error establishing a database connection"); + } + return generateSuccessResponse(); + } + return generateSuccessResponse(); - } finally { - if (ex != null) { - ErrorLogHelper.logException(ex); - } + } catch (AAIException aaiException) { + ErrorLogHelper.logException(aaiException); + return generateFailureResponse(headers, templateVars, aaiException); + } catch (Exception e) { + AAIException aaiException = new AAIException("AAI_4000", e); + ErrorLogHelper.logException(aaiException); + return generateFailureResponse(headers, templateVars, aaiException); + } + } - } - - return response; - } + private Response generateSuccessResponse() { + return Response.status(Status.OK) + .entity(UP_RESPONSE) + .build(); + } + private Response generateFailureResponse(HttpHeaders headers, ArrayList<String> templateVariables, + AAIException aaiException) { + return Response.status(aaiException.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper + .getRESTAPIErrorResponseWithLogging(headers.getAcceptableMediaTypes(), aaiException, templateVariables)) + .build(); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1eb0bed..47ab0bc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -98,7 +98,15 @@ aperture.service.timeout-in-milliseconds=300000 #To Expose the Prometheus scraping endpoint management.server.port=8448 #To Enable Actuator Endpoint, you can override this to True in OOM charts -management.endpoints.enabled-by-default=false +management.endpoints.enabled-by-default=true #To Enable Actuator Endpoint, you can override this in OOM Charts -#management.endpoints.web.exposure.include=info, health, loggers, prometheus +management.endpoints.web.exposure.include=info, health, loggers, prometheus management.metrics.web.server.auto-time-requests=false + +# If true, the actuator health check will be overriden +# to use the AaiGraphChecker check instead +# this does the same as the /echo endpoint, +# but doesn't show up in micrometer metrics +aai.actuator.echo.enabled=false + + |