2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.datastore.node;
11 import junit.framework.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
15 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
16 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
17 import org.opendaylight.controller.cluster.datastore.util.TestModel;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
20 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
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;
25 import java.util.ArrayList;
26 import java.util.List;
28 import static junit.framework.Assert.assertEquals;
29 import static junit.framework.Assert.assertNotNull;
31 public class NormalizedNodeToNodeCodecTest {
35 private SchemaContext schemaContext;
39 schemaContext = TestModel.createTestContext();
40 assertNotNull("Schema context must not be null.", schemaContext);
43 private YangInstanceIdentifier instanceIdentifierFromString(String s){
45 String[] ids = s.split("/");
47 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
48 for(String nodeId : ids){
49 if(!"".equals(nodeId)) {
50 pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
53 final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.create(pathArguments);
54 return instanceIdentifier;
59 public void testNormalizeNodeAttributesToProtoBuffNode(){
60 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
61 String id = "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test" +
62 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list" +
63 "/(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}]" +
64 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
66 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
67 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
68 YangInstanceIdentifier.builder().build().toString(), documentOne);
70 // Validate the value of id can be retrieved from the normalized node
71 NormalizedNode output = normalizedNodeGetter.getOutput();
72 assertNotNull(output);
75 NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(schemaContext);
76 Container container = codec.encode(instanceIdentifierFromString(id),output);
78 assertNotNull(container);
79 assertEquals(id, container.getParentPath()+"/"+container.getNormalizedNode().getPath()) ;
81 // Decode the normalized node from the ProtocolBuffer form
82 //first get the node representation of normalized node
83 final Node node = container.getNormalizedNode();
85 NormalizedNode<?,?> normalizedNode = codec.decode(instanceIdentifierFromString(id),node);
87 assertEquals(normalizedNode.getValue().toString(),output.getValue().toString());
91 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode() throws Exception {
92 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
94 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec = new NormalizedNodeToNodeCodec(schemaContext);
96 Container container = normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder().build(), documentOne);
99 final NormalizedNode<?, ?> decode = normalizedNodeToNodeCodec.decode(instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),container.getNormalizedNode());
100 assertNotNull(decode != null);
102 //let us ensure that the return decode normalized node encode returns same container
103 Container containerResult = normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder().build(), decode);
105 assertEquals(container.getParentPath(),containerResult.getParentPath());
106 assertEquals(container.getNormalizedNode().getChildCount(),container.getNormalizedNode().getChildCount());
108 Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),container.getNormalizedNode().getChildCount());
110 //check first level children are proper
111 List<Node>childrenResult = containerResult.getNormalizedNode().getChildList();
112 List<Node>childrenOriginal = container.getNormalizedNode().getChildList();
114 System.out.println("-------------------------------------------------");
116 System.out.println(childrenOriginal.toString());
118 System.out.println("-------------------------------------------------");
120 System.out.println(childrenResult.toString());
123 for(Node resultChild: childrenResult){
125 for(Node originalChild:childrenOriginal){
126 if(originalChild.getPath().equals(resultChild.getPath())
127 && resultChild.getType().equals(resultChild.getType())){
132 Assert.assertTrue(bFound);
138 public void addAugmentations(){
139 String stringId = "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test" +
140 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list" +
141 "/(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}]";
143 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
145 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
147 NormalizedNodeToNodeCodec codec =
148 new NormalizedNodeToNodeCodec(schemaContext);
150 Container encode = codec
151 .encode(identifier, uno);
153 System.out.println(encode.getNormalizedNode());
155 codec.decode(identifier, encode.getNormalizedNode());