2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import io.netty.util.concurrent.Future;
12 import java.net.InetSocketAddress;
13 import java.util.Comparator;
14 import java.util.HashSet;
17 import javax.annotation.concurrent.GuardedBy;
19 import org.opendaylight.protocol.bgp.parser.BGPSession;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
24 import org.opendaylight.protocol.bgp.rib.spi.Peer;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
30 import org.opendaylight.yangtools.yang.binding.Notification;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Objects;
35 import com.google.common.base.Objects.ToStringHelper;
36 import com.google.common.base.Preconditions;
39 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
42 public final class BGPPeer implements BGPSessionListener, Peer, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
46 private final Set<TablesKey> tables = new HashSet<>();
47 private final String name;
48 private final RIB rib;
50 private Comparator<PathAttributes> comparator;
51 private Future<Void> cf;
52 private BGPSession session;
54 public BGPPeer(final String name, final InetSocketAddress address, final BGPSessionPreferences prefs,
55 final AsNumber remoteAs, final RIB rib) {
56 this.rib = Preconditions.checkNotNull(rib);
57 this.name = Preconditions.checkNotNull(name);
58 this.cf = rib.getDispatcher().createReconnectingClient(address, prefs, remoteAs, this, rib.getTcpStrategyFactory(), rib.getSessionStrategyFactory());
62 public synchronized void close() {
63 if (this.cf != null) {
65 if (this.session != null) {
74 public void onMessage(final BGPSession session, final Notification message) {
75 if (message instanceof Update) {
76 this.rib.updateTables(this, (Update) message);
78 LOG.info("Ignoring unhandled message class " + message.getClass());
83 public synchronized void onSessionUp(final BGPSession session) {
84 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
86 this.session = session;
87 this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), this.rib.getBgpIdentifier(), session.getBgpId());
89 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
90 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
93 this.rib.initTable(this, key);
97 private synchronized void cleanup() {
98 // FIXME: BUG-196: support graceful restart
99 for (final TablesKey key : this.tables) {
100 this.rib.clearTable(this, key);
105 this.comparator = null;
109 public void onSessionDown(final BGPSession session, final Exception e) {
110 LOG.info("Session with peer {} went down", this.name, e);
115 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
116 LOG.info("Session with peer {} terminated: {}", this.name, cause);
121 public String toString() {
122 return addToStringAttributes(Objects.toStringHelper(this)).toString();
125 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
126 toStringHelper.add("name", this.name);
127 toStringHelper.add("tables", this.tables);
128 return toStringHelper;
132 public String getName() {
137 public Comparator<PathAttributes> getComparator() {
138 return this.comparator;