2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.trustlevel;
23 import java.util.Collection;
25 import lombok.RequiredArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig;
28 import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher;
29 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
30 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
32 import org.springframework.beans.factory.annotation.Qualifier;
33 import org.springframework.stereotype.Service;
37 @RequiredArgsConstructor
38 public class TrustLevelManager {
40 @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE)
41 private final Map<String, TrustLevel> trustLevelPerCmHandle;
43 @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN)
44 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
46 private final InventoryPersistence inventoryPersistence;
47 private final AvcEventPublisher avcEventPublisher;
48 private static final String AVC_CHANGED_ATTRIBUTE_NAME = "trustLevel";
49 private static final String AVC_NO_OLD_VALUE = null;
52 * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
54 * @param cmHandlesToBeCreated a list of cmHandles being created
56 public void handleInitialRegistrationOfTrustLevels(final Map<String, TrustLevel> cmHandlesToBeCreated) {
57 for (final Map.Entry<String, TrustLevel> entry : cmHandlesToBeCreated.entrySet()) {
58 final String cmHandleId = entry.getKey();
59 if (trustLevelPerCmHandle.containsKey(cmHandleId)) {
60 log.warn("Cm handle: {} already registered", cmHandleId);
62 TrustLevel initialTrustLevel = entry.getValue();
63 if (initialTrustLevel == null) {
64 initialTrustLevel = TrustLevel.COMPLETE;
66 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
67 if (TrustLevel.NONE.equals(initialTrustLevel)) {
68 avcEventPublisher.publishAvcEvent(cmHandleId,
69 AVC_CHANGED_ATTRIBUTE_NAME,
71 initialTrustLevel.name());
78 * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it
81 * @param dmiServiceName dmi service name
82 * @param affectedCmHandleIds cm handle ids belonging to dmi service name
83 * @param newDmiTrustLevel new trust level of the dmi plugin
85 public void handleUpdateOfDmiTrustLevel(final String dmiServiceName,
86 final Collection<String> affectedCmHandleIds,
87 final TrustLevel newDmiTrustLevel) {
88 final TrustLevel oldDmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
89 trustLevelPerDmiPlugin.put(dmiServiceName, newDmiTrustLevel);
90 for (final String affectedCmHandleId : affectedCmHandleIds) {
91 final TrustLevel deviceTrustLevel = trustLevelPerCmHandle.get(affectedCmHandleId);
92 final TrustLevel oldEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(oldDmiTrustLevel);
93 final TrustLevel newEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(newDmiTrustLevel);
94 sendAvcNotificationIfRequired(affectedCmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
99 * Updates trust level of device in the cache and publish notification for trust level of device if it has
102 * @param cmHandleId cm handle id
103 * @param newDeviceTrustLevel new trust level of the device
105 public void handleUpdateOfDeviceTrustLevel(final String cmHandleId,
106 final TrustLevel newDeviceTrustLevel) {
107 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
108 final String dmiServiceName = yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA);
110 final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
111 final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
113 final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
114 final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
116 trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel);
117 sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
120 private void sendAvcNotificationIfRequired(final String notificationCandidateCmHandleId,
121 final TrustLevel oldEffectiveTrustLevel,
122 final TrustLevel newEffectiveTrustLevel) {
123 if (oldEffectiveTrustLevel.equals(newEffectiveTrustLevel)) {
124 log.debug("The Cm Handle: {} has already the same trust level: {}", notificationCandidateCmHandleId,
125 newEffectiveTrustLevel);
127 log.info("The trust level for Cm Handle: {} is now: {} ", notificationCandidateCmHandleId,
128 newEffectiveTrustLevel);
129 avcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId,
130 AVC_CHANGED_ATTRIBUTE_NAME,
131 oldEffectiveTrustLevel.name(),
132 newEffectiveTrustLevel.name());