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