BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
index 0e9ac2c23634c1ce0c427cb4eef73aa62c01de13..05f03738bcbdfd54344e34342363ced143073ac3 100644 (file)
@@ -1,34 +1,25 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * 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.rib.impl;
 
-import com.google.common.base.Charsets;
 import com.google.common.base.Objects;
 import com.google.common.base.Objects.ToStringHelper;
 import com.google.common.base.Preconditions;
-
-import io.netty.util.concurrent.Future;
-
-import java.net.InetSocketAddress;
 import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Set;
-
 import javax.annotation.concurrent.GuardedBy;
-
-import org.opendaylight.bgpcep.tcpmd5.KeyMapping;
 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.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
+import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
 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.multiprotocol.rev130919.BgpTableType;
@@ -37,49 +28,31 @@ import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+
 /**
  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
  * RIB actions.
  */
-public final class BGPPeer implements BGPSessionListener, Peer, AutoCloseable {
+public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
 
     @GuardedBy("this")
     private final Set<TablesKey> tables = new HashSet<>();
-    private final String name;
     private final RIB rib;
+    private final String name;
 
     private Comparator<PathAttributes> comparator;
-    private Future<Void> cf;
     private BGPSession session;
 
-    public BGPPeer(final String name, final InetSocketAddress address, final String password, final BGPSessionPreferences prefs,
-            final AsNumber remoteAs, final RIB rib) {
+    public BGPPeer(final String name, final RIB rib) {
         this.rib = Preconditions.checkNotNull(rib);
-        this.name = Preconditions.checkNotNull(name);
-
-        final KeyMapping keys;
-        if (password != null) {
-            keys = new KeyMapping();
-            keys.put(address.getAddress(), password.getBytes(Charsets.US_ASCII));
-        } else {
-            keys = null;
-        }
-
-        this.cf = rib.getDispatcher().createReconnectingClient(address, prefs, remoteAs, this, rib.getTcpStrategyFactory(),
-                rib.getSessionStrategyFactory(), keys);
+        this.name = name;
     }
 
     @Override
     public synchronized void close() {
-        if (this.cf != null) {
-            this.cf.cancel(true);
-            if (this.session != null) {
-                this.session.close();
-                this.session = null;
-            }
-            this.cf = null;
-        }
+        dropConnection();
+        // TODO should this perform cleanup ?
     }
 
     @Override
@@ -96,7 +69,7 @@ public final class BGPPeer implements BGPSessionListener, Peer, AutoCloseable {
         LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
 
         this.session = session;
-        this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), this.rib.getBgpIdentifier(), session.getBgpId());
+        this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), rib.getBgpIdentifier(), session.getBgpId());
 
         for (final BgpTableType t : session.getAdvertisedTableTypes()) {
             final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
@@ -149,4 +122,21 @@ public final class BGPPeer implements BGPSessionListener, Peer, AutoCloseable {
     public Comparator<PathAttributes> getComparator() {
         return this.comparator;
     }
+
+    protected RIB getRib() {
+        return rib;
+    }
+
+    @Override
+    public void releaseConnection() {
+        dropConnection();
+        cleanup();
+    }
+
+    private void dropConnection() {
+        if (this.session != null) {
+            this.session.close();
+            this.session = null;
+        }
+    }
 }