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