Remove reliance on deprecated classes in BGP components
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImpl.java
index 65bb948121bb48332b68a3c77b2c3e475b50d235..032cea380d7856c8d2ff310f5fefacb9439e4c5e 100644 (file)
@@ -19,18 +19,23 @@ import java.util.concurrent.TimeUnit;
 
 import javax.annotation.concurrent.GuardedBy;
 
-import org.opendaylight.protocol.bgp.concepts.BGPTableType;
 import org.opendaylight.protocol.bgp.parser.BGPError;
-import org.opendaylight.protocol.bgp.parser.BGPMessage;
-import org.opendaylight.protocol.bgp.parser.BGPParameter;
 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.message.BGPKeepAliveMessage;
-import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
-import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
-import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
+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.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
+import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,24 +46,27 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Sets;
 
 @VisibleForTesting
-public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implements BGPSession {
+public class BGPSessionImpl extends AbstractProtocolSession<Notification> implements BGPSession {
 
        private static final Logger logger = LoggerFactory.getLogger(BGPSessionImpl.class);
 
+       /*
+        * 240
+        */
        private static final int DEFAULT_HOLD_TIMER_VALUE = 15;
 
-       public static int HOLD_TIMER_VALUE = DEFAULT_HOLD_TIMER_VALUE; // 240
+       private static final Notification keepalive = new KeepaliveBuilder().build();
+
+       private static int holdTimerValue = DEFAULT_HOLD_TIMER_VALUE;
 
        /**
         * Internal session state.
         */
        public enum State {
                /**
-                * The session object is created by the negotiator in OpenConfirm state.
-                * While in this state, the session object 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.
+                * The session object is created by the negotiator in OpenConfirm state. While in this state, the session object
+                * 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,
                /**
@@ -74,6 +82,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
        /**
         * System.nanoTime value about when was sent the last message Protected to be updated also in tests.
         */
+       @VisibleForTesting
        protected long lastMessageSentAt;
 
        /**
@@ -99,34 +108,39 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
 
        private final int keepAlive;
 
-       private final Set<BGPTableType> tableTypes;
+       private final Set<BgpTableType> tableTypes;
 
-       BGPSessionImpl(final Timer timer, final BGPSessionListener listener, final Channel channel, final BGPOpenMessage remoteOpen) {
+       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.getHoldTime() / 3;
-
-               final Set<BGPTableType> tts = Sets.newHashSet();
-               if (remoteOpen.getOptParams() != null) {
-                       for (final BGPParameter param : remoteOpen.getOptParams()) {
-                               if (param instanceof MultiprotocolCapability) {
-                                       tts.add(((MultiprotocolCapability) param).getTableType());
+               this.keepAlive = remoteOpen.getHoldTimer() / 3;
+               holdTimerValue = remoteOpen.getHoldTimer();
+
+               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());
+                                       tts.add(tt);
+                                       tats.add(new BgpTableTypeImpl(tt.getAfi(), tt.getSafi()));
                                }
                        }
                }
 
                this.sync = new BGPSynchronization(this, this.listener, tts);
-               this.tableTypes = tts;
+               this.tableTypes = tats;
 
-               if (remoteOpen.getHoldTime() != 0) {
+               if (remoteOpen.getHoldTimer() != 0) {
                        this.stateTimer.newTimeout(new TimerTask() {
 
                                @Override
                                public void run(final Timeout timeout) throws Exception {
                                        handleHoldTimer();
                                }
-                       }, remoteOpen.getHoldTime(), TimeUnit.SECONDS);
+                       }, remoteOpen.getHoldTimer(), TimeUnit.SECONDS);
 
                        this.stateTimer.newTimeout(new TimerTask() {
                                @Override
@@ -141,7 +155,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
        public synchronized void close() {
                logger.debug("Closing session: {}", this);
                if (this.state != State.Idle) {
-                       this.sendMessage(new BGPNotificationMessage(BGPError.CEASE));
+                       this.sendMessage(new NotifyBuilder().setErrorCode(BGPError.CEASE.getCode()).build());
                        this.channel.close();
                        this.state = State.Idle;
                }
@@ -153,19 +167,21 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
         * @param msg incoming message
         */
        @Override
-       public void handleMessage(final BGPMessage msg) {
+       public void handleMessage(final Notification msg) {
                // Update last reception time
                this.lastMessageReceivedAt = System.nanoTime();
 
-               if (msg instanceof BGPOpenMessage) {
+               if (msg instanceof Open) {
                        // Open messages should not be present here
                        this.terminate(BGPError.FSM_ERROR);
-               } else if (msg instanceof BGPNotificationMessage) {
+               } else if (msg instanceof Notify) {
                        // Notifications are handled internally
-                       logger.info("Session closed because Notification message received: {}", ((BGPNotificationMessage) msg).getError());
+                       logger.info("Session closed because Notification message received: {} / {}", ((Notify) msg).getErrorCode(),
+                                       ((Notify) msg).getErrorSubcode());
                        this.closeWithoutMessage();
-                       this.listener.onSessionTerminated(this, new BGPTerminationReason(((BGPNotificationMessage) msg).getError()));
-               } else if (msg instanceof BGPKeepAliveMessage) {
+                       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.");
                        this.kaCounter++;
@@ -185,7 +201,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
                }
        }
 
-       void sendMessage(final BGPMessage msg) {
+       void sendMessage(final Notification msg) {
                try {
                        this.channel.writeAndFlush(msg);
                        this.lastMessageSentAt = System.nanoTime();
@@ -208,7 +224,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
         * @param closeObject
         */
        private void terminate(final BGPError error) {
-               this.sendMessage(new BGPNotificationMessage(error));
+               this.sendMessage(new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode()).build());
                this.closeWithoutMessage();
 
                this.listener.onSessionTerminated(this, new BGPTerminationReason(error));
@@ -226,7 +242,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
                }
 
                final long ct = System.nanoTime();
-               final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(HOLD_TIMER_VALUE);
+               final long nextHold = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(holdTimerValue);
 
                if (ct >= nextHold) {
                        logger.debug("HoldTimer expired. " + new Date());
@@ -256,7 +272,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
                long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
 
                if (ct >= nextKeepalive) {
-                       this.sendMessage(new BGPKeepAliveMessage());
+                       this.sendMessage(keepalive);
                        nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(this.keepAlive);
                }
                this.stateTimer.newTimeout(new TimerTask() {
@@ -268,7 +284,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
        }
 
        @Override
-       final public String toString() {
+       public final String toString() {
                return addToStringAttributes(Objects.toStringHelper(this)).toString();
        }
 
@@ -279,7 +295,7 @@ public class BGPSessionImpl extends AbstractProtocolSession<BGPMessage> implemen
        }
 
        @Override
-       public Set<BGPTableType> getAdvertisedTableTypes() {
+       public Set<BgpTableType> getAdvertisedTableTypes() {
                return this.tableTypes;
        }