From 412d236404aa0ef9aebec658a1aeca80ab968fcd Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Tue, 13 Aug 2024 14:13:25 +0200 Subject: Make echo liveness probe available under /actuator/health for resources - implement spring's HealthIndicator interface to provide health probe based on AaiGraphChecker - conditionally enable the probe when aai.actuator.echo.enabled=true - increase snapshot version to 1.15.6-SNAPSHOT Issue-ID: AAI-3959 Change-Id: Ie2b49ad1f3145285be844bb11aa8119a16fb68c9 Signed-off-by: Fiete Ostkamp --- aai-resources/pom.xml | 2 +- .../onap/aai/rest/util/EchoHealthIndicator.java | 50 ++++++++++++++++ .../src/main/resources/application.properties | 6 ++ .../onap/aai/config/WebClientConfiguration.java | 15 +++++ .../aai/rest/util/EchoHealthIndicatorTest.java | 70 ++++++++++++++++++++++ 5 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 aai-resources/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java create mode 100644 aai-resources/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java (limited to 'aai-resources') diff --git a/aai-resources/pom.xml b/aai-resources/pom.xml index 7334d9a..0c615b5 100644 --- a/aai-resources/pom.xml +++ b/aai-resources/pom.xml @@ -28,7 +28,7 @@ org.onap.aai.resources resources - 1.14.5-SNAPSHOT + 1.14.6-SNAPSHOT 1.8 diff --git a/aai-resources/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java b/aai-resources/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java new file mode 100644 index 0000000..fd5dbcf --- /dev/null +++ b/aai-resources/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java @@ -0,0 +1,50 @@ +/** + * ============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.onap.aai.tasks.AaiGraphChecker; +import org.onap.aai.tasks.AaiGraphChecker.CheckerType; +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(CheckerType.ACTUAL); + } + +} diff --git a/aai-resources/src/main/resources/application.properties b/aai-resources/src/main/resources/application.properties index 2f33d8a..6487dee 100644 --- a/aai-resources/src/main/resources/application.properties +++ b/aai-resources/src/main/resources/application.properties @@ -155,3 +155,9 @@ BOOTSTRAP_SERVERS=localhost:9092 JAAS_CONFIG="" BUNDLECONFIG_DIR=src/main/resources/ AJSC_HOME=./ + +# 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 diff --git a/aai-resources/src/test/java/org/onap/aai/config/WebClientConfiguration.java b/aai-resources/src/test/java/org/onap/aai/config/WebClientConfiguration.java index cce0ae7..e412ded 100644 --- a/aai-resources/src/test/java/org/onap/aai/config/WebClientConfiguration.java +++ b/aai-resources/src/test/java/org/onap/aai/config/WebClientConfiguration.java @@ -5,10 +5,12 @@ import java.util.Collections; import org.onap.aai.setup.SchemaVersions; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Primary; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; @@ -19,6 +21,7 @@ public class WebClientConfiguration { @Lazy @Bean + @Primary WebTestClient webTestClient(@LocalServerPort int port) { return WebTestClient.bindToServer() .baseUrl("http://localhost:" + port + "/aai/" + schemaVersions.getDefaultVersion()) @@ -31,4 +34,16 @@ public class WebClientConfiguration { }) .build(); } + + @Lazy + @Bean + WebTestClient mgmtClient(@Value("${local.management.port}") int port) { + return WebTestClient.bindToServer() + .baseUrl("http://localhost:" + port) + .responseTimeout(Duration.ofSeconds(300)) + .defaultHeaders(headers -> { + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + }) + .build(); + } } diff --git a/aai-resources/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java b/aai-resources/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java new file mode 100644 index 0000000..f6f7480 --- /dev/null +++ b/aai-resources/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java @@ -0,0 +1,70 @@ +/** + * ============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 static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Test; +import org.onap.aai.config.WebClientConfiguration; +import org.onap.aai.tasks.AaiGraphChecker; +import org.onap.aai.tasks.AaiGraphChecker.CheckerType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.web.reactive.server.WebTestClient; + +@Import(WebClientConfiguration.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(properties = "aai.actuator.echo.enabled=true") +public class EchoHealthIndicatorTest { + + @Autowired + @Qualifier("mgmtClient") + WebTestClient webClient; + + @MockBean private AaiGraphChecker aaiGraphChecker; + + @Test + public void thatActuatorCheckIsHealthy() { + when(aaiGraphChecker.isAaiGraphDbAvailable(CheckerType.ACTUAL)).thenReturn(true); + + webClient.get() + .uri("/actuator/health") + .exchange() + .expectStatus() + .isOk(); + } + + @Test + public void thatActuatorCheckIsUnhealthy() { + when(aaiGraphChecker.isAaiGraphDbAvailable(CheckerType.ACTUAL)).thenReturn(false); + + webClient.get() + .uri("/actuator/health") + .exchange() + .expectStatus() + .is5xxServerError(); + } +} -- cgit 1.2.3-korg