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.Future;
12 import javax.annotation.concurrent.ThreadSafe;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.data.DataModification;
16 import org.opendaylight.controller.sal.binding.api.data.DataModification.TransactionStatus;
17 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
18 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Update;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes1;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpReachNlri;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpUnreachNlri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Objects;
31 import com.google.common.base.Objects.ToStringHelper;
32 import com.google.common.base.Preconditions;
35 public final class RIBImpl {
36 private static final Logger logger = LoggerFactory.getLogger(RIBImpl.class);
37 private final DataProviderService dps;
38 private final RIBTables tables;
39 private final String name;
41 public RIBImpl(final String name, final ProviderContext context) {
42 this.name = Preconditions.checkNotNull(name);
43 this.dps = context.getSALService(DataProviderService.class);
44 this.tables = new RIBTables(new BGPObjectComparator(), AdjRIBsInFactoryRegistryImpl.INSTANCE);
47 synchronized void updateTables(final BGPPeer peer, final Update message) {
48 final DataModification trans = dps.beginTransaction();
50 //remove(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
51 // trans, peer, message.getWithdrawnRoutes().getWithdrawnRoutes().iterator());
53 final PathAttributes attrs = message.getPathAttributes();
54 final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
56 final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
58 AdjRIBsIn ari = tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
60 ari.removeRoutes(trans, peer, nlri);
62 logger.debug("Not removing objects from unhandled NLRI {}", nlri);
66 //add(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
67 // trans, peer, message.getNlri().getNlri().iterator(), attrs);
69 final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
71 final MpReachNlri nlri = mpr.getMpReachNlri();
73 final AdjRIBsIn ari = tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
75 ari.addRoutes(trans, peer, nlri, attrs);
77 logger.debug("Not adding objects from unhandled NLRI {}", nlri);
81 // FIXME: we need to attach to this future for failures
82 Future<RpcResult<TransactionStatus>> f = trans.commit();
85 synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
86 final AdjRIBsIn ari = tables.get(key);
88 final DataModification trans = dps.beginTransaction();
89 ari.clear(trans, peer);
91 // FIXME: we need to attach to this future for failures
92 Future<RpcResult<TransactionStatus>> f = trans.commit();
97 public final String toString() {
98 return addToStringAttributes(Objects.toStringHelper(this)).toString();
101 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
102 toStringHelper.add("name", this.name);
103 return toStringHelper;