16e4e71f2cca57b26ca904dea41c11f5f224566e
[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.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;
20
21 import com.google.common.base.Objects;
22 import com.google.common.base.Objects.ToStringHelper;
23 import com.google.common.base.Preconditions;
24
25 /**
26  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
27  * RIB actions.
28  */
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;
34
35         public BGPPeer(final RIBImpl rib, final String name) {
36                 this.rib = Preconditions.checkNotNull(rib);
37                 this.name = Preconditions.checkNotNull(name);
38         }
39
40         @Override
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());
45                 } else {
46                         logger.info("Ignoring unhandled message class " + message.getClass());
47                 }
48         }
49
50         @Override
51         public void onSessionUp(final BGPSession session) {
52                 logger.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
53         }
54
55         @Override
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);
60                 }
61         }
62
63         @Override
64         public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
65                 logger.info("Session with peer {} terminated: {}", this.name, cause);
66         }
67
68         @Override
69         public final String toString() {
70                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
71         }
72
73         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
74                 toStringHelper.add("name", this.name);
75                 toStringHelper.add("tables", this.tables);
76                 return toStringHelper;
77         }
78 }