2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.node.utils.transformer;
10 import static org.junit.Assert.assertEquals;
12 import com.google.common.collect.ImmutableMap;
13 import java.io.IOException;
14 import java.math.BigInteger;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.Uint16;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.opendaylight.yangtools.yang.common.Uint64;
21 import org.opendaylight.yangtools.yang.common.Uint8;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
27 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 public class UintAdaptingPrunerTest {
32 private static final QName CONT = QName.create("urn:odl-ctlr1923", "cont");
33 private static final QName LST = QName.create(CONT, "lst");
34 private static final QName LFLST8 = QName.create(CONT, "lf-lst8");
35 private static final QName LFLST16 = QName.create(CONT, "lf-lst16");
36 private static final QName LFLST32 = QName.create(CONT, "lf-lst32");
37 private static final QName LFLST64 = QName.create(CONT, "lf-lst64");
38 private static final QName A = QName.create(LST, "a");
39 private static final QName B = QName.create(LST, "b");
40 private static final QName C = QName.create(LST, "c");
41 private static final QName D = QName.create(LST, "d");
42 private static final QName E = QName.create(LST, "e");
43 private static final QName F = QName.create(LST, "f");
44 private static final QName G = QName.create(LST, "g");
45 private static final QName H = QName.create(LST, "h");
47 private static EffectiveModelContext CONTEXT;
50 public static void beforeClass() {
51 CONTEXT = YangParserTestUtils.parseYangResource("/odl-ctlr1923.yang");
55 public void testListTranslation() throws IOException {
56 assertEquals(ImmutableNodes.newSystemMapBuilder()
57 .withNodeIdentifier(new NodeIdentifier(LST))
58 .withChild(ImmutableNodes.newMapEntryBuilder()
59 .withNodeIdentifier(NodeIdentifierWithPredicates.of(LST, ImmutableMap.<QName, Object>builder()
69 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
70 .withChild(ImmutableNodes.leafNode(B, (short) 1))
71 .withChild(ImmutableNodes.leafNode(C, 1))
72 .withChild(ImmutableNodes.leafNode(D, 1L))
73 .withChild(ImmutableNodes.leafNode(E, Uint8.ONE))
74 .withChild(ImmutableNodes.leafNode(F, Uint16.ONE))
75 .withChild(ImmutableNodes.leafNode(G, Uint32.ONE))
76 .withChild(ImmutableNodes.leafNode(H, Uint64.ONE))
79 prune(ImmutableNodes.newSystemMapBuilder()
80 .withNodeIdentifier(new NodeIdentifier(LST))
81 .withChild(ImmutableNodes.newMapEntryBuilder()
82 .withNodeIdentifier(NodeIdentifierWithPredicates.of(LST, ImmutableMap.<QName, Object>builder()
90 .put(H, BigInteger.ONE)
92 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
93 .withChild(ImmutableNodes.leafNode(B, (short) 1))
94 .withChild(ImmutableNodes.leafNode(C, 1))
95 .withChild(ImmutableNodes.leafNode(D, 1L))
96 .withChild(ImmutableNodes.leafNode(E, (short) 1))
97 .withChild(ImmutableNodes.leafNode(F, 1))
98 .withChild(ImmutableNodes.leafNode(G, 1L))
99 .withChild(ImmutableNodes.leafNode(H, BigInteger.ONE))
105 public void testContainerTranslation() throws IOException {
106 assertEquals(ImmutableNodes.newContainerBuilder()
107 .withNodeIdentifier(new NodeIdentifier(CONT))
108 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
109 .withChild(ImmutableNodes.leafNode(B, (short) 1))
110 .withChild(ImmutableNodes.leafNode(C, 1))
111 .withChild(ImmutableNodes.leafNode(D, 1L))
112 .withChild(ImmutableNodes.leafNode(E, Uint8.ONE))
113 .withChild(ImmutableNodes.leafNode(F, Uint16.ONE))
114 .withChild(ImmutableNodes.leafNode(G, Uint32.ONE))
115 .withChild(ImmutableNodes.leafNode(H, Uint64.ONE))
117 prune(ImmutableNodes.newContainerBuilder()
118 .withNodeIdentifier(new NodeIdentifier(CONT))
119 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
120 .withChild(ImmutableNodes.leafNode(B, (short) 1))
121 .withChild(ImmutableNodes.leafNode(C, 1))
122 .withChild(ImmutableNodes.leafNode(D, 1L))
123 .withChild(ImmutableNodes.leafNode(E, (short) 1))
124 .withChild(ImmutableNodes.leafNode(F, 1))
125 .withChild(ImmutableNodes.leafNode(G, 1L))
126 .withChild(ImmutableNodes.leafNode(H, BigInteger.ONE))
131 public void testLeafList8() throws IOException {
132 assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
133 .withNodeIdentifier(new NodeIdentifier(LFLST8))
134 .withChildValue(Uint8.ONE)
136 prune(ImmutableNodes.newSystemLeafSetBuilder()
137 .withNodeIdentifier(new NodeIdentifier(LFLST8))
138 .withChildValue((short) 1)
143 public void testLeafList16() throws IOException {
144 assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
145 .withNodeIdentifier(new NodeIdentifier(LFLST16))
146 .withChildValue(Uint16.ONE)
148 prune(ImmutableNodes.newSystemLeafSetBuilder()
149 .withNodeIdentifier(new NodeIdentifier(LFLST16))
155 public void testLeafList32() throws IOException {
156 assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
157 .withNodeIdentifier(new NodeIdentifier(LFLST32))
158 .withChildValue(Uint32.ONE)
160 prune(ImmutableNodes.newSystemLeafSetBuilder()
161 .withNodeIdentifier(new NodeIdentifier(LFLST32))
167 public void testLeafList64() throws IOException {
168 assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
169 .withNodeIdentifier(new NodeIdentifier(LFLST64))
170 .withChildValue(Uint64.ONE)
172 prune(ImmutableNodes.newSystemLeafSetBuilder()
173 .withNodeIdentifier(new NodeIdentifier(LFLST64))
174 .withChildValue(BigInteger.ONE)
178 private static NormalizedNode prune(final NormalizedNode node) throws IOException {
179 final var pruner = ReusableNormalizedNodePruner.forSchemaContext(CONTEXT).withUintAdaption();
180 pruner.initializeForPath(YangInstanceIdentifier.of(node.name()));
182 try (NormalizedNodeWriter writer = NormalizedNodeWriter.forStreamWriter(pruner)) {
186 return pruner.getResult().orElseThrow();