2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.collect.ClassToInstanceMap;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableClassToInstanceMap;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.ImmutableTable;
21 import com.google.common.collect.MapDifference;
22 import com.google.common.collect.MapDifference.ValueDifference;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.Sets;
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.ThreadFactoryBuilder;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.List;
33 import java.util.Map.Entry;
35 import java.util.concurrent.ExecutorService;
36 import java.util.concurrent.Executors;
37 import java.util.concurrent.ThreadFactory;
38 import javax.annotation.PreDestroy;
39 import javax.inject.Inject;
40 import javax.inject.Singleton;
41 import org.checkerframework.checker.lock.qual.GuardedBy;
42 import org.eclipse.jdt.annotation.NonNull;
43 import org.eclipse.jdt.annotation.NonNullByDefault;
44 import org.opendaylight.mdsal.dom.api.DOMActionAvailabilityExtension;
45 import org.opendaylight.mdsal.dom.api.DOMActionAvailabilityExtension.AvailabilityListener;
46 import org.opendaylight.mdsal.dom.api.DOMActionImplementation;
47 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
48 import org.opendaylight.mdsal.dom.api.DOMActionNotAvailableException;
49 import org.opendaylight.mdsal.dom.api.DOMActionProviderService;
50 import org.opendaylight.mdsal.dom.api.DOMActionResult;
51 import org.opendaylight.mdsal.dom.api.DOMActionService;
52 import org.opendaylight.mdsal.dom.api.DOMActionServiceExtension;
53 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
54 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
55 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
56 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
57 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
58 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
59 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
60 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
61 import org.opendaylight.mdsal.dom.api.DOMRpcService;
62 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
63 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
64 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
65 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
66 import org.opendaylight.yangtools.concepts.AbstractRegistration;
67 import org.opendaylight.yangtools.concepts.ListenerRegistration;
68 import org.opendaylight.yangtools.concepts.ObjectRegistration;
69 import org.opendaylight.yangtools.concepts.Registration;
70 import org.opendaylight.yangtools.yang.common.QName;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
73 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
74 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
75 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
76 import org.osgi.service.component.annotations.Activate;
77 import org.osgi.service.component.annotations.Component;
78 import org.osgi.service.component.annotations.Deactivate;
79 import org.osgi.service.component.annotations.Reference;
80 import org.slf4j.Logger;
81 import org.slf4j.LoggerFactory;
84 @Component(service = DOMRpcRouter.class)
85 public final class DOMRpcRouter extends AbstractRegistration implements EffectiveModelContextListener {
86 private static final Logger LOG = LoggerFactory.getLogger(DOMRpcRouter.class);
87 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
88 "DOMRpcRouter-listener-%s").setDaemon(true).build();
90 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
91 private final @NonNull DOMActionProviderService actionProviderService = new ActionProviderServiceFacade();
92 private final @NonNull DOMActionService actionService = new ActionServiceFacade();
93 private final @NonNull DOMRpcProviderService rpcProviderService = new RpcProviderServiceFacade();
94 private final @NonNull DOMRpcService rpcService = new RpcServiceFacade();
97 private ImmutableList<RegImpl<?>> listeners = ImmutableList.of();
100 private ImmutableList<ActionRegistration<?>> actionListeners = ImmutableList.of();
102 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
104 private volatile DOMActionRoutingTable actionRoutingTable = DOMActionRoutingTable.EMPTY;
106 private Registration listenerRegistration;
110 // FIXME: 9.0.0: make this constructor package-private
111 public DOMRpcRouter() {
117 public DOMRpcRouter(@Reference final DOMSchemaService schemaService) {
118 listenerRegistration = schemaService.registerSchemaContextListener(this);
119 LOG.info("DOM RPC/Action router started");
122 @Deprecated(forRemoval = true)
123 public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
124 return new DOMRpcRouter(schemaService);
129 public void shutdown() {
133 public @NonNull DOMActionService actionService() {
134 return actionService;
137 public @NonNull DOMActionProviderService actionProviderService() {
138 return actionProviderService;
141 public @NonNull DOMRpcService rpcService() {
145 public @NonNull DOMRpcProviderService rpcProviderService() {
146 return rpcProviderService;
149 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
150 final Set<DOMRpcIdentifier> rpcs) {
151 final DOMRpcRoutingTable oldTable = routingTable;
152 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.remove(implementation, rpcs);
153 routingTable = newTable;
155 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
158 private synchronized void removeRpcImplementations(
159 final ImmutableTable<QName, YangInstanceIdentifier, DOMRpcImplementation> implTable) {
160 final DOMRpcRoutingTable oldTable = routingTable;
161 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.removeAll(implTable);
162 routingTable = newTable;
164 listenerNotifier.execute(() -> notifyRemoved(newTable, implTable.values()));
167 private synchronized void removeActionImplementation(final DOMActionImplementation implementation,
168 final Set<DOMActionInstance> actions) {
169 final DOMActionRoutingTable oldTable = actionRoutingTable;
170 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.remove(implementation, actions);
171 actionRoutingTable = newTable;
173 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
176 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
177 listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
180 private synchronized void removeActionListener(final ListenerRegistration<? extends AvailabilityListener> reg) {
181 actionListeners = ImmutableList.copyOf(Collections2.filter(actionListeners, input -> !reg.equals(input)));
184 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
185 for (RegImpl<?> l : listeners) {
186 l.addRpc(newTable, impl);
190 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable,
191 final Collection<? extends DOMRpcImplementation> impls) {
192 for (RegImpl<?> l : listeners) {
193 for (DOMRpcImplementation impl : impls) {
194 l.addRpc(newTable, impl);
199 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
200 for (RegImpl<?> l : listeners) {
201 l.removeRpc(newTable, impl);
205 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable,
206 final Collection<? extends DOMRpcImplementation> impls) {
207 for (RegImpl<?> l : listeners) {
208 for (DOMRpcImplementation impl : impls) {
209 l.removeRpc(newTable, impl);
214 private synchronized void notifyActionChanged(final DOMActionRoutingTable newTable,
215 final DOMActionImplementation impl) {
216 for (ActionRegistration<?> l : actionListeners) {
217 l.actionChanged(newTable, impl);
222 public synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
223 final DOMRpcRoutingTable oldTable = routingTable;
224 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.setSchemaContext(newModelContext);
225 routingTable = newTable;
227 final DOMActionRoutingTable oldActionTable = actionRoutingTable;
228 final DOMActionRoutingTable newActionTable =
229 (DOMActionRoutingTable) oldActionTable.setSchemaContext(newModelContext);
230 actionRoutingTable = newActionTable;
234 protected void removeRegistration() {
235 if (listenerRegistration != null) {
236 listenerRegistration.close();
237 listenerRegistration = null;
239 listenerNotifier.shutdown();
240 LOG.info("DOM RPC/Action router stopped");
244 synchronized List<?> listeners() {
249 synchronized List<?> actionListeners() {
250 return actionListeners;
254 DOMRpcRoutingTable routingTable() {
258 private static final class RegImpl<T extends DOMRpcAvailabilityListener> extends AbstractListenerRegistration<T> {
259 private Map<QName, Set<YangInstanceIdentifier>> prevRpcs;
260 private DOMRpcRouter router;
262 RegImpl(final DOMRpcRouter router, final T listener, final Map<QName, Set<YangInstanceIdentifier>> rpcs) {
264 this.router = requireNonNull(router);
265 prevRpcs = requireNonNull(rpcs);
269 protected void removeRegistration() {
270 router.removeListener(this);
274 void initialTable() {
275 final List<DOMRpcIdentifier> added = new ArrayList<>();
276 for (Entry<QName, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
277 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
279 if (!added.isEmpty()) {
280 getInstance().onRpcAvailable(added);
284 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
285 final T l = getInstance();
286 if (!l.acceptsImplementation(impl)) {
290 final Map<QName, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
291 final MapDifference<QName, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
293 final List<DOMRpcIdentifier> added = new ArrayList<>();
294 for (Entry<QName, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
295 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
297 for (Entry<QName, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
298 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
299 added.add(DOMRpcIdentifier.create(e.getKey(), i));
304 if (!added.isEmpty()) {
305 l.onRpcAvailable(added);
309 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
310 final T l = getInstance();
311 if (!l.acceptsImplementation(impl)) {
315 final Map<QName, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
316 final MapDifference<QName, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
318 final List<DOMRpcIdentifier> removed = new ArrayList<>();
319 for (Entry<QName, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
320 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
322 for (Entry<QName, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
323 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
324 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
329 if (!removed.isEmpty()) {
330 l.onRpcUnavailable(removed);
335 private static final class ActionRegistration<T extends AvailabilityListener>
336 extends AbstractListenerRegistration<T> {
338 private Map<Absolute, Set<DOMDataTreeIdentifier>> prevActions;
339 private DOMRpcRouter router;
341 ActionRegistration(final DOMRpcRouter router, final T listener,
342 final Map<Absolute, Set<DOMDataTreeIdentifier>> actions) {
344 this.router = requireNonNull(router);
345 prevActions = requireNonNull(actions);
349 protected void removeRegistration() {
350 router.removeActionListener(this);
354 void initialTable() {
355 final List<DOMActionInstance> added = new ArrayList<>();
356 for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : prevActions.entrySet()) {
357 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
359 if (!added.isEmpty()) {
360 getInstance().onActionsChanged(ImmutableSet.of(), ImmutableSet.copyOf(added));
364 void actionChanged(final DOMActionRoutingTable newTable, final DOMActionImplementation impl) {
365 final T l = getInstance();
366 if (!l.acceptsImplementation(impl)) {
370 final Map<Absolute, Set<DOMDataTreeIdentifier>> actions = verifyNotNull(newTable.getOperations(l));
371 final MapDifference<Absolute, Set<DOMDataTreeIdentifier>> diff = Maps.difference(prevActions, actions);
373 final Set<DOMActionInstance> removed = new HashSet<>();
374 final Set<DOMActionInstance> added = new HashSet<>();
376 for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
377 removed.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
380 for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
381 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
384 for (Entry<Absolute, ValueDifference<Set<DOMDataTreeIdentifier>>> e : diff.entriesDiffering().entrySet()) {
385 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
386 removed.add(DOMActionInstance.of(e.getKey(), i));
389 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
390 added.add(DOMActionInstance.of(e.getKey(), i));
394 prevActions = actions;
395 if (!removed.isEmpty() || !added.isEmpty()) {
396 l.onActionsChanged(removed, added);
402 private final class ActionAvailabilityFacade implements DOMActionAvailabilityExtension {
404 public <T extends AvailabilityListener> ListenerRegistration<T> registerAvailabilityListener(final T listener) {
405 synchronized (DOMRpcRouter.this) {
406 final ActionRegistration<T> ret = new ActionRegistration<>(DOMRpcRouter.this, listener,
407 actionRoutingTable.getOperations(listener));
408 actionListeners = ImmutableList.<ActionRegistration<?>>builder()
409 .addAll(actionListeners)
413 listenerNotifier.execute(ret::initialTable);
420 private final class ActionServiceFacade implements DOMActionService {
421 private final ClassToInstanceMap<DOMActionServiceExtension> extensions = ImmutableClassToInstanceMap.of(
422 DOMActionAvailabilityExtension.class, new ActionAvailabilityFacade());
425 public ClassToInstanceMap<DOMActionServiceExtension> getExtensions() {
430 public ListenableFuture<? extends DOMActionResult> invokeAction(final Absolute type,
431 final DOMDataTreeIdentifier path, final ContainerNode input) {
432 final YangInstanceIdentifier pathRoot = path.getRootIdentifier();
433 checkArgument(!pathRoot.isEmpty(), "Action path must not be empty");
435 final DOMActionRoutingTableEntry entry = (DOMActionRoutingTableEntry) actionRoutingTable.getEntry(type);
436 return entry != null ? OperationInvocation.invoke(entry, type, path, requireNonNull(input))
437 : Futures.immediateFailedFuture(
438 new DOMActionNotAvailableException("No implementation of Action %s available", type));
443 private final class ActionProviderServiceFacade implements DOMActionProviderService {
445 public <T extends DOMActionImplementation> ObjectRegistration<T> registerActionImplementation(
446 final T implementation, final Set<DOMActionInstance> instances) {
447 checkArgument(!instances.isEmpty(), "Instances must not be empty");
449 synchronized (DOMRpcRouter.this) {
450 final DOMActionRoutingTable oldTable = actionRoutingTable;
451 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.add(implementation, instances);
452 actionRoutingTable = newTable;
454 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
457 return new AbstractObjectRegistration<>(implementation) {
459 protected void removeRegistration() {
460 removeActionImplementation(getInstance(), instances);
466 private final class RpcServiceFacade implements DOMRpcService {
468 public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
469 final AbstractDOMRpcRoutingTableEntry entry = (AbstractDOMRpcRoutingTableEntry) routingTable.getEntry(type);
471 return Futures.immediateFailedFuture(
472 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
475 return OperationInvocation.invoke(entry, requireNonNull(input));
479 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
480 synchronized (DOMRpcRouter.this) {
481 final RegImpl<T> ret = new RegImpl<>(DOMRpcRouter.this, listener, routingTable.getOperations(listener));
482 listeners = ImmutableList.<RegImpl<?>>builder().addAll(listeners).add(ret).build();
484 listenerNotifier.execute(ret::initialTable);
490 private final class RpcProviderServiceFacade implements DOMRpcProviderService {
492 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
493 final T implementation, final DOMRpcIdentifier... rpcs) {
494 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
498 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
499 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
501 synchronized (DOMRpcRouter.this) {
502 final DOMRpcRoutingTable oldTable = routingTable;
503 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.add(implementation, rpcs);
504 routingTable = newTable;
506 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
509 return new AbstractDOMRpcImplementationRegistration<>(implementation) {
511 protected void removeRegistration() {
512 removeRpcImplementation(getInstance(), rpcs);
518 public org.opendaylight.yangtools.concepts.Registration registerRpcImplementations(
519 final Map<DOMRpcIdentifier, DOMRpcImplementation> map) {
520 checkArgument(!map.isEmpty());
522 final var builder = ImmutableTable.<QName, YangInstanceIdentifier, DOMRpcImplementation>builder();
523 for (var entry : map.entrySet()) {
524 final var id = entry.getKey();
525 builder.put(id.getType(), id.getContextReference(), entry.getValue());
527 final var implTable = builder.build();
529 synchronized (DOMRpcRouter.this) {
530 final DOMRpcRoutingTable oldTable = routingTable;
531 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.addAll(implTable);
532 routingTable = newTable;
534 listenerNotifier.execute(() -> notifyAdded(newTable, implTable.values()));
537 return new AbstractRegistration() {
539 protected void removeRegistration() {
540 removeRpcImplementations(implTable);