Bug 2383: Fixed minor issues and missing javadocs in RibSupportContext.
[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 com.google.common.base.Preconditions;
11 import java.util.HashSet;
12 import java.util.Set;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.protocol.concepts.AbstractRegistration;
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.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
27
28     private static final Logger LOG = LoggerFactory.getLogger(SimpleRIBExtensionProviderContext.class);
29
30     private final ConcurrentMap<TablesKey, AdjRIBsFactory> factories = new ConcurrentHashMap<>();
31     private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
32     private final ModuleInfoBackedContext classLoadingStrategy = ModuleInfoBackedContext.create();
33
34     @Override
35     public final synchronized AbstractRegistration registerAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
36             final Class<? extends SubsequentAddressFamily> safi, final AdjRIBsFactory factory) {
37         final TablesKey key = new TablesKey(afi, safi);
38
39         if (this.factories.containsKey(key)) {
40             throw new IllegalArgumentException("Specified AFI/SAFI combination is already registered");
41         }
42
43         this.factories.put(key, factory);
44
45         return new AbstractRegistration() {
46             @Override
47             protected void removeRegistration() {
48                 synchronized (SimpleRIBExtensionProviderContext.this) {
49                     SimpleRIBExtensionProviderContext.this.factories.remove(key);
50                 }
51             }
52         };
53     }
54
55     @Override
56     public final synchronized AdjRIBsFactory getAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
57             final Class<? extends SubsequentAddressFamily> safi) {
58         return this.factories.get(new TablesKey(afi, safi));
59     }
60
61     @Override
62     public <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(final Class<? extends AddressFamily> afi,
63             final Class<? extends SubsequentAddressFamily> safi, final T support) {
64         final TablesKey key = new TablesKey(afi, safi);
65         final RIBSupport prev = this.supports.putIfAbsent(key, support);
66         addClassLoadingSupport(afi, safi, support);
67         Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
68
69         return new AbstractRIBSupportRegistration<T>(support) {
70             @Override
71             protected void removeRegistration() {
72                 SimpleRIBExtensionProviderContext.this.supports.remove(key);
73             }
74         };
75     }
76
77     private void addClassLoadingSupport(Class<?> afi, Class<?> safi, RIBSupport s) {
78         final Set<YangModuleInfo> moduleInfos =
79                 getModuleInfos(afi, safi, s.routesListClass(), s.routesContainerClass(), s.routesCaseClass());
80         if(!moduleInfos.isEmpty()) {
81             classLoadingStrategy.addModuleInfos(moduleInfos);
82         }
83     }
84
85     private static Set<YangModuleInfo> getModuleInfos(Class<?>... clazzes) {
86         final Set<YangModuleInfo> moduleInfos = new HashSet<>();
87         for(Class<?> clz : clazzes) {
88             try {
89                 moduleInfos.add(BindingReflections.getModuleInfo(clz));
90             } catch (Exception e) {
91                 LOG.debug("Could not find module info for class {}", clz, e);
92             }
93         }
94         return moduleInfos;
95     }
96
97     @Override
98     public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
99         return getRIBSupport(new TablesKey(afi, safi));
100     }
101
102     @Override
103     public RIBSupport getRIBSupport(final TablesKey key) {
104         return this.supports.get(Preconditions.checkNotNull(key));
105     }
106
107     @Override
108     public GeneratedClassLoadingStrategy getClassLoadingStrategy() {
109         return classLoadingStrategy;
110     }
111 }