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'])
86 def 'Converting NormalizedNode (tree) to a DataNode (tree) -- augmentation case.'() {
87 given: 'a schema context for expected model'
88 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
89 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
90 and: 'the json data parsed into normalized node object'
91 def jsonData = TestUtils.getResourceFileContent('ietf/data/ietf-network-topology-sample-rfc8345.json')
92 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
93 when: 'the normalized node is converted to a data node '
94 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
95 def mappedResult = TestUtils.getFlattenMapByXpath(result)
96 then: 'all expected data nodes are populated'
97 mappedResult.size() == 32
98 println(mappedResult.keySet().sort())
99 and: 'xpaths for augmentation nodes (link and termination-point nodes) were built correctly'
100 mappedResult.keySet().containsAll([
101 "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']",
102 "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-3-1,D3,3-1-1']",
103 "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-1-1,D1,1-2-1']",
104 "/networks/network[@network-id='otn-hc']/link[@link-id='D2,2-3-1,D3,3-2-1']",
105 "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-1-1,D1,1-3-1']",
106 "/networks/network[@network-id='otn-hc']/link[@link-id='D3,3-2-1,D2,2-3-1']",
107 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-0-1']",
108 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-2-1']",
109 "/networks/network[@network-id='otn-hc']/node[@node-id='D1']/termination-point[@tp-id='1-3-1']",
110 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-0-1']",
111 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-1-1']",
112 "/networks/network[@network-id='otn-hc']/node[@node-id='D2']/termination-point[@tp-id='2-3-1']",
113 "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-1-1']",
114 "/networks/network[@network-id='otn-hc']/node[@node-id='D3']/termination-point[@tp-id='3-2-1']"
118 def 'Converting NormalizedNode (tree) to a DataNode (tree) for known parent node -- augmentation case.'() {
119 given: 'a schema context for expected model'
120 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(networkTopologyModelRfc8345)
121 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
122 and: 'parent node xpath referencing augmentation node within a model'
123 def parentNodeXpath = "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']"
124 and: 'the json data fragment parsed into normalized node object for given parent node xpath'
125 def jsonData = '{"source": {"source-node": "D1", "source-tp": "1-2-1"}}'
126 def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath)
127 when: 'the normalized node is converted to a data node with given parent node xpath'
128 def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode)
129 .withParentNodeXpath(parentNodeXpath).build()
130 then: 'the resulting data node represents a child of augmentation node'
131 assert result.xpath == "/networks/network[@network-id='otn-hc']/link[@link-id='D1,1-2-1,D2,2-1-1']/source"
132 assert result.leaves['source-node'] == 'D1'
133 assert result.leaves['source-tp'] == '1-2-1'
136 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
137 expectedLeavesMap.each { key, value ->
139 def actualValue = actualLeavesMap[key]
140 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
141 assert value.size() == actualValue.size()
142 assert value.containsAll(actualValue)
144 assert value == actualValue