Merge "Expose hazelcast cluster info"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / trustlevel / TrustLevelManager.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.trustlevel;
22
23 import java.util.Map;
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;
28
29 @Slf4j
30 @Service
31 @RequiredArgsConstructor
32 public class TrustLevelManager {
33
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";
38
39
40     /**
41      * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
42      *
43      * @param cmHandlesToBeCreated a list of cmHandles being created
44      */
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);
50             } else {
51                 TrustLevel initialTrustLevel = entry.getValue();
52                 if (initialTrustLevel == null) {
53                     initialTrustLevel = TrustLevel.COMPLETE;
54                 }
55                 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
56                 if (TrustLevel.NONE.equals(initialTrustLevel)) {
57                     sendAvcNotificationEvent(cmHandleId, NO_OLD_VALUE, initialTrustLevel.name());
58                 }
59             }
60         }
61     }
62
63
64     /**
65      * Update a cmHandle in the cache and publish notification if the trust level is different.
66      *
67      * @param cmHandleId    id of the cmHandle being updated
68      * @param newTrustLevel new trust level of the cmHandle being updated
69      */
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);
74         } else {
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);
78         }
79     }
80
81     private void sendAvcNotificationEvent(final String cmHandleId,
82                                           final String oldTrustLevel,
83                                           final String newTrustLevel) {
84         avcEventPublisher.publishAvcEvent(cmHandleId,
85                 CHANGED_ATTRIBUTE_NAME,
86                 oldTrustLevel,
87                 newTrustLevel);
88     }
89
90 }