MVPN RFC6514 Extendend communities
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.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;
27
28 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
29
30     private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
31
32     private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
33     private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport> domSupports = new ConcurrentHashMap<>();
34
35     private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
36
37     @Override
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",
43                 afi, safi, prev);
44         this.domSupports.put(RibSupportUtils.toYangTablesKey(afi, safi), support);
45         addClassLoadingSupport(afi, safi, support);
46         return new AbstractRIBSupportRegistration<T>(support) {
47             @Override
48             protected void removeRegistration() {
49                 SimpleRIBExtensionProviderContext.this.supports.remove(key);
50             }
51         };
52     }
53
54     private void addClassLoadingSupport(final Class<?> afi, final Class<?> safi, final RIBSupport support) {
55         final Set<YangModuleInfo> moduleInfos = getModuleInfos(afi, safi, support.routesListClass(),
56                 support.routesContainerClass(), support.routesCaseClass());
57         if (!moduleInfos.isEmpty()) {
58             this.classLoadingStrategy.addModuleInfos(moduleInfos);
59         }
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     private static Set<YangModuleInfo> getModuleInfos(final Class<?>... clazzes) {
64         final Set<YangModuleInfo> moduleInfos = new HashSet<>();
65         for (final Class<?> clz : clazzes) {
66             try {
67                 moduleInfos.add(BindingReflections.getModuleInfo(clz));
68             } catch (final Exception e) {
69                 LOG.debug("Could not find module info for class {}", clz, e);
70             }
71         }
72         return moduleInfos;
73     }
74
75     @Override
76     public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi,
77             final Class<? extends SubsequentAddressFamily> safi) {
78         return getRIBSupport(new TablesKey(afi, safi));
79     }
80
81     @Override
82     public RIBSupport getRIBSupport(final TablesKey key) {
83         return this.supports.get(requireNonNull(key));
84     }
85
86     @Override
87     public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
88         return this.classLoadingStrategy;
89     }
90
91     @Override
92     @SuppressWarnings("checkstyle:OverloadMethodsDeclarationOrder")
93     public RIBSupport getRIBSupport(final NodeIdentifierWithPredicates key) {
94         return this.domSupports.get(key);
95     }
96 }