Merge "Fixed for bug : 1171 - issue while creating subnet"
[controller.git] / opendaylight / md-sal / sal-protocolbuffer-encoding / src / test / java / org / opendaylight / controller / cluster / datastore / node / NormalizedNodeToNodeCodecTest.java
1 /*
2  * Copyright (c) 2014 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 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;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import static junit.framework.Assert.assertEquals;
29 import static junit.framework.Assert.assertNotNull;
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
45         String[] ids = s.split("/");
46
47         List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
48         for(String nodeId : ids){
49             if(!"".equals(nodeId)) {
50                 pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
51             }
52         }
53         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.create(pathArguments);
54         return instanceIdentifier;
55     }
56
57
58     @Test
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";
65
66         NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
67         new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
68             YangInstanceIdentifier.builder().build().toString(), documentOne);
69
70         // Validate the value of id can be retrieved from the normalized node
71         NormalizedNode output = normalizedNodeGetter.getOutput();
72         assertNotNull(output);
73
74
75       NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(schemaContext);
76       Container container = codec.encode(instanceIdentifierFromString(id),output);
77
78        assertNotNull(container);
79        assertEquals(id, container.getParentPath()+"/"+container.getNormalizedNode().getPath()) ;
80
81       // Decode the normalized node from the ProtocolBuffer form
82       //first get the node representation of normalized node
83       final Node node = container.getNormalizedNode();
84
85       NormalizedNode<?,?> normalizedNode = codec.decode(instanceIdentifierFromString(id),node);
86
87       assertEquals(normalizedNode.getValue().toString(),output.getValue().toString());
88     }
89
90     @Test
91     public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode() throws Exception {
92         final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
93
94         final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec = new NormalizedNodeToNodeCodec(schemaContext);
95
96         Container container = normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder().build(), documentOne);
97
98
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);
101
102         //let us ensure that the return decode normalized node encode returns same container
103         Container containerResult =  normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder().build(), decode);
104
105         assertEquals(container.getParentPath(),containerResult.getParentPath());
106         assertEquals(container.getNormalizedNode().getChildCount(),container.getNormalizedNode().getChildCount());
107
108         Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),container.getNormalizedNode().getChildCount());
109
110         //check first level children are proper
111         List<Node>childrenResult = containerResult.getNormalizedNode().getChildList();
112         List<Node>childrenOriginal = container.getNormalizedNode().getChildList();
113
114         System.out.println("-------------------------------------------------");
115
116         System.out.println(childrenOriginal.toString());
117
118         System.out.println("-------------------------------------------------");
119
120         System.out.println(childrenResult.toString());
121
122        boolean bFound;
123         for(Node resultChild: childrenResult){
124            bFound = false;
125           for(Node originalChild:childrenOriginal){
126             if(originalChild.getPath().equals(resultChild.getPath())
127                 && resultChild.getType().equals(resultChild.getType())){
128                bFound=true;
129                break;
130             }
131           }
132           Assert.assertTrue(bFound);
133         }
134
135     }
136
137     @Test
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}]";
142
143         YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
144
145         MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
146
147         NormalizedNodeToNodeCodec codec =
148             new NormalizedNodeToNodeCodec(schemaContext);
149
150         Container encode = codec
151             .encode(identifier, uno);
152
153         System.out.println(encode.getNormalizedNode());
154
155         codec.decode(identifier, encode.getNormalizedNode());
156     }
157
158 }