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.rev180329.Route;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
25 import org.opendaylight.yangtools.yang.binding.ChildOf;
26 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.Identifiable;
29 import org.opendaylight.yangtools.yang.binding.Identifier;
30 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
31 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
38 private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
40 private final ConcurrentMap<TablesKey, RIBSupport<?, ?, ?, ?>> supports = new ConcurrentHashMap<>();
41 private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport<?, ?, ?, ?>> domSupports =
42 new ConcurrentHashMap<>();
44 private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
47 public <T extends RIBSupport<?, ?, ?, ?>> RIBSupportRegistration<T> registerRIBSupport(
48 final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi,
50 final TablesKey key = new TablesKey(afi, safi);
51 final RIBSupport<?, ?, ?, ?> prev = this.supports.putIfAbsent(key, support);
52 Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s",
54 this.domSupports.put(RibSupportUtils.toYangTablesKey(afi, safi), support);
55 addClassLoadingSupport(afi, safi, support);
56 return new AbstractRIBSupportRegistration<T>(support) {
58 protected void removeRegistration() {
59 SimpleRIBExtensionProviderContext.this.supports.remove(key);
64 private void addClassLoadingSupport(final Class<?> afi, final Class<?> safi, final RIBSupport<?, ?, ?, ?> support) {
65 final Set<YangModuleInfo> moduleInfos = getModuleInfos(afi, safi, support.routesListClass(),
66 support.routesContainerClass(), support.routesCaseClass());
67 if (!moduleInfos.isEmpty()) {
68 this.classLoadingStrategy.addModuleInfos(moduleInfos);
72 @SuppressWarnings("checkstyle:IllegalCatch")
73 private static Set<YangModuleInfo> getModuleInfos(final Class<?>... clazzes) {
74 final Set<YangModuleInfo> moduleInfos = new HashSet<>();
75 for (final Class<?> clz : clazzes) {
77 moduleInfos.add(BindingReflections.getModuleInfo(clz));
78 } catch (final Exception e) {
79 LOG.debug("Could not find module info for class {}", clz, e);
86 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<C>,
87 R extends Route & ChildOf<S> & Identifiable<I>, I extends Identifier<R>> RIBSupport<C, S, R, I> getRIBSupport(
88 final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
89 return getRIBSupport(new TablesKey(afi, safi));
93 @SuppressWarnings("unchecked")
94 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<C>,
95 R extends Route & ChildOf<S> & Identifiable<I>, I extends Identifier<R>> RIBSupport<C, S, R, I> getRIBSupport(
96 final TablesKey key) {
97 return (RIBSupport<C, S, R, I>) this.supports.get(requireNonNull(key));
101 @SuppressWarnings("unchecked")
102 public <C extends Routes & DataObject & ChoiceIn<Tables>, S extends ChildOf<C>,
103 R extends Route & ChildOf<S> & Identifiable<I>, I extends Identifier<R>> RIBSupport<C, S, R, I> getRIBSupport(
104 final NodeIdentifierWithPredicates key) {
105 return (RIBSupport<C, S, R, I>) this.domSupports.get(key);
109 public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
110 return this.classLoadingStrategy;