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;
12 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
13 import org.opendaylight.protocol.bgp.parser.BGPSession;
14 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
15 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
16 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
17 import org.opendaylight.yangtools.yang.binding.Notification;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.common.base.Objects;
22 import com.google.common.base.Objects.ToStringHelper;
23 import com.google.common.base.Preconditions;
26 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
29 public final class BGPPeer implements BGPSessionListener {
30 private static final Logger logger = LoggerFactory.getLogger(BGPPeer.class);
31 private Set<BGPTableType> tables;
32 private final String name;
33 private final RIBImpl rib;
35 public BGPPeer(final RIBImpl rib, final String name) {
36 this.rib = Preconditions.checkNotNull(rib);
37 this.name = Preconditions.checkNotNull(name);
41 public void onMessage(final BGPSession session, final Notification message) {
42 if (message instanceof BGPUpdateMessage) {
43 final BGPUpdateMessage m = (BGPUpdateMessage) message;
44 this.rib.updateTables(this, m.getAddedObjects(), m.getRemovedObjects());
46 logger.info("Ignoring unhandled message class " + message.getClass());
51 public void onSessionUp(final BGPSession session) {
52 logger.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
56 public void onSessionDown(final BGPSession session, final Exception e) {
57 // FIXME: support graceful restart
58 for (final BGPTableType t : this.tables) {
59 this.rib.clearTable(this, t);
64 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
65 logger.info("Session with peer {} terminated: {}", this.name, cause);
69 public final String toString() {
70 return addToStringAttributes(Objects.toStringHelper(this)).toString();
73 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
74 toStringHelper.add("name", this.name);
75 toStringHelper.add("tables", this.tables);
76 return toStringHelper;