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.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 public class UintAdaptingPrunerTest {
34 private static final QName CONT = QName.create("urn:odl-ctlr1923", "cont");
35 private static final QName LST = QName.create(CONT, "lst");
36 private static final QName LFLST8 = QName.create(CONT, "lf-lst8");
37 private static final QName LFLST16 = QName.create(CONT, "lf-lst16");
38 private static final QName LFLST32 = QName.create(CONT, "lf-lst32");
39 private static final QName LFLST64 = QName.create(CONT, "lf-lst64");
40 private static final QName A = QName.create(LST, "a");
41 private static final QName B = QName.create(LST, "b");
42 private static final QName C = QName.create(LST, "c");
43 private static final QName D = QName.create(LST, "d");
44 private static final QName E = QName.create(LST, "e");
45 private static final QName F = QName.create(LST, "f");
46 private static final QName G = QName.create(LST, "g");
47 private static final QName H = QName.create(LST, "h");
49 private static EffectiveModelContext CONTEXT;
52 public static void beforeClass() {
53 CONTEXT = YangParserTestUtils.parseYangResource("/odl-ctlr1923.yang");
57 public void testListTranslation() throws IOException {
58 assertEquals(Builders.mapBuilder()
59 .withNodeIdentifier(new NodeIdentifier(LST))
60 .withChild(Builders.mapEntryBuilder()
61 .withNodeIdentifier(NodeIdentifierWithPredicates.of(LST, ImmutableMap.<QName, Object>builder()
71 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
72 .withChild(ImmutableNodes.leafNode(B, (short) 1))
73 .withChild(ImmutableNodes.leafNode(C, 1))
74 .withChild(ImmutableNodes.leafNode(D, 1L))
75 .withChild(ImmutableNodes.leafNode(E, Uint8.ONE))
76 .withChild(ImmutableNodes.leafNode(F, Uint16.ONE))
77 .withChild(ImmutableNodes.leafNode(G, Uint32.ONE))
78 .withChild(ImmutableNodes.leafNode(H, Uint64.ONE))
81 prune(Builders.mapBuilder()
82 .withNodeIdentifier(new NodeIdentifier(LST))
83 .withChild(Builders.mapEntryBuilder()
84 .withNodeIdentifier(NodeIdentifierWithPredicates.of(LST, ImmutableMap.<QName, Object>builder()
92 .put(H, BigInteger.ONE)
94 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
95 .withChild(ImmutableNodes.leafNode(B, (short) 1))
96 .withChild(ImmutableNodes.leafNode(C, 1))
97 .withChild(ImmutableNodes.leafNode(D, 1L))
98 .withChild(ImmutableNodes.leafNode(E, (short) 1))
99 .withChild(ImmutableNodes.leafNode(F, 1))
100 .withChild(ImmutableNodes.leafNode(G, 1L))
101 .withChild(ImmutableNodes.leafNode(H, BigInteger.ONE))
107 public void testContainerTranslation() throws IOException {
108 assertEquals(Builders.containerBuilder()
109 .withNodeIdentifier(new NodeIdentifier(CONT))
110 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
111 .withChild(ImmutableNodes.leafNode(B, (short) 1))
112 .withChild(ImmutableNodes.leafNode(C, 1))
113 .withChild(ImmutableNodes.leafNode(D, 1L))
114 .withChild(ImmutableNodes.leafNode(E, Uint8.ONE))
115 .withChild(ImmutableNodes.leafNode(F, Uint16.ONE))
116 .withChild(ImmutableNodes.leafNode(G, Uint32.ONE))
117 .withChild(ImmutableNodes.leafNode(H, Uint64.ONE))
119 prune(Builders.containerBuilder()
120 .withNodeIdentifier(new NodeIdentifier(CONT))
121 .withChild(ImmutableNodes.leafNode(A, (byte) 1))
122 .withChild(ImmutableNodes.leafNode(B, (short) 1))
123 .withChild(ImmutableNodes.leafNode(C, 1))
124 .withChild(ImmutableNodes.leafNode(D, 1L))
125 .withChild(ImmutableNodes.leafNode(E, (short) 1))
126 .withChild(ImmutableNodes.leafNode(F, 1))
127 .withChild(ImmutableNodes.leafNode(G, 1L))
128 .withChild(ImmutableNodes.leafNode(H, BigInteger.ONE))
133 public void testLeafList8() throws IOException {
134 assertEquals(Builders.leafSetBuilder()
135 .withNodeIdentifier(new NodeIdentifier(LFLST8))
136 .withChild(Builders.leafSetEntryBuilder()
137 .withNodeIdentifier(new NodeWithValue<>(LFLST8, Uint8.ONE))
138 .withValue(Uint8.ONE)
141 prune(Builders.leafSetBuilder()
142 .withNodeIdentifier(new NodeIdentifier(LFLST8))
143 .withChild(Builders.leafSetEntryBuilder()
144 .withNodeIdentifier(new NodeWithValue<>(LFLST8, (short) 1))
145 .withValue((short) 1)
151 public void testLeafList16() throws IOException {
152 assertEquals(Builders.leafSetBuilder()
153 .withNodeIdentifier(new NodeIdentifier(LFLST16))
154 .withChild(Builders.leafSetEntryBuilder()
155 .withNodeIdentifier(new NodeWithValue<>(LFLST16, Uint16.ONE))
156 .withValue(Uint16.ONE)
159 prune(Builders.leafSetBuilder()
160 .withNodeIdentifier(new NodeIdentifier(LFLST16))
161 .withChild(Builders.leafSetEntryBuilder()
162 .withNodeIdentifier(new NodeWithValue<>(LFLST16, 1))
169 public void testLeafList32() throws IOException {
170 assertEquals(Builders.leafSetBuilder()
171 .withNodeIdentifier(new NodeIdentifier(LFLST32))
172 .withChild(Builders.leafSetEntryBuilder()
173 .withNodeIdentifier(new NodeWithValue<>(LFLST32, Uint32.ONE))
174 .withValue(Uint32.ONE)
177 prune(Builders.leafSetBuilder()
178 .withNodeIdentifier(new NodeIdentifier(LFLST32))
179 .withChild(Builders.leafSetEntryBuilder()
180 .withNodeIdentifier(new NodeWithValue<>(LFLST32, 1L))
187 public void testLeafList64() throws IOException {
188 assertEquals(Builders.leafSetBuilder()
189 .withNodeIdentifier(new NodeIdentifier(LFLST64))
190 .withChild(Builders.leafSetEntryBuilder()
191 .withNodeIdentifier(new NodeWithValue<>(LFLST64, Uint64.ONE))
192 .withValue(Uint64.ONE)
195 prune(Builders.leafSetBuilder()
196 .withNodeIdentifier(new NodeIdentifier(LFLST64))
197 .withChild(Builders.leafSetEntryBuilder()
198 .withNodeIdentifier(new NodeWithValue<>(LFLST64, BigInteger.ONE))
199 .withValue(BigInteger.ONE)
204 private static NormalizedNode<?, ?> prune(final NormalizedNode<?, ?> node) throws IOException {
205 final ReusableNormalizedNodePruner pruner = ReusableNormalizedNodePruner.forSchemaContext(CONTEXT)
207 pruner.initializeForPath(YangInstanceIdentifier.create(node.getIdentifier()));
209 try (NormalizedNodeWriter writer = NormalizedNodeWriter.forStreamWriter(pruner)) {
213 return pruner.getResult().get();