22f18cd24377fd21fde1fbc9d78e03695b8de9bf
[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.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;
32
33 @Slf4j
34 @Service
35 @RequiredArgsConstructor
36 public class TrustLevelManager {
37
38     private final Map<String, TrustLevel> trustLevelPerCmHandle;
39     private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
40
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;
45
46     /**
47      * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
48      *
49      * @param cmHandlesToBeCreated a list of cmHandles being created
50      */
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);
56             } else {
57                 TrustLevel initialTrustLevel = entry.getValue();
58                 if (initialTrustLevel == null) {
59                     initialTrustLevel = TrustLevel.COMPLETE;
60                 }
61                 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
62                 if (TrustLevel.NONE.equals(initialTrustLevel)) {
63                     avcEventPublisher.publishAvcEvent(cmHandleId,
64                         AVC_CHANGED_ATTRIBUTE_NAME,
65                         AVC_NO_OLD_VALUE,
66                         initialTrustLevel.name());
67                 }
68             }
69         }
70     }
71
72     /**
73      * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it
74      * has changed.
75      *
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
79      */
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);
90         }
91     }
92
93     /**
94      * Updates trust level of device in the cache and publish notification for trust level of device if it has
95      * changed.
96      *
97      * @param cmHandleId            cm handle id
98      * @param newDeviceTrustLevel   new trust level of the device
99      */
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);
104
105         final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
106         final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
107
108         final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
109         final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
110
111         trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel);
112         sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
113     }
114
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);
121         } else {
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());
128         }
129     }
130
131 }