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.bgpcep.tcpmd5.KeyMapping;
20 import org.opendaylight.protocol.bgp.parser.BGPSession;
21 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
25 import org.opendaylight.protocol.bgp.rib.spi.Peer;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
31 import org.opendaylight.yangtools.yang.binding.Notification;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.base.Charsets;
36 import com.google.common.base.Objects;
37 import com.google.common.base.Objects.ToStringHelper;
38 import com.google.common.base.Preconditions;
41 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
44 public final class BGPPeer implements BGPSessionListener, Peer, AutoCloseable {
45 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
48 private final Set<TablesKey> tables = new HashSet<>();
49 private final String name;
50 private final RIB rib;
52 private Comparator<PathAttributes> comparator;
53 private Future<Void> cf;
54 private BGPSession session;
56 public BGPPeer(final String name, final InetSocketAddress address, final String password, final BGPSessionPreferences prefs,
57 final AsNumber remoteAs, final RIB rib) {
58 this.rib = Preconditions.checkNotNull(rib);
59 this.name = Preconditions.checkNotNull(name);
61 final KeyMapping keys;
62 if (password != null) {
63 keys = new KeyMapping();
64 keys.put(address.getAddress(), password.getBytes(Charsets.US_ASCII));
69 this.cf = rib.getDispatcher().createReconnectingClient(address, prefs, remoteAs, this, rib.getTcpStrategyFactory(), rib.getSessionStrategyFactory(), keys);
73 public synchronized void close() {
74 if (this.cf != null) {
76 if (this.session != null) {
85 public void onMessage(final BGPSession session, final Notification message) {
86 if (message instanceof Update) {
87 this.rib.updateTables(this, (Update) message);
89 LOG.info("Ignoring unhandled message class " + message.getClass());
94 public synchronized void onSessionUp(final BGPSession session) {
95 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
97 this.session = session;
98 this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), this.rib.getBgpIdentifier(), session.getBgpId());
100 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
101 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
103 this.tables.add(key);
104 this.rib.initTable(this, key);
108 private synchronized void cleanup() {
109 // FIXME: BUG-196: support graceful restart
110 for (final TablesKey key : this.tables) {
111 this.rib.clearTable(this, key);
116 this.comparator = null;
120 public void onSessionDown(final BGPSession session, final Exception e) {
121 LOG.info("Session with peer {} went down", this.name, e);
126 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
127 LOG.info("Session with peer {} terminated: {}", this.name, cause);
132 public String toString() {
133 return addToStringAttributes(Objects.toStringHelper(this)).toString();
136 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
137 toStringHelper.add("name", this.name);
138 toStringHelper.add("tables", this.tables);
139 return toStringHelper;
143 public String getName() {
148 public Comparator<PathAttributes> getComparator() {
149 return this.comparator;