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));
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() {
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() {
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);
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);
}
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);
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;
}
}
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);
}
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;
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]);
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);
@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;
}
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;
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;
}
@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;
}
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;
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;
}
@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;
}
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);
}
--- /dev/null
+/*
+ * 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 }));
+ }
+}
*/
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());
}
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;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
+import com.google.common.net.InetAddresses;
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());
}
}
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;
*/
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;
}