82f2c9b1d220f854457eeceedb8cd324937e9ce3
[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
11 import com.google.common.base.Preconditions;
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import org.opendaylight.protocol.concepts.AbstractRegistration;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
20 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
21 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
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, AdjRIBsFactory> factories = new ConcurrentHashMap<>();
33     private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
34     private final ConcurrentMap<NodeIdentifierWithPredicates, RIBSupport> domSupports = new ConcurrentHashMap<>();
35
36     private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
37
38
39     @Override
40     public final synchronized AbstractRegistration registerAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
41             final Class<? extends SubsequentAddressFamily> safi, final AdjRIBsFactory factory) {
42         final TablesKey key = new TablesKey(afi, safi);
43
44         if (this.factories.containsKey(key)) {
45             throw new IllegalArgumentException("Specified AFI/SAFI combination is already registered");
46         }
47
48         this.factories.put(key, factory);
49
50         return new AbstractRegistration() {
51             @Override
52             protected void removeRegistration() {
53                 synchronized (SimpleRIBExtensionProviderContext.this) {
54                     SimpleRIBExtensionProviderContext.this.factories.remove(key);
55                 }
56             }
57         };
58     }
59
60     @Override
61     public final synchronized AdjRIBsFactory getAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
62             final Class<? extends SubsequentAddressFamily> safi) {
63         return this.factories.get(new TablesKey(afi, safi));
64     }
65
66     @Override
67     public <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(final Class<? extends AddressFamily> afi,
68             final Class<? extends SubsequentAddressFamily> safi, final T support) {
69         final TablesKey key = new TablesKey(afi, safi);
70         final RIBSupport prev = this.supports.putIfAbsent(key, support);
71         Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
72         this.domSupports.put(RibSupportUtils.toYangTablesKey(afi,safi), support);
73         addClassLoadingSupport(afi, safi, support);
74         return new AbstractRIBSupportRegistration<T>(support) {
75             @Override
76             protected void removeRegistration() {
77                 SimpleRIBExtensionProviderContext.this.supports.remove(key);
78             }
79         };
80     }
81
82     private void addClassLoadingSupport(final Class<?> afi, final Class<?> safi, final RIBSupport s) {
83         final Set<YangModuleInfo> moduleInfos =
84                 getModuleInfos(afi, safi, s.routesListClass(), s.routesContainerClass(), s.routesCaseClass());
85         if(!moduleInfos.isEmpty()) {
86             classLoadingStrategy.addModuleInfos(moduleInfos);
87         }
88     }
89
90     private static Set<YangModuleInfo> getModuleInfos(final Class<?>... clazzes) {
91         final Set<YangModuleInfo> moduleInfos = new HashSet<>();
92         for(final Class<?> clz : clazzes) {
93             try {
94                 moduleInfos.add(BindingReflections.getModuleInfo(clz));
95             } catch (final Exception e) {
96                 LOG.debug("Could not find module info for class {}", clz, e);
97             }
98         }
99         return moduleInfos;
100     }
101
102     @Override
103     public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
104         return getRIBSupport(new TablesKey(afi, safi));
105     }
106
107     @Override
108     public RIBSupport getRIBSupport(final TablesKey key) {
109         return this.supports.get(Preconditions.checkNotNull(key));
110     }
111
112     @Override
113     public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
114         return classLoadingStrategy;
115     }
116
117     @Override
118     public RIBSupport getRIBSupport(final NodeIdentifierWithPredicates key) {
119         return domSupports.get(key);
120     }
121 }