2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.impl;
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.ArrayList;
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.List;
21 import javax.annotation.concurrent.GuardedBy;
22 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
23 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistration;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistrator;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteTable;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
31 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
59 import org.opendaylight.yangtools.yang.binding.Notification;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
65 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
68 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRuntimeMXBean, TransactionChainListener {
70 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
73 private final Set<TablesKey> tables = new HashSet<>();
74 private final RIB rib;
75 private final String name;
78 private BGPSession session;
80 private byte[] rawIdentifier;
82 private AdjRIBsOutRegistration reg;
84 private BGPPeerRuntimeRegistrator registrator;
85 private BGPPeerRuntimeRegistration runtimeReg;
86 private long sessionEstablishedCounter = 0L;
89 private AdjRibInWriter ribWriter;
91 public BGPPeer(final String name, final RIB rib) {
92 this.rib = Preconditions.checkNotNull(rib);
95 final DOMTransactionChain chain = rib.createPeerChain(this);
96 // FIXME: make this configurable
97 final PeerRole role = PeerRole.Ibgp;
99 this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), role, chain);
103 public synchronized void close() {
105 // TODO should this perform cleanup ?
109 public void onMessage(final BGPSession session, final Notification msg) {
110 if (!(msg instanceof Update)) {
111 LOG.info("Ignoring unhandled message class {}", msg.getClass());
114 final Update message = (Update) msg;
115 this.rib.updateTables(this, message);
117 final PathAttributes attrs = message.getPathAttributes();
118 MpReachNlri mpReach = null;
119 if (message.getNlri() != null) {
120 mpReach = prefixesToMpReach(message);
121 } else if (attrs != null && attrs.getAugmentation(PathAttributes1.class) != null) {
122 mpReach = attrs.getAugmentation(PathAttributes1.class).getMpReachNlri();
124 if (mpReach != null) {
125 this.ribWriter.updateRoutes(mpReach, attrs);
128 MpUnreachNlri mpUnreach = null;
129 if (message.getWithdrawnRoutes() != null) {
130 mpUnreach = prefixesToMpUnreach(message);
131 } else if (attrs != null && attrs.getAugmentation(PathAttributes2.class) != null) {
132 mpUnreach = attrs.getAugmentation(PathAttributes2.class).getMpUnreachNlri();
134 if (mpUnreach != null) {
135 this.ribWriter.removeRoutes(mpUnreach);
140 * Creates MPReach for the prefixes to be handled in the same way as linkstate routes
142 * @param message Update message containing prefixes in NLRI
143 * @return MpReachNlri with prefixes from the nlri field
145 private MpReachNlri prefixesToMpReach(final Update message) {
146 final List<Ipv4Prefixes> prefixes = new ArrayList<>();
147 for (final Ipv4Prefix p : message.getNlri().getNlri()) {
148 prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build());
150 final MpReachNlriBuilder b = new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
151 UnicastSubsequentAddressFamily.class).setAdvertizedRoutes(
152 new AdvertizedRoutesBuilder().setDestinationType(
153 new DestinationIpv4CaseBuilder().setDestinationIpv4(
154 new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build());
155 if (message.getPathAttributes() != null) {
156 b.setCNextHop(message.getPathAttributes().getCNextHop());
162 * Create MPUnreach for the prefixes to be handled in the same way as linkstate routes
164 * @param message Update message containing withdrawn routes
165 * @return MpUnreachNlri with prefixes from the withdrawn routes field
167 private MpUnreachNlri prefixesToMpUnreach(final Update message) {
168 final List<Ipv4Prefixes> prefixes = new ArrayList<>();
169 for (final Ipv4Prefix p : message.getWithdrawnRoutes().getWithdrawnRoutes()) {
170 prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build());
172 return new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
173 new WithdrawnRoutesBuilder().setDestinationType(
174 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
175 new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build()).build();
179 public synchronized void onSessionUp(final BGPSession session) {
180 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
182 this.session = session;
183 this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
185 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
186 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
188 this.tables.add(key);
189 this.rib.initTable(this, key);
192 this.ribWriter = this.ribWriter.transform(session.getBgpId(), this.rib.getRibSupportContext(), this.tables);
194 // Not particularly nice, but what can
195 if (session instanceof BGPSessionImpl) {
196 this.reg = this.rib.registerRIBsOut(this, new SessionRIBsOut((BGPSessionImpl) session));
198 this.sessionEstablishedCounter++;
199 if (this.registrator != null) {
200 this.runtimeReg = this.registrator.register(this);
204 private synchronized void cleanup() {
205 // FIXME: BUG-196: support graceful restart
206 this.ribWriter.cleanTables(this.tables);
207 for (final TablesKey key : this.tables) {
208 this.rib.clearTable(this, key);
211 if (this.reg != null) {
220 public void onSessionDown(final BGPSession session, final Exception e) {
221 LOG.info("Session with peer {} went down", this.name, e);
226 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
227 LOG.info("Session with peer {} terminated: {}", this.name, cause);
232 public String toString() {
233 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
236 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
237 toStringHelper.add("name", this.name);
238 toStringHelper.add("tables", this.tables);
239 return toStringHelper;
243 public String getName() {
247 protected RIB getRib() {
252 public void releaseConnection() {
258 private void dropConnection() {
259 if (this.runtimeReg != null) {
260 this.runtimeReg.close();
261 this.runtimeReg = null;
263 if (this.session != null) {
264 this.session.close();
270 public boolean isSessionActive() {
271 return this.session != null;
275 public synchronized byte[] getRawIdentifier() {
276 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
280 public void resetSession() {
285 public void resetStats() {
286 if (this.session instanceof BGPSessionStatistics) {
287 ((BGPSessionStatistics) this.session).resetSessionStats();
291 public synchronized void registerRootRuntimeBean(final BGPPeerRuntimeRegistrator registrator) {
292 this.registrator = registrator;
296 public BgpSessionState getBgpSessionState() {
297 if (this.session instanceof BGPSessionStatistics) {
298 return ((BGPSessionStatistics) this.session).getBgpSesionState();
300 return new BgpSessionState();
304 public synchronized BgpPeerState getBgpPeerState() {
305 final BgpPeerState peerState = new BgpPeerState();
306 final List<RouteTable> routes = Lists.newArrayList();
307 for (final TablesKey tablesKey : this.tables) {
308 final RouteTable routeTable = new RouteTable();
309 routeTable.setTableType("afi=" + tablesKey.getAfi().getSimpleName() + ",safi=" + tablesKey.getSafi().getSimpleName());
310 routeTable.setRoutesCount(this.rib.getRoutesCount(tablesKey));
311 routes.add(routeTable);
313 peerState.setRouteTable(routes);
314 peerState.setSessionEstablishedCount(this.sessionEstablishedCounter);
319 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
320 // TODO Auto-generated method stub
325 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
326 // TODO Auto-generated method stub