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 com.google.common.base.Preconditions;
12 import java.util.HashSet;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
19 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
20 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
21 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
22 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
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 {
29 private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
31 private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
32 private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport> domSupports = new ConcurrentHashMap<>();
34 private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
37 public <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(final Class<? extends AddressFamily> afi,
38 final Class<? extends SubsequentAddressFamily> safi, final T support) {
39 final TablesKey key = new TablesKey(afi, safi);
40 final RIBSupport prev = this.supports.putIfAbsent(key, support);
41 Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
42 this.domSupports.put(RibSupportUtils.toYangTablesKey(afi,safi), support);
43 addClassLoadingSupport(afi, safi, support);
44 return new AbstractRIBSupportRegistration<T>(support) {
46 protected void removeRegistration() {
47 SimpleRIBExtensionProviderContext.this.supports.remove(key);
52 private void addClassLoadingSupport(final Class<?> afi, final Class<?> safi, final RIBSupport s) {
53 final Set<YangModuleInfo> moduleInfos =
54 getModuleInfos(afi, safi, s.routesListClass(), s.routesContainerClass(), s.routesCaseClass());
55 if(!moduleInfos.isEmpty()) {
56 classLoadingStrategy.addModuleInfos(moduleInfos);
60 private static Set<YangModuleInfo> getModuleInfos(final Class<?>... clazzes) {
61 final Set<YangModuleInfo> moduleInfos = new HashSet<>();
62 for(final Class<?> clz : clazzes) {
64 moduleInfos.add(BindingReflections.getModuleInfo(clz));
65 } catch (final Exception e) {
66 LOG.debug("Could not find module info for class {}", clz, e);
73 public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
74 return getRIBSupport(new TablesKey(afi, safi));
78 public RIBSupport getRIBSupport(final TablesKey key) {
79 return this.supports.get(Preconditions.checkNotNull(key));
83 public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
84 return classLoadingStrategy;
88 public RIBSupport getRIBSupport(final NodeIdentifierWithPredicates key) {
89 return domSupports.get(key);