From c4485f7218fb9b2b4b7c113294ae2902979f5b5e Mon Sep 17 00:00:00 2001 From: "halil.cakal" Date: Thu, 31 Aug 2023 11:45:47 +0100 Subject: Dmi plugin watchdog cheking aliveness - Add capability of GET request into DmiRestClient - Add watchdog job to check aliveness of dmi plugins - DmiPluginStatus enum as UP or DOWN - Add unit tests for the new function in dmi rest client - Add unit tests for dmi watchdog Issue-ID: CPS-1856 Change-Id: Ic38a96f0485a0bfe1b6af5bb2f57f6119d8fa563 Signed-off-by: halil.cakal --- .../cps/ncmp/api/impl/client/DmiRestClient.java | 26 ++++++++++ .../embeddedcache/TrustLevelCacheConfig.java | 19 +++---- .../dmiavailability/DMiPluginWatchDog.java | 60 ++++++++++++++++++++++ .../dmiavailability/DmiPluginStatus.java | 25 +++++++++ 4 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DMiPluginWatchDog.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginStatus.java (limited to 'cps-ncmp-service/src/main/java') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java index 28e09ac4b..7066f8016 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/client/DmiRestClient.java @@ -21,10 +21,13 @@ package org.onap.cps.ncmp.api.impl.client; +import com.fasterxml.jackson.databind.JsonNode; import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties; import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException; import org.onap.cps.ncmp.api.impl.operations.OperationType; +import org.onap.cps.ncmp.api.impl.trustlevel.dmiavailability.DmiPluginStatus; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -35,6 +38,7 @@ import org.springframework.web.client.RestTemplate; @Component @AllArgsConstructor +@Slf4j public class DmiRestClient { private RestTemplate restTemplate; @@ -60,6 +64,28 @@ public class DmiRestClient { } } + /** + * Sends GET operation to DMI plugin's health check URL. + * + * @param dmiPluginBaseUrl the base URL of the dmi-plugin + * @return DmiPluginStatus as UP or DOWN + */ + public DmiPluginStatus getDmiPluginStatus(final String dmiPluginBaseUrl) { + try { + final HttpEntity httpHeaders = new HttpEntity<>(configureHttpHeaders(new HttpHeaders())); + final JsonNode dmiPluginHealthStatus = restTemplate.getForObject(dmiPluginBaseUrl + "/manage/health", + JsonNode.class, httpHeaders); + if (dmiPluginHealthStatus != null) { + if (dmiPluginHealthStatus.get("status").asText().equals("UP")) { + return DmiPluginStatus.UP; + } + } + } catch (final Exception exception) { + log.warn("Could not send request for health check since {}", exception.getMessage()); + } + return DmiPluginStatus.DOWN; + } + private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) { if (dmiProperties.isDmiBasicAuthEnabled()) { httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword()); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/TrustLevelCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/TrustLevelCacheConfig.java index 66f61906d..ebe99057d 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/TrustLevelCacheConfig.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/TrustLevelCacheConfig.java @@ -32,24 +32,25 @@ import org.springframework.context.annotation.Configuration; @Configuration public class TrustLevelCacheConfig extends HazelcastCacheConfig { - private static final SetConfig untrustworthyCmHandlesSetConfig = createSetConfig("untrustworthyCmHandlesSetConfig"); + private static final SetConfig untrustworthyCmHandlesSetConfig = + createSetConfig("untrustworthyCmHandlesSetConfig"); + + private static final MapConfig trustLevelPerDmiPluginCacheConfig = + createMapConfig("trustLevelPerDmiPluginCacheConfig"); /** - * Untrustworthy cmhandle set instance. + * Distributed collection of untrustworthy cm handles. * - * @return instance of distributed set of untrustworthy cmhandles. + * @return instance of distributed set of untrustworthy cm handles. */ @Bean public ISet untrustworthyCmHandlesSet() { - return createHazelcastInstance("untrustworthyCmHandlesSet", untrustworthyCmHandlesSetConfig).getSet( - "untrustworthyCmHandlesSet"); + return createHazelcastInstance("untrustworthyCmHandlesSet", + untrustworthyCmHandlesSetConfig).getSet("untrustworthyCmHandlesSet"); } - private static final MapConfig trustLevelPerDmiPluginCacheConfig = - createMapConfig("trustLevelPerDmiPluginCacheConfig"); - /** - * Distributed instance of trust level cache that contains dmi-plugin name by trust level. + * Distributed instance of trust level cache containing the trust level per dmi plugin service(name). * * @return configured map of dmi-plugin name as keys to trust-level for values. */ diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DMiPluginWatchDog.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DMiPluginWatchDog.java new file mode 100644 index 000000000..dac32aa73 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DMiPluginWatchDog.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.trustlevel.dmiavailability; + +import com.hazelcast.map.IMap; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.client.DmiRestClient; +import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +public class DMiPluginWatchDog { + + private final IMap trustLevelPerDmiPlugin; + + private final DmiRestClient dmiRestClient; + + /** + * Monitors the aliveness of DMI plugins by this watchdog. + * This method periodically checks the health and status of each DMI plugin to ensure that + * they are functioning properly. If a plugin is found to be unresponsive or in an + * unhealthy state, the cache will be updated with the latest status. + * The @fixedDelayString is the time interval, in milliseconds, between consecutive aliveness checks. + */ + @Scheduled(fixedDelayString = "${ncmp.timers.trust-evel.dmi-availability-watchdog-ms:30000}") + public void watchDmiPluginAliveness() { + trustLevelPerDmiPlugin.keySet().forEach((dmiPluginName) -> { + final DmiPluginStatus dmiPluginStatus = dmiRestClient.getDmiPluginStatus(dmiPluginName); + log.debug("Trust level for dmi-plugin: {} is {}", dmiPluginName, dmiPluginStatus.toString()); + if (DmiPluginStatus.UP.equals(dmiPluginStatus)) { + trustLevelPerDmiPlugin.put(dmiPluginName, TrustLevel.COMPLETE); + } else { + trustLevelPerDmiPlugin.put(dmiPluginName, TrustLevel.NONE); + } + }); + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginStatus.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginStatus.java new file mode 100644 index 000000000..352d36f94 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginStatus.java @@ -0,0 +1,25 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.trustlevel.dmiavailability; + +public enum DmiPluginStatus { + UP, DOWN; +} -- cgit 1.2.3-korg