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