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