Merge "Refactor linkstate."
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImpl.java
index 7bf53bf54a609fc58b3688da74407ac8395dc912..5d91d3ed2791ec56d4d189fcb04bec638611e10d 100644 (file)
@@ -40,9 +40,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
 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.message.rev130919.open.bgp.parameters.OptionalCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.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.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.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;
@@ -55,6 +56,8 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
 
     private static final Notification KEEP_ALIVE = new KeepaliveBuilder().build();
 
+    private static final int KA_TO_DEADTIMER_RATIO = 3;
+
     /**
      * Internal session state.
      */
@@ -64,15 +67,15 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
          * is half-alive, e.g. the timers are running, but the session is not completely up, e.g. it has not been
          * announced to the listener. If the session is torn down in this state, we do not inform the listener.
          */
-        OpenConfirm,
+        OPEN_CONFIRM,
         /**
          * The session has been completely established.
          */
-        Up,
+        UP,
         /**
          * The session has been closed. It will not be resurrected.
          */
-        Idle,
+        IDLE,
     }
 
     /**
@@ -95,7 +98,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
     private final Channel channel;
 
     @GuardedBy("this")
-    private State state = State.OpenConfirm;
+    private State state = State.OPEN_CONFIRM;
 
     private final Set<BgpTableType> tableTypes;
     private final int holdTimerValue;
@@ -114,20 +117,22 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
         this.channel = Preconditions.checkNotNull(channel);
         this.holdTimerValue = (remoteOpen.getHoldTimer() < localHoldTimer) ? remoteOpen.getHoldTimer() : localHoldTimer;
         LOG.info("BGP HoldTimer new value: {}", this.holdTimerValue);
-        this.keepAlive = this.holdTimerValue / 3;
+        this.keepAlive = this.holdTimerValue / KA_TO_DEADTIMER_RATIO;
         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()) {
-                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()));
+                for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
+                    final CParameters cp = optCapa.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()));
+                    }
                 }
             }
         }
@@ -158,10 +163,11 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
     @Override
     public synchronized void close() {
         LOG.info("Closing session: {}", this);
-        if (this.state != State.Idle) {
-            this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).setErrorSubcode((short)0).build());
+        if (this.state != State.IDLE) {
+            this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).setErrorSubcode(
+                    BGPError.CEASE.getSubcode()).build());
             this.channel.close();
-            this.state = State.Idle;
+            this.state = State.IDLE;
         }
     }
 
@@ -205,7 +211,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
 
     @Override
     public synchronized void endOfInput() {
-        if (this.state == State.Up) {
+        if (this.state == State.UP) {
             this.listener.onSessionDown(this, new IOException("End of input detected. Close the session."));
         }
     }
@@ -238,7 +244,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
     private synchronized void closeWithoutMessage() {
         LOG.debug("Closing session: {}", this);
         this.channel.close();
-        this.state = State.Idle;
+        this.state = State.IDLE;
     }
 
     /**
@@ -261,7 +267,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
      * state will become IDLE), then rescheduling won't occur.
      */
     private synchronized void handleHoldTimer() {
-        if (this.state == State.Idle) {
+        if (this.state == State.IDLE) {
             return;
         }
 
@@ -288,7 +294,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
      * starts to execute (the session state will become IDLE), that rescheduling won't occur.
      */
     private synchronized void handleKeepaliveTimer() {
-        if (this.state == State.Idle) {
+        if (this.state == State.IDLE) {
             return;
         }
 
@@ -327,7 +333,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
     @Override
     protected synchronized void sessionUp() {
         this.sessionStats.startSessionStopwatch();
-        this.state = State.Up;
+        this.state = State.UP;
         this.listener.onSessionUp(this);
     }
 
@@ -360,7 +366,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<Notification> implem
     }
 
     @Override
-    public BgpSessionState getBgpSesionState() {
+    public synchronized BgpSessionState getBgpSesionState() {
         return this.sessionStats.getBgpSessionState(this.state);
     }