X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2FRpcProviderRegistryImpl.java;h=952d84d885c41e73fe2312bf22c723f62cc44b35;hb=9e879a95e6bed2faee5c2561fd035fc754648689;hp=bc862886d7abc548efc680d8f029c89a6d98f8d6;hpb=523c3f0629438462c5bb7be4adcaf7103a3f7ea6;p=controller.git 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 bc862886d7..952d84d885 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 @@ -1,34 +1,41 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.binding.impl; +import static com.google.common.base.Preconditions.checkState; + +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.spi.RpcContextIdentifier; -import org.opendaylight.controller.sal.binding.spi.RpcRouter; 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; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.RpcService; - -import static com.google.common.base.Preconditions.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class RpcProviderRegistryImpl implements // RpcProviderRegistry, // @@ -36,20 +43,38 @@ public class RpcProviderRegistryImpl implements // private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL; + // publicProxies is a cache of proxy objects where each value in the map corresponds to a specific RpcService private final Map, RpcService> publicProxies = new WeakHashMap<>(); private final Map, RpcRouter> rpcRouters = new WeakHashMap<>(); private final ListenerRegistry>> routeChangeListeners = ListenerRegistry .create(); + private final ListenerRegistry routerInstantiationListener = ListenerRegistry.create(); + + private final static Logger LOG = LoggerFactory.getLogger(RpcProviderRegistryImpl.class); + + private final String name; + + private final ListenerRegistry globalRpcListeners = ListenerRegistry.create(); + + public String getName() { + return 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); if (potentialRouter != null) { checkState(potentialRouter.getDefaultService() == null, @@ -59,36 +84,102 @@ public class RpcProviderRegistryImpl implements // T publicProxy = getRpcService(type); RpcService currentDelegate = RuntimeCodeHelper.getDelegate(publicProxy); checkState(currentDelegate == null, "Rpc service is already registered"); + LOG.debug("Registering {} as global implementation of {} in {}", implementation, type.getSimpleName(), this); RuntimeCodeHelper.setDelegate(publicProxy, implementation); + notifyGlobalRpcAdded(type); return new RpcProxyRegistration(type, implementation, this); } + @SuppressWarnings("unchecked") @Override - public final T getRpcService(Class type) { + public final T getRpcService(final Class type) { - RpcService potentialProxy = publicProxies.get(type); + T potentialProxy = (T) publicProxies.get(type); if (potentialProxy != null) { - return (T) potentialProxy; + 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 potentialProxy; + } + T proxy = rpcFactory.getDirectProxyFor(type); + LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); + publicProxies.put(type, proxy); + return proxy; } - T proxy = rpcFactory.getDirectProxyFor(type); - publicProxies.put(type, proxy); - return proxy; } - private RpcRouter getRpcRouter(Class type) { + @SuppressWarnings({ "unchecked", "rawtypes" }) + public RpcRouter getRpcRouter(final Class type) { RpcRouter potentialRouter = rpcRouters.get(type); if (potentialRouter != null) { return (RpcRouter) potentialRouter; } - RpcRouter router = rpcFactory.getRouterFor(type); - router.registerRouteChangeListener(new RouteChangeForwarder(type)); - RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy()); - rpcRouters.put(type, router); - return router; + synchronized (this) { + /** + * Potential Router could be instantiated by other thread while we + * were waiting for the lock. + */ + potentialRouter = rpcRouters.get(type); + if (potentialRouter != null) { + return (RpcRouter) potentialRouter; + } + RpcRouter router = rpcFactory.getRouterFor(type, name); + router.registerRouteChangeListener(new RouteChangeForwarder(type)); + LOG.debug("Registering router {} as global implementation of {} in {}", router, type.getSimpleName(), this); + RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy()); + rpcRouters.put(type, router); + notifyListenersRoutedCreated(router); + return router; + } + } + + private void notifyGlobalRpcAdded(final Class type) { + for(ListenerRegistration listener : globalRpcListeners) { + try { + listener.getInstance().onGlobalRpcRegistered(type); + } catch (Exception e) { + LOG.error("Unhandled exception during invoking listener {}", e); + } + } + } + private void notifyListenersRoutedCreated(final RpcRouter router) { + + for (ListenerRegistration listener : routerInstantiationListener) { + try { + listener.getInstance().onRpcRouterCreated(router); + } catch (Exception e) { + LOG.error("Unhandled exception during invoking listener {}", e); + } + } + + } + + public ListenerRegistration registerRouterInstantiationListener( + final RouterInstantiationListener listener) { + ListenerRegistration reg = routerInstantiationListener.register(listener); + try { + for (RpcRouter router : rpcRouters.values()) { + listener.onRpcRouterCreated(router); + } + } catch (Exception e) { + LOG.error("Unhandled exception during invoking listener {}", e); + } + return reg; + } + + @SuppressWarnings("unchecked") + @Override public >> ListenerRegistration registerRouteChangeListener( - L listener) { + final L listener) { return (ListenerRegistration) routeChangeListeners.register(listener); } @@ -96,21 +187,35 @@ 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(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 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()) { @@ -141,9 +246,10 @@ public class RpcProviderRegistryImpl implements // 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