2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.events.avc.ncmptoclient.AvcEventPublisher;
28 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
29 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
30 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
31 import org.springframework.stereotype.Service;
35 @RequiredArgsConstructor
36 public class TrustLevelManager {
38 private final Map<String, TrustLevel> trustLevelPerCmHandle;
39 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
41 private final InventoryPersistence inventoryPersistence;
42 private final AvcEventPublisher avcEventPublisher;
43 private static final String AVC_CHANGED_ATTRIBUTE_NAME = "trustLevel";
44 private static final String AVC_NO_OLD_VALUE = null;
47 * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
49 * @param cmHandlesToBeCreated a list of cmHandles being created
51 public void handleInitialRegistrationOfTrustLevels(final Map<String, TrustLevel> cmHandlesToBeCreated) {
52 for (final Map.Entry<String, TrustLevel> entry : cmHandlesToBeCreated.entrySet()) {
53 final String cmHandleId = entry.getKey();
54 if (trustLevelPerCmHandle.containsKey(cmHandleId)) {
55 log.warn("Cm handle: {} already registered", cmHandleId);
57 TrustLevel initialTrustLevel = entry.getValue();
58 if (initialTrustLevel == null) {
59 initialTrustLevel = TrustLevel.COMPLETE;
61 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
62 if (TrustLevel.NONE.equals(initialTrustLevel)) {
63 avcEventPublisher.publishAvcEvent(cmHandleId,
64 AVC_CHANGED_ATTRIBUTE_NAME,
66 initialTrustLevel.name());
73 * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it
76 * @param dmiServiceName dmi service name
77 * @param affectedCmHandleIds cm handle ids belonging to dmi service name
78 * @param newDmiTrustLevel new trust level of the dmi plugin
80 public void handleUpdateOfDmiTrustLevel(final String dmiServiceName,
81 final Collection<String> affectedCmHandleIds,
82 final TrustLevel newDmiTrustLevel) {
83 final TrustLevel oldDmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
84 trustLevelPerDmiPlugin.put(dmiServiceName, newDmiTrustLevel);
85 for (final String affectedCmHandleId : affectedCmHandleIds) {
86 final TrustLevel deviceTrustLevel = trustLevelPerCmHandle.get(affectedCmHandleId);
87 final TrustLevel oldEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(oldDmiTrustLevel);
88 final TrustLevel newEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(newDmiTrustLevel);
89 sendAvcNotificationIfRequired(affectedCmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
94 * Updates trust level of device in the cache and publish notification for trust level of device if it has
97 * @param cmHandleId cm handle id
98 * @param newDeviceTrustLevel new trust level of the device
100 public void handleUpdateOfDeviceTrustLevel(final String cmHandleId,
101 final TrustLevel newDeviceTrustLevel) {
102 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
103 final String dmiServiceName = yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA);
105 final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
106 final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
108 final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
109 final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
111 trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel);
112 sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
115 private void sendAvcNotificationIfRequired(final String notificationCandidateCmHandleId,
116 final TrustLevel oldEffectiveTrustLevel,
117 final TrustLevel newEffectiveTrustLevel) {
118 if (oldEffectiveTrustLevel.equals(newEffectiveTrustLevel)) {
119 log.debug("The Cm Handle: {} has already the same trust level: {}", notificationCandidateCmHandleId,
120 newEffectiveTrustLevel);
122 log.info("The trust level for Cm Handle: {} is now: {} ", notificationCandidateCmHandleId,
123 newEffectiveTrustLevel);
124 avcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId,
125 AVC_CHANGED_ATTRIBUTE_NAME,
126 oldEffectiveTrustLevel.name(),
127 newEffectiveTrustLevel.name());