2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2023 Nordix Foundation.
5 * Modifications Copyright (C) 2022 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.model
24 import org.onap.cps.TestUtils
25 import org.onap.cps.spi.exceptions.DataValidationException
26 import org.onap.cps.utils.DataMapUtils
27 import org.onap.cps.utils.YangUtils
28 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode
30 import org.opendaylight.yangtools.yang.data.api.schema.ForeignDataNode
31 import spock.lang.Specification
33 class DataNodeBuilderSpec extends Specification {
35 def objectUnderTest = new DataNodeBuilder()
37 def expectedLeavesByXpathMap = [
39 '/test-tree/branch[@name=\'Left\']' : [name: 'Left'],
40 '/test-tree/branch[@name=\'Left\']/nest' : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
41 '/test-tree/branch[@name=\'Right\']' : [name: 'Right'],
42 '/test-tree/branch[@name=\'Right\']/nest' : [name: 'Big', birds: ['Owl', 'Raven', 'Crow']],
43 '/test-tree/fruit[@color=\'Green\' and @name=\'Apple\']': [color: 'Green', name: 'Apple']
46 String[] networkTopologyModelRfc8345 = [
47 'ietf/ietf-yang-types@2013-07-15.yang',
48 'ietf/ietf-network-topology-state@2018-02-26.yang',
49 'ietf/ietf-network-topology@2018-02-26.yang',
50 'ietf/ietf-network-state@2018-02-26.yang',
51 'ietf/ietf-network@2018-02-26.yang',
52 'ietf/ietf-inet-types@2013-07-15.yang'
55 def 'Converting ContainerNode (tree) to a DataNode (tree).'() {
56 given: 'the schema context for expected model'
57 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
58 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
59 and: 'the json data parsed into container node object'
60 def jsonData = TestUtils.getResourceFileContent('test-tree.json')
61 def containerNode = YangUtils.parseJsonData(jsonData, schemaContext)
62 when: 'the container node is converted to a data node'
63 def result = objectUnderTest.withContainerNode(containerNode).build()
64 def mappedResult = TestUtils.getFlattenMapByXpath(result)
65 then: '6 DataNode objects with unique xpath were created in total'
66 mappedResult.size() == 6
67 and: 'all expected xpaths were built'
68 mappedResult.keySet().containsAll(expectedLeavesByXpathMap.keySet())
69 and: 'each data node contains the expected attributes'
71 xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
75 def 'Converting ContainerNode (tree) to a DataNode (tree) for known parent node.'() {
76 given: 'a schema context for expected model'
77 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
78 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
79 and: 'the json data parsed into container node object'
80 def jsonData = '{ "branch": [{ "name": "Branch", "nest": { "name": "Nest", "birds": ["bird"] } }] }'
81 def containerNode = YangUtils.parseJsonData(jsonData, schemaContext, "/test-tree")
82 when: 'the container node is converted to a data node with parent node xpath defined'
83 def result = objectUnderTest.withContainerNode(containerNode).withParentNodeXpath('/test-tree').build()
84 def mappedResult = TestUtils.getFlattenMapByXpath(result)
85 then: '2 DataNode objects with unique xpath were created in total'
86 mappedResult.size() == 2
87 and: 'all expected xpaths were built'
88 mappedResult.keySet().containsAll(['/test-tree/branch[@name=\'Branch\']', '/test-tree/branch[@name=\'Branch\']/nest'])
91 def 'Converting ContainerNode (tree) to a DataNode (tree) -- augmentation case.'() {
92 given: 'a schema context for expected model'
93 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
94 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
95 and: 'the json data parsed into container node object'
96 def jsonData = TestUtils.getResourceFileContent('ietf/data/ietf-network-topology-sample-rfc8345.json')
97 def containerNode = YangUtils.parseJsonData(jsonData, schemaContext)
98 when: 'the container node is converted to a data node '
99 def result = objectUnderTest.withContainerNode(containerNode).build()
100 def mappedResult = TestUtils.getFlattenMapByXpath(result)
101 then: 'all expected data nodes are populated'
102 mappedResult.size() == 32
103 and: 'xpaths for augmentation nodes (link and termination-point nodes) were built correctly'
104 mappedResult.keySet().containsAll([
105 "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']",
106 "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-3-1,D3,3-1-1']",
107 "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-1-1,D1,1-2-1']",
108 "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-3-1,D3,3-2-1']",
109 "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-1-1,D1,1-3-1']",
110 "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-2-1,D2,2-3-1']",
111 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-0-1']",
112 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-2-1']",
113 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-3-1']",
114 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-0-1']",
115 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-1-1']",
116 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-3-1']",
117 "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-1-1']",
118 "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-2-1']"
122 def 'Converting ContainerNode (tree) to a DataNode (tree) for known parent node -- augmentation case.'() {
123 given: 'a schema context for expected model'
124 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
125 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
126 and: 'parent node xpath referencing augmentation node within a model'
127 def parentNodeXpath = "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']"
128 and: 'the json data fragment parsed into container node object for given parent node xpath'
129 def jsonData = '{"source": {"source-node": "D1", "source-tp": "1-2-1"}}'
130 def containerNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
131 when: 'the container node is converted to a data node with given parent node xpath'
132 def result = objectUnderTest.withContainerNode(containerNode).withParentNodeXpath(parentNodeXpath).build()
133 then: 'the resulting data node represents a child of augmentation node'
134 assert result.xpath == "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']/source"
135 assert result.leaves['source-node'] == 'D1'
136 assert result.leaves['source-tp'] == '1-2-1'
139 def 'Converting ContainerNode (tree) to a DataNode (tree) -- with ChoiceNode.'() {
140 given: 'a schema context for expected model'
141 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('yang-with-choice-node.yang')
142 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
143 and: 'the json data fragment parsed into container node object'
144 def jsonData = TestUtils.getResourceFileContent('data-with-choice-node.json')
145 def containerNode = YangUtils.parseJsonData(jsonData, schemaContext)
146 when: 'the container node is converted to a data node'
147 def result = objectUnderTest.withContainerNode(containerNode).build()
148 def mappedResult = TestUtils.getFlattenMapByXpath(result)
149 then: 'the resulting data node contains only one xpath with 3 leaves'
150 mappedResult.keySet().containsAll([ '/container-with-choice-leaves' ])
151 assert result.leaves['leaf-1'] == 'test'
152 assert result.leaves['choice-case1-leaf-a'] == 'test'
153 assert result.leaves['choice-case1-leaf-b'] == 'test'
156 def 'Converting ContainerNode into DataNode collection: #scenario.'() {
157 given: 'a schema context for expected model'
158 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
159 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
160 and: 'parent node xpath referencing parent of list element'
161 def parentNodeXpath = '/test-tree'
162 and: 'the json data fragment (list element) parsed into container node object'
163 def containerNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
164 when: 'the container node is converted to a data node collection'
165 def result = objectUnderTest.withContainerNode(containerNode).withParentNodeXpath(parentNodeXpath).buildCollection()
166 def resultXpaths = result.collect { it.getXpath() }
167 then: 'the resulting collection contains data nodes for expected list elements'
168 assert resultXpaths.size() == expectedSize
169 assert resultXpaths.containsAll(expectedXpaths)
170 where: 'following parameters are used'
171 scenario | jsonData | expectedSize | expectedXpaths
172 'single entry' | '{"branch": [{"name": "One"}]}' | 1 | ['/test-tree/branch[@name=\'One\']']
173 'multiple entries' | '{"branch": [{"name": "One"}, {"name": "Two"}]}' | 2 | ['/test-tree/branch[@name=\'One\']', '/test-tree/branch[@name=\'Two\']']
176 def 'Converting ContainerNode to a Collection with #scenario.'() {
177 expect: 'converting null to a collection returns an empty collection'
178 assert objectUnderTest.withContainerNode(containerNode).buildCollection().isEmpty()
179 where: 'the following container node is used'
180 scenario | containerNode
182 'object without body' | Mock(ContainerNode)
185 def 'Converting ContainerNode to a DataNode with unsupported Normalized Node.'() {
186 given: 'a container node of an unsupported type'
187 def mockContainerNode = Mock(ContainerNode)
188 mockContainerNode.body() >> [ Mock(ForeignDataNode) ]
189 when: 'attempt to convert it'
190 objectUnderTest.withContainerNode(mockContainerNode).build()
191 then: 'a data validation exception is thrown'
192 thrown(DataValidationException)
195 def 'Build datanode from attributes.'() {
196 when: 'data node is built'
197 def result = new DataNodeBuilder()
198 .withDataspace('my dataspace')
199 .withAnchor('my anchor')
200 .withModuleNamePrefix('my prefix')
201 .withXpath('some xpath')
202 .withLeaves([leaf1: 'value1'])
203 .withChildDataNodes([Mock(DataNode)])
205 then: 'the datanode has all the defined attributes'
206 assert result.dataspace == 'my dataspace'
207 assert result.anchorName == 'my anchor'
208 assert result.moduleNamePrefix == 'my prefix'
209 assert result.moduleNamePrefix == 'my prefix'
210 assert result.xpath == 'some xpath'
211 assert result.leaves == [leaf1: 'value1']
212 assert result.childDataNodes.size() == 1
215 def 'Use of adding the module name prefix attribute of data node.'() {
216 when: 'data node is built with a prefix'
217 def testDataNode = new DataNodeBuilder()
219 .withLeaves(sampleLeaves)
221 then: 'the result when node request is a #scenario includes the correct prefix'
222 def result = new DataMapUtils().toDataMapWithIdentifier(testDataNode, 'sampleModuleNamePrefix')
223 result.toString() == expectedResult
224 where: 'the following parameters are used'
225 scenario | xPath | sampleLeaves | expectedResult
226 'list attribute' | '/test-tree/branch[@name=\'Right\']/nest' | [name: 'Big', birds: ['Owl']] | '{sampleModuleNamePrefix:nest={name=Big, birds=[Owl]}}'
227 'container xpath' | '/test-tree/branch[@name=\'Left\']' | [name: 'Left'] | '{sampleModuleNamePrefix:branch={name=Left}}'
230 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
231 expectedLeavesMap.each { key, value ->
233 def actualValue = actualLeavesMap[key]
234 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
235 assert value.size() == actualValue.size()
236 assert value.containsAll(actualValue)
238 assert value == actualValue