Uplift Spring Boot to 3.2.2
[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.Collection;
24 import java.util.Map;
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;
34
35 @Slf4j
36 @Service
37 @RequiredArgsConstructor
38 public class TrustLevelManager {
39
40     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE_BEAN_NAME)
41     private final Map<String, TrustLevel> trustLevelPerCmHandle;
42
43     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN_BEAN_NAME)
44     private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
45
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;
50
51     /**
52      * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
53      *
54      * @param cmHandlesToBeCreated a list of cmHandles being created
55      */
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);
61             } else {
62                 TrustLevel initialTrustLevel = entry.getValue();
63                 if (initialTrustLevel == null) {
64                     initialTrustLevel = TrustLevel.COMPLETE;
65                 }
66                 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
67                 if (TrustLevel.NONE.equals(initialTrustLevel)) {
68                     avcEventPublisher.publishAvcEvent(cmHandleId,
69                         AVC_CHANGED_ATTRIBUTE_NAME,
70                         AVC_NO_OLD_VALUE,
71                         initialTrustLevel.name());
72                 }
73             }
74         }
75     }
76
77     /**
78      * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it
79      * has changed.
80      *
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
84      */
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);
95         }
96     }
97
98     /**
99      * Updates trust level of device in the cache and publish notification for trust level of device if it has
100      * changed.
101      *
102      * @param cmHandleId            cm handle id
103      * @param newDeviceTrustLevel   new trust level of the device
104      */
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);
109
110         final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
111         final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
112
113         final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
114         final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
115
116         trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel);
117         sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
118     }
119
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);
126         } else {
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());
133         }
134     }
135
136 }