From e18239631ff2f2f56961c8534a0edf294ee15fdd Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Tue, 25 Jun 2024 14:40:40 +0100 Subject: Move TrustLevel Feature -Moved TrustLevel impl from Controller to Inventory Facade -Applied new package name convention using impl\inventory\trustlevel for this (sub)feature -Renamed some trustlevel related classes to better describe their role -Disabled failing architecture test (agreed this is tech. debt and created CPS-2293) -Refactored YangDataConverter method names and signatures (moved Spec to correct place) -Cleaned up names and javadoc of class now called DeviceTrustLevelMessageConsumer -Applied conventions to modified test methods Issue-ID: CPS-2255 Change-Id: Iccd20541488e6b61444bb2c7406c90e61a34124d Signed-off-by: ToineSiebelink --- .../embeddedcache/TrustLevelCacheConfig.java | 64 ---------- .../LcmEventsCmHandleStateHandlerAsyncHelper.java | 2 +- .../lcm/LcmEventsCmHandleStateHandlerImpl.java | 2 +- .../impl/trustlevel/DeviceHeartbeatConsumer.java | 59 --------- .../cps/ncmp/api/impl/trustlevel/TrustLevel.java | 52 -------- .../api/impl/trustlevel/TrustLevelManager.java | 136 --------------------- .../dmiavailability/DmiPluginWatchDog.java | 80 ------------ .../inventory/NetworkCmProxyInventoryFacade.java | 26 ++-- .../api/inventory/models/NcmpServiceCmHandle.java | 4 +- .../cps/ncmp/api/inventory/models/TrustLevel.java | 52 ++++++++ .../ncmp/impl/datajobs/WriteRequestExaminer.java | 2 +- .../impl/inventory/CmHandleQueryServiceImpl.java | 4 +- .../inventory/CmHandleRegistrationService.java | 6 +- ...CmHandleRegistrationServicePropertyHandler.java | 3 +- .../impl/inventory/InventoryPersistenceImpl.java | 4 +- .../ParameterizedCmHandleQueryServiceImpl.java | 6 +- .../impl/inventory/sync/ModuleOperationsUtils.java | 2 +- .../impl/inventory/sync/ModuleSyncService.java | 2 +- .../ncmp/impl/inventory/sync/ModuleSyncTasks.java | 3 +- .../DeviceTrustLevelMessageConsumer.java | 59 +++++++++ .../trustlevel/DmiPluginTrustLevelWatchDog.java | 78 ++++++++++++ .../trustlevel/TrustLevelCacheConfig.java | 64 ++++++++++ .../inventory/trustlevel/TrustLevelManager.java | 136 +++++++++++++++++++++ .../cps/ncmp/impl/utils/YangDataConverter.java | 36 +++--- 24 files changed, 443 insertions(+), 439 deletions(-) delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/TrustLevelCacheConfig.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumer.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevel.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManager.java delete 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/inventory/models/TrustLevel.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelCacheConfig.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java (limited to 'cps-ncmp-service/src/main/java') 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 deleted file mode 100644 index 440cd3ded1..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/TrustLevelCacheConfig.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ============LICENSE_START======================================================== - * Copyright (C) 2023-2024 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.config.embeddedcache; - -import com.hazelcast.config.MapConfig; -import java.util.Map; -import org.onap.cps.cache.HazelcastCacheConfig; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class TrustLevelCacheConfig extends HazelcastCacheConfig { - - public static final String TRUST_LEVEL_PER_DMI_PLUGIN = "trustLevelPerDmiPlugin"; - - public static final String TRUST_LEVEL_PER_CM_HANDLE = "trustLevelPerCmHandle"; - private static final MapConfig trustLevelPerCmHandleCacheConfig = - createMapConfig("trustLevelPerCmHandleCacheConfig"); - - private static final MapConfig trustLevelPerDmiPluginCacheConfig = - createMapConfig("trustLevelPerDmiPluginCacheConfig"); - - /** - * Distributed instance of trust level cache containing the trust level per cm handle. - * - * @return configured map of cm handle name as keys to trust-level for values. - */ - @Bean(TRUST_LEVEL_PER_CM_HANDLE) - public Map trustLevelPerCmHandle() { - return createHazelcastInstance("hazelcastInstanceTrustLevelPerCmHandleMap", - trustLevelPerCmHandleCacheConfig).getMap(TRUST_LEVEL_PER_CM_HANDLE); - } - - /** - * 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. - */ - @Bean(TRUST_LEVEL_PER_DMI_PLUGIN) - public Map trustLevelPerDmiPlugin() { - return createHazelcastInstance("hazelcastInstanceTrustLevelPerDmiPluginMap", - trustLevelPerDmiPluginCacheConfig).getMap(TRUST_LEVEL_PER_DMI_PLUGIN); - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java index b7e6a7457c..af745caa44 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerAsyncHelper.java @@ -74,6 +74,6 @@ public class LcmEventsCmHandleStateHandlerAsyncHelper { } private static NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) { - return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle); + return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle); } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java index 97e5193512..4376014f36 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/lcm/LcmEventsCmHandleStateHandlerImpl.java @@ -194,7 +194,7 @@ public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleState } private NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) { - return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle); + return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle); } @Getter diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumer.java deleted file mode 100644 index 230a370d65..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumer.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * ============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; - -import io.cloudevents.CloudEvent; -import io.cloudevents.kafka.impl.KafkaHeaders; -import lombok.RequiredArgsConstructor; -import org.apache.kafka.clients.consumer.ConsumerRecord; -import org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper; -import org.onap.cps.ncmp.events.trustlevel.DeviceTrustLevel; -import org.springframework.kafka.annotation.KafkaListener; -import org.springframework.stereotype.Component; - -@Component -@RequiredArgsConstructor -public class DeviceHeartbeatConsumer { - - private static final String CLOUD_EVENT_ID_HEADER_NAME = "ce_id"; - private final TrustLevelManager trustLevelManager; - - /** - * Listening the device heartbeats. - * - * @param deviceHeartbeatConsumerRecord Device Heartbeat record. - */ - @KafkaListener(topics = "${app.dmi.device-heartbeat.topic}", - containerFactory = "cloudEventConcurrentKafkaListenerContainerFactory") - public void heartbeatListener(final ConsumerRecord deviceHeartbeatConsumerRecord) { - - final String cmHandleId = KafkaHeaders.getParsedKafkaHeader(deviceHeartbeatConsumerRecord.headers(), - CLOUD_EVENT_ID_HEADER_NAME); - - final DeviceTrustLevel deviceTrustLevel = - CloudEventMapper.toTargetEvent(deviceHeartbeatConsumerRecord.value(), DeviceTrustLevel.class); - final TrustLevel newDeviceTrustLevel = TrustLevel.valueOf(deviceTrustLevel.getData().getTrustLevel()); - trustLevelManager.handleUpdateOfDeviceTrustLevel(cmHandleId, newDeviceTrustLevel); - - } - -} - diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevel.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevel.java deleted file mode 100644 index f130604a64..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevel.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * ============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; - -import lombok.Getter; - -@Getter -public enum TrustLevel { - NONE(0), COMPLETE(99); - private final int level; - - /** - * Creates TrustLevel enum from a numeric value. - * - * @param level numeric value between 0-99 - */ - TrustLevel(final int level) { - this.level = level; - } - - /** - * Gets the lower trust level (effective) among two. - * - * @param other the trust level compared with this - * @return the lower trust level - */ - public final TrustLevel getEffectiveTrustLevel(final TrustLevel other) { - if (other.level < this.level) { - return other; - } - return this; - } - -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManager.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManager.java deleted file mode 100644 index d01acb8a3b..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManager.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 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; - -import java.util.Collection; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig; -import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher; -import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; -import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; -import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -@Slf4j -@Service -@RequiredArgsConstructor -public class TrustLevelManager { - - @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE) - private final Map trustLevelPerCmHandle; - - @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN) - private final Map trustLevelPerDmiPlugin; - - private final InventoryPersistence inventoryPersistence; - private final AvcEventPublisher avcEventPublisher; - private static final String AVC_CHANGED_ATTRIBUTE_NAME = "trustLevel"; - private static final String AVC_NO_OLD_VALUE = null; - - /** - * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE. - * - * @param cmHandlesToBeCreated a list of cmHandles being created - */ - public void handleInitialRegistrationOfTrustLevels(final Map cmHandlesToBeCreated) { - for (final Map.Entry entry : cmHandlesToBeCreated.entrySet()) { - final String cmHandleId = entry.getKey(); - if (trustLevelPerCmHandle.containsKey(cmHandleId)) { - log.warn("Cm handle: {} already registered", cmHandleId); - } else { - TrustLevel initialTrustLevel = entry.getValue(); - if (initialTrustLevel == null) { - initialTrustLevel = TrustLevel.COMPLETE; - } - trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel); - if (TrustLevel.NONE.equals(initialTrustLevel)) { - avcEventPublisher.publishAvcEvent(cmHandleId, - AVC_CHANGED_ATTRIBUTE_NAME, - AVC_NO_OLD_VALUE, - initialTrustLevel.name()); - } - } - } - } - - /** - * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it - * has changed. - * - * @param dmiServiceName dmi service name - * @param affectedCmHandleIds cm handle ids belonging to dmi service name - * @param newDmiTrustLevel new trust level of the dmi plugin - */ - public void handleUpdateOfDmiTrustLevel(final String dmiServiceName, - final Collection affectedCmHandleIds, - final TrustLevel newDmiTrustLevel) { - final TrustLevel oldDmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName); - trustLevelPerDmiPlugin.put(dmiServiceName, newDmiTrustLevel); - for (final String affectedCmHandleId : affectedCmHandleIds) { - final TrustLevel deviceTrustLevel = trustLevelPerCmHandle.get(affectedCmHandleId); - final TrustLevel oldEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(oldDmiTrustLevel); - final TrustLevel newEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(newDmiTrustLevel); - sendAvcNotificationIfRequired(affectedCmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel); - } - } - - /** - * Updates trust level of device in the cache and publish notification for trust level of device if it has - * changed. - * - * @param cmHandleId cm handle id - * @param newDeviceTrustLevel new trust level of the device - */ - public void handleUpdateOfDeviceTrustLevel(final String cmHandleId, - final TrustLevel newDeviceTrustLevel) { - final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId); - final String dmiServiceName = yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA); - - final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName); - final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId); - - final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel); - final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel); - - trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel); - sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel); - } - - private void sendAvcNotificationIfRequired(final String notificationCandidateCmHandleId, - final TrustLevel oldEffectiveTrustLevel, - final TrustLevel newEffectiveTrustLevel) { - if (oldEffectiveTrustLevel.equals(newEffectiveTrustLevel)) { - log.debug("The Cm Handle: {} has already the same trust level: {}", notificationCandidateCmHandleId, - newEffectiveTrustLevel); - } else { - log.info("The trust level for Cm Handle: {} is now: {} ", notificationCandidateCmHandleId, - newEffectiveTrustLevel); - avcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId, - AVC_CHANGED_ATTRIBUTE_NAME, - oldEffectiveTrustLevel.name(), - newEffectiveTrustLevel.name()); - } - } - -} 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 deleted file mode 100644 index c5098936c9..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginWatchDog.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 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 java.util.Collection; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.impl.client.DmiRestClient; -import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager; -import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -@Slf4j -@RequiredArgsConstructor -@Service -public class DmiPluginWatchDog { - - private final DmiRestClient dmiRestClient; - private final CmHandleQueryService cmHandleQueryService; - private final TrustLevelManager trustLevelManager; - - @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN) - private final Map trustLevelPerDmiPlugin; - - /** - * This class monitors the trust level of all DMI plugin by checking the health status - * the resulting trust level wil be stored in the relevant cache. - * The @fixedDelayString is the time interval, in milliseconds, between consecutive checks. - */ - @Scheduled(fixedDelayString = "${ncmp.timers.trust-level.dmi-availability-watchdog-ms:30000}") - public void checkDmiAvailability() { - trustLevelPerDmiPlugin.entrySet().forEach(entry -> { - final TrustLevel newDmiTrustLevel; - final TrustLevel oldDmiTrustLevel = entry.getValue(); - final String dmiServiceName = entry.getKey(); - final String dmiHealthStatus = getDmiHealthStatus(dmiServiceName); - log.debug("The health status for dmi-plugin: {} is {}", dmiServiceName, dmiHealthStatus); - - if ("UP".equals(dmiHealthStatus)) { - newDmiTrustLevel = TrustLevel.COMPLETE; - } else { - newDmiTrustLevel = TrustLevel.NONE; - } - if (oldDmiTrustLevel.equals(newDmiTrustLevel)) { - log.debug("The Dmi Plugin: {} has already the same trust level: {}", dmiServiceName, newDmiTrustLevel); - } else { - final Collection cmHandleIds = - cmHandleQueryService.getCmHandleIdsByDmiPluginIdentifier(dmiServiceName); - trustLevelManager.handleUpdateOfDmiTrustLevel(dmiServiceName, cmHandleIds, newDmiTrustLevel); - } - }); - } - - private String getDmiHealthStatus(final String dmiServiceName) { - return dmiRestClient.getDmiHealthStatus(dmiServiceName); - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/NetworkCmProxyInventoryFacade.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/NetworkCmProxyInventoryFacade.java index 0ff59c1316..794bc238f4 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/NetworkCmProxyInventoryFacade.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/NetworkCmProxyInventoryFacade.java @@ -27,8 +27,6 @@ package org.onap.cps.ncmp.api.inventory; import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateCmHandleQueryParameters; import java.util.Collection; -import java.util.HashMap; -import java.util.List; import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -38,6 +36,7 @@ import org.onap.cps.ncmp.api.inventory.models.CompositeState; import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistrationResponse; import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService; import org.onap.cps.ncmp.impl.inventory.CmHandleRegistrationService; import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; @@ -45,10 +44,12 @@ import org.onap.cps.ncmp.impl.inventory.ParameterizedCmHandleQueryService; import org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions; import org.onap.cps.ncmp.impl.inventory.models.InventoryQueryConditions; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; +import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelCacheConfig; import org.onap.cps.ncmp.impl.utils.YangDataConverter; import org.onap.cps.spi.model.ModuleDefinition; import org.onap.cps.spi.model.ModuleReference; import org.onap.cps.utils.JsonObjectMapper; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Slf4j @@ -62,6 +63,9 @@ public class NetworkCmProxyInventoryFacade { private final InventoryPersistence inventoryPersistence; private final JsonObjectMapper jsonObjectMapper; + @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE) + private final Map trustLevelPerCmHandle; + /** * Registration of Created, Removed, Updated or Upgraded CM Handles. * @@ -142,7 +146,10 @@ public class NetworkCmProxyInventoryFacade { final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType( cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class); validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES); - return parameterizedCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters); + final Collection ncmpServiceCmHandles = + parameterizedCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters); + ncmpServiceCmHandles.forEach(this::applyCurrentTrustLevel); + return ncmpServiceCmHandles; } /** @@ -176,8 +183,10 @@ public class NetworkCmProxyInventoryFacade { * @return cm handle details */ public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) { - return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle( + final NcmpServiceCmHandle ncmpServiceCmHandle = YangDataConverter.toNcmpServiceCmHandle( inventoryPersistence.getYangModelCmHandle(cmHandleId)); + applyCurrentTrustLevel(ncmpServiceCmHandle); + return ncmpServiceCmHandle; } /** @@ -188,10 +197,7 @@ public class NetworkCmProxyInventoryFacade { */ public Map getCmHandlePublicProperties(final String cmHandleId) { final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId); - final List yangModelPublicProperties = yangModelCmHandle.getPublicProperties(); - final Map cmHandlePublicProperties = new HashMap<>(); - YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties); - return cmHandlePublicProperties; + return YangDataConverter.toPropertiesMap(yangModelCmHandle.getPublicProperties()); } /** @@ -204,5 +210,9 @@ public class NetworkCmProxyInventoryFacade { return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState(); } + private void applyCurrentTrustLevel(final NcmpServiceCmHandle ncmpServiceCmHandle) { + ncmpServiceCmHandle.setCurrentTrustLevel(trustLevelPerCmHandle.get(ncmpServiceCmHandle.getCmHandleId())); + } + } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/NcmpServiceCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/NcmpServiceCmHandle.java index 69b33a16f5..3ebceed9d7 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/NcmpServiceCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/NcmpServiceCmHandle.java @@ -27,7 +27,6 @@ import java.util.Map; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel; import org.springframework.validation.annotation.Validated; /** @@ -57,6 +56,9 @@ public class NcmpServiceCmHandle { @JsonSetter(nulls = Nulls.AS_EMPTY) private TrustLevel registrationTrustLevel; + @JsonSetter(nulls = Nulls.AS_EMPTY) + private TrustLevel currentTrustLevel; + @JsonSetter(nulls = Nulls.AS_EMPTY) private String alternateId; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/TrustLevel.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/TrustLevel.java new file mode 100644 index 0000000000..83e6ecf912 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/models/TrustLevel.java @@ -0,0 +1,52 @@ +/* + * ============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.inventory.models; + +import lombok.Getter; + +@Getter +public enum TrustLevel { + NONE(0), COMPLETE(99); + private final int level; + + /** + * Creates TrustLevel enum from a numeric value. + * + * @param level numeric value between 0-99 + */ + TrustLevel(final int level) { + this.level = level; + } + + /** + * Gets the lower trust level (effective) among two. + * + * @param other the trust level compared with this + * @return the lower trust level + */ + public final TrustLevel getEffectiveTrustLevel(final TrustLevel other) { + if (other.level < this.level) { + return other; + } + return this; + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java index 0e51f0c447..70d08dccdc 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java @@ -99,7 +99,7 @@ public class WriteRequestExaminer { } private Map getPrivatePropertiesFromDataNode(final DataNode dataNode) { - final YangModelCmHandle yangModelCmHandle = YangDataConverter.convertCmHandleToYangModel(dataNode); + final YangModelCmHandle yangModelCmHandle = YangDataConverter.toYangModelCmHandle(dataNode); final Map cmHandleDmiProperties = new LinkedHashMap<>(); yangModelCmHandle.getDmiProperties() .forEach(dmiProperty -> cmHandleDmiProperties.put(dmiProperty.getName(), dmiProperty.getValue())); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleQueryServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleQueryServiceImpl.java index 4c2f93a95e..e36477f8c8 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleQueryServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleQueryServiceImpl.java @@ -34,11 +34,11 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; -import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; import org.onap.cps.ncmp.impl.inventory.models.ModelledDmiServiceLeaves; import org.onap.cps.ncmp.impl.inventory.models.PropertyType; +import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelCacheConfig; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java index f10ccd4aed..1cbba7d6c7 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationService.java @@ -47,10 +47,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.onap.cps.api.CpsDataService; -import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig; import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel; -import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager; import org.onap.cps.ncmp.api.impl.utils.AlternateIdChecker; import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse; import org.onap.cps.ncmp.api.inventory.models.CompositeState; @@ -58,9 +55,12 @@ import org.onap.cps.ncmp.api.inventory.models.CompositeStateBuilder; import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistrationResponse; import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; import org.onap.cps.ncmp.impl.inventory.models.CmHandleState; import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; import org.onap.cps.ncmp.impl.inventory.sync.ModuleOperationsUtils; +import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelCacheConfig; +import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelManager; import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationServicePropertyHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationServicePropertyHandler.java index 164d917257..fe47c0e2f2 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationServicePropertyHandler.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/CmHandleRegistrationServicePropertyHandler.java @@ -129,8 +129,7 @@ public class CmHandleRegistrationServicePropertyHandler { final NcmpServiceCmHandle ncmpServiceCmHandle) { final String newDataProducerIdentifier = ncmpServiceCmHandle.getDataProducerIdentifier(); if (StringUtils.hasText(newDataProducerIdentifier)) { - final YangModelCmHandle yangModelCmHandle = - YangDataConverter.convertCmHandleToYangModel(cmHandleDataNode); + final YangModelCmHandle yangModelCmHandle = YangDataConverter.toYangModelCmHandle(cmHandleDataNode); final String existingDataProducerIdentifier = yangModelCmHandle.getDataProducerIdentifier(); if (StringUtils.hasText(existingDataProducerIdentifier)) { if (!existingDataProducerIdentifier.equals(newDataProducerIdentifier)) { diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java index 40aefed2aa..2930d4f87c 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java @@ -112,7 +112,7 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) { cpsValidator.validateNameCharacters(cmHandleId); final DataNode dataNode = getCmHandleDataNodeByCmHandleId(cmHandleId).iterator().next(); - return YangDataConverter.convertCmHandleToYangModel(dataNode); + return YangDataConverter.toYangModelCmHandle(dataNode); } @Override @@ -127,7 +127,7 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv dataValidationException.getMessage()); } }); - return YangDataConverter.convertDataNodesToYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds)); + return YangDataConverter.toYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds)); } @Override diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/ParameterizedCmHandleQueryServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/ParameterizedCmHandleQueryServiceImpl.java index 51e403fd66..45922454fd 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/ParameterizedCmHandleQueryServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/ParameterizedCmHandleQueryServiceImpl.java @@ -27,7 +27,7 @@ import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HA import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_PROPERTIES; import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_CPS_PATH; import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_TRUST_LEVEL; -import static org.onap.cps.ncmp.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle; +import static org.onap.cps.ncmp.impl.utils.YangDataConverter.toNcmpServiceCmHandle; import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY; import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS; @@ -232,13 +232,13 @@ public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHan final Collection ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size()); yangModelcmHandles.forEach(yangModelcmHandle -> - ncmpServiceCmHandles.add(YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelcmHandle)) + ncmpServiceCmHandles.add(YangDataConverter.toNcmpServiceCmHandle(yangModelcmHandle)) ); return ncmpServiceCmHandles; } private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) { - return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter.convertCmHandleToYangModel(dataNode)); + return toNcmpServiceCmHandle(YangDataConverter.toYangModelCmHandle(dataNode)); } private Collection executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleOperationsUtils.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleOperationsUtils.java index 15e4653c92..e344be8e1f 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleOperationsUtils.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleOperationsUtils.java @@ -238,7 +238,7 @@ public class ModuleOperationsUtils { private List convertCmHandlesDataNodesToYangModelCmHandles( final List cmHandlesAsDataNodeList) { - return cmHandlesAsDataNodeList.stream().map(YangDataConverter::convertCmHandleToYangModel).toList(); + return cmHandlesAsDataNodeList.stream().map(YangDataConverter::toYangModelCmHandle).toList(); } private boolean isRetryDue(final CompositeState.LockReason compositeStateLockReason, final OffsetDateTime time) { diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncService.java index 2c8639a4dd..57d28aa743 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncService.java @@ -139,7 +139,7 @@ public class ModuleSyncService { final List dataNodes = cmHandleQueryService.queryNcmpRegistryByCpsPath( NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@module-set-tag='" + escapedModuleSetTag + "']", FetchDescendantsOption.DIRECT_CHILDREN_ONLY); - return dataNodes.stream().map(YangDataConverter::convertCmHandleToYangModel) + return dataNodes.stream().map(YangDataConverter::toYangModelCmHandle) .filter(cmHandle -> cmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY) .findFirst().orElse(null); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java index 80c290cb54..08db195ad6 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/ModuleSyncTasks.java @@ -64,8 +64,7 @@ public class ModuleSyncTasks { = new HashMap<>(cmHandlesAsDataNodes.size()); for (final DataNode cmHandleAsDataNode : cmHandlesAsDataNodes) { final String cmHandleId = String.valueOf(cmHandleAsDataNode.getLeaves().get("id")); - final YangModelCmHandle yangModelCmHandle = - YangDataConverter.convertCmHandleToYangModel(cmHandleAsDataNode); + final YangModelCmHandle yangModelCmHandle = YangDataConverter.toYangModelCmHandle(cmHandleAsDataNode); final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId); final boolean inUpgrade = ModuleOperationsUtils.inUpgradeOrUpgradeFailed(compositeState); try { diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java new file mode 100644 index 0000000000..64ae800879 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DeviceTrustLevelMessageConsumer.java @@ -0,0 +1,59 @@ +/* + * ============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.impl.inventory.trustlevel; + +import io.cloudevents.CloudEvent; +import io.cloudevents.kafka.impl.KafkaHeaders; +import lombok.RequiredArgsConstructor; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; +import org.onap.cps.ncmp.events.trustlevel.DeviceTrustLevel; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class DeviceTrustLevelMessageConsumer { + + private static final String CLOUD_EVENT_ID_HEADER_NAME = "ce_id"; + private final TrustLevelManager trustLevelManager; + + /** + * Listening to the device trust level updates. + * + * @param consumerRecord Device trust level record. + */ + @KafkaListener(topics = "${app.dmi.device-heartbeat.topic}", + containerFactory = "cloudEventConcurrentKafkaListenerContainerFactory") + public void deviceTrustLevelListener(final ConsumerRecord consumerRecord) { + + final String cmHandleId = KafkaHeaders.getParsedKafkaHeader(consumerRecord.headers(), + CLOUD_EVENT_ID_HEADER_NAME); + + final DeviceTrustLevel deviceTrustLevel = + CloudEventMapper.toTargetEvent(consumerRecord.value(), DeviceTrustLevel.class); + final String trustLevelAsString = deviceTrustLevel.getData().getTrustLevel(); + trustLevelManager.handleUpdateOfDeviceTrustLevel(cmHandleId, TrustLevel.valueOf(trustLevelAsString)); + } + +} + diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java new file mode 100644 index 0000000000..19597a205b --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/DmiPluginTrustLevelWatchDog.java @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 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.impl.inventory.trustlevel; + +import java.util.Collection; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.client.DmiRestClient; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; +import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +public class DmiPluginTrustLevelWatchDog { + + private final DmiRestClient dmiRestClient; + private final CmHandleQueryService cmHandleQueryService; + private final TrustLevelManager trustLevelManager; + + @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN) + private final Map trustLevelPerDmiPlugin; + + /** + * This class monitors the trust level of all DMI plugin by checking the health status + * the resulting trust level wil be stored in the relevant cache. + * The @fixedDelayString is the time interval, in milliseconds, between consecutive checks. + */ + @Scheduled(fixedDelayString = "${ncmp.timers.trust-level.dmi-availability-watchdog-ms:30000}") + public void checkDmiAvailability() { + trustLevelPerDmiPlugin.entrySet().forEach(entry -> { + final TrustLevel newDmiTrustLevel; + final TrustLevel oldDmiTrustLevel = entry.getValue(); + final String dmiServiceName = entry.getKey(); + final String dmiHealthStatus = getDmiHealthStatus(dmiServiceName); + log.debug("The health status for dmi-plugin: {} is {}", dmiServiceName, dmiHealthStatus); + + if ("UP".equals(dmiHealthStatus)) { + newDmiTrustLevel = TrustLevel.COMPLETE; + } else { + newDmiTrustLevel = TrustLevel.NONE; + } + if (oldDmiTrustLevel.equals(newDmiTrustLevel)) { + log.debug("The Dmi Plugin: {} has already the same trust level: {}", dmiServiceName, newDmiTrustLevel); + } else { + final Collection cmHandleIds = + cmHandleQueryService.getCmHandleIdsByDmiPluginIdentifier(dmiServiceName); + trustLevelManager.handleUpdateOfDmiTrustLevel(dmiServiceName, cmHandleIds, newDmiTrustLevel); + } + }); + } + + private String getDmiHealthStatus(final String dmiServiceName) { + return dmiRestClient.getDmiHealthStatus(dmiServiceName); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelCacheConfig.java new file mode 100644 index 0000000000..b64d3a24b3 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelCacheConfig.java @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START======================================================== + * Copyright (C) 2023-2024 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.impl.inventory.trustlevel; + +import com.hazelcast.config.MapConfig; +import java.util.Map; +import org.onap.cps.cache.HazelcastCacheConfig; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class TrustLevelCacheConfig extends HazelcastCacheConfig { + + public static final String TRUST_LEVEL_PER_DMI_PLUGIN = "trustLevelPerDmiPlugin"; + + public static final String TRUST_LEVEL_PER_CM_HANDLE = "trustLevelPerCmHandle"; + private static final MapConfig trustLevelPerCmHandleCacheConfig = + createMapConfig("trustLevelPerCmHandleCacheConfig"); + + private static final MapConfig trustLevelPerDmiPluginCacheConfig = + createMapConfig("trustLevelPerDmiPluginCacheConfig"); + + /** + * Distributed instance of trust level cache containing the trust level per cm handle. + * + * @return configured map of cm handle name as keys to trust-level for values. + */ + @Bean(TRUST_LEVEL_PER_CM_HANDLE) + public Map trustLevelPerCmHandle() { + return createHazelcastInstance("hazelcastInstanceTrustLevelPerCmHandleMap", + trustLevelPerCmHandleCacheConfig).getMap(TRUST_LEVEL_PER_CM_HANDLE); + } + + /** + * 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. + */ + @Bean(TRUST_LEVEL_PER_DMI_PLUGIN) + public Map trustLevelPerDmiPlugin() { + return createHazelcastInstance("hazelcastInstanceTrustLevelPerDmiPluginMap", + trustLevelPerDmiPluginCacheConfig).getMap(TRUST_LEVEL_PER_DMI_PLUGIN); + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java new file mode 100644 index 0000000000..c7bfb1dbb9 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/trustlevel/TrustLevelManager.java @@ -0,0 +1,136 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 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.impl.inventory.trustlevel; + +import java.util.Collection; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher; +import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; +import org.onap.cps.ncmp.api.inventory.models.TrustLevel; +import org.onap.cps.ncmp.impl.inventory.InventoryPersistence; +import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class TrustLevelManager { + + @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE) + private final Map trustLevelPerCmHandle; + + @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN) + private final Map trustLevelPerDmiPlugin; + + private final InventoryPersistence inventoryPersistence; + private final AvcEventPublisher avcEventPublisher; + private static final String AVC_CHANGED_ATTRIBUTE_NAME = "trustLevel"; + private static final String AVC_NO_OLD_VALUE = null; + + /** + * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE. + * + * @param cmHandlesToBeCreated a list of cmHandles being created + */ + public void handleInitialRegistrationOfTrustLevels(final Map cmHandlesToBeCreated) { + for (final Map.Entry entry : cmHandlesToBeCreated.entrySet()) { + final String cmHandleId = entry.getKey(); + if (trustLevelPerCmHandle.containsKey(cmHandleId)) { + log.warn("Cm handle: {} already registered", cmHandleId); + } else { + TrustLevel initialTrustLevel = entry.getValue(); + if (initialTrustLevel == null) { + initialTrustLevel = TrustLevel.COMPLETE; + } + trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel); + if (TrustLevel.NONE.equals(initialTrustLevel)) { + avcEventPublisher.publishAvcEvent(cmHandleId, + AVC_CHANGED_ATTRIBUTE_NAME, + AVC_NO_OLD_VALUE, + initialTrustLevel.name()); + } + } + } + } + + /** + * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it + * has changed. + * + * @param dmiServiceName dmi service name + * @param affectedCmHandleIds cm handle ids belonging to dmi service name + * @param newDmiTrustLevel new trust level of the dmi plugin + */ + public void handleUpdateOfDmiTrustLevel(final String dmiServiceName, + final Collection affectedCmHandleIds, + final TrustLevel newDmiTrustLevel) { + final TrustLevel oldDmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName); + trustLevelPerDmiPlugin.put(dmiServiceName, newDmiTrustLevel); + for (final String affectedCmHandleId : affectedCmHandleIds) { + final TrustLevel deviceTrustLevel = trustLevelPerCmHandle.get(affectedCmHandleId); + final TrustLevel oldEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(oldDmiTrustLevel); + final TrustLevel newEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(newDmiTrustLevel); + sendAvcNotificationIfRequired(affectedCmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel); + } + } + + /** + * Updates trust level of device in the cache and publish notification for trust level of device if it has + * changed. + * + * @param cmHandleId cm handle id + * @param newDeviceTrustLevel new trust level of the device + */ + public void handleUpdateOfDeviceTrustLevel(final String cmHandleId, + final TrustLevel newDeviceTrustLevel) { + final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId); + final String dmiServiceName = yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA); + + final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName); + final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId); + + final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel); + final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel); + + trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel); + sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel); + } + + private void sendAvcNotificationIfRequired(final String notificationCandidateCmHandleId, + final TrustLevel oldEffectiveTrustLevel, + final TrustLevel newEffectiveTrustLevel) { + if (oldEffectiveTrustLevel.equals(newEffectiveTrustLevel)) { + log.debug("The Cm Handle: {} has already the same trust level: {}", notificationCandidateCmHandleId, + newEffectiveTrustLevel); + } else { + log.info("The trust level for Cm Handle: {} is now: {} ", notificationCandidateCmHandleId, + newEffectiveTrustLevel); + avcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId, + AVC_CHANGED_ATTRIBUTE_NAME, + oldEffectiveTrustLevel.name(), + newEffectiveTrustLevel.name()); + } + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/YangDataConverter.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/YangDataConverter.java index bde13684af..ac0c44e1c3 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/YangDataConverter.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/utils/YangDataConverter.java @@ -43,12 +43,12 @@ public class YangDataConverter { private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']"); /** - * This method convert yang model cm handle to ncmp service cm handle. + * This method converts yang model cm handle to ncmp service cm handle. * @param yangModelCmHandle the yang model of the cm handle * @return ncmp service cm handle */ - public static NcmpServiceCmHandle convertYangModelCmHandleToNcmpServiceCmHandle( - final YangModelCmHandle yangModelCmHandle) { + public static NcmpServiceCmHandle toNcmpServiceCmHandle( + final YangModelCmHandle yangModelCmHandle) { final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle(); final List dmiProperties = yangModelCmHandle.getDmiProperties(); final List publicProperties = yangModelCmHandle.getPublicProperties(); @@ -63,23 +63,24 @@ public class YangDataConverter { } /** - * This method convert yang model cm handle properties to simple map. + * This method converts yang model cm handle properties to simple map. * @param properties the yang model cm handle properties - * @param propertiesMap the String, String map for the results + * @return simple map representing the properties */ - public static void asPropertiesMap(final List properties, - final Map propertiesMap) { + public static Map toPropertiesMap(final List properties) { + final Map propertiesMap = new LinkedHashMap<>(properties.size()); for (final YangModelCmHandle.Property property : properties) { propertiesMap.put(property.getName(), property.getValue()); } + return propertiesMap; } /** - * This method convert cm handle data node to yang model cm handle. + * This method converts cm handle data node to yang model cm handle. * @param cmHandleDataNode the datanode of the cm handle * @return yang model cm handle */ - public static YangModelCmHandle convertCmHandleToYangModel(final DataNode cmHandleDataNode) { + public static YangModelCmHandle toYangModelCmHandle(final DataNode cmHandleDataNode) { final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle(); final String cmHandleId = cmHandleDataNode.getLeaves().get("id").toString(); ncmpServiceCmHandle.setCmHandleId(cmHandleId); @@ -96,18 +97,17 @@ public class YangDataConverter { } /** - * This method convert cm handle data nodes to yang model cm handles. + * This method converts cm handle data nodes to yang model cm handles. * @param cmHandleDataNodes the datanode of the cm handle * @return yang model cm handles */ - public static Collection convertDataNodesToYangModelCmHandles( + public static Collection toYangModelCmHandles( final Collection cmHandleDataNodes) { - return cmHandleDataNodes.stream().map(YangDataConverter::convertCmHandleToYangModel) - .collect(Collectors.toList()); + return cmHandleDataNodes.stream().map(YangDataConverter::toYangModelCmHandle).collect(Collectors.toList()); } /** - * This method extract cm handle id from xpath of data node. + * This method extracts cm handle id from xpath of data node. * @param xpath for data node of the cm handle * @return cm handle Id */ @@ -145,15 +145,11 @@ public class YangDataConverter { private static void setDmiProperties(final List dmiProperties, final NcmpServiceCmHandle ncmpServiceCmHandle) { - final Map dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size()); - asPropertiesMap(dmiProperties, dmiPropertiesMap); - ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap); + ncmpServiceCmHandle.setDmiProperties(toPropertiesMap(dmiProperties)); } private static void setPublicProperties(final List publicProperties, final NcmpServiceCmHandle ncmpServiceCmHandle) { - final Map publicPropertiesMap = new LinkedHashMap<>(); - asPropertiesMap(publicProperties, publicPropertiesMap); - ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap); + ncmpServiceCmHandle.setPublicProperties(toPropertiesMap(publicProperties)); } } -- cgit