package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
+import com.google.protobuf.ByteString;
import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
InstanceIdentifierUtils.toSerializable((YangInstanceIdentifier) value, context));
} else if(value instanceof Set) {
Set set = (Set) value;
- if(!set.isEmpty()){
- for(Object o : set){
- if(o instanceof String){
+ if (!set.isEmpty()) {
+ for (Object o : set) {
+ if (o instanceof String) {
builder.addBitsValue(o.toString());
} else {
throw new IllegalArgumentException("Expected value type to be Bits but was : " +
- value.toString());
+ value.toString());
}
}
}
+ } else if(value instanceof byte[]){
+ builder.setBytesValue(ByteString.copyFrom((byte[]) value));
} else {
builder.setValue(value.toString());
}
node.getInstanceIdentifierValue(), context);
} else if(node.getIntValueType() == ValueType.BITS_TYPE.ordinal()){
return new HashSet(node.getBitsValueList());
+ } else if(node.getIntValueType() == ValueType.BINARY_TYPE.ordinal()){
+ return node.getBytesValue().toByteArray();
}
return deSerializeBasicTypes(node.getIntValueType(), node.getValue());
}
YANG_IDENTIFIER_TYPE,
STRING_TYPE,
BIG_INTEGER_TYPE,
- BIG_DECIMAL_TYPE;
+ BIG_DECIMAL_TYPE,
+ BINARY_TYPE;
private static Map<Class, ValueType> types = new HashMap<>();
types.put(Short.class,SHORT_TYPE);
types.put(BigInteger.class, BIG_INTEGER_TYPE);
types.put(BigDecimal.class, BIG_DECIMAL_TYPE);
+ types.put(byte[].class, BINARY_TYPE);
}
public static final ValueType getSerializableType(Object node){
*/
com.google.protobuf.ByteString
getCodeBytes(int index);
+
+ // optional bytes bytesValue = 13;
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ boolean hasBytesValue();
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ com.google.protobuf.ByteString getBytesValue();
}
/**
* Protobuf type {@code org.opendaylight.controller.mdsal.Node}
code_.add(input.readBytes());
break;
}
+ case 106: {
+ bitField0_ |= 0x00000100;
+ bytesValue_ = input.readBytes();
+ break;
+ }
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
return code_.getByteString(index);
}
+ // optional bytes bytesValue = 13;
+ public static final int BYTESVALUE_FIELD_NUMBER = 13;
+ private com.google.protobuf.ByteString bytesValue_;
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ public boolean hasBytesValue() {
+ return ((bitField0_ & 0x00000100) == 0x00000100);
+ }
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ public com.google.protobuf.ByteString getBytesValue() {
+ return bytesValue_;
+ }
+
private void initFields() {
path_ = "";
type_ = "";
instanceIdentifierValue_ = org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.InstanceIdentifier.getDefaultInstance();
bitsValue_ = com.google.protobuf.LazyStringArrayList.EMPTY;
code_ = com.google.protobuf.LazyStringArrayList.EMPTY;
+ bytesValue_ = com.google.protobuf.ByteString.EMPTY;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
for (int i = 0; i < code_.size(); i++) {
output.writeBytes(12, code_.getByteString(i));
}
+ if (((bitField0_ & 0x00000100) == 0x00000100)) {
+ output.writeBytes(13, bytesValue_);
+ }
getUnknownFields().writeTo(output);
}
size += dataSize;
size += 1 * getCodeList().size();
}
+ if (((bitField0_ & 0x00000100) == 0x00000100)) {
+ size += com.google.protobuf.CodedOutputStream
+ .computeBytesSize(13, bytesValue_);
+ }
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
bitField0_ = (bitField0_ & ~0x00000400);
code_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000800);
+ bytesValue_ = com.google.protobuf.ByteString.EMPTY;
+ bitField0_ = (bitField0_ & ~0x00001000);
return this;
}
bitField0_ = (bitField0_ & ~0x00000800);
}
result.code_ = code_;
+ if (((from_bitField0_ & 0x00001000) == 0x00001000)) {
+ to_bitField0_ |= 0x00000100;
+ }
+ result.bytesValue_ = bytesValue_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
onChanged();
}
+ if (other.hasBytesValue()) {
+ setBytesValue(other.getBytesValue());
+ }
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
return this;
}
+ // optional bytes bytesValue = 13;
+ private com.google.protobuf.ByteString bytesValue_ = com.google.protobuf.ByteString.EMPTY;
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ public boolean hasBytesValue() {
+ return ((bitField0_ & 0x00001000) == 0x00001000);
+ }
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ public com.google.protobuf.ByteString getBytesValue() {
+ return bytesValue_;
+ }
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ public Builder setBytesValue(com.google.protobuf.ByteString value) {
+ if (value == null) {
+ throw new NullPointerException();
+ }
+ bitField0_ |= 0x00001000;
+ bytesValue_ = value;
+ onChanged();
+ return this;
+ }
+ /**
+ * <code>optional bytes bytesValue = 13;</code>
+ */
+ public Builder clearBytesValue() {
+ bitField0_ = (bitField0_ & ~0x00001000);
+ bytesValue_ = getDefaultInstance().getBytesValue();
+ onChanged();
+ return this;
+ }
+
// @@protoc_insertion_point(builder_scope:org.opendaylight.controller.mdsal.Node)
}
"ntroller.mdsal.Attribute\022\017\n\007intType\030\006 \001(" +
"\005\"f\n\022InstanceIdentifier\022B\n\targuments\030\001 \003" +
"(\0132/.org.opendaylight.controller.mdsal.P" +
- "athArgument\022\014\n\004code\030\002 \003(\t\"\245\003\n\004Node\022\014\n\004pa" +
+ "athArgument\022\014\n\004code\030\002 \003(\t\"\271\003\n\004Node\022\014\n\004pa" +
"th\030\001 \001(\t\022\014\n\004type\030\002 \001(\t\022E\n\014pathArgument\030\003" +
" \001(\0132/.org.opendaylight.controller.mdsal" +
".PathArgument\022\017\n\007intType\030\004 \001(\005\022@\n\nattrib",
"pe\030\t \001(\005\022V\n\027instanceIdentifierValue\030\n \001(" +
"\01325.org.opendaylight.controller.mdsal.In" +
"stanceIdentifier\022\021\n\tbitsValue\030\013 \003(\t\022\014\n\004c" +
- "ode\030\014 \003(\t\"`\n\tContainer\022\022\n\nparentPath\030\001 \002" +
- "(\t\022?\n\016normalizedNode\030\002 \001(\0132\'.org.openday" +
- "light.controller.mdsal.Node\"\246\001\n\014NodeMapE",
- "ntry\022U\n\026instanceIdentifierPath\030\001 \002(\01325.o" +
- "rg.opendaylight.controller.mdsal.Instanc" +
- "eIdentifier\022?\n\016normalizedNode\030\002 \001(\0132\'.or" +
- "g.opendaylight.controller.mdsal.Node\"N\n\007" +
- "NodeMap\022C\n\nmapEntries\030\001 \003(\0132/.org.openda" +
- "ylight.controller.mdsal.NodeMapEntryBO\n5" +
- "org.opendaylight.controller.protobuff.me" +
- "ssages.commonB\026NormalizedNodeMessages"
+ "ode\030\014 \003(\t\022\022\n\nbytesValue\030\r \001(\014\"`\n\tContain" +
+ "er\022\022\n\nparentPath\030\001 \002(\t\022?\n\016normalizedNode" +
+ "\030\002 \001(\0132\'.org.opendaylight.controller.mds",
+ "al.Node\"\246\001\n\014NodeMapEntry\022U\n\026instanceIden" +
+ "tifierPath\030\001 \002(\01325.org.opendaylight.cont" +
+ "roller.mdsal.InstanceIdentifier\022?\n\016norma" +
+ "lizedNode\030\002 \001(\0132\'.org.opendaylight.contr" +
+ "oller.mdsal.Node\"N\n\007NodeMap\022C\n\nmapEntrie" +
+ "s\030\001 \003(\0132/.org.opendaylight.controller.md" +
+ "sal.NodeMapEntryBO\n5org.opendaylight.con" +
+ "troller.protobuff.messages.commonB\026Norma" +
+ "lizedNodeMessages"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
internal_static_org_opendaylight_controller_mdsal_Node_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_org_opendaylight_controller_mdsal_Node_descriptor,
- new java.lang.String[] { "Path", "Type", "PathArgument", "IntType", "Attributes", "Child", "Value", "ValueType", "IntValueType", "InstanceIdentifierValue", "BitsValue", "Code", });
+ new java.lang.String[] { "Path", "Type", "PathArgument", "IntType", "Attributes", "Child", "Value", "ValueType", "IntValueType", "InstanceIdentifierValue", "BitsValue", "Code", "BytesValue", });
internal_static_org_opendaylight_controller_mdsal_Container_descriptor =
getDescriptor().getMessageTypes().get(6);
internal_static_org_opendaylight_controller_mdsal_Container_fieldAccessorTable = new
repeated string bitsValue = 11; // intValueType = Bits
repeated string code = 12; // A list of string codes which can be used for any repeated strings in the NormalizedNode
+
+ optional bytes bytesValue = 13;
}
message Container{
package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
+import com.google.common.base.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opendaylight.controller.cluster.datastore.util.TestModel;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
public class NormalizedNodeSerializerTest {
// created by serializing the original node and deSerializing it back.
assertEquals(expectedNode, actualNode);
+ byte[] binaryData = new byte[5];
+ for(byte i=0;i<5;i++){
+ binaryData[i] = i;
+ }
+
+ ContainerNode node1 = TestModel.createBaseTestContainerBuilder()
+ .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATE_QNAME, binaryData))
+ .build();
+
+ NormalizedNodeMessages.Node serializedNode1 = NormalizedNodeSerializer
+ .serialize(node1);
+
+ ContainerNode node2 =
+ (ContainerNode) NormalizedNodeSerializer.deSerialize(serializedNode1);
+
+
+ // FIXME: This will not work due to BUG 2326. Once that is fixed we can uncomment this assertion
+ // assertEquals(node1, node2);
+
+ Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = node2.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.SOME_BINARY_DATE_QNAME));
+
+ Object value = child.get().getValue();
+
+ assertTrue("value should be of type byte[]", value instanceof byte[]);
+
+ byte[] bytesValue = (byte[]) value;
+
+ for(byte i=0;i<5;i++){
+ assertEquals(i, bytesValue[i]);
+ }
+
}
@Test(expected = NullPointerException.class)
--- /dev/null
+package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ValueTypeTest {
+
+ @Test
+ public void testGetSerializableType(){
+ byte[] b = new byte[10];
+ b[0] = 1;
+ b[2] = 2;
+
+ ValueType serializableType = ValueType.getSerializableType(b);
+ assertEquals(ValueType.BINARY_TYPE, serializableType);
+ }
+}
\ No newline at end of file
public static final QName DESC_QNAME = QName.create(TEST_QNAME, "desc");
public static final QName POINTER_QNAME = QName.create(TEST_QNAME, "pointer");
+ public static final QName SOME_BINARY_DATE_QNAME = QName.create(TEST_QNAME, "some-binary-data");
public static final QName SOME_REF_QNAME = QName.create(TEST_QNAME,
"some-ref");
public static final QName MYIDENTITY_QNAME = QName.create(TEST_QNAME,
}
- public static ContainerNode createTestContainer() {
-
-
- // Create a list of shoes
- // This is to test leaf list entry
- final LeafSetEntryNode<Object> nike =
- ImmutableLeafSetEntryNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeWithValue(QName.create(
- TEST_QNAME, "shoe"), "nike")).withValue("nike").build();
-
- final LeafSetEntryNode<Object> puma =
- ImmutableLeafSetEntryNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeWithValue(QName.create(
- TEST_QNAME, "shoe"), "puma")).withValue("puma").build();
-
- final LeafSetNode<Object> shoes =
- ImmutableLeafSetNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(QName.create(
- TEST_QNAME, "shoe"))).withChild(nike).withChild(puma)
- .build();
-
-
- // Test a leaf-list where each entry contains an identity
- final LeafSetEntryNode<Object> cap1 =
- ImmutableLeafSetEntryNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeWithValue(QName.create(
- TEST_QNAME, "capability"), DESC_QNAME))
- .withValue(DESC_QNAME).build();
-
- final LeafSetNode<Object> capabilities =
- ImmutableLeafSetNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(QName.create(
- TEST_QNAME, "capability"))).withChild(cap1).build();
-
- ContainerNode switchFeatures =
- ImmutableContainerNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(SWITCH_FEATURES_QNAME))
- .withChild(capabilities).build();
-
- // Create a leaf list with numbers
- final LeafSetEntryNode<Object> five =
- ImmutableLeafSetEntryNodeBuilder
- .create()
- .withNodeIdentifier(
- (new YangInstanceIdentifier.NodeWithValue(QName.create(
- TEST_QNAME, "number"), 5))).withValue(5).build();
- final LeafSetEntryNode<Object> fifteen =
- ImmutableLeafSetEntryNodeBuilder
- .create()
- .withNodeIdentifier(
- (new YangInstanceIdentifier.NodeWithValue(QName.create(
- TEST_QNAME, "number"), 15))).withValue(15).build();
- final LeafSetNode<Object> numbers =
- ImmutableLeafSetNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(QName.create(
- TEST_QNAME, "number"))).withChild(five).withChild(fifteen)
- .build();
-
-
- // Create augmentations
- MapEntryNode mapEntry = createAugmentedListEntry(1, "First Test");
-
- // Create a bits leaf
+ public static DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> createBaseTestContainerBuilder() {
+ // Create a list of shoes
+ // This is to test leaf list entry
+ final LeafSetEntryNode<Object> nike =
+ ImmutableLeafSetEntryNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeWithValue(QName.create(
+ TEST_QNAME, "shoe"), "nike")).withValue("nike").build();
+
+ final LeafSetEntryNode<Object> puma =
+ ImmutableLeafSetEntryNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeWithValue(QName.create(
+ TEST_QNAME, "shoe"), "puma")).withValue("puma").build();
+
+ final LeafSetNode<Object> shoes =
+ ImmutableLeafSetNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeIdentifier(QName.create(
+ TEST_QNAME, "shoe"))).withChild(nike).withChild(puma)
+ .build();
+
+
+ // Test a leaf-list where each entry contains an identity
+ final LeafSetEntryNode<Object> cap1 =
+ ImmutableLeafSetEntryNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeWithValue(QName.create(
+ TEST_QNAME, "capability"), DESC_QNAME))
+ .withValue(DESC_QNAME).build();
+
+ final LeafSetNode<Object> capabilities =
+ ImmutableLeafSetNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeIdentifier(QName.create(
+ TEST_QNAME, "capability"))).withChild(cap1).build();
+
+ ContainerNode switchFeatures =
+ ImmutableContainerNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeIdentifier(SWITCH_FEATURES_QNAME))
+ .withChild(capabilities).build();
+
+ // Create a leaf list with numbers
+ final LeafSetEntryNode<Object> five =
+ ImmutableLeafSetEntryNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ (new YangInstanceIdentifier.NodeWithValue(QName.create(
+ TEST_QNAME, "number"), 5))).withValue(5).build();
+ final LeafSetEntryNode<Object> fifteen =
+ ImmutableLeafSetEntryNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ (new YangInstanceIdentifier.NodeWithValue(QName.create(
+ TEST_QNAME, "number"), 15))).withValue(15).build();
+ final LeafSetNode<Object> numbers =
+ ImmutableLeafSetNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeIdentifier(QName.create(
+ TEST_QNAME, "number"))).withChild(five).withChild(fifteen)
+ .build();
+
+
+ // Create augmentations
+ MapEntryNode mapEntry = createAugmentedListEntry(1, "First Test");
+
+ // Create a bits leaf
NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>>
- myBits = Builders.leafBuilder().withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(
- QName.create(TEST_QNAME, "my-bits"))).withValue(
- ImmutableSet.of("foo", "bar"));
+ myBits = Builders.leafBuilder().withNodeIdentifier(
+ new YangInstanceIdentifier.NodeIdentifier(
+ QName.create(TEST_QNAME, "my-bits"))).withValue(
+ ImmutableSet.of("foo", "bar"));
// Create the document
- return ImmutableContainerNodeBuilder
- .create()
- .withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME))
- .withChild(myBits.build())
- .withChild(ImmutableNodes.leafNode(DESC_QNAME, DESC))
- .withChild(ImmutableNodes.leafNode(POINTER_QNAME, "pointer"))
- .withChild(
- ImmutableNodes.leafNode(SOME_REF_QNAME, YangInstanceIdentifier
- .builder().build()))
- .withChild(ImmutableNodes.leafNode(MYIDENTITY_QNAME, DESC_QNAME))
-
- // .withChild(augmentationNode)
- .withChild(shoes)
- .withChild(numbers)
- .withChild(switchFeatures)
- .withChild(
- mapNodeBuilder(AUGMENTED_LIST_QNAME).withChild(mapEntry).build())
- .withChild(
- mapNodeBuilder(OUTER_LIST_QNAME)
- .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
- .withChild(BAR_NODE).build()).build();
+ return ImmutableContainerNodeBuilder
+ .create()
+ .withNodeIdentifier(
+ new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME))
+ .withChild(myBits.build())
+ .withChild(ImmutableNodes.leafNode(DESC_QNAME, DESC))
+ .withChild(ImmutableNodes.leafNode(POINTER_QNAME, "pointer"))
+ .withChild(
+ ImmutableNodes.leafNode(SOME_REF_QNAME, YangInstanceIdentifier
+ .builder().build()))
+ .withChild(ImmutableNodes.leafNode(MYIDENTITY_QNAME, DESC_QNAME))
+
+ // .withChild(augmentationNode)
+ .withChild(shoes)
+ .withChild(numbers)
+ .withChild(switchFeatures)
+ .withChild(
+ mapNodeBuilder(AUGMENTED_LIST_QNAME).withChild(mapEntry).build())
+ .withChild(
+ mapNodeBuilder(OUTER_LIST_QNAME)
+ .withChild(mapEntry(OUTER_LIST_QNAME, ID_QNAME, ONE_ID))
+ .withChild(BAR_NODE).build());
+ }
+ public static ContainerNode createTestContainer() {
+ return createBaseTestContainerBuilder().build();
}
public static MapEntryNode createAugmentedListEntry(int id, String name) {
}
}
+ leaf some-binary-data {
+ type binary;
+ }
+
}
}