2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.model
22 import org.onap.cps.TestUtils
23 import org.onap.cps.spi.model.DataNodeBuilder
24 import org.onap.cps.utils.YangUtils
25 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
26 import spock.lang.Specification
28 class DataNodeBuilderSpec extends Specification {
30 Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
32 '/test-tree/branch[@name=\'Left\']' : [name: 'Left'],
33 '/test-tree/branch[@name=\'Left\']/nest' : [name: 'Small', birds: ['Sparrow', 'Robin', 'Finch']],
34 '/test-tree/branch[@name=\'Right\']' : [name: 'Right'],
35 '/test-tree/branch[@name=\'Right\']/nest': [name: 'Big', birds: ['Owl', 'Raven', 'Crow']]
38 String[] networkTopologyModelRfc8345 = [
39 'ietf/ietf-yang-types@2013-07-15.yang',
40 'ietf/ietf-network-topology-state@2018-02-26.yang',
41 'ietf/ietf-network-topology@2018-02-26.yang',
42 'ietf/ietf-network-state@2018-02-26.yang',
43 'ietf/ietf-network@2018-02-26.yang',
44 'ietf/ietf-inet-types@2013-07-15.yang'
47 def 'Converting NormalizedNode (tree) to a DataNode (tree).'() {
48 given: 'the schema context for expected model'
49 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
50 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
51 and: 'the json data parsed into normalized node object'
52 def jsonData = TestUtils.getResourceFileContent('test-tree.json')
53 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
54 when: 'the normalized node is converted to a data node'
55 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
56 def mappedResult = TestUtils.getFlattenMapByXpath(result)
57 then: '5 DataNode objects with unique xpath were created in total'
58 mappedResult.size() == 5
59 and: 'all expected xpaths were built'
60 mappedResult.keySet().containsAll(expectedLeavesByXpathMap.keySet())
61 and: 'each data node contains the expected attributes'
63 xpath, dataNode -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
67 def 'Converting NormalizedNode (tree) to a DataNode (tree) for known parent node.'() {
68 given: 'a schema context for expected model'
69 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
70 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
71 and: 'the json data parsed into normalized node object'
72 def jsonData = '{ "branch": [{ "name": "Branch", "nest": { "name": "Nest", "birds": ["bird"] } }] }'
73 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, "/test-tree")
74 when: 'the normalized node is converted to a data node with parent node xpath defined'
75 def result = new DataNodeBuilder()
76 .withNormalizedNodeTree(normalizedNode)
77 .withParentNodeXpath("/test-tree")
79 def mappedResult = TestUtils.getFlattenMapByXpath(result)
80 then: '2 DataNode objects with unique xpath were created in total'
81 mappedResult.size() == 2
82 and: 'all expected xpaths were built'
84 .containsAll(['/test-tree/branch[@name=\'Branch\']', '/test-tree/branch[@name=\'Branch\']/nest'])
87 def 'Converting NormalizedNode (tree) to a DataNode (tree) -- augmentation case.'() {
88 given: 'a schema context for expected model'
89 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
90 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
91 and: 'the json data parsed into normalized node object'
92 def jsonData = TestUtils.getResourceFileContent('ietf/data/ietf-network-topology-sample-rfc8345.json')
93 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
94 when: 'the normalized node is converted to a data node '
95 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
96 def mappedResult = TestUtils.getFlattenMapByXpath(result)
97 then: 'all expected data nodes are populated'
98 mappedResult.size() == 32
99 println(mappedResult.keySet().sort())
100 and: 'xpaths for augmentation nodes (link and termination-point nodes) were built correctly'
101 mappedResult.keySet().containsAll([
102 "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']",
103 "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-3-1,D3,3-1-1']",
104 "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-1-1,D1,1-2-1']",
105 "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-3-1,D3,3-2-1']",
106 "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-1-1,D1,1-3-1']",
107 "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-2-1,D2,2-3-1']",
108 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-0-1']",
109 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-2-1']",
110 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-3-1']",
111 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-0-1']",
112 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-1-1']",
113 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-3-1']",
114 "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-1-1']",
115 "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-2-1']"
119 def 'Converting NormalizedNode (tree) to a DataNode (tree) for known parent node -- augmentation case.'() {
120 given: 'a schema context for expected model'
121 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
122 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
123 and: 'parent node xpath referencing augmentation node within a model'
124 def parentNodeXpath = "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']"
125 and: 'the json data fragment parsed into normalized node object for given parent node xpath'
126 def jsonData = '{"source": {"source-node": "D1", "source-tp": "1-2-1"}}'
127 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
128 when: 'the normalized node is converted to a data node with given parent node xpath'
129 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode)
130 .withParentNodeXpath(parentNodeXpath).build()
131 then: 'the resulting data node represents a child of augmentation node'
132 assert result.xpath == "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']/source"
133 assert result.leaves['source-node'] == 'D1'
134 assert result.leaves['source-tp'] == '1-2-1'
137 def 'Converting NormalizedNode into DataNode collection: #scenario.'() {
138 given: 'a schema context for expected model'
139 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
140 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
141 and: 'parent node xpath referencing parent of list-node element'
142 def parentNodeXpath = "/test-tree"
143 and: 'the json data fragment (list-node element) parsed into normalized node object'
144 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
145 when: 'the normalized node is converted to a data node collection'
146 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode)
147 .withParentNodeXpath(parentNodeXpath).buildCollection()
148 def resultXpaths = result.collect { it.getXpath() }
149 then: 'the resulting collection contains data nodes for expected list elements'
150 assert resultXpaths.size() == expectedSize
151 assert resultXpaths.containsAll(expectedXpaths)
152 where: 'following parameters are used'
153 scenario | jsonData | expectedSize | expectedXpaths
154 'single entry' | '{"branch": [{"name": "One"}]}' | 1 | ['/test-tree/branch[@name=\'One\']']
155 'multiple entries' | '{"branch": [{"name": "One"}, {"name": "Two"}]}' | 2 | ['/test-tree/branch[@name=\'One\']', '/test-tree/branch[@name=\'Two\']']
158 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
159 expectedLeavesMap.each { key, value ->
161 def actualValue = actualLeavesMap[key]
162 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
163 assert value.size() == actualValue.size()
164 assert value.containsAll(actualValue)
166 assert value == actualValue