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.spi;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
20 import org.opendaylight.yangtools.yang.binding.ChildOf;
21 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
28 private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
30 private final ConcurrentMap<TablesKey, RIBSupport<?, ?>> supports = new ConcurrentHashMap<>();
31 private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport<?, ?>> domSupports = new ConcurrentHashMap<>();
34 public <T extends RIBSupport<?, ?>> RIBSupportRegistration<T> registerRIBSupport(
35 final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi,
37 final TablesKey key = new TablesKey(afi, safi);
38 final RIBSupport<?, ?> prev = this.supports.putIfAbsent(key, support);
39 checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
40 this.domSupports.put(RibSupportUtils.toYangTablesKey(afi, safi), support);
41 return new AbstractRIBSupportRegistration<>(support) {
43 protected void removeRegistration() {
44 // FIXME: clean up registrations, too
45 SimpleRIBExtensionProviderContext.this.supports.remove(key);
51 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<C>> RIBSupport<C, S> getRIBSupport(
52 final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
53 return getRIBSupport(new TablesKey(afi, safi));
57 @SuppressWarnings("unchecked")
58 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<C>> RIBSupport<C, S> getRIBSupport(
59 final TablesKey key) {
60 return (RIBSupport<C, S>) this.supports.get(requireNonNull(key));
64 @SuppressWarnings("unchecked")
65 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<C>> RIBSupport<C, S> getRIBSupport(
66 final NodeIdentifierWithPredicates key) {
67 return (RIBSupport<C, S>) this.domSupports.get(key);