BUG-353: add remote-as awareness
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImpl.java
index 032cea380d7856c8d2ff310f5fefacb9439e4c5e..e40f60f3d3d086f3e4dbf7a421b464f072d6f799 100644 (file)
@@ -19,21 +19,25 @@ import java.util.concurrent.TimeUnit;
 
 import javax.annotation.concurrent.GuardedBy;
 
+import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BGPSession;
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 import org.opendaylight.protocol.framework.AbstractProtocolSession;
-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.NotifyBuilder;
-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.open.BgpParameters;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.BgpTableType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocol;
+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.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.NotifyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.CParameters;
+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.open.bgp.parameters.c.parameters.MultiprotocolCase;
 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;
@@ -48,16 +52,14 @@ import com.google.common.collect.Sets;
 @VisibleForTesting
 public class BGPSessionImpl extends AbstractProtocolSession<Notification> implements BGPSession {
 
-       private static final Logger logger = LoggerFactory.getLogger(BGPSessionImpl.class);
+       private static final Logger LOG = LoggerFactory.getLogger(BGPSessionImpl.class);
 
        /*
         * 240
         */
        private static final int DEFAULT_HOLD_TIMER_VALUE = 15;
 
-       private static final Notification keepalive = new KeepaliveBuilder().build();
-
-       private static int holdTimerValue = DEFAULT_HOLD_TIMER_VALUE;
+       private static final Notification KEEP_ALIVE = new KeepaliveBuilder().build();
 
        /**
         * Internal session state.
@@ -106,24 +108,29 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
        @GuardedBy("this")
        private State state = State.OpenConfirm;
 
-       private final int keepAlive;
 
        private final Set<BgpTableType> tableTypes;
+       private final int holdTimerValue;
+       private final int keepAlive;
+       private final AsNumber asNumber;
+       private final Ipv4Address bgpId;
 
        BGPSessionImpl(final Timer timer, final BGPSessionListener listener, final Channel channel, final Open remoteOpen) {
                this.listener = Preconditions.checkNotNull(listener);
                this.stateTimer = Preconditions.checkNotNull(timer);
                this.channel = Preconditions.checkNotNull(channel);
                this.keepAlive = remoteOpen.getHoldTimer() / 3;
-               holdTimerValue = remoteOpen.getHoldTimer();
+               this.holdTimerValue = remoteOpen.getHoldTimer();
+               this.asNumber = AsNumberUtil.advertizedAsNumber(remoteOpen);
 
                final Set<TablesKey> tts = Sets.newHashSet();
                final Set<BgpTableType> tats = Sets.newHashSet();
                if (remoteOpen.getBgpParameters() != null) {
                        for (final BgpParameters param : remoteOpen.getBgpParameters()) {
-                               if (param instanceof CParameters) {
-                                       final CParameters cp = (CParameters) param;
-                                       final TablesKey tt = new TablesKey(((CMultiprotocol) cp).getMultiprotocolCapability().getAfi(), ((CMultiprotocol) cp).getMultiprotocolCapability().getSafi());
+                               final CParameters cp = param.getCParameters();
+                               if (cp instanceof MultiprotocolCase) {
+                                       final TablesKey tt = new TablesKey(((MultiprotocolCase) cp).getMultiprotocolCapability().getAfi(), ((MultiprotocolCase) cp).getMultiprotocolCapability().getSafi());
+                                       LOG.trace("Added table type to sync {}", tt);
                                        tts.add(tt);
                                        tats.add(new BgpTableTypeImpl(tt.getAfi(), tt.getSafi()));
                                }
@@ -149,11 +156,12 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
                                }
                        }, this.keepAlive, TimeUnit.SECONDS);
                }
+               this.bgpId = remoteOpen.getBgpIdentifier();
        }
 
        @Override
        public synchronized void close() {
-               logger.debug("Closing session: {}", this);
+               LOG.debug("Closing session: {}", this);
                if (this.state != State.Idle) {
                        this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build());
                        this.channel.close();
@@ -176,14 +184,14 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
                        this.terminate(BGPError.FSM_ERROR);
                } else if (msg instanceof Notify) {
                        // Notifications are handled internally
-                       logger.info("Session closed because Notification message received: {} / {}", ((Notify) msg).getErrorCode(),
+                       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())));
                } else if (msg instanceof Keepalive) {
                        // Keepalives are handled internally
-                       logger.debug("Received KeepAlive messsage.");
+                       LOG.debug("Received KeepAlive messsage.");
                        this.kaCounter++;
                        if (this.kaCounter >= 2) {
                                this.sync.kaReceived();
@@ -191,6 +199,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
                } else {
                        // All others are passed up
                        this.listener.onMessage(this, msg);
+                       this.sync.updReceived((Update) msg);
                }
        }
 
@@ -205,14 +214,14 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
                try {
                        this.channel.writeAndFlush(msg);
                        this.lastMessageSentAt = System.nanoTime();
-                       logger.debug("Sent message: {}", msg);
+                       LOG.debug("Sent message: {}", msg);
                } catch (final Exception e) {
-                       logger.warn("Message {} was not sent.", msg, e);
+                       LOG.warn("Message {} was not sent.", msg, e);
                }
        }
 
        private synchronized void closeWithoutMessage() {
-               logger.debug("Closing session: {}", this);
+               LOG.debug("Closing session: {}", this);
                this.channel.close();
                this.state = State.Idle;
        }
@@ -245,7 +254,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
                final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(holdTimerValue);
 
                if (ct >= nextHold) {
-                       logger.debug("HoldTimer expired. " + new Date());
+                       LOG.debug("HoldTimer expired. " + new Date());
                        this.terminate(BGPError.HOLD_TIMER_EXPIRED);
                } else {
                        this.stateTimer.newTimeout(new TimerTask() {
@@ -272,7 +281,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
                long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
 
                if (ct >= nextKeepalive) {
-                       this.sendMessage(keepalive);
+                       this.sendMessage(KEEP_ALIVE);
                        nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
                }
                this.stateTimer.newTimeout(new TimerTask() {
@@ -308,4 +317,14 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
        public synchronized State getState() {
                return this.state;
        }
+
+       @Override
+       public final byte[] getBgpId() {
+               return this.bgpId.getValue().getBytes();
+       }
+
+       @Override
+       public final AsNumber getAsNumber() {
+               return this.asNumber;
+       }
 }