2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import javax.annotation.concurrent.ThreadSafe;
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
14 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
15 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
16 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.Nlri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Objects;
39 import com.google.common.base.Objects.ToStringHelper;
40 import com.google.common.base.Preconditions;
41 import com.google.common.util.concurrent.FutureCallback;
42 import com.google.common.util.concurrent.Futures;
43 import com.google.common.util.concurrent.JdkFutureAdapters;
46 public class RIBImpl {
47 private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
48 private static final Update EOR = new UpdateBuilder().build();
49 private final DataProviderService dps;
50 private final RIBTables tables;
52 public RIBImpl(final RIBExtensionConsumerContext extensions, final DataProviderService dps) {
53 this.dps = Preconditions.checkNotNull(dps);
54 this.tables = new RIBTables(BGPObjectComparator.INSTANCE, extensions);
57 synchronized void updateTables(final BGPPeer peer, final Update message) {
58 final DataModificationTransaction trans = this.dps.beginTransaction();
60 if (EOR.equals(message)) {
61 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
63 ari.markUptodate(trans, peer);
65 LOG.debug("End-of-RIB for IPv4 Unicast ignored");
70 final WithdrawnRoutes wr = message.getWithdrawnRoutes();
72 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
74 ari.removeRoutes(trans, peer, new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
75 new WithdrawnRoutesBuilder().setDestinationType(new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build());
77 LOG.debug("Not removing objects from unhandled IPv4 Unicast");
81 final PathAttributes attrs = message.getPathAttributes();
82 final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
84 final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
86 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
88 ari.removeRoutes(trans, peer, nlri);
90 LOG.debug("Not removing objects from unhandled NLRI {}", nlri);
94 final Nlri ar = message.getNlri();
96 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
98 ari.addRoutes(trans, peer, new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).
99 setCNextHop(attrs.getCNextHop()).setAdvertizedRoutes(
100 new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build(), attrs);
102 LOG.debug("Not adding objects from unhandled IPv4 Unicast");
106 final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
108 final MpReachNlri nlri = mpr.getMpReachNlri();
110 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
112 ari.addRoutes(trans, peer, nlri, attrs);
113 if (message.equals(ari.endOfRib())) {
114 ari.markUptodate(trans, peer);
117 LOG.debug("Not adding objects from unhandled NLRI {}", nlri);
121 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
123 public void onSuccess(final RpcResult<TransactionStatus> result) {
124 LOG.debug("RIB modification successfully committed.");
128 public void onFailure(final Throwable t) {
129 LOG.error("Failed to commit RIB modification", t);
134 synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
135 final AdjRIBsIn ari = this.tables.get(key);
137 final DataModificationTransaction trans = this.dps.beginTransaction();
138 ari.clear(trans, peer);
140 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
142 public void onSuccess(final RpcResult<TransactionStatus> result) {
147 public void onFailure(final Throwable t) {
148 LOG.error("Failed to commit RIB modification", t);
155 public final String toString() {
156 return addToStringAttributes(Objects.toStringHelper(this)).toString();
159 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
160 return toStringHelper;