Merge changes Ic12888cc,I669cc282,Iad2d6119
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import java.util.Set;
11
12 import org.opendaylight.protocol.bgp.parser.BGPSession;
13 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
14 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
15 import org.opendaylight.protocol.bgp.rib.spi.Peer;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Objects;
24 import com.google.common.base.Objects.ToStringHelper;
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Sets;
27
28 /**
29  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
30  * RIB actions.
31  */
32 public final class BGPPeer implements BGPSessionListener, Peer {
33         private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
34         private final Set<TablesKey> tables = Sets.newHashSet();
35         private final String name;
36         private final RIBImpl rib;
37
38         public BGPPeer(final RIBImpl rib, final String name) {
39                 this.rib = Preconditions.checkNotNull(rib);
40                 this.name = Preconditions.checkNotNull(name);
41         }
42
43         @Override
44         public void onMessage(final BGPSession session, final Notification message) {
45                 if (message instanceof Update) {
46                         this.rib.updateTables(this, (Update) message);
47                 } else {
48                         LOG.info("Ignoring unhandled message class " + message.getClass());
49                 }
50         }
51
52         @Override
53         public void onSessionUp(final BGPSession session) {
54                 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
55
56                 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
57                         this.tables.add(new TablesKey(t.getAfi(), t.getSafi()));
58                 }
59         }
60
61         private void cleanup() {
62                 // FIXME: BUG-196: support graceful restart
63                 for (final TablesKey key : this.tables) {
64                         this.rib.clearTable(this, key);
65                 }
66
67                 this.tables.clear();
68         }
69
70         @Override
71         public void onSessionDown(final BGPSession session, final Exception e) {
72                 LOG.info("Session with peer {} went down", this.name, e);
73                 cleanup();
74         }
75
76         @Override
77         public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
78                 LOG.info("Session with peer {} terminated: {}", this.name, cause);
79                 cleanup();
80         }
81
82         @Override
83         public String toString() {
84                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
85         }
86
87         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
88                 toStringHelper.add("name", this.name);
89                 toStringHelper.add("tables", this.tables);
90                 return toStringHelper;
91         }
92 }