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.AdjRIBsOutRegistration;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
24 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
25 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
26 import org.opendaylight.protocol.bgp.rib.spi.Peer;
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;
36 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
39 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
43 private final Set<TablesKey> tables = new HashSet<>();
44 private final RIB rib;
45 private final String name;
48 private BGPSession session;
50 private byte[] rawIdentifier;
52 private AdjRIBsOutRegistration reg;
54 public BGPPeer(final String name, final RIB rib) {
55 this.rib = Preconditions.checkNotNull(rib);
60 public synchronized void close() {
62 // TODO should this perform cleanup ?
66 public void onMessage(final BGPSession session, final Notification message) {
67 if (message instanceof Update) {
68 this.rib.updateTables(this, (Update) message);
70 LOG.info("Ignoring unhandled message class {}", message.getClass());
75 public synchronized void onSessionUp(final BGPSession session) {
76 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
78 this.session = session;
79 this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
81 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
82 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
85 this.rib.initTable(this, key);
88 // Not particularly nice, but what can
89 if (session instanceof BGPSessionImpl) {
90 reg = rib.registerRIBsOut(this, new SessionRIBsOut((BGPSessionImpl) session));
94 private synchronized void cleanup() {
95 // FIXME: BUG-196: support graceful restart
96 for (final TablesKey key : this.tables) {
97 this.rib.clearTable(this, key);
110 public void onSessionDown(final BGPSession session, final Exception e) {
111 LOG.info("Session with peer {} went down", this.name, e);
116 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
117 LOG.info("Session with peer {} terminated: {}", this.name, cause);
122 public String toString() {
123 return addToStringAttributes(Objects.toStringHelper(this)).toString();
126 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
127 toStringHelper.add("name", this.name);
128 toStringHelper.add("tables", this.tables);
129 return toStringHelper;
133 public String getName() {
137 protected RIB getRib() {
142 public void releaseConnection() {
148 private void dropConnection() {
149 if (this.session != null) {
150 this.session.close();
156 public synchronized byte[] getRawIdentifier() {
157 return rawIdentifier;