import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
import org.opendaylight.protocol.framework.DeserializerException;
import org.opendaylight.protocol.framework.DocumentedException;
import org.opendaylight.protocol.util.ByteArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedBytes;
private final static Logger logger = LoggerFactory.getLogger(BGPMessageFactoryImpl.class);
- private final static int TYPE_FIELD_LENGTH = 1; // bytes
-
- @VisibleForTesting
- final static int LENGTH_FIELD_LENGTH = 2; // bytes
-
- @VisibleForTesting
- final static int MARKER_LENGTH = 16; // bytes
-
- @VisibleForTesting
- final static int COMMON_HEADER_LENGTH = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH + MARKER_LENGTH;
-
private final HandlerRegistry<Notification, MessageParser, MessageSerializer> handlers;
private BGPMessageFactoryImpl(final HandlerRegistry<Notification, MessageParser, MessageSerializer> handlers) {
}
final byte[] msgBody = serializer.serializeMessage(message);
- final byte[] retBytes = formatMessage(serializer.messageType(), msgBody);
+ final byte[] retBytes = MessageUtil.formatMessage(serializer.messageType(), msgBody);
return retBytes;
}
if (bytes == null) {
throw new IllegalArgumentException("Array of bytes is mandatory.");
}
- if (bytes.length < COMMON_HEADER_LENGTH) {
+ if (bytes.length < MessageUtil.COMMON_HEADER_LENGTH) {
throw new IllegalArgumentException("Too few bytes in passed array. Passed: " + bytes.length + ". Expected: >= "
- + COMMON_HEADER_LENGTH + ".");
+ + MessageUtil.COMMON_HEADER_LENGTH + ".");
}
/*
* byte array starts with message length
// Arrays.fill(ones, (byte)0xff);
// if (Arrays.equals(bytes, ones))
// throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
- final byte[] bs = ByteArray.cutBytes(bytes, MARKER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(bs, 0, LENGTH_FIELD_LENGTH));
- final int messageType = UnsignedBytes.toInt(bs[LENGTH_FIELD_LENGTH]);
+ final byte[] bs = ByteArray.cutBytes(bytes, MessageUtil.MARKER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(bs, 0, MessageUtil.LENGTH_FIELD_LENGTH));
+ final int messageType = UnsignedBytes.toInt(bs[MessageUtil.LENGTH_FIELD_LENGTH]);
- final byte[] msgBody = ByteArray.cutBytes(bs, LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH);
+ final byte[] msgBody = ByteArray.cutBytes(bs, MessageUtil.LENGTH_FIELD_LENGTH + MessageUtil.TYPE_FIELD_LENGTH);
- if (messageLength < COMMON_HEADER_LENGTH) {
+ if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
}
- if (msgBody.length != messageLength - COMMON_HEADER_LENGTH) {
+ if (msgBody.length != messageLength - MessageUtil.COMMON_HEADER_LENGTH) {
throw new DeserializerException("Size doesn't match size specified in header. Passed: " + msgBody.length + "; Expected: "
- + (messageLength - COMMON_HEADER_LENGTH) + ". ");
+ + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
}
logger.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
final Notification msg = parseBody(messageType, msgBody, messageLength);
if (msg == null) {
- throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[LENGTH_FIELD_LENGTH] });
+ throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[MessageUtil.LENGTH_FIELD_LENGTH] });
}
return Lists.newArrayList(msg);
logger.trace("Serialized BGP message {}.", Arrays.toString(ret));
return ret;
}
-
- /**
- * Serializes this BGP Message header to byte array.
- *
- * @return byte array representation of this header
- */
- private byte[] formatMessage(final int msgType, 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,
- retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH);
-
- retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(msgType);
- ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
-
- return retBytes;
- }
}
import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
import org.opendaylight.protocol.concepts.IGPMetric;
import org.opendaylight.protocol.concepts.IPv4;
import org.opendaylight.protocol.concepts.IPv4Prefix;
// FIXME: to be fixed in testing phase
public void testGetUpdateMessage1() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(0), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
@Ignore
// FIXME: to be fixed in testing phase
public void testGetUpdateMessage2() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(1), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
@Ignore
// FIXME: to be fixed in testing phase
public void testGetUpdateMessage3() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(2), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
@Ignore
// FIXME: to be fixed in testing phase
public void testGetUpdateMessage4() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(3), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
@Ignore
// FIXME: to be fixed in testing phase
public void testGetUpdateMessage5() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(4), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// attributes
*/
@Test
public void testEORIpv4() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(5), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
assertEquals(new UpdateBuilder().build(), message);
@Ignore
//FIXME: to be fixed in testing phase
public void testEORIpv6() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(6), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
@Ignore
//FIXME: to be fixed in testing phase
public void testEORLS() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(7), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(7), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes1.class).getMpReachNlri().getAfi();
@Ignore
// FIXME: to be fixed in testing phase
public void testBGPLink() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(8), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(8), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
@Ignore
// FIXME: to be fixed in testing phase
public void testBGPNode() throws Exception {
- final byte[] body = ByteArray.cutBytes(inputBytes.get(9), BGPMessageFactoryImpl.COMMON_HEADER_LENGTH);
- final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), BGPMessageFactoryImpl.MARKER_LENGTH,
- BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ final byte[] body = ByteArray.cutBytes(inputBytes.get(9), MessageUtil.COMMON_HEADER_LENGTH);
+ final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), MessageUtil.MARKER_LENGTH,
+ MessageUtil.LENGTH_FIELD_LENGTH));
final Update message = updateParser.parseMessage(body, messageLength);
// check fields
--- /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 java.util.Arrays;
+
+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 COMMON_HEADER_LENGTH = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH + MARKER_LENGTH;
+
+ private MessageUtil() {
+
+ }
+
+ /**
+ * Serializes this BGP Message header to byte array.
+ *
+ * @param type message type to be formatted
+ * @param body message body
+ *
+ * @return byte array representation of this header
+ */
+ 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,
+ retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH);
+
+ retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(type);
+ ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
+
+ return retBytes;
+ }
+}