2 * Copyright (c) 2014, 2015 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.utils.stream;
11 import com.google.common.io.ByteStreams;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import org.apache.commons.lang.SerializationUtils;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
18 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
19 import org.opendaylight.controller.cluster.datastore.util.TestModel;
20 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
21 import org.opendaylight.yangtools.yang.common.QName;
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.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 public class NormalizedNodeStreamReaderWriterTest {
36 public void testNormalizedNodeStreaming() throws IOException {
38 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
39 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
40 ByteStreams.newDataOutput(byteArrayOutputStream));
42 NormalizedNode<?, ?> testContainer = createTestContainer();
43 writer.writeNormalizedNode(testContainer);
45 QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
46 QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
47 QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
48 ContainerNode toasterNode = Builders.containerBuilder().
49 withNodeIdentifier(new NodeIdentifier(toaster)).
50 withChild(ImmutableNodes.leafNode(darknessFactor, "1000")).
51 withChild(ImmutableNodes.leafNode(description, largeString(20)))
54 ContainerNode toasterContainer = Builders.containerBuilder().
55 withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).
56 withChild(toasterNode).build();
57 writer.writeNormalizedNode(toasterContainer);
59 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
60 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
62 NormalizedNode<?,?> node = reader.readNormalizedNode();
63 Assert.assertEquals(testContainer, node);
65 node = reader.readNormalizedNode();
66 Assert.assertEquals(toasterContainer, node);
71 private static NormalizedNode<?, ?> createTestContainer() {
72 byte[] bytes1 = {1,2,3};
73 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
74 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).
75 withValue(bytes1).build();
78 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
79 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).
80 withValue(bytes2).build();
82 LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
83 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, null)).
84 withValue(null).build();
87 return TestModel.createBaseTestContainerBuilder().
88 withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
89 new YangInstanceIdentifier.NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)).
90 withChild(entry1).withChild(entry2).withChild(entry3).build()).
91 withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1,2,3,4})).
92 withChild(Builders.orderedMapBuilder().
93 withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.ORDERED_LIST_QNAME)).
94 withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
95 TestModel.ID_QNAME, 11)).build()).
100 public void testYangInstanceIdentifierStreaming() throws IOException {
101 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
102 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
103 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
105 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
106 NormalizedNodeOutputStreamWriter writer =
107 new NormalizedNodeOutputStreamWriter(ByteStreams.newDataOutput(byteArrayOutputStream));
108 writer.writeYangInstanceIdentifier(path);
110 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
111 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
113 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
114 Assert.assertEquals(path, newPath);
120 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
122 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
123 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
124 ByteStreams.newDataOutput(byteArrayOutputStream));
126 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
127 writer.writeNormalizedNode(testContainer);
129 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
130 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
131 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
133 writer.writeYangInstanceIdentifier(path);
135 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
136 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
138 NormalizedNode<?,?> node = reader.readNormalizedNode();
139 Assert.assertEquals(testContainer, node);
141 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
142 Assert.assertEquals(path, newPath);
147 @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
148 public void testInvalidNormalizedNodeStream() throws IOException {
149 byte[] protobufBytes = new NormalizedNodeToNodeCodec(null).encode(
150 TestModel.createBaseTestContainerBuilder().build()).getNormalizedNode().toByteArray();
152 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
153 ByteStreams.newDataInput(protobufBytes));
155 reader.readNormalizedNode();
158 @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
159 public void testInvalidYangInstanceIdentifierStream() throws IOException {
160 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).build();
162 byte[] protobufBytes = ShardTransactionMessages.DeleteData.newBuilder().setInstanceIdentifierPathArguments(
163 InstanceIdentifierUtils.toSerializable(path)).build().toByteArray();
165 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
166 ByteStreams.newDataInput(protobufBytes));
168 reader.readYangInstanceIdentifier();
172 public void testWithSerializable() {
173 NormalizedNode<?, ?> input = TestModel.createTestContainer();
174 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input );
175 SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
177 Assert.assertEquals(input, clone.getInput());
181 private static String largeString(final int pow){
183 for(int i=0;i<pow;i++){
184 StringBuilder b = new StringBuilder();
185 b.append(s).append(s);