f2e16e3cb872b064821394ee7f8d9c45019cd31d
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / SimpleRIBExtensionProviderContext.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.util.HashSet;
14 import java.util.Set;
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.mdsal.binding.spec.reflect.BindingReflections;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
26 import org.opendaylight.yangtools.yang.binding.ChildOf;
27 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.Identifier;
31 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
37
38     private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
39
40     private final ConcurrentMap<TablesKey, RIBSupport<?, ?, ?, ?>> supports = new ConcurrentHashMap<>();
41     private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport<?, ?, ?, ?>> domSupports =
42             new ConcurrentHashMap<>();
43
44     private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
45
46     @Override
47     public <T extends RIBSupport<?, ?, ?, ?>> RIBSupportRegistration<T> registerRIBSupport(
48             final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi,
49             final T support) {
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",
53                 afi, safi, prev);
54         this.domSupports.put(RibSupportUtils.toYangTablesKey(afi, safi), support);
55         addClassLoadingSupport(afi, safi, support);
56         return new AbstractRIBSupportRegistration<T>(support) {
57             @Override
58             protected void removeRegistration() {
59                 SimpleRIBExtensionProviderContext.this.supports.remove(key);
60             }
61         };
62     }
63
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);
69         }
70     }
71
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) {
76             try {
77                 moduleInfos.add(BindingReflections.getModuleInfo(clz));
78             } catch (final Exception e) {
79                 LOG.debug("Could not find module info for class {}", clz, e);
80             }
81         }
82         return moduleInfos;
83     }
84
85     @Override
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));
90     }
91
92     @Override
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));
98     }
99
100     @Override
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);
106     }
107
108     @Override
109     public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
110         return this.classLoadingStrategy;
111     }
112 }