import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
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;
}
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);
- ByteArray.copyWhole(msgBody, retBytes, COMMON_HEADER_LENGTH);
+ final byte[] retBytes = formatMessage(serializer.messageType(), msgBody);
logger.trace("Serialized BGP message {}.", Arrays.toString(retBytes));
return retBytes;
*
* @return byte array representation of this header
*/
- public byte[] headerToBytes(final int msgLength, final int msgType) {
- final byte[] retBytes = new byte[COMMON_HEADER_LENGTH];
+ 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);
- System.arraycopy(ByteArray.intToBytes(msgLength), Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH, retBytes, MARKER_LENGTH,
- LENGTH_FIELD_LENGTH);
-
- retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = (byte) msgType;
+ retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(msgType);
+ ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
return retBytes;
}
* 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;
+package org.opendaylight.protocol.bgp.parser.impl;
+import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
+import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
import org.opendaylight.yangtools.yang.binding.Notification;
public interface MessageRegistry {
- public AutoCloseable registerMessageParser(int messageType, MessageParser parser);
public MessageParser getMessageParser(int messageType);
-
- public AutoCloseable registerMessageSerializer(Class<? extends Notification> msgClass, MessageSerializer serializer);
public MessageSerializer getMessageSerializer(Notification message);
}
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;
public static final MessageRegistry INSTANCE;
static {
- final MessageRegistry reg = new MessageRegistryImpl();
+ final MessageRegistryImpl reg = new MessageRegistryImpl();
reg.registerMessageParser(1, BGPOpenMessageParser.PARSER);
reg.registerMessageSerializer(Open.class, BGPOpenMessageParser.SERIALIZER);
INSTANCE = reg;
}
- @Override
- public synchronized AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
+ private synchronized AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
return super.registerParser(messageType, parser);
}
return super.getParser(messageType);
}
- @Override
- public AutoCloseable registerMessageSerializer(final Class<? extends Notification> msgClass, final MessageSerializer serializer) {
+ private AutoCloseable registerMessageSerializer(final Class<? extends Notification> msgClass, final MessageSerializer serializer) {
return super.registerSerializer(msgClass, serializer);
}