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(immediate = true, service = DOMRpcRouterServices.class)
85 public final class DOMRpcRouter extends AbstractRegistration
86 implements DOMRpcRouterServices, EffectiveModelContextListener {
87 private static final Logger LOG = LoggerFactory.getLogger(DOMRpcRouter.class);
88 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
89 "DOMRpcRouter-listener-%s").setDaemon(true).build();
91 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
92 private final @NonNull DOMActionProviderService actionProviderService = new ActionProviderServiceFacade();
93 private final @NonNull DOMActionService actionService = new ActionServiceFacade();
94 private final @NonNull DOMRpcProviderService rpcProviderService = new RpcProviderServiceFacade();
95 private final @NonNull DOMRpcService rpcService = new RpcServiceFacade();
98 private ImmutableList<RegImpl<?>> listeners = ImmutableList.of();
101 private ImmutableList<ActionRegistration<?>> actionListeners = ImmutableList.of();
103 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
105 private volatile DOMActionRoutingTable actionRoutingTable = DOMActionRoutingTable.EMPTY;
107 private Registration listenerRegistration;
111 // FIXME: 9.0.0: make this constructor package-private
112 public DOMRpcRouter() {
118 public DOMRpcRouter(@Reference final DOMSchemaService schemaService) {
119 listenerRegistration = schemaService.registerSchemaContextListener(this);
120 LOG.info("DOM RPC/Action router started");
123 @Deprecated(forRemoval = true)
124 public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
125 return new DOMRpcRouter(schemaService);
130 public void shutdown() {
135 public DOMActionService getActionService() {
136 return actionService;
140 public DOMActionProviderService getActionProviderService() {
141 return actionProviderService;
145 public DOMRpcService getRpcService() {
150 public DOMRpcProviderService getRpcProviderService() {
151 return rpcProviderService;
154 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
155 final Set<DOMRpcIdentifier> rpcs) {
156 final DOMRpcRoutingTable oldTable = routingTable;
157 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.remove(implementation, rpcs);
158 routingTable = newTable;
160 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
163 private synchronized void removeRpcImplementations(
164 final ImmutableTable<QName, YangInstanceIdentifier, DOMRpcImplementation> implTable) {
165 final DOMRpcRoutingTable oldTable = routingTable;
166 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.removeAll(implTable);
167 routingTable = newTable;
169 listenerNotifier.execute(() -> notifyRemoved(newTable, implTable.values()));
172 private synchronized void removeActionImplementation(final DOMActionImplementation implementation,
173 final Set<DOMActionInstance> actions) {
174 final DOMActionRoutingTable oldTable = actionRoutingTable;
175 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.remove(implementation, actions);
176 actionRoutingTable = newTable;
178 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
181 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
182 listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
185 private synchronized void removeActionListener(final ListenerRegistration<? extends AvailabilityListener> reg) {
186 actionListeners = ImmutableList.copyOf(Collections2.filter(actionListeners, input -> !reg.equals(input)));
189 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
190 for (RegImpl<?> l : listeners) {
191 l.addRpc(newTable, impl);
195 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable,
196 final Collection<? extends DOMRpcImplementation> impls) {
197 for (RegImpl<?> l : listeners) {
198 for (DOMRpcImplementation impl : impls) {
199 l.addRpc(newTable, impl);
204 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
205 for (RegImpl<?> l : listeners) {
206 l.removeRpc(newTable, impl);
210 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable,
211 final Collection<? extends DOMRpcImplementation> impls) {
212 for (RegImpl<?> l : listeners) {
213 for (DOMRpcImplementation impl : impls) {
214 l.removeRpc(newTable, impl);
219 private synchronized void notifyActionChanged(final DOMActionRoutingTable newTable,
220 final DOMActionImplementation impl) {
221 for (ActionRegistration<?> l : actionListeners) {
222 l.actionChanged(newTable, impl);
227 public synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
228 final DOMRpcRoutingTable oldTable = routingTable;
229 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.setSchemaContext(newModelContext);
230 routingTable = newTable;
232 final DOMActionRoutingTable oldActionTable = actionRoutingTable;
233 final DOMActionRoutingTable newActionTable =
234 (DOMActionRoutingTable) oldActionTable.setSchemaContext(newModelContext);
235 actionRoutingTable = newActionTable;
239 protected void removeRegistration() {
240 if (listenerRegistration != null) {
241 listenerRegistration.close();
242 listenerRegistration = null;
244 listenerNotifier.shutdown();
245 LOG.info("DOM RPC/Action router stopped");
249 synchronized List<?> listeners() {
254 synchronized List<?> actionListeners() {
255 return actionListeners;
259 DOMRpcRoutingTable routingTable() {
263 private static final class RegImpl<T extends DOMRpcAvailabilityListener> extends AbstractListenerRegistration<T> {
264 private Map<QName, Set<YangInstanceIdentifier>> prevRpcs;
265 private DOMRpcRouter router;
267 RegImpl(final DOMRpcRouter router, final T listener, final Map<QName, Set<YangInstanceIdentifier>> rpcs) {
269 this.router = requireNonNull(router);
270 prevRpcs = requireNonNull(rpcs);
274 protected void removeRegistration() {
275 router.removeListener(this);
279 void initialTable() {
280 final List<DOMRpcIdentifier> added = new ArrayList<>();
281 for (Entry<QName, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
282 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
284 if (!added.isEmpty()) {
285 getInstance().onRpcAvailable(added);
289 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
290 final T l = getInstance();
291 if (!l.acceptsImplementation(impl)) {
295 final Map<QName, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
296 final MapDifference<QName, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
298 final List<DOMRpcIdentifier> added = new ArrayList<>();
299 for (Entry<QName, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
300 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
302 for (Entry<QName, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
303 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
304 added.add(DOMRpcIdentifier.create(e.getKey(), i));
309 if (!added.isEmpty()) {
310 l.onRpcAvailable(added);
314 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
315 final T l = getInstance();
316 if (!l.acceptsImplementation(impl)) {
320 final Map<QName, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
321 final MapDifference<QName, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
323 final List<DOMRpcIdentifier> removed = new ArrayList<>();
324 for (Entry<QName, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
325 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
327 for (Entry<QName, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
328 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
329 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
334 if (!removed.isEmpty()) {
335 l.onRpcUnavailable(removed);
340 private static final class ActionRegistration<T extends AvailabilityListener>
341 extends AbstractListenerRegistration<T> {
343 private Map<Absolute, Set<DOMDataTreeIdentifier>> prevActions;
344 private DOMRpcRouter router;
346 ActionRegistration(final DOMRpcRouter router, final T listener,
347 final Map<Absolute, Set<DOMDataTreeIdentifier>> actions) {
349 this.router = requireNonNull(router);
350 prevActions = requireNonNull(actions);
354 protected void removeRegistration() {
355 router.removeActionListener(this);
359 void initialTable() {
360 final List<DOMActionInstance> added = new ArrayList<>();
361 for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : prevActions.entrySet()) {
362 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
364 if (!added.isEmpty()) {
365 getInstance().onActionsChanged(ImmutableSet.of(), ImmutableSet.copyOf(added));
369 void actionChanged(final DOMActionRoutingTable newTable, final DOMActionImplementation impl) {
370 final T l = getInstance();
371 if (!l.acceptsImplementation(impl)) {
375 final Map<Absolute, Set<DOMDataTreeIdentifier>> actions = verifyNotNull(newTable.getOperations(l));
376 final MapDifference<Absolute, Set<DOMDataTreeIdentifier>> diff = Maps.difference(prevActions, actions);
378 final Set<DOMActionInstance> removed = new HashSet<>();
379 final Set<DOMActionInstance> added = new HashSet<>();
381 for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
382 removed.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
385 for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
386 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
389 for (Entry<Absolute, ValueDifference<Set<DOMDataTreeIdentifier>>> e : diff.entriesDiffering().entrySet()) {
390 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
391 removed.add(DOMActionInstance.of(e.getKey(), i));
394 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
395 added.add(DOMActionInstance.of(e.getKey(), i));
399 prevActions = actions;
400 if (!removed.isEmpty() || !added.isEmpty()) {
401 l.onActionsChanged(removed, added);
407 private final class ActionAvailabilityFacade implements DOMActionAvailabilityExtension {
409 public <T extends AvailabilityListener> ListenerRegistration<T> registerAvailabilityListener(final T listener) {
410 synchronized (DOMRpcRouter.this) {
411 final ActionRegistration<T> ret = new ActionRegistration<>(DOMRpcRouter.this, listener,
412 actionRoutingTable.getOperations(listener));
413 actionListeners = ImmutableList.<ActionRegistration<?>>builder()
414 .addAll(actionListeners)
418 listenerNotifier.execute(ret::initialTable);
425 private final class ActionServiceFacade implements DOMActionService {
426 private final ClassToInstanceMap<DOMActionServiceExtension> extensions = ImmutableClassToInstanceMap.of(
427 DOMActionAvailabilityExtension.class, new ActionAvailabilityFacade());
430 public ClassToInstanceMap<DOMActionServiceExtension> getExtensions() {
435 public ListenableFuture<? extends DOMActionResult> invokeAction(final Absolute type,
436 final DOMDataTreeIdentifier path, final ContainerNode input) {
437 final YangInstanceIdentifier pathRoot = path.getRootIdentifier();
438 checkArgument(!pathRoot.isEmpty(), "Action path must not be empty");
440 final DOMActionRoutingTableEntry entry = (DOMActionRoutingTableEntry) actionRoutingTable.getEntry(type);
441 return entry != null ? OperationInvocation.invoke(entry, type, path, requireNonNull(input))
442 : Futures.immediateFailedFuture(
443 new DOMActionNotAvailableException("No implementation of Action %s available", type));
448 private final class ActionProviderServiceFacade implements DOMActionProviderService {
450 public <T extends DOMActionImplementation> ObjectRegistration<T> registerActionImplementation(
451 final T implementation, final Set<DOMActionInstance> instances) {
452 checkArgument(!instances.isEmpty(), "Instances must not be empty");
454 synchronized (DOMRpcRouter.this) {
455 final DOMActionRoutingTable oldTable = actionRoutingTable;
456 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.add(implementation, instances);
457 actionRoutingTable = newTable;
459 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
462 return new AbstractObjectRegistration<>(implementation) {
464 protected void removeRegistration() {
465 removeActionImplementation(getInstance(), instances);
471 private final class RpcServiceFacade implements DOMRpcService {
473 public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
474 final AbstractDOMRpcRoutingTableEntry entry = (AbstractDOMRpcRoutingTableEntry) routingTable.getEntry(type);
476 return Futures.immediateFailedFuture(
477 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
480 return OperationInvocation.invoke(entry, requireNonNull(input));
484 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
485 synchronized (DOMRpcRouter.this) {
486 final RegImpl<T> ret = new RegImpl<>(DOMRpcRouter.this, listener, routingTable.getOperations(listener));
487 listeners = ImmutableList.<RegImpl<?>>builder().addAll(listeners).add(ret).build();
489 listenerNotifier.execute(ret::initialTable);
495 private final class RpcProviderServiceFacade implements DOMRpcProviderService {
497 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
498 final T implementation, final DOMRpcIdentifier... rpcs) {
499 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
503 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
504 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
506 synchronized (DOMRpcRouter.this) {
507 final DOMRpcRoutingTable oldTable = routingTable;
508 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.add(implementation, rpcs);
509 routingTable = newTable;
511 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
514 return new AbstractDOMRpcImplementationRegistration<>(implementation) {
516 protected void removeRegistration() {
517 removeRpcImplementation(getInstance(), rpcs);
523 public org.opendaylight.yangtools.concepts.Registration registerRpcImplementations(
524 final Map<DOMRpcIdentifier, DOMRpcImplementation> map) {
525 checkArgument(!map.isEmpty());
527 final var builder = ImmutableTable.<QName, YangInstanceIdentifier, DOMRpcImplementation>builder();
528 for (var entry : map.entrySet()) {
529 final var id = entry.getKey();
530 builder.put(id.getType(), id.getContextReference(), entry.getValue());
532 final var implTable = builder.build();
534 synchronized (DOMRpcRouter.this) {
535 final DOMRpcRoutingTable oldTable = routingTable;
536 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.addAll(implTable);
537 routingTable = newTable;
539 listenerNotifier.execute(() -> notifyAdded(newTable, implTable.values()));
542 return new AbstractRegistration() {
544 protected void removeRegistration() {
545 removeRpcImplementations(implTable);