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.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;
32
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");
48
49     private static EffectiveModelContext CONTEXT;
50
51     @BeforeClass
52     public static void beforeClass() {
53         CONTEXT = YangParserTestUtils.parseYangResource("/odl-ctlr1923.yang");
54     }
55
56     @Test
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()
62                     .put(A, (byte) 1)
63                     .put(B, (short) 1)
64                     .put(C, 1)
65                     .put(D, 1L)
66                     .put(E, Uint8.ONE)
67                     .put(F, Uint16.ONE)
68                     .put(G, Uint32.ONE)
69                     .put(H, Uint64.ONE)
70                     .build()))
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))
79                 .build())
80             .build(),
81             prune(Builders.mapBuilder()
82                 .withNodeIdentifier(new NodeIdentifier(LST))
83                 .withChild(Builders.mapEntryBuilder()
84                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(LST,  ImmutableMap.<QName, Object>builder()
85                         .put(A, (byte) 1)
86                         .put(B, (short) 1)
87                         .put(C, 1)
88                         .put(D, 1L)
89                         .put(E, (short) 1)
90                         .put(F, 1)
91                         .put(G, 1L)
92                         .put(H, BigInteger.ONE)
93                         .build()))
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))
102                     .build())
103                 .build()));
104     }
105
106     @Test
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))
118             .build(),
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))
129                 .build()));
130     }
131
132     @Test
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)
139                 .build())
140             .build(),
141             prune(Builders.leafSetBuilder()
142                 .withNodeIdentifier(new NodeIdentifier(LFLST8))
143                 .withChild(Builders.leafSetEntryBuilder()
144                     .withNodeIdentifier(new NodeWithValue<>(LFLST8, (short) 1))
145                     .withValue((short) 1)
146                     .build())
147                 .build()));
148     }
149
150     @Test
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)
157                 .build())
158             .build(),
159             prune(Builders.leafSetBuilder()
160                 .withNodeIdentifier(new NodeIdentifier(LFLST16))
161                 .withChild(Builders.leafSetEntryBuilder()
162                     .withNodeIdentifier(new NodeWithValue<>(LFLST16,  1))
163                     .withValue(1)
164                     .build())
165                 .build()));
166     }
167
168     @Test
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)
175                 .build())
176             .build(),
177             prune(Builders.leafSetBuilder()
178                 .withNodeIdentifier(new NodeIdentifier(LFLST32))
179                 .withChild(Builders.leafSetEntryBuilder()
180                     .withNodeIdentifier(new NodeWithValue<>(LFLST32, 1L))
181                     .withValue(1L)
182                     .build())
183                 .build()));
184     }
185
186     @Test
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)
193                 .build())
194             .build(),
195             prune(Builders.leafSetBuilder()
196                 .withNodeIdentifier(new NodeIdentifier(LFLST64))
197                 .withChild(Builders.leafSetEntryBuilder()
198                     .withNodeIdentifier(new NodeWithValue<>(LFLST64, BigInteger.ONE))
199                     .withValue(BigInteger.ONE)
200                     .build())
201                 .build()));
202     }
203
204     private static NormalizedNode<?, ?> prune(final NormalizedNode<?, ?> node) throws IOException {
205         final ReusableNormalizedNodePruner pruner = ReusableNormalizedNodePruner.forSchemaContext(CONTEXT)
206                 .withUintAdaption();
207         pruner.initializeForPath(YangInstanceIdentifier.create(node.getIdentifier()));
208
209         try (NormalizedNodeWriter writer = NormalizedNodeWriter.forStreamWriter(pruner)) {
210             writer.write(node);
211         }
212         pruner.close();
213         return pruner.getResult().get();
214     }
215 }