package org.opendaylight.controller.sal.binding.codegen.impl; import org.opendaylight.yangtools.yang.binding.RpcService; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration; import org.opendaylight.controller.sal.binding.spi.RpcRouter; import org.opendaylight.yangtools.yang.binding.BaseIdentity; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*; import java.util.Map; import java.util.Set; import java.util.HashMap; import org.opendaylight.controller.sal.binding.spi.RpcRoutingTable; import org.opendaylight.yangtools.yang.binding.DataContainer; import org.opendaylight.yangtools.yang.binding.RpcImplementation; import org.opendaylight.controller.md.sal.common.api.routing.MutableRoutingTable; import org.opendaylight.controller.md.sal.common.api.routing.RouteChange; import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener; import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.util.ListenerRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; public class RpcRouterCodegenInstance implements // RpcRouter, RouteChangeListener, InstanceIdentifier> { private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class); private T defaultService; private final Class serviceType; private final T invocationProxy; private final Set> contexts; private final ListenerRegistry, InstanceIdentifier>> listeners; private final Map, RpcRoutingTableImpl> routingTables; public RpcRouterCodegenInstance(Class type, T routerImpl, Set> contexts, Set> inputs) { this.listeners = ListenerRegistry.create(); this.serviceType = type; this.invocationProxy = routerImpl; this.contexts = ImmutableSet.copyOf(contexts); Map, RpcRoutingTableImpl> mutableRoutingTables = new HashMap<>(); for (Class ctx : contexts) { RpcRoutingTableImpl table = new RpcRoutingTableImpl<>(ctx); Map invokerView = table.getRoutes(); setRoutingTable((RpcService) invocationProxy, ctx, invokerView); mutableRoutingTables.put(ctx, table); table.registerRouteChangeListener(this); } this.routingTables = ImmutableMap.copyOf(mutableRoutingTables); } @Override public Class getServiceType() { return serviceType; } @Override public T getInvocationProxy() { return invocationProxy; } @Override @SuppressWarnings("unchecked") public RpcRoutingTable getRoutingTable(Class routeContext) { return (RpcRoutingTable) routingTables.get(routeContext); } @Override public T getDefaultService() { return defaultService; } @Override public Set> getContexts() { return contexts; } @Override public , InstanceIdentifier>> ListenerRegistration registerRouteChangeListener( L listener) { return listeners.registerWithType(listener); } @Override public void onRouteChange(RouteChange, InstanceIdentifier> change) { for (ListenerRegistration, InstanceIdentifier>> listener : listeners) { try { listener.getInstance().onRouteChange(change); } catch (Exception e) { LOG.error("Error occured during invoker listener {}", listener.getInstance(), e); } } } @Override public T getService(Class context, InstanceIdentifier path) { return routingTables.get(context).getRoute(path); } @Override public RoutedRpcRegistration addRoutedRpcImplementation(T service) { return new RoutedRpcRegistrationImpl(service); } @Override public RpcRegistration registerDefaultService(T service) { // TODO Auto-generated method stub return null; } private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration implements RoutedRpcRegistration { public RoutedRpcRegistrationImpl(T instance) { super(instance); } @Override public Class getServiceType() { return serviceType; } @Override public void registerPath(Class context, InstanceIdentifier path) { routingTables.get(context).updateRoute(path, getInstance()); } @Override public void unregisterPath(Class context, InstanceIdentifier path) { routingTables.get(context).removeRoute(path, getInstance()); } @Override public void registerInstance(Class context, InstanceIdentifier instance) { registerPath(context, instance); } @Override public void unregisterInstance(Class context, InstanceIdentifier instance) { unregisterPath(context, instance); } @Override protected void removeRegistration() { } } }