2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.inventory.trustlevel
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
31 class TrustLevelManagerSpec extends Specification {
33 def trustLevelPerCmHandle = [:]
34 def trustLevelPerDmiPlugin = [:]
36 def mockInventoryPersistence = Mock(InventoryPersistence)
37 def mockAttributeValueChangeEventPublisher = Mock(CmAvcEventPublisher)
38 def objectUnderTest = new TrustLevelManager(trustLevelPerCmHandle, trustLevelPerDmiPlugin, mockInventoryPersistence, mockAttributeValueChangeEventPublisher)
40 def 'Initial dmi registration'() {
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
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
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(*_)
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
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
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')
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(*_)
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(*_)
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
152 def 'CmHandle trust level removed'() {
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