Merge "Unify YANG->DTO generation plugin definition"
[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
27 /**
28  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
29  * RIB actions.
30  */
31 public final class BGPPeer implements BGPSessionListener, Peer {
32         private static final Logger logger = LoggerFactory.getLogger(BGPPeer.class);
33         private Set<TablesKey> tables;
34         private final String name;
35         private final RIBImpl rib;
36
37         public BGPPeer(final RIBImpl rib, final String name) {
38                 this.rib = Preconditions.checkNotNull(rib);
39                 this.name = Preconditions.checkNotNull(name);
40         }
41
42         @Override
43         public void onMessage(final BGPSession session, final Notification message) {
44                 if (message instanceof Update) {
45                         this.rib.updateTables(this, (Update) message);
46                 } else {
47                         logger.info("Ignoring unhandled message class " + message.getClass());
48                 }
49         }
50
51         @Override
52         public void onSessionUp(final BGPSession session) {
53                 logger.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
54
55                 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
56                         this.tables.add(new TablesKey(t.getAfi(), t.getSafi()));
57                 }
58         }
59
60         private void cleanup() {
61                 // FIXME: support graceful restart
62                 for (final TablesKey key : this.tables) {
63                         this.rib.clearTable(this, key);
64                 }
65
66                 this.tables.clear();
67         }
68
69         @Override
70         public void onSessionDown(final BGPSession session, final Exception e) {
71                 logger.info("Session with peer {} went down", this.name, e);
72                 cleanup();
73         }
74
75         @Override
76         public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
77                 logger.info("Session with peer {} terminated: {}", this.name, cause);
78                 cleanup();
79         }
80
81         @Override
82         public final String toString() {
83                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
84         }
85
86         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
87                 toStringHelper.add("name", this.name);
88                 toStringHelper.add("tables", this.tables);
89                 return toStringHelper;
90         }
91 }