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.BGPError;
14 import org.opendaylight.protocol.bgp.parser.BGPMessage;
15 import org.opendaylight.protocol.bgp.parser.BGPSession;
16 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
17 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
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 extends 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 BGPMessage 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());
50 public void onSessionUp(final Set<BGPTableType> remoteParams) {
51 logger.info("Session with peer {} went up with tables: {}", this.name, remoteParams);
55 public void onSessionDown(final BGPSession session, final Exception e) {
56 // FIXME: support graceful restart
57 for (final BGPTableType t : this.tables)
58 this.rib.clearTable(this, t);
62 public void onSessionTerminated(final BGPError cause) {
63 logger.info("Session with peer {} terminated", this.name);
67 public final String toString() {
68 return addToStringAttributes(Objects.toStringHelper(this)).toString();
71 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
72 toStringHelper.add("name", this.name);
73 toStringHelper.add("tables", this.tables);
74 return toStringHelper;