import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableTable;
import com.google.common.collect.MapDifference;
import com.google.common.collect.MapDifference.ValueDifference;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.opendaylight.yangtools.concepts.AbstractRegistration;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.concepts.ObjectRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
-import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
-@Component(immediate = true, service = DOMRpcRouterServices.class)
-@RequireServiceComponentRuntime
-public final class DOMRpcRouter extends AbstractRegistration
- implements DOMRpcRouterServices, EffectiveModelContextListener {
+@Component(service = DOMRpcRouter.class)
+public final class DOMRpcRouter extends AbstractRegistration implements EffectiveModelContextListener {
private static final Logger LOG = LoggerFactory.getLogger(DOMRpcRouter.class);
private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
"DOMRpcRouter-listener-%s").setDaemon(true).build();
private final @NonNull DOMRpcService rpcService = new RpcServiceFacade();
@GuardedBy("this")
- private ImmutableList<Registration<?>> listeners = ImmutableList.of();
+ private ImmutableList<RegImpl<?>> listeners = ImmutableList.of();
@GuardedBy("this")
private ImmutableList<ActionRegistration<?>> actionListeners = ImmutableList.of();
private volatile DOMActionRoutingTable actionRoutingTable = DOMActionRoutingTable.EMPTY;
- private ListenerRegistration<?> listenerRegistration;
+ private Registration listenerRegistration;
@Deprecated
@VisibleForTesting
close();
}
- @Override
- public DOMActionService getActionService() {
+ public @NonNull DOMActionService actionService() {
return actionService;
}
- @Override
- public DOMActionProviderService getActionProviderService() {
+ public @NonNull DOMActionProviderService actionProviderService() {
return actionProviderService;
}
- @Override
- public DOMRpcService getRpcService() {
+ public @NonNull DOMRpcService rpcService() {
return rpcService;
}
- @Override
- public DOMRpcProviderService getRpcProviderService() {
+ public @NonNull DOMRpcProviderService rpcProviderService() {
return rpcProviderService;
}
listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
}
- private synchronized void removeRpcImplementations(final Map<DOMRpcIdentifier, DOMRpcImplementation> map) {
+ private synchronized void removeRpcImplementations(
+ final ImmutableTable<QName, YangInstanceIdentifier, DOMRpcImplementation> implTable) {
final DOMRpcRoutingTable oldTable = routingTable;
- final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.removeAll(map);
+ final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.removeAll(implTable);
routingTable = newTable;
- listenerNotifier.execute(() -> notifyRemoved(newTable, map.values()));
+ listenerNotifier.execute(() -> notifyRemoved(newTable, implTable.values()));
}
private synchronized void removeActionImplementation(final DOMActionImplementation implementation,
}
private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
- for (Registration<?> l : listeners) {
+ for (RegImpl<?> l : listeners) {
l.addRpc(newTable, impl);
}
}
private synchronized void notifyAdded(final DOMRpcRoutingTable newTable,
final Collection<? extends DOMRpcImplementation> impls) {
- for (Registration<?> l : listeners) {
+ for (RegImpl<?> l : listeners) {
for (DOMRpcImplementation impl : impls) {
l.addRpc(newTable, impl);
}
}
private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
- for (Registration<?> l : listeners) {
+ for (RegImpl<?> l : listeners) {
l.removeRpc(newTable, impl);
}
}
private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable,
final Collection<? extends DOMRpcImplementation> impls) {
- for (Registration<?> l : listeners) {
+ for (RegImpl<?> l : listeners) {
for (DOMRpcImplementation impl : impls) {
l.removeRpc(newTable, impl);
}
return routingTable;
}
- private static final class Registration<T extends DOMRpcAvailabilityListener>
- extends AbstractListenerRegistration<T> {
-
+ private static final class RegImpl<T extends DOMRpcAvailabilityListener> extends AbstractListenerRegistration<T> {
private Map<QName, Set<YangInstanceIdentifier>> prevRpcs;
private DOMRpcRouter router;
- Registration(final DOMRpcRouter router, final T listener, final Map<QName, Set<YangInstanceIdentifier>> rpcs) {
+ RegImpl(final DOMRpcRouter router, final T listener, final Map<QName, Set<YangInstanceIdentifier>> rpcs) {
super(listener);
this.router = requireNonNull(router);
- this.prevRpcs = requireNonNull(rpcs);
+ prevRpcs = requireNonNull(rpcs);
}
@Override
final Map<Absolute, Set<DOMDataTreeIdentifier>> actions) {
super(listener);
this.router = requireNonNull(router);
- this.prevActions = requireNonNull(actions);
+ prevActions = requireNonNull(actions);
}
@Override
@Override
public <T extends DOMActionImplementation> ObjectRegistration<T> registerActionImplementation(
final T implementation, final Set<DOMActionInstance> instances) {
+ checkArgument(!instances.isEmpty(), "Instances must not be empty");
synchronized (DOMRpcRouter.this) {
final DOMActionRoutingTable oldTable = actionRoutingTable;
private final class RpcServiceFacade implements DOMRpcService {
@Override
- public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
+ public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
final AbstractDOMRpcRoutingTableEntry entry = (AbstractDOMRpcRoutingTableEntry) routingTable.getEntry(type);
if (entry == null) {
return Futures.immediateFailedFuture(
@Override
public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
synchronized (DOMRpcRouter.this) {
- final Registration<T> ret = new Registration<>(DOMRpcRouter.this, listener,
- routingTable.getOperations(listener));
- listeners = ImmutableList.<Registration<?>>builder().addAll(listeners).add(ret).build();
+ final RegImpl<T> ret = new RegImpl<>(DOMRpcRouter.this, listener, routingTable.getOperations(listener));
+ listeners = ImmutableList.<RegImpl<?>>builder().addAll(listeners).add(ret).build();
listenerNotifier.execute(ret::initialTable);
return ret;
@Override
public org.opendaylight.yangtools.concepts.Registration registerRpcImplementations(
final Map<DOMRpcIdentifier, DOMRpcImplementation> map) {
- final ImmutableMap<DOMRpcIdentifier, DOMRpcImplementation> defensive = ImmutableMap.copyOf(map);
checkArgument(!map.isEmpty());
+ final var builder = ImmutableTable.<QName, YangInstanceIdentifier, DOMRpcImplementation>builder();
+ for (var entry : map.entrySet()) {
+ final var id = entry.getKey();
+ builder.put(id.getType(), id.getContextReference(), entry.getValue());
+ }
+ final var implTable = builder.build();
+
synchronized (DOMRpcRouter.this) {
final DOMRpcRoutingTable oldTable = routingTable;
- final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.addAll(defensive);
+ final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.addAll(implTable);
routingTable = newTable;
- listenerNotifier.execute(() -> notifyAdded(newTable, defensive.values()));
+ listenerNotifier.execute(() -> notifyAdded(newTable, implTable.values()));
}
return new AbstractRegistration() {
@Override
protected void removeRegistration() {
- removeRpcImplementations(defensive);
+ removeRpcImplementations(implTable);
}
};
}
}
-
- static final class OperationInvocation {
- private static final Logger LOG = LoggerFactory.getLogger(OperationInvocation.class);
-
- static ListenableFuture<? extends DOMActionResult> invoke(final DOMActionRoutingTableEntry entry,
- final Absolute type, final DOMDataTreeIdentifier path, final ContainerNode input) {
- List<DOMActionImplementation> impls = entry.getImplementations(path);
- if (impls == null) {
- impls = entry.getImplementations(
- new DOMDataTreeIdentifier(path.getDatastoreType(), YangInstanceIdentifier.empty()));
- if (impls == null) {
- return Futures.immediateFailedFuture(new DOMActionNotAvailableException(
- "No implementation of Action %s available for %s", type, path));
- }
- }
-
- return impls.get(0).invokeAction(type, path, input);
- }
-
- static ListenableFuture<? extends DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
- final NormalizedNode input) {
- if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
- return Futures.immediateFailedFuture(
- new DOMRpcImplementationNotAvailableException("%s is not resolved to an RPC", entry.getType()));
- } else if (entry instanceof RoutedDOMRpcRoutingTableEntry routed) {
- return invokeRoutedRpc(routed, input);
- } else if (entry instanceof GlobalDOMRpcRoutingTableEntry global) {
- return invokeGlobalRpc(global, input);
- }
-
- return Futures.immediateFailedFuture(
- new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
- }
-
- private static ListenableFuture<? extends DOMRpcResult> invokeRoutedRpc(
- final RoutedDOMRpcRoutingTableEntry entry, final NormalizedNode input) {
- final Optional<NormalizedNode> maybeKey = NormalizedNodes.findNode(input,
- entry.getRpcId().getContextReference());
-
- // Routing key is present, attempt to deliver as a routed RPC
- if (maybeKey.isPresent()) {
- final NormalizedNode key = maybeKey.get();
- final Object value = key.body();
- if (value instanceof YangInstanceIdentifier iid) {
- // Find a DOMRpcImplementation for a specific iid
- final List<DOMRpcImplementation> specificImpls = entry.getImplementations(iid);
- if (specificImpls != null) {
- return specificImpls.get(0)
- .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
- }
-
- LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
-
- // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
- // implementation this way
- final List<DOMRpcImplementation> mayBeRemoteImpls =
- entry.getImplementations(YangInstanceIdentifier.empty());
-
- if (mayBeRemoteImpls != null) {
- return mayBeRemoteImpls.get(0)
- .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
- }
-
- } else {
- LOG.warn("Ignoring wrong context value {}", value);
- }
- }
-
- final List<DOMRpcImplementation> impls = entry.getImplementations(null);
- if (impls != null) {
- return impls.get(0).invokeRpc(entry.getRpcId(), input);
- }
-
- return Futures.immediateFailedFuture(
- new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
- entry.getType()));
- }
-
- private static ListenableFuture<? extends DOMRpcResult> invokeGlobalRpc(
- final GlobalDOMRpcRoutingTableEntry entry, final NormalizedNode input) {
- return entry.getImplementations(YangInstanceIdentifier.empty()).get(0).invokeRpc(entry.getRpcId(), input);
- }
- }
}