82c720402852828c6d369cf4bc6b7e92e1db75d0
[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     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;
44
45     /**
46      * Add cmHandles to the cache and publish notification for initial trust level of cmHandles if it is NONE.
47      *
48      * @param cmHandlesToBeCreated a list of cmHandles being created
49      */
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);
55             } else {
56                 TrustLevel initialTrustLevel = entry.getValue();
57                 if (initialTrustLevel == null) {
58                     initialTrustLevel = TrustLevel.COMPLETE;
59                 }
60                 trustLevelPerCmHandle.put(cmHandleId, initialTrustLevel);
61                 if (TrustLevel.NONE.equals(initialTrustLevel)) {
62                     avcEventPublisher.publishAvcEvent(cmHandleId,
63                         AVC_CHANGED_ATTRIBUTE_NAME,
64                         AVC_NO_OLD_VALUE,
65                         initialTrustLevel.name());
66                 }
67             }
68         }
69     }
70
71     /**
72      * Updates trust level of dmi plugin in the cache and publish notification for trust level of cmHandles if it
73      * has changed.
74      *
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
78      */
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);
89         }
90     }
91
92     /**
93      * Updates trust level of device in the cache and publish notification for trust level of device if it has
94      * changed.
95      *
96      * @param cmHandleId            cm handle id
97      * @param newDeviceTrustLevel   new trust level of the device
98      */
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);
103
104         final TrustLevel dmiTrustLevel = trustLevelPerDmiPlugin.get(dmiServiceName);
105         final TrustLevel oldDeviceTrustLevel = trustLevelPerCmHandle.get(cmHandleId);
106
107         final TrustLevel oldEffectiveTrustLevel = oldDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
108         final TrustLevel newEffectiveTrustLevel = newDeviceTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
109
110         trustLevelPerCmHandle.put(cmHandleId, newDeviceTrustLevel);
111         sendAvcNotificationIfRequired(cmHandleId, oldEffectiveTrustLevel, newEffectiveTrustLevel);
112     }
113
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);
120         } else {
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());
127         }
128     }
129
130 }