X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2FRpcProviderRegistryImpl.java;h=c61ec4926a65f58f31fd03657162b0af70b23c0e;hp=f93457110181967063ec64c27a3f29c026320121;hb=190373163c45f5984b13f3e3b6ed4f461e02f5b3;hpb=2705c12ece71bbf3961a2bb24bd809c2fff5886a diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java index f934571101..c61ec4926a 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java @@ -7,29 +7,32 @@ */ package org.opendaylight.controller.sal.binding.impl; +import static com.google.common.base.Preconditions.checkState; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + import java.util.EventListener; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import java.util.HashMap; import java.util.Set; import java.util.WeakHashMap; -import javax.swing.tree.ExpandVetoException; - import org.opendaylight.controller.md.sal.common.api.routing.RouteChange; import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener; import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher; import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier; import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter; import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator; import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper; import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder; -import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier; import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; -import org.opendaylight.yangtools.concepts.Identifiable; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.util.ListenerRegistry; import org.opendaylight.yangtools.yang.binding.BaseIdentity; @@ -38,15 +41,21 @@ import org.opendaylight.yangtools.yang.binding.RpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static com.google.common.base.Preconditions.*; - -public class RpcProviderRegistryImpl implements // - RpcProviderRegistry, // - RouteChangePublisher> { +public class RpcProviderRegistryImpl implements RpcProviderRegistry, RouteChangePublisher> { private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL; - private final Map, RpcService> publicProxies = new WeakHashMap<>(); + // cache of proxy objects where each value in the map corresponds to a specific RpcService + private final LoadingCache, RpcService> publicProxies = CacheBuilder.newBuilder().weakKeys(). + build(new CacheLoader, RpcService>() { + @Override + public RpcService load(final Class type) { + final RpcService proxy = rpcFactory.getDirectProxyFor(type); + LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); + return proxy; + } + }); + private final Map, RpcRouter> rpcRouters = new WeakHashMap<>(); private final ListenerRegistry>> routeChangeListeners = ListenerRegistry .create(); @@ -56,25 +65,25 @@ public class RpcProviderRegistryImpl implements // private final String name; - private ListenerRegistry globalRpcListeners = ListenerRegistry.create(); + private final ListenerRegistry globalRpcListeners = ListenerRegistry.create(); public String getName() { return name; } - public RpcProviderRegistryImpl(String name) { + public RpcProviderRegistryImpl(final String name) { super(); this.name = name; } @Override - public final RoutedRpcRegistration addRoutedRpcImplementation(Class type, - T implementation) throws IllegalStateException { + public final RoutedRpcRegistration addRoutedRpcImplementation(final Class type, + final T implementation) throws IllegalStateException { return getRpcRouter(type).addRoutedRpcImplementation(implementation); } @Override - public final RpcRegistration addRpcImplementation(Class type, T implementation) + public final RpcRegistration addRpcImplementation(final Class type, final T implementation) throws IllegalStateException { @SuppressWarnings("unchecked") RpcRouter potentialRouter = (RpcRouter) rpcRouters.get(type); @@ -94,32 +103,12 @@ public class RpcProviderRegistryImpl implements // @SuppressWarnings("unchecked") @Override - public final T getRpcService(Class type) { - - @SuppressWarnings("unchecked") - T potentialProxy = (T) publicProxies.get(type); - if (potentialProxy != null) { - return potentialProxy; - } - synchronized (this) { - /** - * Potential proxy could be instantiated by other thread while we - * were waiting for the lock. - */ - - potentialProxy = (T) publicProxies.get(type); - if (potentialProxy != null) { - return (T) potentialProxy; - } - T proxy = rpcFactory.getDirectProxyFor(type); - LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); - publicProxies.put(type, proxy); - return proxy; - } + public final T getRpcService(final Class type) { + return (T) publicProxies.getUnchecked(type); } - @SuppressWarnings("unchecked") - public RpcRouter getRpcRouter(Class type) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + public RpcRouter getRpcRouter(final Class type) { RpcRouter potentialRouter = rpcRouters.get(type); if (potentialRouter != null) { return (RpcRouter) potentialRouter; @@ -143,7 +132,7 @@ public class RpcProviderRegistryImpl implements // } } - private void notifyGlobalRpcAdded(Class type) { + private void notifyGlobalRpcAdded(final Class type) { for(ListenerRegistration listener : globalRpcListeners) { try { listener.getInstance().onGlobalRpcRegistered(type); @@ -151,10 +140,10 @@ public class RpcProviderRegistryImpl implements // LOG.error("Unhandled exception during invoking listener {}", e); } } - + } - private void notifyListenersRoutedCreated(RpcRouter router) { + private void notifyListenersRoutedCreated(final RpcRouter router) { for (ListenerRegistration listener : routerInstantiationListener) { try { @@ -167,7 +156,7 @@ public class RpcProviderRegistryImpl implements // } public ListenerRegistration registerRouterInstantiationListener( - RouterInstantiationListener listener) { + final RouterInstantiationListener listener) { ListenerRegistration reg = routerInstantiationListener.register(listener); try { for (RpcRouter router : rpcRouters.values()) { @@ -179,9 +168,10 @@ public class RpcProviderRegistryImpl implements // return reg; } + @SuppressWarnings("unchecked") @Override public >> ListenerRegistration registerRouteChangeListener( - L listener) { + final L listener) { return (ListenerRegistration) routeChangeListeners.register(listener); } @@ -189,35 +179,34 @@ public class RpcProviderRegistryImpl implements // return rpcFactory; } - public void setRpcFactory(RuntimeCodeGenerator rpcFactory) { + public void setRpcFactory(final RuntimeCodeGenerator rpcFactory) { this.rpcFactory = rpcFactory; } public interface RouterInstantiationListener extends EventListener { void onRpcRouterCreated(RpcRouter router); } - - public ListenerRegistration registerGlobalRpcRegistrationListener(GlobalRpcRegistrationListener listener) { + + public ListenerRegistration registerGlobalRpcRegistrationListener(final GlobalRpcRegistrationListener listener) { return globalRpcListeners.register(listener); } public interface GlobalRpcRegistrationListener extends EventListener { void onGlobalRpcRegistered(Class cls); void onGlobalRpcUnregistered(Class cls); - + } - private class RouteChangeForwarder implements - RouteChangeListener, InstanceIdentifier> { + private class RouteChangeForwarder implements RouteChangeListener, InstanceIdentifier> { private final Class type; - public RouteChangeForwarder(Class type) { + public RouteChangeForwarder(final Class type) { this.type = type; } @Override - public void onRouteChange(RouteChange, InstanceIdentifier> change) { + public void onRouteChange(final RouteChange, InstanceIdentifier> change) { Map>> announcements = new HashMap<>(); for (Entry, Set>> entry : change.getAnnouncements() .entrySet()) { @@ -242,15 +231,15 @@ public class RpcProviderRegistryImpl implements // } } - public static class RpcProxyRegistration extends AbstractObjectRegistration implements - RpcRegistration { + public static class RpcProxyRegistration extends AbstractObjectRegistration implements RpcRegistration { private final Class serviceType; private RpcProviderRegistryImpl registry; - public RpcProxyRegistration(Class type, T service, RpcProviderRegistryImpl registry) { + public RpcProxyRegistration(final Class type, final T service, final RpcProviderRegistryImpl registry) { super(service); - serviceType = type; + this.serviceType = type; + this.registry = registry; } @Override