95d3db16cbd933fc3c4c1fa202e8558a351851f7
[cps.git] /
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.impl.inventory.trustlevel
22
23 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
24 import org.onap.cps.ncmp.api.inventory.models.TrustLevel
25 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence
26 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
27 import org.onap.cps.ncmp.utils.events.CmAvcEventPublisher
28 import spock.lang.Ignore
29 import spock.lang.Specification
30
31 class TrustLevelManagerSpec extends Specification {
32
33     def trustLevelPerCmHandle = [:]
34     def trustLevelPerDmiPlugin = [:]
35
36     def mockInventoryPersistence = Mock(InventoryPersistence)
37     def mockAttributeValueChangeEventPublisher = Mock(CmAvcEventPublisher)
38     def objectUnderTest = new TrustLevelManager(trustLevelPerCmHandle, trustLevelPerDmiPlugin, mockInventoryPersistence, mockAttributeValueChangeEventPublisher)
39
40     def 'Initial dmi registration'() {
41         given: 'a dmi plugin'
42             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'dmi-1')
43         when: 'method to register to the cache is called'
44             objectUnderTest.registerDmiPlugin(dmiPluginRegistration)
45         then: 'dmi plugin in the cache and trusted'
46             assert trustLevelPerDmiPlugin.get('dmi-1') == TrustLevel.COMPLETE
47     }
48
49     def 'Initial cm handle registration'() {
50         given: 'two cm handles: one with no trust level and one trusted'
51             def cmHandleModelsToBeCreated = ['ch-1': null, 'ch-2': TrustLevel.COMPLETE]
52         when: 'method to register to the cache is called'
53             objectUnderTest.registerCmHandles(cmHandleModelsToBeCreated)
54         then: 'no notification sent'
55             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
56         and: 'both cm handles are in the cache and are trusted'
57             assert trustLevelPerCmHandle.get('ch-1') == TrustLevel.COMPLETE
58             assert trustLevelPerCmHandle.get('ch-2') == TrustLevel.COMPLETE
59     }
60
61     def 'Initial cm handle registration with a cm handle that is not trusted'() {
62         given: 'a not trusted cm handle'
63             def cmHandleModelsToBeCreated = ['ch-2': TrustLevel.NONE]
64         when: 'method to register to the cache is called'
65             objectUnderTest.registerCmHandles(cmHandleModelsToBeCreated)
66         then: 'notification is sent'
67             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
68     }
69
70     def 'Dmi trust level updated'() {
71         given: 'a trusted dmi plugin'
72             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
73         and: 'a trusted cm handle'
74             trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE)
75         when: 'the update is handled'
76             objectUnderTest.updateDmi('my-dmi', ['ch-1'], TrustLevel.NONE)
77         then: 'notification is sent'
78             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'COMPLETE', 'NONE')
79         and: 'the dmi in the cache is not trusted'
80             assert trustLevelPerDmiPlugin.get('my-dmi') == TrustLevel.NONE
81     }
82
83     def 'Dmi trust level updated with same value'() {
84         given: 'a trusted dmi plugin'
85             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
86         and: 'a trusted cm handle'
87             trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE)
88         when: 'the update is handled'
89             objectUnderTest.updateDmi('my-dmi', ['ch-1'], TrustLevel.COMPLETE)
90         then: 'no notification is sent'
91             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
92         and: 'the dmi in the cache is trusted'
93             assert trustLevelPerDmiPlugin.get('my-dmi') == TrustLevel.COMPLETE
94     }
95
96     def 'CmHandle trust level updated'() {
97         given: 'a non trusted cm handle'
98             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
99         and: 'a trusted dmi plugin'
100             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
101         and: 'inventory persistence service returns yang model cm handle'
102             mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(id: 'ch-1', dmiDataServiceName: 'my-dmi')
103         when: 'update of CmHandle to COMPLETE trust level handled'
104             objectUnderTest.updateCmHandleTrustLevel('ch-1', TrustLevel.COMPLETE)
105         then: 'the cm handle in the cache is trusted'
106             assert trustLevelPerCmHandle.get('ch-1', TrustLevel.COMPLETE)
107         and: 'notification is sent'
108             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'NONE', 'COMPLETE')
109     }
110
111     def 'CmHandle trust level updated with same value'() {
112         given: 'a non trusted cm handle'
113             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
114         and: 'a trusted dmi plugin'
115             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE)
116         and: 'inventory persistence service returns yang model cm handle'
117             mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(id: 'ch-1', dmiDataServiceName: 'my-dmi')
118         when: 'update of CmHandle trust to the same level (NONE)'
119             objectUnderTest.updateCmHandleTrustLevel('ch-1', TrustLevel.NONE)
120         then: 'the cm handle in the cache is not trusted'
121             assert trustLevelPerCmHandle.get('ch-1', TrustLevel.NONE)
122         and: 'no notification is sent'
123             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
124     }
125
126     def 'Dmi trust level restored to complete with non trusted CmHandle'() {
127         given: 'a non trusted dmi'
128             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.NONE)
129         and: 'a non trusted CmHandle'
130             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
131         when: 'restore the dmi trust level to COMPLETE'
132             objectUnderTest.updateDmi('my-dmi', ['ch-1'], TrustLevel.COMPLETE)
133         then: 'the cm handle in the cache is still NONE'
134             assert trustLevelPerCmHandle.get('ch-1') == TrustLevel.NONE
135         and: 'no notification is sent'
136             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
137     }
138
139     @Ignore
140     // TODO: CPS-2375
141     def 'Select effective trust level among CmHandle and dmi plugin'() {
142         given: 'a non trusted dmi'
143             trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.NONE)
144         and: 'a trusted CmHandle'
145             trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE)
146         when: 'effective trust level selected'
147             def effectiveTrustLevel = objectUnderTest.getEffectiveTrustLevel('ch-1')
148         then: 'effective trust level is trusted'
149             assert effectiveTrustLevel == TrustLevel.NONE
150     }
151
152     def 'CmHandle trust level (COMPLETE) removed'() {
153         given: 'a trusted cm handle'
154             trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE)
155         when: 'the remove is handled'
156             objectUnderTest.removeCmHandles(['ch-1'])
157         then: 'cm handle removed from the cache'
158             assert trustLevelPerCmHandle.get('ch-1') == null
159         and: 'notification is sent'
160             1 * mockAttributeValueChangeEventPublisher.publishAvcEvent(_,'trustLevel','COMPLETE','NONE')
161     }
162
163     def 'CmHandle trust level (NONE) removed'() {
164         given: 'a non-trusted cm handle'
165             trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE)
166         when: 'the remove is handled'
167             objectUnderTest.removeCmHandles(['ch-1'])
168         then: 'cm handle removed from the cache'
169             assert trustLevelPerCmHandle.get('ch-1') == null
170         and: 'no notification is sent'
171             0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_)
172     }
173
174 }