diff options
author | halil.cakal <halil.cakal@est.tech> | 2023-11-29 10:03:29 +0000 |
---|---|---|
committer | halil.cakal <halil.cakal@est.tech> | 2023-11-29 15:01:13 +0000 |
commit | a36b3f28624a14da75eb5ea1cd0bd677ebce325c (patch) | |
tree | 2d6cb4d54105b48ab8e7f48dad7476def2a1e59b /cps-ncmp-service/src/test | |
parent | 0afd8642a9195a34a7b3c119496982fefbbda3e9 (diff) |
Fix the algorithm for device trust level
- Add new method to handle device trust level changes
in trust level manager
- Change algorithm to find effective trust level for
each use case
- Separate responsibilities of each function
- Change device heartbeat consumer accordingly
Issue-ID: CPS-1910
Change-Id: I228f82955b224dc4ebfd1b305082e06aac6008d2
Signed-off-by: halil.cakal <halil.cakal@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/test')
3 files changed, 79 insertions, 34 deletions
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumerSpec.groovy index 8886fc189f..42a1c5d5ae 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/DeviceHeartbeatConsumerSpec.groovy @@ -26,7 +26,6 @@ import io.cloudevents.core.builder.CloudEventBuilder import org.apache.kafka.clients.consumer.ConsumerRecord import org.onap.cps.ncmp.events.trustlevel.DeviceTrustLevel import org.onap.cps.utils.JsonObjectMapper -import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import spock.lang.Specification @@ -37,28 +36,24 @@ class DeviceHeartbeatConsumerSpec extends Specification { def objectUnderTest = new DeviceHeartbeatConsumer(mockTrustLevelManager) def objectMapper = new ObjectMapper() + def jsonObjectMapper = new JsonObjectMapper(objectMapper) - @Autowired - JsonObjectMapper jsonObjectMapper - - def static trustLevelString = '{"data":{"trustLevel": "COMPLETE"}}' - - def 'Consume a trustlevel event'() { + def 'Consume a trust level event'() { given: 'an event from dmi with trust level complete' - def payload = jsonObjectMapper.convertJsonString(trustLevelString, DeviceTrustLevel.class) + def payload = jsonObjectMapper.convertJsonString('{"data":{"trustLevel": "COMPLETE"}}', DeviceTrustLevel.class) def eventFromDmi = createTrustLevelEvent(payload) and: 'transformed to a consumer record with a cloud event id (ce_id)' def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'sample-message-key', eventFromDmi) - consumerRecord.headers().add('ce_id', objectMapper.writeValueAsBytes('cmhandle1')) + consumerRecord.headers().add('ce_id', objectMapper.writeValueAsBytes('ch-1')) when: 'the event is consumed' objectUnderTest.heartbeatListener(consumerRecord) then: 'cm handles are stored with correct trust level' - 1 * mockTrustLevelManager.handleUpdateOfTrustLevels('"cmhandle1"', 'COMPLETE') + 1 * mockTrustLevelManager.handleUpdateOfDeviceTrustLevel('"ch-1"', TrustLevel.COMPLETE) } def createTrustLevelEvent(eventPayload) { return CloudEventBuilder.v1().withData(objectMapper.writeValueAsBytes(eventPayload)) - .withId("cmhandle1") + .withId('ch-1') .withSource(URI.create('DMI')) .withDataSchema(URI.create('test')) .withType('org.onap.cps.ncmp.events.trustlevel.DeviceTrustLevel') diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManagerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManagerSpec.groovy index b3559e41ee..886648a7e9 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManagerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/TrustLevelManagerSpec.groovy @@ -21,13 +21,18 @@ package org.onap.cps.ncmp.api.impl.trustlevel import org.onap.cps.ncmp.api.impl.events.avc.ncmptoclient.AvcEventPublisher +import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import spock.lang.Specification class TrustLevelManagerSpec extends Specification { def trustLevelPerCmHandle = [:] + def trustLevelPerDmiPlugin = [:] + + def mockInventoryPersistence = Mock(InventoryPersistence) def mockAttributeValueChangeEventPublisher = Mock(AvcEventPublisher) - def objectUnderTest = new TrustLevelManager(trustLevelPerCmHandle, mockAttributeValueChangeEventPublisher) + def objectUnderTest = new TrustLevelManager(trustLevelPerCmHandle, trustLevelPerDmiPlugin, mockInventoryPersistence, mockAttributeValueChangeEventPublisher) def 'Initial cm handle registration'() { given: 'two cm handles: one with no trust level and one trusted' @@ -50,24 +55,73 @@ class TrustLevelManagerSpec extends Specification { 1 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_) } - def 'Trust level updated'() { - given: 'a not trusted cm handle' - trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE) + def 'Dmi trust level updated'() { + given: 'a trusted dmi plugin' + trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE) + and: 'a trusted cm handle' + trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE) when: 'the update is handled' - objectUnderTest.handleUpdateOfTrustLevels('ch-1', 'COMPLETE') + objectUnderTest.handleUpdateOfDmiTrustLevel('my-dmi', ['ch-1'], TrustLevel.NONE) then: 'notification is sent' - 1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'NONE', 'COMPLETE') - and: 'the cm handle in the cache is trusted' - assert trustLevelPerCmHandle.get('ch-1') == TrustLevel.COMPLETE + 1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'COMPLETE', 'NONE') + and: 'the dmi in the cache is not trusted' + assert trustLevelPerDmiPlugin.get('my-dmi') == TrustLevel.NONE } - def 'Trust level updated with same value'() { - given: 'a trusted cm handle' + def 'Dmi trust level updated with same value'() { + given: 'a trusted dmi plugin' + trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE) + and: 'a trusted cm handle' trustLevelPerCmHandle.put('ch-1', TrustLevel.COMPLETE) when: 'the update is handled' - objectUnderTest.handleUpdateOfTrustLevels('ch-1', 'COMPLETE') + objectUnderTest.handleUpdateOfDmiTrustLevel('my-dmi', ['ch-1'], TrustLevel.COMPLETE) then: 'no notification is sent' 0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_) + and: 'the dmi in the cache is trusted' + assert trustLevelPerDmiPlugin.get('my-dmi') == TrustLevel.COMPLETE + } + + def 'Device trust level updated'() { + given: 'a non trusted cm handle' + trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE) + and: 'a trusted dmi plugin' + trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE) + and: 'inventory persistence service returns yang model cm handle' + mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(id: 'ch-1', dmiDataServiceName: 'my-dmi') + when: 'update of device to COMPLETE trust level handled' + objectUnderTest.handleUpdateOfDeviceTrustLevel('ch-1', TrustLevel.COMPLETE) + then: 'the cm handle in the cache is trusted' + assert trustLevelPerCmHandle.get('ch-1', TrustLevel.COMPLETE) + and: 'notification is sent' + 1 * mockAttributeValueChangeEventPublisher.publishAvcEvent('ch-1', 'trustLevel', 'NONE', 'COMPLETE') + } + + def 'Device trust level updated with same value'() { + given: 'a non trusted cm handle' + trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE) + and: 'a trusted dmi plugin' + trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.COMPLETE) + and: 'inventory persistence service returns yang model cm handle' + mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(id: 'ch-1', dmiDataServiceName: 'my-dmi') + when: 'update of device trust to the same level (NONE)' + objectUnderTest.handleUpdateOfDeviceTrustLevel('ch-1', TrustLevel.NONE) + then: 'the cm handle in the cache is not trusted' + assert trustLevelPerCmHandle.get('ch-1', TrustLevel.NONE) + and: 'no notification is sent' + 0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_) + } + + def 'Dmi trust level restored to complete with non trusted device'() { + given: 'a non trusted dmi' + trustLevelPerDmiPlugin.put('my-dmi', TrustLevel.NONE) + and: 'a non trusted device' + trustLevelPerCmHandle.put('ch-1', TrustLevel.NONE) + when: 'restore the dmi trust level to COMPLETE' + objectUnderTest.handleUpdateOfDmiTrustLevel('my-dmi', ['ch-1'], TrustLevel.COMPLETE) + then: 'the cm handle in the cache is still NONE' + assert trustLevelPerCmHandle.get('ch-1') == TrustLevel.NONE + and: 'no notification is sent' + 0 * mockAttributeValueChangeEventPublisher.publishAvcEvent(*_) } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginWatchDogSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginWatchDogSpec.groovy index ec1d8e8f3d..446126be94 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginWatchDogSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/trustlevel/dmiavailability/DmiPluginWatchDogSpec.groovy @@ -44,21 +44,17 @@ class DmiPluginWatchDogSpec extends Specification { trustLevelPerDmiPlugin.put('dmi-1', dmiOldTrustLevel) and: 'dmi client returns health status #dmiHealhStatus' mockDmiRestClient.getDmiHealthStatus('dmi-1') >> dmiHealhStatus - and: 'network cm proxy data returns a list of all cm handle ids belonging to a dmi' - mockNetworkCmProxyDataService.getAllCmHandleIdsByDmiPluginIdentifier('dmi-1') >> ['ch-1'] when: 'dmi watch dog method runs' objectUnderTest.checkDmiAvailability() - then: 'the result is as expected' - assert trustLevelPerDmiPlugin.get('dmi-1') == newDmiTrustLevel - and: 'the update delegated to manager' - times * mockTrustLevelManager.handleUpdateOfTrustLevels(*_) + then: 'the update delegated to manager' + numberOfCalls * mockTrustLevelManager.handleUpdateOfDmiTrustLevel('dmi-1', _, newDmiTrustLevel) where: 'the following parameters are used' - dmiHealhStatus | dmiOldTrustLevel || newDmiTrustLevel || times - 'UP' | TrustLevel.COMPLETE || TrustLevel.COMPLETE || 0 - 'DOWN' | TrustLevel.COMPLETE || TrustLevel.NONE || 1 - 'DOWN' | TrustLevel.NONE || TrustLevel.NONE || 0 - 'UP' | TrustLevel.NONE || TrustLevel.COMPLETE || 1 - '' | TrustLevel.COMPLETE || TrustLevel.NONE || 1 + dmiHealhStatus | dmiOldTrustLevel | newDmiTrustLevel || numberOfCalls + 'UP' | TrustLevel.COMPLETE | TrustLevel.COMPLETE || 0 + 'DOWN' | TrustLevel.COMPLETE | TrustLevel.NONE || 1 + 'DOWN' | TrustLevel.NONE | TrustLevel.NONE || 0 + 'UP' | TrustLevel.NONE | TrustLevel.COMPLETE || 1 + '' | TrustLevel.COMPLETE | TrustLevel.NONE || 1 } } |