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.DestinationIpv4CaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.destination.ipv4._case.DestinationIpv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.base.Objects;
40 import com.google.common.base.Objects.ToStringHelper;
41 import com.google.common.base.Preconditions;
42 import com.google.common.util.concurrent.FutureCallback;
43 import com.google.common.util.concurrent.Futures;
44 import com.google.common.util.concurrent.JdkFutureAdapters;
47 public class RIBImpl {
48 private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
49 private static final Update EOR = new UpdateBuilder().build();
50 private final DataProviderService dps;
51 private final RIBTables tables;
53 public RIBImpl(final RIBExtensionConsumerContext extensions, final DataProviderService dps) {
54 this.dps = Preconditions.checkNotNull(dps);
55 this.tables = new RIBTables(BGPObjectComparator.INSTANCE, extensions);
58 synchronized void updateTables(final BGPPeer peer, final Update message) {
59 final DataModificationTransaction trans = this.dps.beginTransaction();
61 if (EOR.equals(message)) {
62 final AdjRIBsIn ari = this.tables.getOrCreate(trans,
63 new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
65 ari.markUptodate(trans, peer);
67 LOG.debug("End-of-RIB for IPv4 Unicast ignored");
72 final WithdrawnRoutes wr = message.getWithdrawnRoutes();
74 final AdjRIBsIn ari = this.tables.getOrCreate(trans,
75 new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
80 new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
81 new WithdrawnRoutesBuilder().setDestinationType(
82 new DestinationIpv4CaseBuilder().setDestinationIpv4(
83 new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build()).build());
85 LOG.debug("Not removing objects from unhandled IPv4 Unicast");
89 final PathAttributes attrs = message.getPathAttributes();
90 final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
92 final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
94 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
96 ari.removeRoutes(trans, peer, nlri);
98 LOG.debug("Not removing objects from unhandled NLRI {}", nlri);
102 final Nlri ar = message.getNlri();
104 final AdjRIBsIn ari = this.tables.getOrCreate(trans,
105 new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
110 new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setCNextHop(
111 attrs.getCNextHop()).setAdvertizedRoutes(
112 new AdvertizedRoutesBuilder().setDestinationType(
113 new DestinationIpv4CaseBuilder().setDestinationIpv4(
114 new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build()).build(),
117 LOG.debug("Not adding objects from unhandled IPv4 Unicast");
121 final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
123 final MpReachNlri nlri = mpr.getMpReachNlri();
125 final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
127 ari.addRoutes(trans, peer, nlri, attrs);
128 if (message.equals(ari.endOfRib())) {
129 ari.markUptodate(trans, peer);
132 LOG.debug("Not adding objects from unhandled NLRI {}", nlri);
136 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
138 public void onSuccess(final RpcResult<TransactionStatus> result) {
139 LOG.debug("RIB modification successfully committed.");
143 public void onFailure(final Throwable t) {
144 LOG.error("Failed to commit RIB modification", t);
149 synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
150 final AdjRIBsIn ari = this.tables.get(key);
152 final DataModificationTransaction trans = this.dps.beginTransaction();
153 ari.clear(trans, peer);
155 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
157 public void onSuccess(final RpcResult<TransactionStatus> result) {
162 public void onFailure(final Throwable t) {
163 LOG.error("Failed to commit RIB modification", t);
170 public final String toString() {
171 return addToStringAttributes(Objects.toStringHelper(this)).toString();
174 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
175 return toStringHelper;