BUG-1116: Move BGPSession and friends to a more private place
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.bgp.rib.impl;
10
11 import com.google.common.base.Objects;
12 import com.google.common.base.Objects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import com.google.common.net.InetAddresses;
15
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import javax.annotation.concurrent.GuardedBy;
20
21 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
23 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
24 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
25 import org.opendaylight.protocol.bgp.rib.spi.Peer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 /**
35  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
36  * RIB actions.
37  */
38 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
39     private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
40
41     @GuardedBy("this")
42     private final Set<TablesKey> tables = new HashSet<>();
43     private final RIB rib;
44     private final String name;
45
46     private BGPSession session;
47     private byte[] rawIdentifier;
48
49     public BGPPeer(final String name, final RIB rib) {
50         this.rib = Preconditions.checkNotNull(rib);
51         this.name = name;
52     }
53
54     @Override
55     public synchronized void close() {
56         dropConnection();
57         // TODO should this perform cleanup ?
58     }
59
60     @Override
61     public void onMessage(final BGPSession session, final Notification message) {
62         if (message instanceof Update) {
63             this.rib.updateTables(this, (Update) message);
64         } else {
65             LOG.info("Ignoring unhandled message class {}", message.getClass());
66         }
67     }
68
69     @Override
70     public synchronized void onSessionUp(final BGPSession session) {
71         LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
72
73         this.session = session;
74         this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
75
76         for (final BgpTableType t : session.getAdvertisedTableTypes()) {
77             final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
78
79             this.tables.add(key);
80             this.rib.initTable(this, key);
81         }
82     }
83
84     private synchronized void cleanup() {
85         // FIXME: BUG-196: support graceful restart
86         for (final TablesKey key : this.tables) {
87             this.rib.clearTable(this, key);
88         }
89
90         this.tables.clear();
91         this.session = null;
92     }
93
94     @Override
95     public void onSessionDown(final BGPSession session, final Exception e) {
96         LOG.info("Session with peer {} went down", this.name, e);
97         cleanup();
98     }
99
100     @Override
101     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
102         LOG.info("Session with peer {} terminated: {}", this.name, cause);
103         cleanup();
104     }
105
106     @Override
107     public String toString() {
108         return addToStringAttributes(Objects.toStringHelper(this)).toString();
109     }
110
111     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
112         toStringHelper.add("name", this.name);
113         toStringHelper.add("tables", this.tables);
114         return toStringHelper;
115     }
116
117     @Override
118     public String getName() {
119         return this.name;
120     }
121
122     protected RIB getRib() {
123         return this.rib;
124     }
125
126     @Override
127     public void releaseConnection() {
128         dropConnection();
129         cleanup();
130     }
131
132     private void dropConnection() {
133         if (this.session != null) {
134             this.session.close();
135             this.session = null;
136         }
137     }
138
139     @Override
140     public byte[] getRawIdentifier() {
141         return rawIdentifier;
142     }
143 }