2 * Copyright (c) 2014 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
9 package org.opendaylight.protocol.bgp.rib.impl;
11 import com.google.common.base.Objects;
12 import com.google.common.base.Objects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import java.util.Comparator;
15 import java.util.HashSet;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.protocol.bgp.parser.BGPSession;
19 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
22 import org.opendaylight.protocol.bgp.rib.spi.Peer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
33 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
36 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
40 private final Set<TablesKey> tables = new HashSet<>();
41 private final RIB rib;
42 private final String name;
44 private Comparator<PathAttributes> comparator;
45 private BGPSession session;
47 public BGPPeer(final String name, final RIB rib) {
48 this.rib = Preconditions.checkNotNull(rib);
53 public synchronized void close() {
55 // TODO should this perform cleanup ?
59 public void onMessage(final BGPSession session, final Notification message) {
60 if (message instanceof Update) {
61 this.rib.updateTables(this, (Update) message);
63 LOG.info("Ignoring unhandled message class {}", message.getClass());
68 public synchronized void onSessionUp(final BGPSession session) {
69 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
71 this.session = session;
72 this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), rib.getBgpIdentifier(), session.getBgpId());
74 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
75 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
78 this.rib.initTable(this, key);
82 private synchronized void cleanup() {
83 // FIXME: BUG-196: support graceful restart
84 for (final TablesKey key : this.tables) {
85 this.rib.clearTable(this, key);
90 this.comparator = null;
94 public void onSessionDown(final BGPSession session, final Exception e) {
95 LOG.info("Session with peer {} went down", this.name, e);
100 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
101 LOG.info("Session with peer {} terminated: {}", this.name, cause);
106 public String toString() {
107 return addToStringAttributes(Objects.toStringHelper(this)).toString();
110 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
111 toStringHelper.add("name", this.name);
112 toStringHelper.add("tables", this.tables);
113 return toStringHelper;
117 public String getName() {
122 public Comparator<PathAttributes> getComparator() {
123 return this.comparator;
126 protected RIB getRib() {
131 public void releaseConnection() {
136 private void dropConnection() {
137 if (this.session != null) {
138 this.session.close();