Make echo liveness probe available under /actuator/health for resources 17/138717/2
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Tue, 13 Aug 2024 12:13:25 +0000 (14:13 +0200)
committerFiete Ostkamp <fiete.ostkamp@telekom.de>
Tue, 13 Aug 2024 13:25:58 +0000 (13:25 +0000)
- 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 <Fiete.Ostkamp@telekom.de>
aai-resources/pom.xml
aai-resources/src/main/java/org/onap/aai/rest/util/EchoHealthIndicator.java [new file with mode: 0644]
aai-resources/src/main/resources/application.properties
aai-resources/src/test/java/org/onap/aai/config/WebClientConfiguration.java
aai-resources/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java [new file with mode: 0644]
pom.xml
version.properties

index 7334d9a..0c615b5 100644 (file)
@@ -28,7 +28,7 @@
     <parent>
         <groupId>org.onap.aai.resources</groupId>
         <artifactId>resources</artifactId>
-        <version>1.14.5-SNAPSHOT</version>
+        <version>1.14.6-SNAPSHOT</version>
     </parent>
     <properties>
         <java.version>1.8</java.version>
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 (file)
index 0000000..fd5dbcf
--- /dev/null
@@ -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);
+       }
+
+}
index 2f33d8a..6487dee 100644 (file)
@@ -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
index cce0ae7..e412ded 100644 (file)
@@ -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 (file)
index 0000000..f6f7480
--- /dev/null
@@ -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();
+  }
+}
diff --git a/pom.xml b/pom.xml
index 8bb57d0..f6f2e92 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -30,7 +30,7 @@
     </parent>
     <groupId>org.onap.aai.resources</groupId>
     <artifactId>resources</artifactId>
-    <version>1.14.5-SNAPSHOT</version>
+    <version>1.14.6-SNAPSHOT</version>
     <name>aai-resources</name>
     <packaging>pom</packaging>
     <modules>
index 82f9610..cae43c2 100644 (file)
@@ -5,7 +5,7 @@
 
 major_version=1
 minor_version=14
-patch_version=4
+patch_version=6
 
 base_version=${major_version}.${minor_version}.${patch_version}