import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import javax.annotation.concurrent.GuardedBy;
import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
import javax.annotation.concurrent.GuardedBy;
import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics;
import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics;
import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.AddPathCapability;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCapability;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.add.path.capability.AddressFamilies;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
import org.opendaylight.yangtools.yang.binding.Notification;
import org.slf4j.Logger;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
import org.opendaylight.yangtools.yang.binding.Notification;
import org.slf4j.Logger;
+ if (! this.addPathTypes.isEmpty()) {
+ final ChannelPipeline pipeline = this.channel.pipeline();
+ final BGPByteToMessageDecoder decoder = pipeline.get(BGPByteToMessageDecoder.class);
+ decoder.addDecoderConstraint(MultiPathSupport.class,
+ MultiPathSupportImpl.createParserMultiPathSupport(this.addPathTypes));
+ }
+
- if (this.state != State.IDLE && this.channel.isActive()) {
- this.writeAndFlush(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).setErrorSubcode(
- BGPError.CEASE.getSubcode()).build());
+ if (this.state != State.IDLE) {
+ this.writeAndFlush(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).setErrorSubcode(BGPError.CEASE.getSubcode()).build());
// Update last reception time
this.lastMessageReceivedAt = System.nanoTime();
this.sessionStats.updateReceivedMsgTotal();
if (msg instanceof Open) {
// Open messages should not be present here
// Update last reception time
this.lastMessageReceivedAt = System.nanoTime();
this.sessionStats.updateReceivedMsgTotal();
if (msg instanceof Open) {
// Open messages should not be present here
} else if (msg instanceof Notify) {
// Notifications are handled internally
LOG.info("Session closed because Notification message received: {} / {}", ((Notify) msg).getErrorCode(),
((Notify) msg).getErrorSubcode());
this.closeWithoutMessage();
} else if (msg instanceof Notify) {
// Notifications are handled internally
LOG.info("Session closed because Notification message received: {} / {}", ((Notify) msg).getErrorCode(),
((Notify) msg).getErrorSubcode());
this.closeWithoutMessage();
- this.listener.onSessionTerminated(this, new BGPTerminationReason(BGPError.forValue(((Notify) msg).getErrorCode(),
- ((Notify) msg).getErrorSubcode())));
+ this.listener.onSessionTerminated(this, new BGPTerminationReason(
+ BGPError.forValue(((Notify) msg).getErrorCode(), ((Notify) msg).getErrorSubcode())));
this.sessionStats.updateReceivedMsgErr((Notify) msg);
} else if (msg instanceof Keepalive) {
// Keepalives are handled internally
this.sessionStats.updateReceivedMsgErr((Notify) msg);
} else if (msg instanceof Keepalive) {
// Keepalives are handled internally
} else if (msg instanceof RouteRefresh) {
this.listener.onMessage(this, msg);
this.sessionStats.updateReceivedMsgRR();
} else if (msg instanceof RouteRefresh) {
this.listener.onMessage(this, msg);
this.sessionStats.updateReceivedMsgRR();
this.listener.onMessage(this, msg);
this.sync.updReceived((Update) msg);
this.sessionStats.updateReceivedMsgUpd();
this.listener.onMessage(this, msg);
this.sync.updReceived((Update) msg);
this.sessionStats.updateReceivedMsgUpd();
if (this.state == State.UP) {
LOG.info(END_OF_INPUT);
this.listener.onSessionDown(this, new IOException(END_OF_INPUT));
if (this.state == State.UP) {
LOG.info(END_OF_INPUT);
this.listener.onSessionDown(this, new IOException(END_OF_INPUT));
- new ChannelFutureListener() {
- @Override
- public void operationComplete(final ChannelFuture f) {
- if (!f.isSuccess()) {
- LOG.warn("Failed to send message {} to socket {}", msg, f.cause(), BGPSessionImpl.this.channel);
- } else {
- LOG.trace("Message {} sent to socket {}", msg, BGPSessionImpl.this.channel);
- }
+ new ChannelFutureListener() {
+ @Override
+ public void operationComplete(final ChannelFuture f) {
+ if (!f.isSuccess()) {
+ LOG.warn("Failed to send message {} to socket {}", msg, BGPSessionImpl.this.channel, f.cause());
+ } else {
+ LOG.trace("Message {} sent to socket {}", msg, BGPSessionImpl.this.channel);
this.lastMessageSentAt = System.nanoTime();
this.sessionStats.updateSentMsgTotal();
if (msg instanceof Update) {
this.lastMessageSentAt = System.nanoTime();
this.sessionStats.updateSentMsgTotal();
if (msg instanceof Update) {
- synchronized void writeAndFlush(final Notification msg) {
- writeEpilogue(this.channel.writeAndFlush(msg), msg);
+ synchronized ChannelFuture writeAndFlush(final Notification msg) {
+ if (isWritable()) {
+ return writeEpilogue(this.channel.writeAndFlush(msg), msg);
+ }
+ return this.channel.newFailedFuture(new NonWritableChannelException());
}
private synchronized void closeWithoutMessage() {
LOG.info("Closing session: {}", this);
removePeerSession();
}
private synchronized void closeWithoutMessage() {
LOG.info("Closing session: {}", this);
removePeerSession();
+ this.channel.close().addListener(new ChannelFutureListener() {
+ @Override
+ public void operationComplete(final ChannelFuture future) throws Exception {
+ Preconditions.checkArgument(future.isSuccess(), "Channel failed to close: %s", future.cause());
+ }
+ });
- private void terminate(final BGPError error) {
- this.writeAndFlush(new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode()).build());
+ private synchronized void terminate(final BGPDocumentedException e) {
+ final BGPError error = e.getError();
+ final byte[] data = e.getData();
+ final NotifyBuilder builder = new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode());
+ if (data != null && data.length != 0) {
+ builder.setData(data);
+ }
+ this.writeAndFlush(builder.build());
this.closeWithoutMessage();
this.listener.onSessionTerminated(this, new BGPTerminationReason(error));
this.closeWithoutMessage();
this.listener.onSessionTerminated(this, new BGPTerminationReason(error));
@Override
protected final void channelRead0(final ChannelHandlerContext ctx, final Notification msg) {
LOG.debug("Message was received: {}", msg);
@Override
protected final void channelRead0(final ChannelHandlerContext ctx, final Notification msg) {
LOG.debug("Message was received: {}", msg);
+
+ @Override
+ public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
+ LOG.warn("BGP session encountered error", cause);
+ if (cause.getCause() instanceof BGPDocumentedException) {
+ this.terminate((BGPDocumentedException) cause.getCause());
+ } else {
+ this.close();
+ }
+ }