From de111801728536ded19c587b67e80e5a33e8118d Mon Sep 17 00:00:00 2001 From: Milos Fabian Date: Mon, 14 Jul 2014 09:22:42 +0200 Subject: [PATCH] Bug-612: Added check - before subobjects are serialized. Change-Id: I1ec19c5529bc85c6dadb02f0e7979db447fcf48b Signed-off-by: Milos Fabian --- .../object/AbstractEROWithSubobjectsParser.java | 9 ++++----- .../object/AbstractRROWithSubobjectsParser.java | 4 +--- .../object/AbstractXROWithSubobjectsParser.java | 4 +--- .../protocol/pcep/impl/PCEPObjectParserTest.java | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java index 21038feae1..96fd9b3690 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java @@ -9,13 +9,10 @@ package org.opendaylight.protocol.pcep.impl.object; import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; - import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; - import java.util.ArrayList; import java.util.List; - import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry; import org.opendaylight.protocol.pcep.spi.ObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; @@ -62,8 +59,10 @@ public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, O } protected final void serializeSubobject(final List subobjects, final ByteBuf buffer) { - for (final Subobject subobject : subobjects) { - this.subobjReg.serializeSubobject(subobject, buffer); + if(subobjects != null && !subobjects.isEmpty()) { + for (final Subobject subobject : subobjects) { + this.subobjReg.serializeSubobject(subobject, buffer); + } } } } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractRROWithSubobjectsParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractRROWithSubobjectsParser.java index 6b6e474de3..9eb0b93073 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractRROWithSubobjectsParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractRROWithSubobjectsParser.java @@ -9,13 +9,10 @@ package org.opendaylight.protocol.pcep.impl.object; import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; - import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; - import java.util.ArrayList; import java.util.List; - import org.opendaylight.protocol.pcep.spi.ObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; @@ -60,6 +57,7 @@ public abstract class AbstractRROWithSubobjectsParser implements ObjectParser, O } protected final void serializeSubobject(final List subobjects, final ByteBuf buffer) { + Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(), "RRO must contain at least one subobject."); for (final Subobject subobject : subobjects) { this.subobjReg.serializeSubobject(subobject, buffer); } diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java index e1a5f94e1a..987f25a40f 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java @@ -9,13 +9,10 @@ package org.opendaylight.protocol.pcep.impl.object; import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; - import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; - import java.util.ArrayList; import java.util.List; - import org.opendaylight.protocol.pcep.spi.ObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; @@ -61,6 +58,7 @@ public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, O } protected final void serializeSubobject(final List subobjects, final ByteBuf buffer) { + Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(), "XRO must contain at least one subobject."); for (final Subobject subobject : subobjects) { this.subobjReg.serializeSubobject(subobject, buffer); } diff --git a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java index 1f3790775c..3eff2f60d2 100644 --- a/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java +++ b/pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java @@ -74,6 +74,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.GcBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.IroBuilder; @@ -717,4 +718,18 @@ public class PCEPObjectParserTest { parser.serializeObject(builder.build(), buf); assertArrayEquals(result.array(), ByteArray.getAllBytes(buf)); } + + @Test + public void testEmptyEroObject() throws PCEPDeserializerException { + final Object object = this.ctx.getObjectHandlerRegistry().parseObject(PCEPExplicitRouteObjectParser.CLASS, PCEPExplicitRouteObjectParser.TYPE, new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER); + assertNotNull(object); + assertTrue(object instanceof Ero); + final Ero eroObject = (Ero) object; + assertTrue(eroObject.getSubobject().isEmpty()); + + final ByteBuf buffer = Unpooled.buffer(); + this.ctx.getObjectHandlerRegistry().serializeObject(eroObject, buffer); + final byte[] expected = {0x07, 0x13, 0x00, 0x04}; + assertArrayEquals(expected, ByteArray.getAllBytes(buffer)); + } } -- 2.36.6