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 com.google.common.net.InetAddresses;
16 import java.util.HashSet;
19 import javax.annotation.concurrent.GuardedBy;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
23 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
24 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
25 import org.opendaylight.protocol.bgp.rib.spi.Peer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
35 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
38 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
42 private final Set<TablesKey> tables = new HashSet<>();
43 private final RIB rib;
44 private final String name;
46 private BGPSession session;
47 private byte[] rawIdentifier;
49 public BGPPeer(final String name, final RIB rib) {
50 this.rib = Preconditions.checkNotNull(rib);
55 public synchronized void close() {
57 // TODO should this perform cleanup ?
61 public void onMessage(final BGPSession session, final Notification message) {
62 if (message instanceof Update) {
63 this.rib.updateTables(this, (Update) message);
65 LOG.info("Ignoring unhandled message class {}", message.getClass());
70 public synchronized void onSessionUp(final BGPSession session) {
71 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
73 this.session = session;
74 this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
76 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
77 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
80 this.rib.initTable(this, key);
84 private synchronized void cleanup() {
85 // FIXME: BUG-196: support graceful restart
86 for (final TablesKey key : this.tables) {
87 this.rib.clearTable(this, key);
95 public void onSessionDown(final BGPSession session, final Exception e) {
96 LOG.info("Session with peer {} went down", this.name, e);
101 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
102 LOG.info("Session with peer {} terminated: {}", this.name, cause);
107 public String toString() {
108 return addToStringAttributes(Objects.toStringHelper(this)).toString();
111 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
112 toStringHelper.add("name", this.name);
113 toStringHelper.add("tables", this.tables);
114 return toStringHelper;
118 public String getName() {
122 protected RIB getRib() {
127 public void releaseConnection() {
132 private void dropConnection() {
133 if (this.session != null) {
134 this.session.close();
140 public byte[] getRawIdentifier() {
141 return rawIdentifier;