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;
40 private final InventoryPersistence inventoryPersistence;
41 private final AvcEventPublisher avcEventPublisher;
42 private static final String AVC_CHANGED_ATTRIBUTE_NAME = "trustLevel";
43 private static final String AVC_NO_OLD_VALUE = null;
46 * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
48 * @param cmHandlesToBeCreated a list of cmHandles being created
50 public void handleInitialRegistrationOfTrustLevels(final Map<String, TrustLevel> cmHandlesToBeCreated) {
51 for (final Map.Entry<String, TrustLevel> entry : cmHandlesToBeCreated.entrySet()) {
52 final String cmHandleId = entry.getKey();
53 if (trustLevelPerCmHandle.containsKey(cmHandleId)) {
54 log.warn("Cm handle: {} already registered", cmHandleId);
56 TrustLevel initialTrustLevel = entry.getValue();
57 if (initialTrustLevel == null) {
58 initialTrustLevel = TrustLevel.COMPLETE;
60 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
61 if (TrustLevel.NONE.equals(initialTrustLevel)) {
62 avcEventPublisher.publishAvcEvent(cmHandleId,
63 AVC_CHANGED_ATTRIBUTE_NAME,
65 initialTrustLevel.name());
72 * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it
75 * @param dmiServiceName dmi service name
76 * @param affectedCmHandleIds cm handle ids belonging to dmi service name
77 * @param newDmiTrustLevel new trust level of the dmi plugin
79 public void handleUpdateOfDmiTrustLevel(final String dmiServiceName,
80 final Collection<String> affectedCmHandleIds,
81 final TrustLevel newDmiTrustLevel) {
82 final TrustLevel oldDmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
83 trustLevelPerDmiPlugin.put(dmiServiceName, newDmiTrustLevel);
84 for (final String affectedCmHandleId : affectedCmHandleIds) {
85 final TrustLevel deviceTrustLevel = trustLevelPerCmHandle.get(affectedCmHandleId);
86 final TrustLevel oldEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(oldDmiTrustLevel);
87 final TrustLevel newEffectiveTrustLevel = deviceTrustLevel.getEffectiveTrustLevel(newDmiTrustLevel);
88 sendAvcNotificationIfRequired(affectedCmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
93 * Updates trust level of device in the cache and publish notification for trust level of device if it has
96 * @param cmHandleId cm handle id
97 * @param newDeviceTrustLevel new trust level of the device
99 public void handleUpdateOfDeviceTrustLevel(final String cmHandleId,
100 final TrustLevel newDeviceTrustLevel) {
101 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
102 final String dmiServiceName = yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA);
104 final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
105 final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
107 final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
108 final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
110 trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel);
111 sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
114 private void sendAvcNotificationIfRequired(final String notificationCandidateCmHandleId,
115 final TrustLevel oldEffectiveTrustLevel,
116 final TrustLevel newEffectiveTrustLevel) {
117 if (oldEffectiveTrustLevel.equals(newEffectiveTrustLevel)) {
118 log.debug("The Cm Handle: {} has already the same trust level: {}", notificationCandidateCmHandleId,
119 newEffectiveTrustLevel);
121 log.info("The trust level for Cm Handle: {} is now: {} ", notificationCandidateCmHandleId,
122 newEffectiveTrustLevel);
123 avcEventPublisher.publishAvcEvent(notificationCandidateCmHandleId,
124 AVC_CHANGED_ATTRIBUTE_NAME,
125 oldEffectiveTrustLevel.name(),
126 newEffectiveTrustLevel.name());