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;
24 import lombok.RequiredArgsConstructor;
25 import lombok.extern.slf4j.Slf4j;
26 import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher;
27 import org.springframework.stereotype.Service;
31 @RequiredArgsConstructor
32 public class TrustLevelManager {
34 private final Map<String, TrustLevel> trustLevelPerCmHandle;
35 private final AvcEventPublisher avcEventPublisher;
36 private static final String NO_OLD_VALUE = null;
37 private static final String CHANGED_ATTRIBUTE_NAME = "trustLevel";
41 * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
43 * @param cmHandlesToBeCreated a list of cmHandles being created
45 public void handleInitialRegistrationOfTrustLevels(final Map<String, TrustLevel> cmHandlesToBeCreated) {
46 for (final Map.Entry<String, TrustLevel> entry : cmHandlesToBeCreated.entrySet()) {
47 final String cmHandleId = entry.getKey();
48 if (trustLevelPerCmHandle.containsKey(cmHandleId)) {
49 log.warn("Cm handle: {} already registered", cmHandleId);
51 TrustLevel initialTrustLevel = entry.getValue();
52 if (initialTrustLevel == null) {
53 initialTrustLevel = TrustLevel.COMPLETE;
55 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
56 if (TrustLevel.NONE.equals(initialTrustLevel)) {
57 sendAvcNotificationEvent(cmHandleId, NO_OLD_VALUE, initialTrustLevel.name());
65 * Update a cmHandle in the cache and publish notification if the trust level is different.
67 * @param cmHandleId id of the cmHandle being updated
68 * @param newTrustLevel new trust level of the cmHandle being updated
70 public void handleUpdateOfTrustLevels(final String cmHandleId, final String newTrustLevel) {
71 final TrustLevel oldTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
72 if (newTrustLevel.equals(oldTrustLevel.name())) {
73 log.debug("The Cm Handle: {} has already the same trust level: {}", cmHandleId, newTrustLevel);
75 trustLevelPerCmHandle.put(cmHandleId, TrustLevel.valueOf(newTrustLevel));
76 sendAvcNotificationEvent(cmHandleId, oldTrustLevel.name(), newTrustLevel);
77 log.info("The new trust level: {} has been updated for Cm Handle: {}", newTrustLevel, cmHandleId);
81 private void sendAvcNotificationEvent(final String cmHandleId,
82 final String oldTrustLevel,
83 final String newTrustLevel) {
84 avcEventPublisher.publishAvcEvent(cmHandleId,
85 CHANGED_ATTRIBUTE_NAME,