8ee94510d1dacb77d77c9fdd99eb2696ab8719a2
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / NormalizedNodeToNodeCodecTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.datastore.node;
10
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
14 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
15 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
16 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
17 import org.opendaylight.controller.cluster.datastore.util.TestModel;
18 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
19 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 import java.util.List;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 public class NormalizedNodeToNodeCodecTest {
32
33
34
35   private SchemaContext schemaContext;
36
37   @Before
38   public void setUp() {
39     schemaContext = TestModel.createTestContext();
40     assertNotNull("Schema context must not be null.", schemaContext);
41   }
42
43   private YangInstanceIdentifier instanceIdentifierFromString(String s) {
44       return PathUtils.toYangInstanceIdentifier(s);
45   }
46
47   @Test
48   public void testNormalizeNodeAttributesToProtoBuffNode() {
49     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
50     String id =
51         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
52             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
53             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=2}]"
54             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
55
56     NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
57     new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
58         PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne);
59
60     // Validate the value of id can be retrieved from the normalized node
61     NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
62     assertNotNull(output);
63
64
65     NormalizedNodeToNodeCodec codec =
66         new NormalizedNodeToNodeCodec(schemaContext);
67     long start = System.currentTimeMillis();
68     Container container =
69         codec.encode(output);
70     long end = System.currentTimeMillis();
71
72     System.out.println("Timetaken to encode :"+(end-start));
73
74     assertNotNull(container);
75
76     // Decode the normalized node from the ProtocolBuffer form
77     // first get the node representation of normalized node
78     final Node node = container.getNormalizedNode();
79
80     start = System.currentTimeMillis();
81     NormalizedNode<?, ?> normalizedNode =
82         codec.decode(node);
83     end = System.currentTimeMillis();
84
85     System.out.println("Timetaken to decode :"+(end-start));
86
87     assertEquals(normalizedNode.getValue().toString(), output.getValue()
88         .toString());
89   }
90
91   @Test
92   public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
93       throws Exception {
94     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
95
96     final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
97         new NormalizedNodeToNodeCodec(schemaContext);
98
99     Container container =
100         normalizedNodeToNodeCodec.encode(documentOne);
101
102
103     final NormalizedNode<?, ?> decode =
104         normalizedNodeToNodeCodec
105             .decode(
106                 container.getNormalizedNode());
107     assertNotNull(decode);
108
109     // let us ensure that the return decode normalized node encode returns same container
110     Container containerResult =
111         normalizedNodeToNodeCodec.encode(decode);
112
113     // check first level children are proper
114     List<Node> childrenResult =
115         containerResult.getNormalizedNode().getChildList();
116     List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
117
118     System.out.println("-------------------------------------------------");
119
120     System.out.println(childrenOriginal.toString());
121
122     System.out.println("-------------------------------------------------");
123
124     System.out.println(childrenResult.toString());
125
126     boolean bFound;
127     for (Node resultChild : childrenResult) {
128       bFound = false;
129       for (Node originalChild : childrenOriginal) {
130
131         YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
132               containerResult.getNormalizedNode(),
133               resultChild.getPathArgument());
134
135           YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
136               container.getNormalizedNode(),
137               originalChild.getPathArgument());
138
139         if (original.equals(result)
140             && resultChild.getIntType() == resultChild.getIntType()) {
141           bFound = true;
142           break;
143         }
144       }
145       assertTrue(bFound);
146     }
147
148   }
149
150   @Test
151   public void addAugmentations() {
152     String stringId =
153         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
154             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
155             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=1}]";
156
157     YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
158
159     MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
160
161     NormalizedNodeToNodeCodec codec =
162         new NormalizedNodeToNodeCodec(schemaContext);
163
164     Container encode = codec.encode(uno);
165
166     System.out.println(encode.getNormalizedNode());
167
168     codec.decode(encode.getNormalizedNode());
169   }
170
171 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.