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 java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import java.util.HashSet;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
18 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
22 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
30 private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
32 private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
33 private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport> domSupports = new ConcurrentHashMap<>();
35 private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
38 public <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(final Class<? extends AddressFamily> afi,
39 final Class<? extends SubsequentAddressFamily> safi, final T support) {
40 final TablesKey key = new TablesKey(afi, safi);
41 final RIBSupport prev = this.supports.putIfAbsent(key, support);
42 Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
43 this.domSupports.put(RibSupportUtils.toYangTablesKey(afi,safi), support);
44 addClassLoadingSupport(afi, safi, support);
45 return new AbstractRIBSupportRegistration<T>(support) {
47 protected void removeRegistration() {
48 SimpleRIBExtensionProviderContext.this.supports.remove(key);
53 private void addClassLoadingSupport(final Class<?> afi, final Class<?> safi, final RIBSupport s) {
54 final Set<YangModuleInfo> moduleInfos =
55 getModuleInfos(afi, safi, s.routesListClass(), s.routesContainerClass(), s.routesCaseClass());
56 if(!moduleInfos.isEmpty()) {
57 this.classLoadingStrategy.addModuleInfos(moduleInfos);
61 private static Set<YangModuleInfo> getModuleInfos(final Class<?>... clazzes) {
62 final Set<YangModuleInfo> moduleInfos = new HashSet<>();
63 for(final Class<?> clz : clazzes) {
65 moduleInfos.add(BindingReflections.getModuleInfo(clz));
66 } catch (final Exception e) {
67 LOG.debug("Could not find module info for class {}", clz, e);
74 public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
75 return getRIBSupport(new TablesKey(afi, safi));
79 public RIBSupport getRIBSupport(final TablesKey key) {
80 return this.supports.get(requireNonNull(key));
84 public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
85 return this.classLoadingStrategy;
89 public RIBSupport getRIBSupport(final NodeIdentifierWithPredicates key) {
90 return this.domSupports.get(key);