2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-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
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.api.impl.inventory.sync
23 import org.onap.cps.api.CpsAnchorService
25 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
26 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE
28 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
29 import org.onap.cps.spi.FetchDescendantsOption
30 import org.onap.cps.spi.model.DataNode
31 import org.onap.cps.api.CpsDataService
32 import org.onap.cps.api.CpsModuleService
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
35 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
36 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries
37 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
38 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
39 import org.onap.cps.spi.CascadeDeleteAllowed
40 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
41 import org.onap.cps.spi.model.ModuleReference
42 import org.onap.cps.utils.JsonObjectMapper
43 import spock.lang.Specification
45 class ModuleSyncServiceSpec extends Specification {
47 def mockCpsModuleService = Mock(CpsModuleService)
48 def mockDmiModelOperations = Mock(DmiModelOperations)
49 def mockCpsAnchorService = Mock(CpsAnchorService)
50 def mockCmHandleQueries = Mock(CmHandleQueries)
51 def mockCpsDataService = Mock(CpsDataService)
52 def mockJsonObjectMapper = Mock(JsonObjectMapper)
53 def mockModuleSetTagCache = [:]
55 def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService,
56 mockCmHandleQueries, mockCpsDataService, mockCpsAnchorService, mockJsonObjectMapper, mockModuleSetTagCache)
58 def expectedDataspaceName = NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
59 def static cmHandleWithModuleSetTag = new DataNodeBuilder().withXpath("//cm-handles[@module-set-tag='tag-1'][@id='otherId']").withAnchor('otherId').build()
61 def 'Sync model for a NEW cm handle without cache with #scenario'() {
62 given: 'a cm handle having some state'
63 def ncmpServiceCmHandle = new NcmpServiceCmHandle()
64 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED).build())
65 ncmpServiceCmHandle.cmHandleId = 'ch-1'
66 def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('some service name', '', '', ncmpServiceCmHandle, moduleSetTag, '')
67 and: 'DMI operations returns some module references'
68 def moduleReferences = [ new ModuleReference('module1','1'), new ModuleReference('module2','2') ]
69 mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
70 and: 'DMI-Plugin returns resource(s) for "new" module(s)'
71 mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> newModuleNameContentToMap
72 and: 'the module service identifies #identifiedNewModuleReferences.size() new modules'
73 mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReferences(identifiedNewModuleReferences)
74 and: 'system contains #existingCmHandlesWithSameTag.size() cm handles with same tag'
75 mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='new-tag-1']",
76 FetchDescendantsOption.OMIT_DESCENDANTS) >> []
77 when: 'module sync is triggered'
78 objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle)
79 then: 'create schema set from module is invoked with correct parameters'
80 1 * mockCpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', newModuleNameContentToMap, moduleReferences)
81 and: 'anchor is created with the correct parameters'
82 1 * mockCpsAnchorService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', 'ch-1')
83 where: 'the following parameters are used'
84 scenario | existingModuleResourcesInCps | identifiedNewModuleReferences | newModuleNameContentToMap | moduleSetTag
85 'one new module' | [['module2': '2'], ['module3': '3']] | [['module1': '1']] | [module1: 'some yang source'] | ''
86 'no new module' | [['module1': '1'], ['module2': '2']] | [] | [:] | 'new-tag-1'
89 def 'Upgrade model for an existing cm handle with Module Set Tag where the modules are #scenario'() {
90 given: 'a cm handle being upgraded to module set tag: tag-1'
91 def ncmpServiceCmHandle = new NcmpServiceCmHandle()
92 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withLockReason(MODULE_UPGRADE, 'Upgrade to ModuleSetTag: tag-1').build())
93 def dmiServiceName = 'some service name'
94 ncmpServiceCmHandle.cmHandleId = 'upgraded-ch'
95 def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '', '', ncmpServiceCmHandle,'tag-1', '')
96 and: 'some module references'
97 def moduleReferences = [ new ModuleReference('module1','1') ]
98 and: 'cache or DMI operations returns some module references for upgraded cm handle'
100 mockModuleSetTagCache.put('tag-1', moduleReferences)
102 mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
104 and: 'none of these module references are new (unknown to the system)'
105 mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> []
106 and: 'CPS-Core returns list of existing module resources for TBD'
107 mockCpsModuleService.getYangResourcesModuleReferences(*_) >> [ new ModuleReference('module1','1') ]
108 and: 'system contains #existingCmHandlesWithSameTag.size() cm handles with same tag'
109 mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='tag-1']", FetchDescendantsOption.OMIT_DESCENDANTS) >> existingCmHandlesWithSameTag
110 and: 'the other cm handle is a state ready'
111 mockCmHandleQueries.cmHandleHasState('otherId', CmHandleState.READY) >> true
112 when: 'module sync is triggered'
113 objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle)
114 then: 'update schema set from module is invoked for the upgraded cm handle'
115 expectedCallsToUpgradeSchemaSet * mockCpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'upgraded-ch', [:], moduleReferences)
116 and: 'create schema set from module is invoked for the upgraded cm handle'
117 expectedCallsToCeateSchemaSet * mockCpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'upgraded-ch', [:], moduleReferences)
118 and: 'No anchor is created for the upgraded cm handle'
119 0 * mockCpsAnchorService.createAnchor(*_)
120 where: 'the following parameters are used'
121 scenario | populateCache | existingCmHandlesWithSameTag || expectedCallsToUpgradeSchemaSet | expectedCallsToCeateSchemaSet
122 'new' | false | [] || 0 | 1
123 'in cache' | true | [] || 1 | 0
124 'in database' | false | [cmHandleWithModuleSetTag] || 1 | 0
127 def 'upgrade model for a existing cm handle'() {
128 given: 'a cm handle that is ready but locked for upgrade'
129 def ncmpServiceCmHandle = new NcmpServiceCmHandle()
130 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder()
131 .withLockReason(MODULE_UPGRADE, 'Upgrade to ModuleSetTag: targetModuleSetTag').build())
132 ncmpServiceCmHandle.setCmHandleId('cmHandleId-1')
133 def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('some service name', '', '', ncmpServiceCmHandle, 'targetModuleSetTag', '')
134 mockCmHandleQueries.cmHandleHasState('cmHandleId-1', CmHandleState.READY) >> true
135 and: 'the module service returns some module references'
136 def moduleReferences = [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')]
137 mockCpsModuleService.getYangResourcesModuleReferences(*_)>> moduleReferences
138 and: 'a cm handle with the same moduleSetTag can be found in the registry'
139 mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='targetModuleSetTag']",
140 FetchDescendantsOption.OMIT_DESCENDANTS) >> [new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'cmHandleId-1\']', leaves: ['id': 'cmHandleId-1', 'cm-handle-state': 'READY'])]
141 when: 'module upgrade is triggered'
142 objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle)
143 then: 'the upgrade is delegated to the module service (with the correct parameters)'
144 1 * mockCpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', Collections.emptyMap(), moduleReferences)
147 def 'Delete Schema Set for CmHandle'() {
148 when: 'delete schema set if exists is called'
149 objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
150 then: 'the module service is invoked to delete the correct schema set'
151 1 * mockCpsModuleService.deleteSchemaSet(expectedDataspaceName, 'some-cmhandle-id', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
154 def 'Delete a non-existing Schema Set for CmHandle' () {
155 given: 'the DB throws an exception because its Schema Set does not exist'
156 mockCpsModuleService.deleteSchemaSet(*_) >> { throw new SchemaSetNotFoundException('some-dataspace-name', 'some-cmhandle-id') }
157 when: 'delete schema set if exists is called'
158 objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
159 then: 'the exception from the DB is ignored; there are no exceptions'
163 def 'Delete Schema Set for CmHandle with other exception' () {
164 given: 'an exception other than SchemaSetNotFoundException is thrown'
165 UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException();
166 1 * mockCpsModuleService.deleteSchemaSet(*_) >> { throw unsupportedOperationException }
167 when: 'delete schema set if exists is called'
168 objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
169 then: 'an exception is thrown'
170 def result = thrown(UnsupportedOperationException)
171 result == unsupportedOperationException
174 def toModuleReferences(moduleReferenceAsMap) {
175 def moduleReferences = [].withDefault { [:] }
176 moduleReferenceAsMap.forEach(property ->
177 property.forEach((moduleName, revision) -> {
178 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
180 return moduleReferences