BUG-49 : added tests & removed warnings: bgp-parser-spi 78/2078/1
authorDana Kutenicsova <dkutenic@cisco.com>
Tue, 22 Oct 2013 15:49:40 +0000 (17:49 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Tue, 22 Oct 2013 15:50:09 +0000 (17:50 +0200)
Change-Id: I60c6ba3ab3c1f6a07d3bbf47e2e3cb1c2cde0f91
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
15 files changed:
bgp/linkstate/src/test/java/org/opendaylight/protocol/bgp/linkstate/ActivatorTest.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/CapabilityUtil.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/NlriRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/NlriUtil.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/ParameterUtil.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleAddressFamilyRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleAttributeRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleCapabilityRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleParameterRegistry.java
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/pojo/SimpleSubsequentAddressFamilyRegistry.java
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/UtilsTest.java [new file with mode: 0644]
concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv4Util.java
concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv6Util.java
util/src/main/java/org/opendaylight/protocol/util/Util.java

index d8422cee5235fadc6dbbf79a15b36fe8efea542e..74c029e735974486df7cb8030e979dba691df17e 100644 (file)
@@ -23,17 +23,13 @@ public class ActivatorTest {
        private final Activator act = new Activator();
 
        @Test
-       public void testActivator() {
+       public void testActivator() throws Exception {
                final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
 
                assertNull(context.getAddressFamilyRegistry().classForFamily(16388));
                assertNull(context.getSubsequentAddressFamilyRegistry().classForFamily(71));
 
-               try {
-                       this.act.start(context);
-               } catch (final Exception e) {
-                       fail("This exception should not occurr.");
-               }
+               this.act.start(context);
 
                assertEquals(LinkstateAddressFamily.class, context.getAddressFamilyRegistry().classForFamily(16388));
                assertEquals(LinkstateSubsequentAddressFamily.class, context.getSubsequentAddressFamilyRegistry().classForFamily(71));
index 9fe27d022e76822103dd3c4440f7df61026a4fd5..67e527665247627122574c3c1e9d4ad54f565a81 100644 (file)
@@ -13,9 +13,7 @@ import com.google.common.primitives.UnsignedBytes;
 
 public final class CapabilityUtil {
 
-       public static final int CODE_SIZE = 1; // bytes
-       public static final int LENGTH_SIZE = 1; // bytes
-       private static final int HEADER_SIZE = CODE_SIZE + LENGTH_SIZE;
+       private static final int HEADER_SIZE = 2;
 
        private CapabilityUtil() {
 
index 16175b7ac0b5d83d8a05682e3933c25ce3dda727..890c5e4d22a8c72fc1066d2f7ce6813a20760712 100644 (file)
@@ -13,12 +13,11 @@ import org.opendaylight.protocol.util.ByteArray;
 
 import com.google.common.primitives.UnsignedBytes;
 
-
 public final class MessageUtil {
 
-       public static final int LENGTH_FIELD_LENGTH = 2; // bytes
-       public static final int MARKER_LENGTH = 16; // bytes
-       public static final int TYPE_FIELD_LENGTH = 1; // bytes
+       public static final int LENGTH_FIELD_LENGTH = 2;
+       public static final int MARKER_LENGTH = 16;
+       public static final int TYPE_FIELD_LENGTH = 1;
        public static final int COMMON_HEADER_LENGTH = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH + MARKER_LENGTH;
 
        private MessageUtil() {
@@ -36,9 +35,8 @@ public final class MessageUtil {
        public static byte[] formatMessage(final int type, final byte[] body) {
                final byte[] retBytes = new byte[COMMON_HEADER_LENGTH + body.length];
 
-               Arrays.fill(retBytes, 0, MARKER_LENGTH, (byte) 0xff);
-               System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH),
-                               Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH,
+               Arrays.fill(retBytes, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE);
+               System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH), Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH,
                                retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH);
 
                retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(type);
index f8218e4a07751c0f891c14eed368617bac950b6a..651107d9d4cd66910f2864b1d04033814f089841 100644 (file)
@@ -15,6 +15,4 @@ public interface NlriRegistry {
        public MpUnreachNlri parseMpUnreach(final byte[] bytes) throws BGPParsingException;
 
        public MpReachNlri parseMpReach(final byte[] bytes) throws BGPParsingException;
-       // FIXME: PMD remove or put a comment what is this
-       // public byte[] serializeNlri(DataObject attribute);
 }
index 452c5af27b847ba08e2e6823c599c7d6758ae6dd..ea3c63238e70e05befbb8eda113765bc61ab4ee9 100644 (file)
@@ -20,23 +20,22 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.type
 
 public final class NlriUtil {
        private NlriUtil() {
-
        }
 
        public static void parseNextHop(final byte[] bytes, final MpReachNlriBuilder builder) throws BGPParsingException {
                final CNextHop addr;
 
                switch (bytes.length) {
-               case 4:
+               case Ipv4Util.IP4_LENGTH:
                        addr = new CIpv4NextHopBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForBytes(bytes)).build()).build();
                        break;
-               case 16:
+               case Ipv6Util.IPV6_LENGTH:
                        addr = new CIpv6NextHopBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForBytes(bytes)).build()).build();
                        break;
-               case 32:
+               case Ipv6Util.IPV6_LENGTH * 2:
                        addr = new CIpv6NextHopBuilder().setIpv6NextHop(
-                                       new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, 0, 16))).setLinkLocal(
-                                                       Ipv6Util.addressForBytes(ByteArray.subByte(bytes, 16, 16))).build()).build();
+                                       new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, 0, Ipv6Util.IPV6_LENGTH))).setLinkLocal(
+                                                       Ipv6Util.addressForBytes(ByteArray.subByte(bytes, Ipv6Util.IPV6_LENGTH, Ipv6Util.IPV6_LENGTH))).build()).build();
                        break;
                default:
                        throw new BGPParsingException("Cannot parse NEXT_HOP attribute. Wrong bytes length: " + bytes.length);
index a331666f784d378aced83a8b77cbff380fbbe4de..bc9e44689969094cda7d9021903d7f89c28b7f17 100644 (file)
@@ -10,15 +10,18 @@ package org.opendaylight.protocol.bgp.parser.spi;
 import com.google.common.primitives.UnsignedBytes;
 
 public final class ParameterUtil {
+
+       private static final int HEADER_SIZE = 2;
+
        private ParameterUtil() {
 
        }
 
        public static byte[] formatParameter(final int type, final byte[] value) {
-               final byte[] bytes = new byte[2 + value.length];
+               final byte[] bytes = new byte[HEADER_SIZE + value.length];
                bytes[0] = UnsignedBytes.checkedCast(type);
                bytes[1] = UnsignedBytes.checkedCast(value.length);
-               System.arraycopy(value, 0, bytes, 2, value.length);
+               System.arraycopy(value, 0, bytes, HEADER_SIZE, value.length);
                return bytes;
        }
 }
index a2e896deb76c016a2fc8129faa6444a0d1557815..463f172617731e4bbe5b6001394bb8c97f27ed15 100644 (file)
@@ -8,13 +8,14 @@
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
+import org.opendaylight.protocol.util.Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
 
 import com.google.common.base.Preconditions;
 
 final class SimpleAddressFamilyRegistry extends AbstractFamilyRegistry<AddressFamily, Integer> implements AddressFamilyRegistry {
        AutoCloseable registerAddressFamily(final Class<? extends AddressFamily> clazz, final int number) {
-               Preconditions.checkArgument(number >= 0 && number <= 65535);
+               Preconditions.checkArgument(number >= 0 && number <= Util.UNSIGNED_SHORT_MAX_VALUE);
                return super.registerFamily(clazz, number);
        }
 
index f46127c3db3efb38eff97416c08411266fdf2c9a..7fda5a50b1ad0701a3400654f1d59c1483381ad8 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
 import org.opendaylight.protocol.concepts.HandlerRegistry;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributesBuilder;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
@@ -27,16 +28,16 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
        private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
 
        AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
-               Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
-               return handlers.registerParser(attributeType, parser);
+               Preconditions.checkArgument(attributeType >= 0 && attributeType <= Util.UNSIGNED_BYTE_MAX_VALUE);
+               return this.handlers.registerParser(attributeType, parser);
        }
 
        AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
-               return handlers.registerSerializer(paramClass, serializer);
+               return this.handlers.registerSerializer(paramClass, serializer);
        }
 
-       private int parseAttribute( final byte[] bytes, final int offset, final PathAttributesBuilder builder)
-                       throws BGPDocumentedException, BGPParsingException {
+       private int parseAttribute(final byte[] bytes, final int offset, final PathAttributesBuilder builder) throws BGPDocumentedException,
+                       BGPParsingException {
                // FIXME: validate minimum length
                final boolean[] flags = ByteArray.parseBits(bytes[offset]);
                final int type = UnsignedBytes.toInt(bytes[offset + 1]);
@@ -50,7 +51,7 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
                        hdrlen = 3;
                }
 
-               final AttributeParser parser = handlers.getParser(type);
+               final AttributeParser parser = this.handlers.getParser(type);
                if (parser == null) {
                        if (!flags[0]) {
                                throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
@@ -74,7 +75,7 @@ final class SimpleAttributeRegistry implements AttributeRegistry {
 
        @Override
        public byte[] serializeAttribute(final DataObject attribute) {
-               final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
+               final AttributeSerializer serializer = this.handlers.getSerializer(attribute.getImplementedInterface());
                if (serializer == null) {
                        return null;
                }
index d50dc5900c1fda499472456973fea99145e60876..71c33fcdb8c7447f854d1b7101bca800100562f9 100644 (file)
@@ -13,6 +13,7 @@ import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
 import org.opendaylight.protocol.concepts.HandlerRegistry;
+import org.opendaylight.protocol.util.Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 
@@ -22,17 +23,17 @@ final class SimpleCapabilityRegistry implements CapabilityRegistry {
        private final HandlerRegistry<DataContainer, CapabilityParser, CapabilitySerializer> handlers = new HandlerRegistry<>();
 
        AutoCloseable registerCapabilityParser(final int messageType, final CapabilityParser parser) {
-               Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
-               return handlers.registerParser(messageType, parser);
+               Preconditions.checkArgument(messageType >= 0 && messageType <= Util.UNSIGNED_BYTE_MAX_VALUE);
+               return this.handlers.registerParser(messageType, parser);
        }
 
        AutoCloseable registerCapabilitySerializer(final Class<? extends CParameters> paramClass, final CapabilitySerializer serializer) {
-               return handlers.registerSerializer(paramClass, serializer);
+               return this.handlers.registerSerializer(paramClass, serializer);
        }
 
        @Override
        public CParameters parseCapability(final int type, final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
-               final CapabilityParser parser = handlers.getParser(type);
+               final CapabilityParser parser = this.handlers.getParser(type);
                if (parser == null) {
                        return null;
                }
@@ -42,7 +43,7 @@ final class SimpleCapabilityRegistry implements CapabilityRegistry {
 
        @Override
        public byte[] serializeCapability(final CParameters capability) {
-               final CapabilitySerializer serializer = handlers.getSerializer(capability.getImplementedInterface());
+               final CapabilitySerializer serializer = this.handlers.getSerializer(capability.getImplementedInterface());
                if (serializer == null) {
                        return null;
                }
index 57567134323f0f9a7654e0911e715eeed371a53a..9c5e943187cb251982ad747d4976efd801937743 100644 (file)
@@ -13,6 +13,7 @@ import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
 import org.opendaylight.protocol.concepts.HandlerRegistry;
+import org.opendaylight.protocol.util.Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 
@@ -22,17 +23,17 @@ final class SimpleParameterRegistry implements ParameterRegistry {
        private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers = new HandlerRegistry<>();
 
        AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
-               Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
-               return handlers.registerParser(messageType, parser);
+               Preconditions.checkArgument(messageType >= 0 && messageType <= Util.UNSIGNED_BYTE_MAX_VALUE);
+               return this.handlers.registerParser(messageType, parser);
        }
 
        AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
-               return handlers.registerSerializer(paramClass, serializer);
+               return this.handlers.registerSerializer(paramClass, serializer);
        }
 
        @Override
        public BgpParameters parseParameter(final int parameterType, final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
-               final ParameterParser parser = handlers.getParser(parameterType);
+               final ParameterParser parser = this.handlers.getParser(parameterType);
                if (parser == null) {
                        return null;
                }
@@ -42,7 +43,7 @@ final class SimpleParameterRegistry implements ParameterRegistry {
 
        @Override
        public byte[] serializeParameter(final BgpParameters parameter) {
-               final ParameterSerializer serializer = handlers.getSerializer(parameter.getImplementedInterface());
+               final ParameterSerializer serializer = this.handlers.getSerializer(parameter.getImplementedInterface());
                if (serializer == null) {
                        return null;
                }
index 2c88c22d34045557913143f175b38445ac9ef2ba..b8ce1b81434cea47f557918c3d1d4bdabb2c98e2 100644 (file)
@@ -8,13 +8,15 @@
 package org.opendaylight.protocol.bgp.parser.spi.pojo;
 
 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
+import org.opendaylight.protocol.util.Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
 
 import com.google.common.base.Preconditions;
 
-final class SimpleSubsequentAddressFamilyRegistry extends AbstractFamilyRegistry<SubsequentAddressFamily, Integer> implements SubsequentAddressFamilyRegistry {
+final class SimpleSubsequentAddressFamilyRegistry extends AbstractFamilyRegistry<SubsequentAddressFamily, Integer> implements
+               SubsequentAddressFamilyRegistry {
        AutoCloseable registerSubsequentAddressFamily(final Class<? extends SubsequentAddressFamily> clazz, final int number) {
-               Preconditions.checkArgument(number >= 0 && number <= 255);
+               Preconditions.checkArgument(number >= 0 && number <= Util.UNSIGNED_BYTE_MAX_VALUE);
                return super.registerFamily(clazz, number);
        }
 
diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/UtilsTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/UtilsTest.java
new file mode 100644 (file)
index 0000000..fe5974f
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * 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.bgp.parser.spi;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpReachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv4NextHop;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv6NextHop;
+
+import com.google.common.primitives.UnsignedBytes;
+
+public class UtilsTest {
+
+       @Test
+       public void testCapabilityUtil() {
+               final byte[] result = new byte[] { 1, 2, 4, 8 };
+               assertArrayEquals(result, CapabilityUtil.formatCapability(1, new byte[] { 4, 8 }));
+       }
+
+       @Test
+       public void testMessageUtil() {
+               final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+                               UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+                               UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+                               UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+                               UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
+               assertArrayEquals(result, MessageUtil.formatMessage(3, new byte[] { 32, 5, 14, 21 }));
+       }
+
+       @Test
+       public void testNlriUtil() {
+               final MpReachNlriBuilder builder = new MpReachNlriBuilder();
+               final byte[] ipv4 = new byte[] { 42, 42, 42, 42 };
+               try {
+                       NlriUtil.parseNextHop(ipv4, builder);
+               } catch (final BGPParsingException e) {
+                       fail("This exception should not happen");
+               }
+               CNextHop hop = builder.getCNextHop();
+               assertEquals("42.42.42.42", ((CIpv4NextHop) hop).getIpv4NextHop().getGlobal().getValue());
+
+               final byte[] ipv6 = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01 };
+               try {
+                       NlriUtil.parseNextHop(ipv6, builder);
+               } catch (final BGPParsingException e) {
+                       fail("This exception should not happen");
+               }
+               hop = builder.getCNextHop();
+               assertEquals("2001:db8::1", ((CIpv6NextHop) hop).getIpv6NextHop().getGlobal().getValue());
+               assertNull(((CIpv6NextHop) hop).getIpv6NextHop().getLinkLocal());
+
+               final byte[] ipv6l = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
+                               01, (byte) 0xfe, (byte) 0x80, 00, 00, 00, 00, 00, 00, (byte) 0xc0, 01, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 00, 00 };
+               try {
+                       NlriUtil.parseNextHop(ipv6l, builder);
+               } catch (final BGPParsingException e) {
+                       fail("This exception should not happen");
+               }
+               hop = builder.getCNextHop();
+               assertEquals("2001:db8::1", ((CIpv6NextHop) hop).getIpv6NextHop().getGlobal().getValue());
+               assertEquals("fe80::c001:bff:fe7e:0", ((CIpv6NextHop) hop).getIpv6NextHop().getLinkLocal().getValue());
+
+               final byte[] wrong = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d };
+               try {
+                       NlriUtil.parseNextHop(wrong, builder);
+                       fail("Exception should happen");
+               } catch (final BGPParsingException e) {
+                       assertEquals("Cannot parse NEXT_HOP attribute. Wrong bytes length: 3", e.getMessage());
+               }
+       }
+
+       @Test
+       public void testParameterUtil() {
+               final byte[] result = new byte[] { 1, 2, 4, 8 };
+               assertArrayEquals(result, ParameterUtil.formatParameter(1, new byte[] { 4, 8 }));
+       }
+}
index 60560dc2f5b00a1bc8a3a4a78f91a09dad9e4589..3244d3c5e0a48d0604ddb93d52f82febcd4944c5 100644 (file)
@@ -27,6 +27,8 @@ import com.google.common.primitives.UnsignedBytes;
  */
 public final class Ipv4Util {
 
+       public static final int IP4_LENGTH = 4;
+
        public static Ipv4Address addressForBytes(final byte[] bytes) {
                try {
                        return new Ipv4Address(Inet4Address.getByAddress(bytes).getHostAddress());
@@ -51,8 +53,9 @@ public final class Ipv4Util {
        }
 
        public static List<Ipv4Prefix> prefixListForBytes(final byte[] bytes) {
-               if (bytes.length == 0)
+               if (bytes.length == 0) {
                        return Collections.emptyList();
+               }
 
                final List<Ipv4Prefix> list = Lists.newArrayList();
                int byteOffset = 0;
index 6eb959d8de32b944340df9631346cfe713000ff6..eea9d0aa48e238fee11acd095c17ea4537ea7f95 100644 (file)
@@ -19,6 +19,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
+import com.google.common.net.InetAddresses;
 import com.google.common.primitives.UnsignedBytes;
 
 /**
@@ -26,9 +27,11 @@ import com.google.common.primitives.UnsignedBytes;
  */
 public class Ipv6Util {
 
+       public static final int IPV6_LENGTH = 16;
+
        public static Ipv6Address addressForBytes(final byte[] bytes) {
                try {
-                       return new Ipv6Address(Inet6Address.getByAddress(bytes).toString());
+                       return new Ipv6Address(InetAddresses.toAddrString(Inet6Address.getByAddress(bytes)));
                } catch (final UnknownHostException e) {
                        throw new IllegalArgumentException(e.getMessage());
                }
@@ -50,8 +53,9 @@ public class Ipv6Util {
        }
 
        public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
-               if (bytes.length == 0)
+               if (bytes.length == 0) {
                        return Collections.emptyList();
+               }
 
                final List<Ipv6Prefix> list = Lists.newArrayList();
                int byteOffset = 0;
index b3a002b1844fbfe51ebc154b4926710ae23a361c..6386ffebc44e8c51c1903ed0a9c531088b214c44 100644 (file)
@@ -7,8 +7,10 @@
  */
 package org.opendaylight.protocol.util;
 
+
 public class Util {
 
        public static final int UNSIGNED_SHORT_MAX_VALUE = 65535;
 
+       public static final int UNSIGNED_BYTE_MAX_VALUE = 255;
 }