Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / transformer / UintAdaptingPrunerTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.node.utils.transformer;
9
10 import static org.junit.Assert.assertEquals;
11
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;
30
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");
46
47     private static EffectiveModelContext CONTEXT;
48
49     @BeforeClass
50     public static void beforeClass() {
51         CONTEXT = YangParserTestUtils.parseYangResource("/odl-ctlr1923.yang");
52     }
53
54     @Test
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()
60                     .put(A, (byte) 1)
61                     .put(B, (short) 1)
62                     .put(C, 1)
63                     .put(D, 1L)
64                     .put(E, Uint8.ONE)
65                     .put(F, Uint16.ONE)
66                     .put(G, Uint32.ONE)
67                     .put(H, Uint64.ONE)
68                     .build()))
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))
77                 .build())
78             .build(),
79             prune(ImmutableNodes.newSystemMapBuilder()
80                 .withNodeIdentifier(new NodeIdentifier(LST))
81                 .withChild(ImmutableNodes.newMapEntryBuilder()
82                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(LST,  ImmutableMap.<QName, Object>builder()
83                         .put(A, (byte) 1)
84                         .put(B, (short) 1)
85                         .put(C, 1)
86                         .put(D, 1L)
87                         .put(E, (short) 1)
88                         .put(F, 1)
89                         .put(G, 1L)
90                         .put(H, BigInteger.ONE)
91                         .build()))
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))
100                     .build())
101                 .build()));
102     }
103
104     @Test
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))
116             .build(),
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))
127                 .build()));
128     }
129
130     @Test
131     public void testLeafList8() throws IOException {
132         assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
133             .withNodeIdentifier(new NodeIdentifier(LFLST8))
134             .withChildValue(Uint8.ONE)
135             .build(),
136             prune(ImmutableNodes.newSystemLeafSetBuilder()
137                 .withNodeIdentifier(new NodeIdentifier(LFLST8))
138                 .withChildValue((short) 1)
139                 .build()));
140     }
141
142     @Test
143     public void testLeafList16() throws IOException {
144         assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
145             .withNodeIdentifier(new NodeIdentifier(LFLST16))
146             .withChildValue(Uint16.ONE)
147             .build(),
148             prune(ImmutableNodes.newSystemLeafSetBuilder()
149                 .withNodeIdentifier(new NodeIdentifier(LFLST16))
150                 .withChildValue(1)
151                 .build()));
152     }
153
154     @Test
155     public void testLeafList32() throws IOException {
156         assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
157             .withNodeIdentifier(new NodeIdentifier(LFLST32))
158             .withChildValue(Uint32.ONE)
159             .build(),
160             prune(ImmutableNodes.newSystemLeafSetBuilder()
161                 .withNodeIdentifier(new NodeIdentifier(LFLST32))
162                 .withChildValue(1L)
163                 .build()));
164     }
165
166     @Test
167     public void testLeafList64() throws IOException {
168         assertEquals(ImmutableNodes.newSystemLeafSetBuilder()
169             .withNodeIdentifier(new NodeIdentifier(LFLST64))
170             .withChildValue(Uint64.ONE)
171             .build(),
172             prune(ImmutableNodes.newSystemLeafSetBuilder()
173                 .withNodeIdentifier(new NodeIdentifier(LFLST64))
174                 .withChildValue(BigInteger.ONE)
175                 .build()));
176     }
177
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()));
181
182         try (NormalizedNodeWriter writer = NormalizedNodeWriter.forStreamWriter(pruner)) {
183             writer.write(node);
184         }
185         pruner.close();
186         return pruner.getResult().orElseThrow();
187     }
188 }