Bug-612: Added check - before subobjects are serialized. 76/8976/2
authorMilos Fabian <milfabia@cisco.com>
Mon, 14 Jul 2014 07:22:42 +0000 (09:22 +0200)
committerMilos Fabian <milfabia@cisco.com>
Wed, 16 Jul 2014 06:35:33 +0000 (08:35 +0200)
Change-Id: I1ec19c5529bc85c6dadb02f0e7979db447fcf48b
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractEROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractRROWithSubobjectsParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractXROWithSubobjectsParser.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPObjectParserTest.java

index 21038feae1308cdeb0237471f349364ed3083fc7..96fd9b369080f1175c236413af2c06e218d259bd 100644 (file)
@@ -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<Subobject> 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);
+            }
         }
     }
 }
index 6b6e474de32bd7a0798d3453672cb415615a0559..9eb0b930731560f79607a24c6369e33c234eeec4 100644 (file)
@@ -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<Subobject> 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);
         }
index e1a5f94e1a35a2dbfa7c60b67a8d5091698cf8a6..987f25a40f41b1f8f91a0f01b7f2b8aeb54619b3 100644 (file)
@@ -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<Subobject> 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);
         }
index 1f3790775cec27a142a6e323583b256eee9f3349..3eff2f60d2353e6467f2de39805578b9f2c0a590 100644 (file)
@@ -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));
+    }
 }