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 java.util.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
13 import javax.annotation.concurrent.ThreadSafe;
15 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
16 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
17 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
18 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
19 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Update;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpReachNlri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpUnreachNlri;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.Objects;
32 import com.google.common.base.Objects.ToStringHelper;
33 import com.google.common.base.Preconditions;
36 public class RIBImpl {
37 private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
38 private final DataProviderService dps;
39 private final RIBTables tables;
41 public RIBImpl(final RIBExtensionConsumerContext extensions, final DataProviderService dps) {
42 this.dps = Preconditions.checkNotNull(dps);
43 this.tables = new RIBTables(BGPObjectComparator.INSTANCE, extensions);
46 synchronized void updateTables(final BGPPeer peer, final Update message) {
47 final DataModificationTransaction trans = this.dps.beginTransaction();
49 // FIXME: detect and handle end-of-RIB markers
51 // remove(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
52 // trans, peer, message.getWithdrawnRoutes().getWithdrawnRoutes().iterator());
54 final PathAttributes attrs = message.getPathAttributes();
55 final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
57 final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
59 final AdjRIBsIn ari = this.tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
61 ari.removeRoutes(trans, peer, nlri);
63 LOG.debug("Not removing objects from unhandled NLRI {}", nlri);
67 // add(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
68 // trans, peer, message.getNlri().getNlri().iterator(), attrs);
70 final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
72 final MpReachNlri nlri = mpr.getMpReachNlri();
74 final AdjRIBsIn ari = this.tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
76 ari.addRoutes(trans, peer, nlri, attrs);
78 LOG.debug("Not adding objects from unhandled NLRI {}", nlri);
82 // FIXME: we need to attach to this future for failures
83 final Future<RpcResult<TransactionStatus>> f = trans.commit();
86 } catch (InterruptedException | ExecutionException e) {
87 LOG.error("Failed to commit RIB modification", e);
91 synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
92 final AdjRIBsIn ari = this.tables.get(key);
94 final DataModificationTransaction trans = this.dps.beginTransaction();
95 ari.clear(trans, peer);
97 // FIXME: we need to attach to this future for failures
98 final Future<RpcResult<TransactionStatus>> f = trans.commit();
101 } catch (InterruptedException | ExecutionException e) {
102 LOG.error("Failed to commit RIB modification", e);
108 public String toString() {
109 return addToStringAttributes(Objects.toStringHelper(this)).toString();
112 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
113 return toStringHelper;