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;
15 import java.util.HashSet;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.AdjRIBsOutRegistration;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
22 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
23 import org.opendaylight.protocol.bgp.rib.spi.Peer;
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;
45 private BGPSession session;
47 private byte[] rawIdentifier;
49 private AdjRIBsOutRegistration reg;
51 public BGPPeer(final String name, final RIB rib) {
52 this.rib = Preconditions.checkNotNull(rib);
57 public synchronized void close() {
59 // TODO should this perform cleanup ?
63 public void onMessage(final BGPSession session, final Notification message) {
64 if (message instanceof Update) {
65 this.rib.updateTables(this, (Update) message);
67 LOG.info("Ignoring unhandled message class {}", message.getClass());
72 public synchronized void onSessionUp(final BGPSession session) {
73 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
75 this.session = session;
76 this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
78 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
79 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
82 this.rib.initTable(this, key);
85 // Not particularly nice, but what can
86 if (session instanceof BGPSessionImpl) {
87 reg = rib.registerRIBsOut(this, new SessionRIBsOut((BGPSessionImpl) session));
91 private synchronized void cleanup() {
92 // FIXME: BUG-196: support graceful restart
93 for (final TablesKey key : this.tables) {
94 this.rib.clearTable(this, key);
97 if (this.reg != null) {
106 public void onSessionDown(final BGPSession session, final Exception e) {
107 LOG.info("Session with peer {} went down", this.name, e);
112 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
113 LOG.info("Session with peer {} terminated: {}", this.name, cause);
118 public String toString() {
119 return addToStringAttributes(Objects.toStringHelper(this)).toString();
122 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
123 toStringHelper.add("name", this.name);
124 toStringHelper.add("tables", this.tables);
125 return toStringHelper;
129 public String getName() {
133 protected RIB getRib() {
138 public void releaseConnection() {
144 private void dropConnection() {
145 if (this.session != null) {
146 this.session.close();
152 public synchronized byte[] getRawIdentifier() {
153 return rawIdentifier;