BUG-130 : introduced RROSubobjectUtil. 42/3642/2
authorDana Kutenicsova <dkutenic@cisco.com>
Wed, 11 Dec 2013 14:36:14 +0000 (15:36 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Wed, 11 Dec 2013 15:42:18 +0000 (16:42 +0100)
Change-Id: I14e3585315d309bb04e76fc23d787f66b19527ee
Signed-off-by: Dana Kutenicsova <dkutenic@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/RROSubobjectUtil.java [new file with mode: 0644]
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROIpv4PrefixSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROIpv6PrefixSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROLabelSubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROPathKey128SubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROPathKey32SubobjectParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/subobject/RROUnnumberedInterfaceSubobjectParser.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPRROSubobjectParserTest.java

index 157514073feeb56528505ea24344a4cc4fa250d8..eb01a5b2097e31d83d8cb514b7036c0c52d0ebe6 100644 (file)
@@ -28,7 +28,6 @@ public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, O
 
        private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
        private static final int SUB_LENGTH_F_LENGTH = 1;
-       private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
 
        private static final int TYPE_FLAG_F_OFFSET = 0;
        private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
index eff58f0e7c9f08cbd52af936bce3090aab85483e..7451908b503a62d1534c2d6d288558abeb8179f0 100644 (file)
@@ -21,7 +21,6 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-import com.google.common.primitives.UnsignedBytes;
 
 public abstract class AbstractRROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
 
@@ -29,7 +28,6 @@ public abstract class AbstractRROWithSubobjectsParser implements ObjectParser, O
 
        private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
        private static final int SUB_LENGTH_F_LENGTH = 1;
-       private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
 
        private static final int TYPE_FLAG_F_OFFSET = 0;
        private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
@@ -89,17 +87,7 @@ public abstract class AbstractRROWithSubobjectsParser implements ObjectParser, O
 
                        final RROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject.getSubobjectType());
 
-                       final byte[] valueBytes = serializer.serializeSubobject(subobject);
-
-                       final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
-
-                       final byte typeBytes = UnsignedBytes.checkedCast(serializer.getType());
-                       final byte lengthBytes = UnsignedBytes.checkedCast(valueBytes.length + SUB_HEADER_LENGTH);
-
-                       bytes[0] = typeBytes;
-                       bytes[1] = lengthBytes;
-                       System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
-
+                       final byte[] bytes = serializer.serializeSubobject(subobject);
                        finalLength += bytes.length;
                        result.add(bytes);
                }
diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/RROSubobjectUtil.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/RROSubobjectUtil.java
new file mode 100644 (file)
index 0000000..f952fb5
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.pcep.impl.object;
+
+import com.google.common.primitives.UnsignedBytes;
+
+public class RROSubobjectUtil {
+       private static final int HEADER_SIZE = 2;
+
+       private RROSubobjectUtil() {
+       }
+
+       public static byte[] formatSubobject(final int type, final byte[] value) {
+               final byte[] bytes = new byte[HEADER_SIZE + value.length];
+               bytes[0] = UnsignedBytes.checkedCast(type);
+               bytes[1] = UnsignedBytes.checkedCast(value.length + HEADER_SIZE);
+               System.arraycopy(value, 0, bytes, HEADER_SIZE, value.length);
+               return bytes;
+       }
+}
index d584108fe2d6791f60eb960a94cc7cea88c64a3f..8e5b7dbf760391598b1c2b67bf90f087dcec5eee 100644 (file)
@@ -11,6 +11,7 @@ import java.util.Arrays;
 import java.util.BitSet;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
+import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
@@ -89,7 +90,7 @@ public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubo
                ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
                retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
                ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS4_F_OFFSET);
-               return retBytes;
+               return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
        }
 
        @Override
index 8a130ac158b6972fa77adff774db3b112e1b8dd8..8b5af74224566e9c1a69604be6e9d0fbf34e5c7d 100644 (file)
@@ -12,6 +12,7 @@ import java.util.BitSet;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.concepts.Ipv6Util;
+import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
@@ -82,7 +83,7 @@ public class RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubo
                ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
                retBytes[PREFIX_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
                ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
-               return retBytes;
+               return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
        }
 
        @Override
index 5ffafb256a9e7b1383bfbc87384f92fcb6365690..0bf35f48fbe81d7b408a1bfa9dedc9789e20d878 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import java.util.Arrays;
 import java.util.BitSet;
 
+import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.LabelHandlerRegistry;
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
@@ -93,7 +94,7 @@ public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobject
                reserved.set(G_FLAG_OFFSET, label.isGlobal());
                System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
                retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(parser.getType());
-               return retBytes;
+               return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
        }
 
        @Override
index ced673b9fd23d86d79848fc270479d4717b40a11..f7e7964479739f501e86fb165339c380beb06794 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 
 import java.util.Arrays;
 
+import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
@@ -61,7 +62,7 @@ public class RROPathKey128SubobjectParser implements RROSubobjectParser, RROSubo
                final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
                System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
                System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
-               return retBytes;
+               return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
        }
 
        @Override
index 49910e6d6c70704d203071cc6bc6e92fad1a5b38..6220ead64b659de71a6c87b0bf40608c2276febe 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 
 import java.util.Arrays;
 
+import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
@@ -60,7 +61,7 @@ public class RROPathKey32SubobjectParser implements RROSubobjectParser, RROSubob
                final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
                System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
                System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
-               return retBytes;
+               return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
        }
 
        @Override
index 47d4352179175434f9d72fd71f68d0f09f10fd4e..ef07f101b1b72ac088f067234c013652a5432d33 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep.impl.subobject;
 import java.util.Arrays;
 import java.util.BitSet;
 
+import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
@@ -79,7 +80,7 @@ public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser
                ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
                System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
                                INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
-               return retBytes;
+               return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
        }
 
        @Override
index 7b1576c9f13aec2ed2271302d8b1f3d46e67f1b9..5a789d171368e555b4ed65b6bc559eed2754f667 100644 (file)
@@ -20,6 +20,7 @@ import org.opendaylight.protocol.pcep.impl.subobject.RROPathKey32SubobjectParser
 import org.opendaylight.protocol.pcep.impl.subobject.RROUnnumberedInterfaceSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
+import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
@@ -38,17 +39,20 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
 
 public class PCEPRROSubobjectParserTest {
 
-       private static final byte[] ip4PrefixBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01 };
-       private static final byte[] ip6PrefixBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+       private static final byte[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+                       (byte) 0x16, (byte) 0x01 };
+       private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
-                       (byte) 0xFF, (byte) 0x16, (byte) 0x02 };
-       private static final byte[] unnumberedBytes = { (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
-                       (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
-       private static final byte[] pathKey32Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 };
-       private static final byte[] pathKey128Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
-                       (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-                       (byte) 0x00, (byte) 0x00, (byte) 0x00 };
-       private static final byte[] labelBytes = { (byte) 0x81, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF };
+                       (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02 };
+       private static final byte[] unnumberedBytes = { (byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34,
+                       (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
+       private static final byte[] pathKey32Bytes = { (byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
+                       (byte) 0x50, (byte) 0x00 };
+       private static final byte[] pathKey128Bytes = { (byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
+                       (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00,
+                       (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
+       private static final byte[] labelBytes = { (byte) 0x03, (byte) 0x08, (byte) 0x81, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25,
+                       (byte) 0xFF };
 
        @Test
        public void testRROIp4PrefixSubobject() throws PCEPDeserializerException {
@@ -58,7 +62,7 @@ public class PCEPRROSubobjectParserTest {
                subs.setProtectionInUse(false);
                subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
                                new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
-               assertEquals(subs.build(), parser.parseSubobject(ip4PrefixBytes));
+               assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip4PrefixBytes, 2)));
                assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build()));
        }
 
@@ -73,7 +77,7 @@ public class PCEPRROSubobjectParserTest {
                                                new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                                                                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                                                                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
-               assertEquals(subs.build(), parser.parseSubobject(ip6PrefixBytes));
+               assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip6PrefixBytes, 2)));
                assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build()));
        }
 
@@ -85,7 +89,7 @@ public class PCEPRROSubobjectParserTest {
                subs.setProtectionInUse(true);
                subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
                                new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
-               assertEquals(subs.build(), parser.parseSubobject(unnumberedBytes));
+               assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(unnumberedBytes, 2)));
                assertArrayEquals(unnumberedBytes, parser.serializeSubobject(subs.build()));
        }
 
@@ -97,7 +101,7 @@ public class PCEPRROSubobjectParserTest {
                pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
                pBuilder.setPathKey(new PathKey(4660));
                subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
-               assertEquals(subs.build(), parser.parseSubobject(pathKey32Bytes));
+               assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(pathKey32Bytes, 2)));
                assertArrayEquals(pathKey32Bytes, parser.serializeSubobject(subs.build()));
        }
 
@@ -110,7 +114,7 @@ public class PCEPRROSubobjectParserTest {
                                (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
                pBuilder.setPathKey(new PathKey(4660));
                subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
-               assertEquals(subs.build(), parser.parseSubobject(pathKey128Bytes));
+               assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(pathKey128Bytes, 2)));
                assertArrayEquals(pathKey128Bytes, parser.serializeSubobject(subs.build()));
        }
 
@@ -123,7 +127,7 @@ public class PCEPRROSubobjectParserTest {
                                                new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
                                                                new GeneralizedLabelBuilder().setGeneralizedLabel(
                                                                                new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
-               assertEquals(subs.build(), parser.parseSubobject(labelBytes));
+               assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(labelBytes, 2)));
                assertArrayEquals(labelBytes, parser.serializeSubobject(subs.build()));
        }
 }