BUG-2383: wire up AdjRibInWriter
[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.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Lists;
15 import com.google.common.net.InetAddresses;
16 import java.util.Arrays;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20 import javax.annotation.concurrent.GuardedBy;
21 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
22 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistration;
23 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistrator;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteTable;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
30 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
31 import org.opendaylight.protocol.bgp.rib.RibReference;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.AdjRIBsOutRegistration;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
35 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
36 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
37 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
38 import org.opendaylight.protocol.bgp.rib.spi.Peer;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
43 import org.opendaylight.yangtools.yang.binding.Notification;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48 /**
49  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
50  * RIB actions.
51  */
52 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRuntimeMXBean, TransactionChainListener {
53
54     private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
55
56     @GuardedBy("this")
57     private final Set<TablesKey> tables = new HashSet<>();
58     private final RIB rib;
59     private final String name;
60
61     @GuardedBy("this")
62     private BGPSession session;
63     @GuardedBy("this")
64     private byte[] rawIdentifier;
65     @GuardedBy("this")
66     private AdjRIBsOutRegistration reg;
67
68     private BGPPeerRuntimeRegistrator registrator;
69     private BGPPeerRuntimeRegistration runtimeReg;
70     private long sessionEstablishedCounter = 0L;
71
72     @GuardedBy("this")
73     private AdjRibInWriter ribWriter;
74
75     public BGPPeer(final String name, final RIB rib) {
76         this.rib = Preconditions.checkNotNull(rib);
77         this.name = name;
78
79         final DOMTransactionChain chain = rib.createPeerChain(this);
80         // FIXME: make this configurable
81         final PeerRole role = PeerRole.Ibgp;
82
83         ribWriter = AdjRibInWriter.create(((RibReference)rib).getInstanceIdentifier().getKey(), role, chain);
84     }
85
86     @Override
87     public synchronized void close() {
88         dropConnection();
89         // TODO should this perform cleanup ?
90     }
91
92     @Override
93     public void onMessage(final BGPSession session, final Notification message) {
94         if (message instanceof Update) {
95             this.rib.updateTables(this, (Update) message);
96         } else {
97             LOG.info("Ignoring unhandled message class {}", message.getClass());
98         }
99     }
100
101     @Override
102     public synchronized void onSessionUp(final BGPSession session) {
103         LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
104
105         this.session = session;
106         this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
107
108         for (final BgpTableType t : session.getAdvertisedTableTypes()) {
109             final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
110
111             this.tables.add(key);
112             this.rib.initTable(this, key);
113         }
114
115         this.ribWriter = ribWriter.transform(session.getBgpId(), rib.getRibExtensions(), tables);
116
117         // Not particularly nice, but what can
118         if (session instanceof BGPSessionImpl) {
119             this.reg = this.rib.registerRIBsOut(this, new SessionRIBsOut((BGPSessionImpl) session));
120         }
121         this.sessionEstablishedCounter++;
122         if (this.registrator != null) {
123             this.runtimeReg = this.registrator.register(this);
124         }
125     }
126
127     private synchronized void cleanup() {
128         // FIXME: BUG-196: support graceful restart
129         this.ribWriter.cleanTables(tables);
130         for (final TablesKey key : this.tables) {
131             this.rib.clearTable(this, key);
132         }
133
134         if (this.reg != null) {
135             this.reg.close();
136             this.reg = null;
137         }
138
139         this.tables.clear();
140     }
141
142     @Override
143     public void onSessionDown(final BGPSession session, final Exception e) {
144         LOG.info("Session with peer {} went down", this.name, e);
145         releaseConnection();
146     }
147
148     @Override
149     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
150         LOG.info("Session with peer {} terminated: {}", this.name, cause);
151         releaseConnection();
152     }
153
154     @Override
155     public String toString() {
156         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
157     }
158
159     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
160         toStringHelper.add("name", this.name);
161         toStringHelper.add("tables", this.tables);
162         return toStringHelper;
163     }
164
165     @Override
166     public String getName() {
167         return this.name;
168     }
169
170     protected RIB getRib() {
171         return this.rib;
172     }
173
174     @Override
175     public void releaseConnection() {
176         dropConnection();
177         cleanup();
178     }
179
180     @GuardedBy("this")
181     private void dropConnection() {
182         if (this.runtimeReg != null) {
183             this.runtimeReg.close();
184             this.runtimeReg = null;
185         }
186         if (this.session != null) {
187             this.session.close();
188             this.session = null;
189         }
190     }
191
192     @Override
193     public boolean isSessionActive() {
194         return this.session != null;
195     }
196
197     @Override
198     public synchronized byte[] getRawIdentifier() {
199         return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
200     }
201
202     @Override
203     public void resetSession() {
204         releaseConnection();
205     }
206
207     @Override
208     public void resetStats() {
209         if (this.session instanceof BGPSessionStatistics) {
210             ((BGPSessionStatistics) this.session).resetSessionStats();
211         }
212     }
213
214     public synchronized void registerRootRuntimeBean(final BGPPeerRuntimeRegistrator registrator) {
215         this.registrator = registrator;
216     }
217
218     @Override
219     public BgpSessionState getBgpSessionState() {
220         if (this.session instanceof BGPSessionStatistics) {
221             return ((BGPSessionStatistics) this.session).getBgpSesionState();
222         }
223         return new BgpSessionState();
224     }
225
226     @Override
227     public synchronized BgpPeerState getBgpPeerState() {
228         final BgpPeerState peerState = new BgpPeerState();
229         final List<RouteTable> routes = Lists.newArrayList();
230         for (final TablesKey tablesKey : this.tables) {
231             final RouteTable routeTable = new RouteTable();
232             routeTable.setTableType("afi=" + tablesKey.getAfi().getSimpleName() + ",safi=" + tablesKey.getSafi().getSimpleName());
233             routeTable.setRoutesCount(this.rib.getRoutesCount(tablesKey));
234             routes.add(routeTable);
235         }
236         peerState.setRouteTable(routes);
237         peerState.setSessionEstablishedCount(this.sessionEstablishedCounter);
238         return peerState;
239     }
240
241     @Override
242     public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
243         // TODO Auto-generated method stub
244
245     }
246
247     @Override
248     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
249         // TODO Auto-generated method stub
250     }
251 }