<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>bgp-parser-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>bgp-parser-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
--- /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.impl;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import org.opendaylight.protocol.concepts.AbstractRegistration;
+
+import com.google.common.base.Preconditions;
+
+@ThreadSafe
+public abstract class AbstractRegistryImpl<CLASS, PARSER, SERIALIZER> {
+ private final Map<Class<? extends CLASS>, SERIALIZER> serializers = new ConcurrentHashMap<>();
+ private final Map<Integer, PARSER> parsers = new ConcurrentHashMap<>();
+
+ protected AutoCloseable registerParser(final Integer type, final PARSER parser) {
+ synchronized (parsers) {
+ Preconditions.checkArgument(!parsers.containsKey(type), "Type %s already registered", type);
+ parsers.put(type, parser);
+
+ return new AbstractRegistration() {
+ @Override
+ protected void removeRegistration() {
+ synchronized (parsers) {
+ parsers.remove(type);
+ }
+ }
+ };
+ }
+ }
+
+ protected PARSER getParser(final int messageType) {
+ return parsers.get(messageType);
+ }
+
+ protected AutoCloseable registerSerializer(final Class<? extends CLASS> clazz, final SERIALIZER serializer) {
+ synchronized (serializers) {
+ Preconditions.checkArgument(!serializers.containsKey(clazz), "Message class %s already registered", clazz);
+ serializers.put(clazz, serializer);
+
+ return new AbstractRegistration() {
+ @Override
+ protected void removeRegistration() {
+ synchronized (serializers) {
+ serializers.remove(clazz);
+ }
+ }
+ };
+ }
+ }
+
+ protected SERIALIZER getSerializer(final CLASS obj) {
+ final Class<?> c = obj.getClass();
+
+ for (Map.Entry<Class<? extends CLASS>, SERIALIZER> e : serializers.entrySet()) {
+ if (e.getKey().isAssignableFrom(c)) {
+ return e.getValue();
+ }
+ }
+
+ return null;
+ }
+}
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
-import org.opendaylight.protocol.bgp.parser.impl.message.BGPNotificationMessageParser;
-import org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser;
-import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
import org.opendaylight.protocol.framework.DeserializerException;
import org.opendaylight.protocol.framework.DocumentedException;
import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Open;
import org.opendaylight.yangtools.yang.binding.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedBytes;
* The byte array
*/
public final class BGPMessageFactoryImpl implements BGPMessageFactory {
+ public static final BGPMessageFactory INSTANCE = new BGPMessageFactoryImpl(MessageRegistryImpl.INSTANCE);
private final static Logger logger = LoggerFactory.getLogger(BGPMessageFactoryImpl.class);
- final static int LENGTH_FIELD_LENGTH = 2; // bytes
+ public final static int LENGTH_FIELD_LENGTH = 2; // bytes
private final static int TYPE_FIELD_LENGTH = 1; // bytes
public final static int COMMON_HEADER_LENGTH = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH + MARKER_LENGTH;
+ private final MessageRegistry registry;
+
+ private BGPMessageFactoryImpl(final MessageRegistry registry) {
+ this.registry = Preconditions.checkNotNull(registry);
+ }
+
/*
* (non-Javadoc)
* @see org.opendaylight.protocol.bgp.parser.BGPMessageParser#parse(byte[])
logger.debug("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(msgBody));
- final Notification msg;
-
- switch (messageType) {
- case 1:
- msg = BGPOpenMessageParser.parse(msgBody);
- logger.debug("Received and parsed Open Message: {}", msg);
- break;
- case 2:
- msg = BGPUpdateMessageParser.parse(msgBody, messageLength);
- logger.debug("Received and parsed Update Message: {}", msg);
- break;
- case 3:
- msg = BGPNotificationMessageParser.parse(msgBody);
- logger.debug("Received and parsed Notification Message: {}", msg);
- break;
- case 4:
- msg = new KeepaliveBuilder().build();
- if (messageLength != COMMON_HEADER_LENGTH) {
- throw new BGPDocumentedException("Message length field not within valid range.", BGPError.BAD_MSG_LENGTH, ByteArray.subByte(
- bs, 0, LENGTH_FIELD_LENGTH));
- }
- break;
- default:
+ final MessageParser parser = registry.getMessageParser(messageType);
+ if (parser == null) {
throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { bs[LENGTH_FIELD_LENGTH] });
}
+ final Notification msg = parser.parseMessage(msgBody, messageLength);
+
return Lists.newArrayList(msg);
}
logger.trace("Serializing {}", msg);
- byte[] msgBody = null;
- int msgType = 0;
-
- /*
- * Update message is not supported
- */
- if (msg instanceof Open) {
- msgType = 1;
- msgBody = BGPOpenMessageParser.put((Open) msg);
- } else if (msg instanceof Notify) {
- msgType = 3;
- msgBody = BGPNotificationMessageParser.put((Notify) msg);
- } else if (msg instanceof Keepalive) {
- msgType = 4;
- msgBody = new byte[0];
- } else {
+ final MessageSerializer serializer = registry.getMessageSerializer(msg);
+ if (serializer == null) {
throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + msg.getClass());
}
- final byte[] headerBytes = headerToBytes(msgBody.length + COMMON_HEADER_LENGTH, msgType);
+ final byte[] msgBody = serializer.serializeMessage(msg);
+ final byte[] headerBytes = headerToBytes(msgBody.length + COMMON_HEADER_LENGTH, serializer.messageType());
final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
ByteArray.copyWhole(headerBytes, retBytes, 0);
--- /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.impl;
+
+import org.opendaylight.protocol.bgp.parser.impl.message.BGPKeepAliveMessageParser;
+import org.opendaylight.protocol.bgp.parser.impl.message.BGPNotificationMessageParser;
+import org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser;
+import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Open;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+import com.google.common.base.Preconditions;
+
+public final class MessageRegistryImpl extends AbstractRegistryImpl<Notification, MessageParser, MessageSerializer>
+implements MessageRegistry {
+ public static final MessageRegistry INSTANCE;
+
+ static {
+ final MessageRegistry reg = new MessageRegistryImpl();
+
+ reg.registerMessageParser(1, BGPOpenMessageParser.PARSER);
+ reg.registerMessageSerializer(Open.class, BGPOpenMessageParser.SERIALIZER);
+ reg.registerMessageParser(2, BGPUpdateMessageParser.PARSER);
+ // Serialization of Update message is not supported
+ reg.registerMessageParser(3, BGPNotificationMessageParser.PARSER);
+ reg.registerMessageSerializer(Notify.class, BGPNotificationMessageParser.SERIALIZER);
+ reg.registerMessageParser(4, BGPKeepAliveMessageParser.PARSER);
+ reg.registerMessageSerializer(Keepalive.class, BGPKeepAliveMessageParser.SERIALIZER);
+
+ INSTANCE = reg;
+ }
+
+ @Override
+ public synchronized AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
+ Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
+ return super.registerParser(messageType, parser);
+ }
+
+ @Override
+ public MessageParser getMessageParser(final int messageType) {
+ return super.getParser(messageType);
+ }
+
+ @Override
+ public AutoCloseable registerMessageSerializer(final Class<? extends Notification> msgClass, final MessageSerializer serializer) {
+ return super.registerSerializer(msgClass, serializer);
+ }
+
+ @Override
+ public MessageSerializer getMessageSerializer(final Notification message) {
+ return super.getSerializer(message);
+ }
+}
--- /dev/null
+package org.opendaylight.protocol.bgp.parser.impl.message;
+
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPError;
+import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
+import org.opendaylight.yangtools.yang.binding.Notification;
+
+import com.google.common.base.Preconditions;
+
+public class BGPKeepAliveMessageParser implements MessageParser, MessageSerializer {
+ private static final Keepalive msg = new KeepaliveBuilder().build();
+ private static final byte[] bytes = new byte[0];
+
+ public static final MessageParser PARSER;
+ public static final MessageSerializer SERIALIZER;
+
+ static {
+ final BGPKeepAliveMessageParser p = new BGPKeepAliveMessageParser();
+ PARSER = p;
+ SERIALIZER = p;
+ }
+
+ private BGPKeepAliveMessageParser() {
+
+ }
+
+ @Override
+ public Keepalive parseMessage(final byte[] bytes, final int messageLength) throws BGPDocumentedException {
+ if (messageLength != BGPMessageFactoryImpl.COMMON_HEADER_LENGTH) {
+ throw new BGPDocumentedException("Message length field not within valid range.",
+ BGPError.BAD_MSG_LENGTH, ByteArray.subByte(ByteArray.intToBytes(messageLength),
+ 4 - BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH,
+ BGPMessageFactoryImpl.LENGTH_FIELD_LENGTH));
+ }
+
+ return msg;
+ }
+
+ @Override
+ public int messageType() {
+ return 4;
+ }
+
+ @Override
+ public byte[] serializeMessage(final Notification message) {
+ Preconditions.checkArgument(message instanceof Keepalive);
+ return bytes;
+ }
+}
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPError;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.NotifyBuilder;
+import org.opendaylight.yangtools.yang.binding.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Parser for BGPNotification message.
*/
-public final class BGPNotificationMessageParser {
+public final class BGPNotificationMessageParser implements MessageParser, MessageSerializer {
+ public static final MessageParser PARSER;
+ public static final MessageSerializer SERIALIZER;
+
+ static {
+ final BGPNotificationMessageParser p = new BGPNotificationMessageParser();
+ PARSER = p;
+ SERIALIZER = p;
+ }
private static final Logger logger = LoggerFactory.getLogger(BGPNotificationMessageParser.class);
private static final int ERROR_SIZE = 2; // bytes
+ private BGPNotificationMessageParser() {
+
+ }
+
/**
* Serializes BGP Notification message.
*
* @param msg to be serialized
* @return BGP Notification message converted to byte array
*/
- public static byte[] put(final Notify msg) {
- if (msg == null)
+ @Override
+ public byte[] serializeMessage(final Notification msg) {
+ if (msg == null) {
throw new IllegalArgumentException("BGP Notification message cannot be null");
- logger.trace("Started serializing Notification message: {}", msg);
+ }
+
+ final Notify ntf = (Notify)msg;
+ logger.trace("Started serializing Notification message: {}", ntf);
- final byte[] msgBody = (msg.getData() == null) ? new byte[ERROR_SIZE] : new byte[ERROR_SIZE + msg.getData().length];
+ final byte[] msgBody = (ntf.getData() == null) ? new byte[ERROR_SIZE] : new byte[ERROR_SIZE + ntf.getData().length];
- msgBody[0] = ByteArray.intToBytes(msg.getErrorCode())[Integer.SIZE / Byte.SIZE - 1];
+ msgBody[0] = ByteArray.intToBytes(ntf.getErrorCode())[Integer.SIZE / Byte.SIZE - 1];
- msgBody[1] = ByteArray.intToBytes(msg.getErrorSubcode())[Integer.SIZE / Byte.SIZE - 1];
+ msgBody[1] = ByteArray.intToBytes(ntf.getErrorSubcode())[Integer.SIZE / Byte.SIZE - 1];
- if (msg.getData() != null)
- System.arraycopy(msg.getData(), 0, msgBody, ERROR_SIZE, msg.getData().length);
+ if (ntf.getData() != null) {
+ System.arraycopy(ntf.getData(), 0, msgBody, ERROR_SIZE, ntf.getData().length);
+ }
logger.trace("Notification message serialized to: {}", Arrays.toString(msgBody));
return msgBody;
}
* @return BGPNotification message
* @throws BGPDocumentedException
*/
- public static Notify parse(final byte[] bytes) throws BGPDocumentedException {
- if (bytes == null || bytes.length == 0)
+ @Override
+ public Notify parseMessage(final byte[] bytes, final int messageLength) throws BGPDocumentedException {
+ if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("Byte array cannot be null or empty.");
+ }
logger.trace("Started parsing of notification message: {}", Arrays.toString(bytes));
- if (bytes.length < ERROR_SIZE)
+ if (bytes.length < ERROR_SIZE) {
throw new BGPDocumentedException("Notification message too small.", BGPError.BAD_MSG_LENGTH, ByteArray.intToBytes(bytes.length));
+ }
final int errorCode = UnsignedBytes.toInt(bytes[0]);
final int errorSubcode = UnsignedBytes.toInt(bytes[1]);
logger.trace("Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode),
Arrays.toString(data));
final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode).setErrorSubcode((short) errorSubcode);
- if (data != null)
+ if (data != null) {
builder.setData(data);
+ }
return builder.build();
}
+
+ @Override
+ public int messageType() {
+ return 3;
+ }
}
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.bgp.parser.impl.message.open.BGPParameterParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Open;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.OpenBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
+import org.opendaylight.yangtools.yang.binding.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Parser for BGP Open message.
*/
-public final class BGPOpenMessageParser {
+public final class BGPOpenMessageParser implements MessageParser, MessageSerializer {
+ public static final MessageParser PARSER;
+ public static final MessageSerializer SERIALIZER;
+
+ static {
+ final BGPOpenMessageParser p = new BGPOpenMessageParser();
+ PARSER = p;
+ SERIALIZER = p;
+ }
private static final Logger logger = LoggerFactory.getLogger(BGPOpenMessageParser.class);
* @param msg BGP Open message to be serialized.
* @return BGP Open message converted to byte array
*/
- public static byte[] put(final Open msg) {
- if (msg == null)
+ @Override
+ public byte[] serializeMessage(final Notification msg) {
+ if (msg == null) {
throw new IllegalArgumentException("BGPOpen message cannot be null");
+ }
logger.trace("Started serializing open message: {}", msg);
+ final Open open = (Open) msg;
final Map<byte[], Integer> optParams = Maps.newHashMap();
int optParamsLength = 0;
- if (msg.getBgpParameters() != null) {
- for (final BgpParameters param : msg.getBgpParameters()) {
+ if (open.getBgpParameters() != null) {
+ for (final BgpParameters param : open.getBgpParameters()) {
final byte[] p = BGPParameterParser.put(param);
optParams.put(p, p.length);
optParamsLength += p.length;
}
}
- final byte[] msgBody = (msg.getBgpParameters() == null || msg.getBgpParameters().isEmpty()) ? new byte[MIN_MSG_LENGTH]
+ final byte[] msgBody = (open.getBgpParameters() == null || open.getBgpParameters().isEmpty()) ? new byte[MIN_MSG_LENGTH]
: new byte[MIN_MSG_LENGTH + optParamsLength];
int offset = 0;
offset += VERSION_SIZE;
// When our AS number does not fit into two bytes, we report it as AS_TRANS
- int openAS = msg.getMyAsNumber();
- if (openAS > 65535)
+ int openAS = open.getMyAsNumber();
+ if (openAS > 65535) {
openAS = 2345;
+ }
System.arraycopy(ByteArray.longToBytes(openAS), 6, msgBody, offset, AS_SIZE);
offset += AS_SIZE;
- System.arraycopy(ByteArray.intToBytes(msg.getHoldTimer()), 2, msgBody, offset, HOLD_TIME_SIZE);
+ System.arraycopy(ByteArray.intToBytes(open.getHoldTimer()), 2, msgBody, offset, HOLD_TIME_SIZE);
offset += HOLD_TIME_SIZE;
- System.arraycopy(Ipv4Util.bytesForAddress(msg.getBgpIdentifier()), 0, msgBody, offset, BGP_ID_SIZE);
+ System.arraycopy(Ipv4Util.bytesForAddress(open.getBgpIdentifier()), 0, msgBody, offset, BGP_ID_SIZE);
offset += BGP_ID_SIZE;
msgBody[offset] = ByteArray.intToBytes(optParamsLength)[Integer.SIZE / Byte.SIZE - 1];
* @return BGP Open Message
* @throws BGPDocumentedException if the parsing was unsuccessful
*/
- public static Open parse(final byte[] bytes) throws BGPDocumentedException {
- if (bytes == null || bytes.length == 0)
+ @Override
+ public Open parseMessage(final byte[] bytes, final int messageLength) throws BGPDocumentedException {
+ if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("Byte array cannot be null or empty.");
+ }
logger.trace("Started parsing of open message: {}", Arrays.toString(bytes));
- if (bytes.length < MIN_MSG_LENGTH)
+ if (bytes.length < MIN_MSG_LENGTH) {
throw new BGPDocumentedException("Open message too small.", BGPError.BAD_MSG_LENGTH, ByteArray.intToBytes(bytes.length));
- if (UnsignedBytes.toInt(bytes[0]) != BGP_VERSION)
+ }
+ if (UnsignedBytes.toInt(bytes[0]) != BGP_VERSION) {
throw new BGPDocumentedException("BGP Protocol version " + UnsignedBytes.toInt(bytes[0]) + " not supported.", BGPError.VERSION_NOT_SUPPORTED, ByteArray.subByte(
ByteArray.intToBytes(BGP_VERSION), 2, 2));
+ }
int offset = VERSION_SIZE;
final AsNumber as = new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, offset, AS_SIZE)));
final short holdTime = ByteArray.bytesToShort(ByteArray.subByte(bytes, offset, HOLD_TIME_SIZE));
offset += HOLD_TIME_SIZE;
- if (holdTime == 1 || holdTime == 2)
+ if (holdTime == 1 || holdTime == 2) {
throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
+ }
Ipv4Address bgpId = null;
try {
return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer((int) holdTime).setBgpIdentifier(bgpId).setBgpParameters(
optParams).build();
}
+
+ @Override
+ public int messageType() {
+ return 1;
+ }
}
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
import org.opendaylight.protocol.bgp.parser.impl.message.update.PathAttributeParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
* @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
*
*/
-public class BGPUpdateMessageParser {
+public class BGPUpdateMessageParser implements MessageParser {
+ public static final BGPUpdateMessageParser PARSER = new BGPUpdateMessageParser();
private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
// Constructors -------------------------------------------------------
-
- public BGPUpdateMessageParser() {
+ private BGPUpdateMessageParser() {
}
// Getters & setters --------------------------------------------------
- public static Update parse(final byte[] bytes, final int msgLength) throws BGPDocumentedException {
+ @Override
+ public Update parseMessage(final byte[] bytes, final int msgLength) throws BGPDocumentedException {
if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("Byte array cannot be null or empty.");
}
--- /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.impl.message.open;
+
+import org.opendaylight.protocol.bgp.parser.impl.AbstractRegistryImpl;
+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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
+
+import com.google.common.base.Preconditions;
+
+public final class CapabilityRegistryImpl extends AbstractRegistryImpl<CParameters, CapabilityParser, CapabilitySerializer> implements CapabilityRegistry {
+ public static final CapabilityRegistry INSTANCE;
+
+ static {
+ final CapabilityRegistry reg = new CapabilityRegistryImpl();
+
+ // FIXME: fix registry
+
+ INSTANCE = reg;
+ }
+
+ @Override
+ public AutoCloseable registerCapabilityParser(final int messageType, final CapabilityParser parser) {
+ Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
+ return super.registerParser(messageType, parser);
+ }
+
+ @Override
+ public CapabilityParser getCapabilityParser(final int messageType) {
+ return super.getParser(messageType);
+ }
+
+ @Override
+ public AutoCloseable registerCapabilitySerializer(final Class<? extends CParameters> paramClass, final CapabilitySerializer serializer) {
+ return super.registerSerializer(paramClass, serializer);
+ }
+
+ @Override
+ public CapabilitySerializer getCapabilitySerializer(final CParameters capability) {
+ return super.getSerializer(capability);
+ }
+}
--- /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.impl.message.open;
+
+import org.opendaylight.protocol.bgp.parser.impl.AbstractRegistryImpl;
+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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
+
+import com.google.common.base.Preconditions;
+
+public final class ParameterRegistryImpl extends AbstractRegistryImpl<BgpParameters, ParameterParser, ParameterSerializer> implements ParameterRegistry {
+ public static final ParameterRegistry INSTANCE;
+
+ static {
+ final ParameterRegistry reg = new ParameterRegistryImpl();
+
+ // FIXME: fix registry
+
+ INSTANCE = reg;
+ }
+
+ @Override
+ public AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
+ Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
+ return super.registerParser(messageType, parser);
+ }
+
+ @Override
+ public ParameterParser getParameterParser(final int messageType) {
+ return super.getParser(messageType);
+ }
+
+ @Override
+ public AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
+ return super.registerSerializer(paramClass, serializer);
+ }
+
+ @Override
+ public ParameterSerializer getParameterSerializer(final BgpParameters message) {
+ return super.getSerializer(message);
+ }
+}
--- /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.impl.message.update;
+
+import org.opendaylight.protocol.bgp.parser.impl.AbstractRegistryImpl;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+
+import com.google.common.base.Preconditions;
+
+public final class AttributeRegistryImpl extends AbstractRegistryImpl<DataObject, AttributeParser, AttributeSerializer>
+implements AttributeRegistry {
+ public static final AttributeRegistry INSTANCE;
+
+ static {
+ final AttributeRegistry reg = new AttributeRegistryImpl();
+
+ // FIXME: fix registry
+
+ INSTANCE = reg;
+ }
+
+ @Override
+ public AutoCloseable registerAttributeParser(final int messageType, final AttributeParser parser) {
+ Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
+ return super.registerParser(messageType, parser);
+ }
+
+ @Override
+ public AttributeParser getAttributeParser(final int messageType) {
+ return super.getParser(messageType);
+ }
+
+ @Override
+ public AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
+ return super.registerSerializer(paramClass, serializer);
+ }
+
+ @Override
+ public AttributeSerializer getAttributeSerializer(final DataObject attribute) {
+ return super.getSerializer(attribute);
+ }
+}
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
+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.concepts.IGPMetric;
private static int MAX_SIZE = 300;
+ private final BGPUpdateMessageParser updateParser = BGPUpdateMessageParser.PARSER;
+
@BeforeClass
public static void setUp() 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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// check fields
// final Set<IPv4Prefix> nlri = Sets.newHashSet(pref1, pref2, pref3);
// assertEquals(nlri, ret.getBgpUpdateMessageBuilder().getNlri());
-// final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
-// final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(asPath, comms, Collections.<ExtendedCommunity> emptySet()), nextHop);
+ // final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
+ // final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(asPath, comms, Collections.<ExtendedCommunity> emptySet()), nextHop);
// check API message
-// final Set<BGPObject> addedObjects = Sets.newHashSet();
-//
-// final BGPRoute route1 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.2.0/24"), state, routeState);
-//
-// addedObjects.add(route1);
-//
-// final BGPRoute route2 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.1.0/24"), state, routeState);
-//
-// addedObjects.add(route2);
-//
-// final BGPRoute route3 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.0.0/24"), state, routeState);
-//
-// addedObjects.add(route3);
+ // final Set<BGPObject> addedObjects = Sets.newHashSet();
+ //
+ // final BGPRoute route1 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.2.0/24"), state, routeState);
+ //
+ // addedObjects.add(route1);
+ //
+ // final BGPRoute route2 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.1.0/24"), state, routeState);
+ //
+ // addedObjects.add(route2);
+ //
+ // final BGPRoute route3 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.0.0/24"), state, routeState);
+ //
+ // addedObjects.add(route3);
}
/*
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// check fields
assertNull(message.getWithdrawnRoutes());
// TypeCode.CLUSTER_LIST, true, false, false, false, clusters);
// assertEquals(clusterAttr, attrs.get(4));
-// final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
-// final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
+ // final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
+ // final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
// check API message
-// final Set<BGPObject> addedObjects = Sets.newHashSet();
-//
-// final BGPRoute route1 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1:2::/64"), state, routeState);
-//
-// addedObjects.add(route1);
-//
-// final BGPRoute route2 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1:1::/64"), state, routeState);
-//
-// addedObjects.add(route2);
-//
-// final BGPRoute route3 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1::/64"), state, routeState);
-//
-// addedObjects.add(route3);
+ // final Set<BGPObject> addedObjects = Sets.newHashSet();
+ //
+ // final BGPRoute route1 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1:2::/64"), state, routeState);
+ //
+ // addedObjects.add(route1);
+ //
+ // final BGPRoute route2 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1:1::/64"), state, routeState);
+ //
+ // addedObjects.add(route2);
+ //
+ // final BGPRoute route3 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1::/64"), state, routeState);
+ //
+ // addedObjects.add(route3);
}
/*
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
-
+ final Update message = updateParser.parseMessage(body, messageLength);
+
// check fields
assertNull(message.getWithdrawnRoutes());
//
// final Set<IPv4Prefix> nlri = Sets.newHashSet(pref1);
// assertEquals(nlri, ret.getBgpUpdateMessageBuilder().getNlri());
-//
-// final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Incomplete, aggregator);
-// final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
+ //
+ // final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Incomplete, aggregator);
+ // final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
// check API message
-// final Set<BGPObject> addedObjects = Sets.newHashSet();
-//
-// final BGPRoute route1 = new BGPIPv4RouteImpl(pref1, state, routeState);
-//
-// addedObjects.add(route1);
+ // final Set<BGPObject> addedObjects = Sets.newHashSet();
+ //
+ // final BGPRoute route1 = new BGPIPv4RouteImpl(pref1, state, routeState);
+ //
+ // addedObjects.add(route1);
}
/*
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// check fields
// final Set<IPv4Prefix> nlri = Sets.newHashSet(pref1, pref2, pref3);
// assertEquals(nlri, ret.getBgpUpdateMessageBuilder().getNlri());
-// final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Egp, null);
-// final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(Collections.<AsPathSegment> emptyList(), Collections.<Community> emptySet(), comms), nextHop);
+ // final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Egp, null);
+ // final NetworkRouteState routeState = new NetworkRouteState(new NetworkObjectState(Collections.<AsPathSegment> emptyList(), Collections.<Community> emptySet(), comms), nextHop);
// check API message
-// final Set<BGPObject> addedObjects = Sets.newHashSet();
-//
-// final BGPRoute route1 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.3.0/24"), state, routeState);
-//
-// addedObjects.add(route1);
-//
-// final BGPRoute route2 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.2.0/24"), state, routeState);
-//
-// addedObjects.add(route2);
-//
-// final BGPRoute route3 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.1.0/24"), state, routeState);
-//
-// addedObjects.add(route3);
+ // final Set<BGPObject> addedObjects = Sets.newHashSet();
+ //
+ // final BGPRoute route1 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.3.0/24"), state, routeState);
+ //
+ // addedObjects.add(route1);
+ //
+ // final BGPRoute route2 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.2.0/24"), state, routeState);
+ //
+ // addedObjects.add(route2);
+ //
+ // final BGPRoute route3 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.1.0/24"), state, routeState);
+ //
+ // addedObjects.add(route3);
}
/*
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// attributes
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
assertEquals(new UpdateBuilder().build(), message);
}
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// check fields
-
+
Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes1.class).getMpReachNlri().getAfi();
SubsequentAddressFamily safi = message.getPathAttributes().getAugmentation(PathAttributes1.class).getMpReachNlri().getSafi().newInstance();
-
+
assertEquals(Ipv6AddressFamily.class, afi);
assertEquals(UnicastSubsequentAddressFamily.INSTANCE, safi);
}
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes1.class).getMpReachNlri().getAfi();
SubsequentAddressFamily safi = message.getPathAttributes().getAugmentation(PathAttributes1.class).getMpReachNlri().getSafi().newInstance();
-
+
assertEquals(LinkstateAddressFamily.class, afi);
assertEquals(LinkstateSubsequentAddressFamily.INSTANCE, safi);
}
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// check fields
assertNull(message.getWithdrawnRoutes());
// network object state
-// final NetworkObjectState objState = new NetworkObjectState(Collections.<AsPathSegment> emptyList(), Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
-// final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
+ // final NetworkObjectState objState = new NetworkObjectState(Collections.<AsPathSegment> emptyList(), Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
+ // final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
// network link state
final DefaultingTypesafeContainer<Metric<?>> container = new DefaultingTypesafeContainer<Metric<?>>();
container.setDefaultEntry(new IGPMetric(1));
//final NetworkLinkState linkState = new NetworkLinkState(objState, container, null, LinkProtectionType.UNPROTECTED, null, null, null);
-// final NodeIdentifierFactory f100 = new NodeIdentifierFactory(new AsNumber((long) 100), new DomainIdentifier(new byte[] { 25, 25,
-// 25, 1 }), new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
-//
-// final NodeIdentifier nodeid1 = f100.identifierForRouter(new OSPFv3LANIdentifier(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }), new OSPFInterfaceIdentifier(new byte[] {
-// 0x0b, 0x0b, 0x0b, 0x03 })));
-// final NodeIdentifier nodeid2 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }));
-//
-// final NodeIdentifier nodeid3 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 1, 1, 1, 2 }));
+ // final NodeIdentifierFactory f100 = new NodeIdentifierFactory(new AsNumber((long) 100), new DomainIdentifier(new byte[] { 25, 25,
+ // 25, 1 }), new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
+ //
+ // final NodeIdentifier nodeid1 = f100.identifierForRouter(new OSPFv3LANIdentifier(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }), new OSPFInterfaceIdentifier(new byte[] {
+ // 0x0b, 0x0b, 0x0b, 0x03 })));
+ // final NodeIdentifier nodeid2 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }));
+ //
+ // final NodeIdentifier nodeid3 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 1, 1, 1, 2 }));
// check API message
-// final LinkIdentifier linkId1 = new LinkIdentifier(null, new LinkAnchor(nodeid1, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.3"))), new LinkAnchor(nodeid2, null));
-// final LinkIdentifier linkId2 = new LinkIdentifier(null, new LinkAnchor(nodeid1, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.1"))), new LinkAnchor(nodeid3, null));
-// final LinkIdentifier linkId3 = new LinkIdentifier(null, new LinkAnchor(nodeid3, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.1"))), new LinkAnchor(nodeid1, null));
-//
-// final BGPLink link1 = new BGPLinkImpl(state, linkId1, linkState);
-// final BGPLink link2 = new BGPLinkImpl(state, linkId2, linkState);
-// final BGPLink link3 = new BGPLinkImpl(state, linkId3, linkState);
-//
-// final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(Sets.newHashSet((BGPObject) link1, (BGPObject) link2,
-// (BGPObject) link3), Collections.<Identifier> emptySet());
-//
-// assertEquals(expectedMessage, message);
+ // final LinkIdentifier linkId1 = new LinkIdentifier(null, new LinkAnchor(nodeid1, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.3"))), new LinkAnchor(nodeid2, null));
+ // final LinkIdentifier linkId2 = new LinkIdentifier(null, new LinkAnchor(nodeid1, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.1"))), new LinkAnchor(nodeid3, null));
+ // final LinkIdentifier linkId3 = new LinkIdentifier(null, new LinkAnchor(nodeid3, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.1"))), new LinkAnchor(nodeid1, null));
+ //
+ // final BGPLink link1 = new BGPLinkImpl(state, linkId1, linkState);
+ // final BGPLink link2 = new BGPLinkImpl(state, linkId2, linkState);
+ // final BGPLink link3 = new BGPLinkImpl(state, linkId3, linkState);
+ //
+ // final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(Sets.newHashSet((BGPObject) link1, (BGPObject) link2,
+ // (BGPObject) link3), Collections.<Identifier> emptySet());
+ //
+ // assertEquals(expectedMessage, message);
}
/*
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 Update message = BGPUpdateMessageParser.parse(body, messageLength);
+ final Update message = updateParser.parseMessage(body, messageLength);
// check fields
assertNull(message.getWithdrawnRoutes());
// network object state
-// final NetworkObjectState objState = new NetworkObjectState(Collections.<AsPathSegment> emptyList(), Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
-// final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
-// final NetworkNodeState nstate = new NetworkNodeState(objState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), null);
+ // final NetworkObjectState objState = new NetworkObjectState(Collections.<AsPathSegment> emptyList(), Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
+ // final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
+ // final NetworkNodeState nstate = new NetworkNodeState(objState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), null);
// network link state
-// final NodeIdentifierFactory f100 = new NodeIdentifierFactory(new AsNumber((long) 100), new DomainIdentifier(new byte[] { 25, 25,
-// 25, 1 }), new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
-//
-// final NodeIdentifier nodeid1 = f100.identifierForRouter(new OSPFv3LANIdentifier(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }), new OSPFInterfaceIdentifier(new byte[] {
-// 0x0b, 0x0b, 0x0b, 0x03 })));
-// final NodeIdentifier nodeid2 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }));
-//
-// final NodeIdentifier nodeid3 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 1, 1, 1, 2 }));
+ // final NodeIdentifierFactory f100 = new NodeIdentifierFactory(new AsNumber((long) 100), new DomainIdentifier(new byte[] { 25, 25,
+ // 25, 1 }), new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
+ //
+ // final NodeIdentifier nodeid1 = f100.identifierForRouter(new OSPFv3LANIdentifier(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }), new OSPFInterfaceIdentifier(new byte[] {
+ // 0x0b, 0x0b, 0x0b, 0x03 })));
+ // final NodeIdentifier nodeid2 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }));
+ //
+ // final NodeIdentifier nodeid3 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 1, 1, 1, 2 }));
// check API message
-// final BGPNode node1 = new BGPNodeImpl(state, nodeid1, nstate);
-// final BGPNode node2 = new BGPNodeImpl(state, nodeid2, nstate);
-// final BGPNode node3 = new BGPNodeImpl(state, nodeid3, nstate);
-//
-// final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(Sets.newHashSet((BGPObject) node1, (BGPObject) node2,
-// (BGPObject) node3), Collections.<Identifier> emptySet());
-//
-// assertEquals(expectedMessage, message);
+ // final BGPNode node1 = new BGPNodeImpl(state, nodeid1, nstate);
+ // final BGPNode node2 = new BGPNodeImpl(state, nodeid2, nstate);
+ // final BGPNode node3 = new BGPNodeImpl(state, nodeid3, nstate);
+ //
+ // final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(Sets.newHashSet((BGPObject) node1, (BGPObject) node2,
+ // (BGPObject) node3), Collections.<Identifier> emptySet());
+ //
+ // assertEquals(expectedMessage, message);
}
/*
*/
@Test
public void testOpenMessage() throws Exception {
- final BGPMessageFactoryImpl msgFactory = new BGPMessageFactoryImpl();
+ final BGPMessageFactory msgFactory = BGPMessageFactoryImpl.INSTANCE;
final Open open = (Open) msgFactory.parse(inputBytes.get(13)).get(0);
final Set<BgpTableType> types = Sets.newHashSet();
for (final BgpParameters param : open.getBgpParameters()) {
import org.junit.Test;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
import org.opendaylight.protocol.framework.DeserializerException;
import com.google.common.collect.Maps;
public class ComplementaryTest {
-
+
@Test
public void testBGPParameter() {
@Test
public void testBGPHeaderParser() throws IOException {
- final BGPMessageFactoryImpl h = new BGPMessageFactoryImpl();
+ final BGPMessageFactory h = BGPMessageFactoryImpl.INSTANCE;
try {
h.parse(new byte[] { (byte) 0, (byte) 0 });
fail("Exception should have occured.");
@Test
public void testMessageParser() throws IOException {
- final BGPMessageFactoryImpl parser = new BGPMessageFactoryImpl();
+ final BGPMessageFactory parser = BGPMessageFactoryImpl.INSTANCE;
String ex = "";
try {
parser.put(null);
public AttributeParser getAttributeParser(int attributeType);
public AutoCloseable registerAttributeSerializer(Class<? extends DataObject> attrClass, AttributeSerializer serializer);
- public AttributeSerializer getAttributeSerializer(DataObject object);
+ public AttributeSerializer getAttributeSerializer(DataObject attribute);
}
public CapabilityParser getCapabilityParser(int capabilityType);
public AutoCloseable registerCapabilitySerializer(Class<? extends CParameters> capabilityClass, CapabilitySerializer serializer);
- public CapabilitySerializer getCapabilitySerializer(CParameters object);
+ public CapabilitySerializer getCapabilitySerializer(CParameters capability);
}
import org.opendaylight.yangtools.yang.binding.Notification;
public interface MessageParser {
- public Notification parseMessage(final byte[] bytes) throws BGPDocumentedException;
+ public Notification parseMessage(final byte[] bytes, final int messageLength) throws BGPDocumentedException;
}
import org.opendaylight.yangtools.yang.binding.Notification;
public interface MessageSerializer {
+ /**
+ * Deprecated method. Needs to be refactored such that we have an abstract
+ * class which will encode the header, too.
+ *
+ * @return message type to be encoded in the header.
+ */
+ @Deprecated
+ public int messageType();
public byte[] serializeMessage(final Notification message);
}
public class ParserTest {
public static final byte[] openBMsg = 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, (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
- (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
+ (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00 };
public static final byte[] keepAliveBMsg = 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, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
public static final byte[] notificationBMsg = 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, (byte) 0x00, (byte) 0x17, (byte) 0x03, (byte) 0x02, (byte) 0x04, (byte) 0x04, (byte) 0x09 };
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x17, (byte) 0x03, (byte) 0x02, (byte) 0x04, (byte) 0x04, (byte) 0x09 };
public static final byte[] openWithCpblt1 = 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, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
- (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
- (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x40,
- (byte) 0x04, (byte) 0x00, (byte) 0x47 };
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
+ (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
+ (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x40,
+ (byte) 0x04, (byte) 0x00, (byte) 0x47 };
public static final byte[] openWithCpblt2 = 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, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
- (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
- (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00,
- (byte) 0x01, (byte) 0x00, (byte) 0x01 };
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0x00, (byte) 0x2d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x48, (byte) 0x00, (byte) 0xb4,
+ (byte) 0xac, (byte) 0x14, (byte) 0xa0, (byte) 0xaa, (byte) 0x10, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04,
+ (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00,
+ (byte) 0x01, (byte) 0x00, (byte) 0x01 };
- final ProtocolMessageFactory<Notification> factory = new BGPMessageFactoryImpl();
+ final ProtocolMessageFactory<Notification> factory = BGPMessageFactoryImpl.INSTANCE;
@Test
public void testHeaderErrors() throws DeserializerException, DocumentedException {
private List<Notification> parsePrevious(final List<byte[]> msgs) {
final List<Notification> messages = Lists.newArrayList();
- final ProtocolMessageFactory<Notification> parser = new BGPMessageFactoryImpl();
+ final ProtocolMessageFactory<Notification> parser = BGPMessageFactoryImpl.INSTANCE;
try {
for (final byte[] b : msgs) {
BGPDispatcherImpl dispatcher;
public Main() throws IOException {
- this.dispatcher = new BGPDispatcherImpl(new BGPMessageFactoryImpl());
+ this.dispatcher = new BGPDispatcherImpl(BGPMessageFactoryImpl.INSTANCE);
}
public static void main(final String[] args) throws NumberFormatException, IOException {
final SessionNegotiatorFactory<Notification, BGPSessionImpl, BGPSessionListener> snf = new BGPSessionNegotiatorFactory(new HashedWheelTimer(), prefs);
- final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener>(snf, new BGPHandlerFactory(new BGPMessageFactoryImpl()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
+ final BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener> mock = new BGPSpeakerMock<Notification, BGPSessionImpl, BGPSessionListener>(snf, new BGPHandlerFactory(BGPMessageFactoryImpl.INSTANCE), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
mock.createServer(new InetSocketAddress("127.0.0.2", 12345), f);
}