Migrate Optional.get() callers
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMRpcRouter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
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;
13
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;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Optional;
35 import java.util.Set;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ThreadFactory;
39 import javax.annotation.PreDestroy;
40 import javax.inject.Inject;
41 import javax.inject.Singleton;
42 import org.checkerframework.checker.lock.qual.GuardedBy;
43 import org.eclipse.jdt.annotation.NonNull;
44 import org.eclipse.jdt.annotation.NonNullByDefault;
45 import org.opendaylight.mdsal.dom.api.DOMActionAvailabilityExtension;
46 import org.opendaylight.mdsal.dom.api.DOMActionAvailabilityExtension.AvailabilityListener;
47 import org.opendaylight.mdsal.dom.api.DOMActionImplementation;
48 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
49 import org.opendaylight.mdsal.dom.api.DOMActionNotAvailableException;
50 import org.opendaylight.mdsal.dom.api.DOMActionProviderService;
51 import org.opendaylight.mdsal.dom.api.DOMActionResult;
52 import org.opendaylight.mdsal.dom.api.DOMActionService;
53 import org.opendaylight.mdsal.dom.api.DOMActionServiceExtension;
54 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
55 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
56 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
57 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
58 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
59 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
60 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
61 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
62 import org.opendaylight.mdsal.dom.api.DOMRpcService;
63 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
64 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
65 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
66 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
67 import org.opendaylight.yangtools.concepts.AbstractRegistration;
68 import org.opendaylight.yangtools.concepts.ListenerRegistration;
69 import org.opendaylight.yangtools.concepts.ObjectRegistration;
70 import org.opendaylight.yangtools.concepts.Registration;
71 import org.opendaylight.yangtools.yang.common.QName;
72 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
73 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
76 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
77 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
78 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
79 import org.osgi.service.component.annotations.Activate;
80 import org.osgi.service.component.annotations.Component;
81 import org.osgi.service.component.annotations.Deactivate;
82 import org.osgi.service.component.annotations.Reference;
83 import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
86
87 @Singleton
88 @Component(immediate = true, service = DOMRpcRouterServices.class)
89 @RequireServiceComponentRuntime
90 public final class DOMRpcRouter extends AbstractRegistration
91         implements DOMRpcRouterServices, EffectiveModelContextListener {
92     private static final Logger LOG = LoggerFactory.getLogger(DOMRpcRouter.class);
93     private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
94             "DOMRpcRouter-listener-%s").setDaemon(true).build();
95
96     private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
97     private final @NonNull DOMActionProviderService actionProviderService = new ActionProviderServiceFacade();
98     private final @NonNull DOMActionService actionService = new ActionServiceFacade();
99     private final @NonNull DOMRpcProviderService rpcProviderService = new RpcProviderServiceFacade();
100     private final @NonNull DOMRpcService rpcService = new RpcServiceFacade();
101
102     @GuardedBy("this")
103     private ImmutableList<RegImpl<?>> listeners = ImmutableList.of();
104
105     @GuardedBy("this")
106     private ImmutableList<ActionRegistration<?>> actionListeners = ImmutableList.of();
107
108     private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
109
110     private volatile DOMActionRoutingTable actionRoutingTable = DOMActionRoutingTable.EMPTY;
111
112     private Registration listenerRegistration;
113
114     @Deprecated
115     @VisibleForTesting
116     // FIXME: 9.0.0: make this constructor package-private
117     public DOMRpcRouter() {
118
119     }
120
121     @Inject
122     @Activate
123     public DOMRpcRouter(@Reference final DOMSchemaService schemaService) {
124         listenerRegistration = schemaService.registerSchemaContextListener(this);
125         LOG.info("DOM RPC/Action router started");
126     }
127
128     @Deprecated(forRemoval = true)
129     public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
130         return new DOMRpcRouter(schemaService);
131     }
132
133     @PreDestroy
134     @Deactivate
135     public void shutdown() {
136         close();
137     }
138
139     @Override
140     public DOMActionService getActionService() {
141         return actionService;
142     }
143
144     @Override
145     public DOMActionProviderService getActionProviderService() {
146         return actionProviderService;
147     }
148
149     @Override
150     public DOMRpcService getRpcService() {
151         return rpcService;
152     }
153
154     @Override
155     public DOMRpcProviderService getRpcProviderService() {
156         return rpcProviderService;
157     }
158
159     private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
160             final Set<DOMRpcIdentifier> rpcs) {
161         final DOMRpcRoutingTable oldTable = routingTable;
162         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.remove(implementation, rpcs);
163         routingTable = newTable;
164
165         listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
166     }
167
168     private synchronized void removeRpcImplementations(
169             final ImmutableTable<QName, YangInstanceIdentifier, DOMRpcImplementation> implTable) {
170         final DOMRpcRoutingTable oldTable = routingTable;
171         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.removeAll(implTable);
172         routingTable = newTable;
173
174         listenerNotifier.execute(() -> notifyRemoved(newTable, implTable.values()));
175     }
176
177     private synchronized void removeActionImplementation(final DOMActionImplementation implementation,
178             final Set<DOMActionInstance> actions) {
179         final DOMActionRoutingTable oldTable = actionRoutingTable;
180         final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.remove(implementation, actions);
181         actionRoutingTable = newTable;
182
183         listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
184     }
185
186     private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
187         listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
188     }
189
190     private synchronized void removeActionListener(final ListenerRegistration<? extends AvailabilityListener> reg) {
191         actionListeners = ImmutableList.copyOf(Collections2.filter(actionListeners, input -> !reg.equals(input)));
192     }
193
194     private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
195         for (RegImpl<?> l : listeners) {
196             l.addRpc(newTable, impl);
197         }
198     }
199
200     private synchronized void notifyAdded(final DOMRpcRoutingTable newTable,
201             final Collection<? extends DOMRpcImplementation> impls) {
202         for (RegImpl<?> l : listeners) {
203             for (DOMRpcImplementation impl : impls) {
204                 l.addRpc(newTable, impl);
205             }
206         }
207     }
208
209     private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
210         for (RegImpl<?> l : listeners) {
211             l.removeRpc(newTable, impl);
212         }
213     }
214
215     private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable,
216             final Collection<? extends DOMRpcImplementation> impls) {
217         for (RegImpl<?> l : listeners) {
218             for (DOMRpcImplementation impl : impls) {
219                 l.removeRpc(newTable, impl);
220             }
221         }
222     }
223
224     private synchronized void notifyActionChanged(final DOMActionRoutingTable newTable,
225             final DOMActionImplementation impl) {
226         for (ActionRegistration<?> l : actionListeners) {
227             l.actionChanged(newTable, impl);
228         }
229     }
230
231     @Override
232     public synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
233         final DOMRpcRoutingTable oldTable = routingTable;
234         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.setSchemaContext(newModelContext);
235         routingTable = newTable;
236
237         final DOMActionRoutingTable oldActionTable = actionRoutingTable;
238         final DOMActionRoutingTable newActionTable =
239                 (DOMActionRoutingTable) oldActionTable.setSchemaContext(newModelContext);
240         actionRoutingTable = newActionTable;
241     }
242
243     @Override
244     protected void removeRegistration() {
245         if (listenerRegistration != null) {
246             listenerRegistration.close();
247             listenerRegistration = null;
248         }
249         listenerNotifier.shutdown();
250         LOG.info("DOM RPC/Action router stopped");
251     }
252
253     @VisibleForTesting
254     synchronized List<?> listeners() {
255         return listeners;
256     }
257
258     @VisibleForTesting
259     synchronized List<?> actionListeners() {
260         return actionListeners;
261     }
262
263     @VisibleForTesting
264     DOMRpcRoutingTable routingTable() {
265         return routingTable;
266     }
267
268     private static final class RegImpl<T extends DOMRpcAvailabilityListener> extends AbstractListenerRegistration<T> {
269         private Map<QName, Set<YangInstanceIdentifier>> prevRpcs;
270         private DOMRpcRouter router;
271
272         RegImpl(final DOMRpcRouter router, final T listener, final Map<QName, Set<YangInstanceIdentifier>> rpcs) {
273             super(listener);
274             this.router = requireNonNull(router);
275             prevRpcs = requireNonNull(rpcs);
276         }
277
278         @Override
279         protected void removeRegistration() {
280             router.removeListener(this);
281             router = null;
282         }
283
284         void initialTable() {
285             final List<DOMRpcIdentifier> added = new ArrayList<>();
286             for (Entry<QName, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
287                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
288             }
289             if (!added.isEmpty()) {
290                 getInstance().onRpcAvailable(added);
291             }
292         }
293
294         void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
295             final T l = getInstance();
296             if (!l.acceptsImplementation(impl)) {
297                 return;
298             }
299
300             final Map<QName, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
301             final MapDifference<QName, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
302
303             final List<DOMRpcIdentifier> added = new ArrayList<>();
304             for (Entry<QName, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
305                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
306             }
307             for (Entry<QName, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
308                 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
309                     added.add(DOMRpcIdentifier.create(e.getKey(), i));
310                 }
311             }
312
313             prevRpcs = rpcs;
314             if (!added.isEmpty()) {
315                 l.onRpcAvailable(added);
316             }
317         }
318
319         void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
320             final T l = getInstance();
321             if (!l.acceptsImplementation(impl)) {
322                 return;
323             }
324
325             final Map<QName, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
326             final MapDifference<QName, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
327
328             final List<DOMRpcIdentifier> removed = new ArrayList<>();
329             for (Entry<QName, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
330                 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
331             }
332             for (Entry<QName, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
333                 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
334                     removed.add(DOMRpcIdentifier.create(e.getKey(), i));
335                 }
336             }
337
338             prevRpcs = rpcs;
339             if (!removed.isEmpty()) {
340                 l.onRpcUnavailable(removed);
341             }
342         }
343     }
344
345     private static final class ActionRegistration<T extends AvailabilityListener>
346         extends AbstractListenerRegistration<T> {
347
348         private Map<Absolute, Set<DOMDataTreeIdentifier>> prevActions;
349         private DOMRpcRouter router;
350
351         ActionRegistration(final DOMRpcRouter router, final T listener,
352                 final Map<Absolute, Set<DOMDataTreeIdentifier>> actions) {
353             super(listener);
354             this.router = requireNonNull(router);
355             prevActions = requireNonNull(actions);
356         }
357
358         @Override
359         protected void removeRegistration() {
360             router.removeActionListener(this);
361             router = null;
362         }
363
364         void initialTable() {
365             final List<DOMActionInstance> added = new ArrayList<>();
366             for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : prevActions.entrySet()) {
367                 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
368             }
369             if (!added.isEmpty()) {
370                 getInstance().onActionsChanged(ImmutableSet.of(), ImmutableSet.copyOf(added));
371             }
372         }
373
374         void actionChanged(final DOMActionRoutingTable newTable, final DOMActionImplementation impl) {
375             final T l = getInstance();
376             if (!l.acceptsImplementation(impl)) {
377                 return;
378             }
379
380             final Map<Absolute, Set<DOMDataTreeIdentifier>> actions = verifyNotNull(newTable.getOperations(l));
381             final MapDifference<Absolute, Set<DOMDataTreeIdentifier>> diff = Maps.difference(prevActions, actions);
382
383             final Set<DOMActionInstance> removed = new HashSet<>();
384             final Set<DOMActionInstance> added = new HashSet<>();
385
386             for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
387                 removed.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
388             }
389
390             for (Entry<Absolute, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
391                 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
392             }
393
394             for (Entry<Absolute, ValueDifference<Set<DOMDataTreeIdentifier>>> e : diff.entriesDiffering().entrySet()) {
395                 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
396                     removed.add(DOMActionInstance.of(e.getKey(), i));
397                 }
398
399                 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
400                     added.add(DOMActionInstance.of(e.getKey(), i));
401                 }
402             }
403
404             prevActions = actions;
405             if (!removed.isEmpty() || !added.isEmpty()) {
406                 l.onActionsChanged(removed, added);
407             }
408         }
409     }
410
411     @NonNullByDefault
412     private final class ActionAvailabilityFacade implements DOMActionAvailabilityExtension {
413         @Override
414         public <T extends AvailabilityListener> ListenerRegistration<T> registerAvailabilityListener(final T listener) {
415             synchronized (DOMRpcRouter.this) {
416                 final ActionRegistration<T> ret = new ActionRegistration<>(DOMRpcRouter.this, listener,
417                     actionRoutingTable.getOperations(listener));
418                 actionListeners = ImmutableList.<ActionRegistration<?>>builder()
419                     .addAll(actionListeners)
420                     .add(ret)
421                     .build();
422
423                 listenerNotifier.execute(ret::initialTable);
424                 return ret;
425             }
426         }
427     }
428
429     @NonNullByDefault
430     private final class ActionServiceFacade implements DOMActionService {
431         private final ClassToInstanceMap<DOMActionServiceExtension> extensions = ImmutableClassToInstanceMap.of(
432             DOMActionAvailabilityExtension.class, new ActionAvailabilityFacade());
433
434         @Override
435         public ClassToInstanceMap<DOMActionServiceExtension> getExtensions() {
436             return extensions;
437         }
438
439         @Override
440         public ListenableFuture<? extends DOMActionResult> invokeAction(final Absolute type,
441                 final DOMDataTreeIdentifier path, final ContainerNode input) {
442             final YangInstanceIdentifier pathRoot = path.getRootIdentifier();
443             checkArgument(!pathRoot.isEmpty(), "Action path must not be empty");
444
445             final DOMActionRoutingTableEntry entry = (DOMActionRoutingTableEntry) actionRoutingTable.getEntry(type);
446             return entry != null ? OperationInvocation.invoke(entry, type, path, requireNonNull(input))
447                 : Futures.immediateFailedFuture(
448                     new DOMActionNotAvailableException("No implementation of Action %s available", type));
449         }
450     }
451
452     @NonNullByDefault
453     private final class ActionProviderServiceFacade implements DOMActionProviderService {
454         @Override
455         public <T extends DOMActionImplementation> ObjectRegistration<T> registerActionImplementation(
456                 final T implementation, final Set<DOMActionInstance> instances) {
457             checkArgument(!instances.isEmpty(), "Instances must not be empty");
458
459             synchronized (DOMRpcRouter.this) {
460                 final DOMActionRoutingTable oldTable = actionRoutingTable;
461                 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.add(implementation, instances);
462                 actionRoutingTable = newTable;
463
464                 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
465             }
466
467             return new AbstractObjectRegistration<>(implementation) {
468                 @Override
469                 protected void removeRegistration() {
470                     removeActionImplementation(getInstance(), instances);
471                 }
472             };
473         }
474     }
475
476     private final class RpcServiceFacade implements DOMRpcService {
477         @Override
478         public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
479             final AbstractDOMRpcRoutingTableEntry entry = (AbstractDOMRpcRoutingTableEntry) routingTable.getEntry(type);
480             if (entry == null) {
481                 return Futures.immediateFailedFuture(
482                     new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
483             }
484
485             return OperationInvocation.invoke(entry, requireNonNull(input));
486         }
487
488         @Override
489         public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
490             synchronized (DOMRpcRouter.this) {
491                 final RegImpl<T> ret = new RegImpl<>(DOMRpcRouter.this, listener, routingTable.getOperations(listener));
492                 listeners = ImmutableList.<RegImpl<?>>builder().addAll(listeners).add(ret).build();
493
494                 listenerNotifier.execute(ret::initialTable);
495                 return ret;
496             }
497         }
498     }
499
500     private final class RpcProviderServiceFacade implements DOMRpcProviderService {
501         @Override
502         public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
503                 final T implementation, final DOMRpcIdentifier... rpcs) {
504             return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
505         }
506
507         @Override
508         public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
509                 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
510
511             synchronized (DOMRpcRouter.this) {
512                 final DOMRpcRoutingTable oldTable = routingTable;
513                 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.add(implementation, rpcs);
514                 routingTable = newTable;
515
516                 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
517             }
518
519             return new AbstractDOMRpcImplementationRegistration<>(implementation) {
520                 @Override
521                 protected void removeRegistration() {
522                     removeRpcImplementation(getInstance(), rpcs);
523                 }
524             };
525         }
526
527         @Override
528         public org.opendaylight.yangtools.concepts.Registration registerRpcImplementations(
529                 final Map<DOMRpcIdentifier, DOMRpcImplementation> map) {
530             checkArgument(!map.isEmpty());
531
532             final var builder = ImmutableTable.<QName, YangInstanceIdentifier, DOMRpcImplementation>builder();
533             for (var entry : map.entrySet()) {
534                 final var id = entry.getKey();
535                 builder.put(id.getType(), id.getContextReference(), entry.getValue());
536             }
537             final var implTable = builder.build();
538
539             synchronized (DOMRpcRouter.this) {
540                 final DOMRpcRoutingTable oldTable = routingTable;
541                 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.addAll(implTable);
542                 routingTable = newTable;
543
544                 listenerNotifier.execute(() -> notifyAdded(newTable, implTable.values()));
545             }
546
547             return new AbstractRegistration() {
548                 @Override
549                 protected void removeRegistration() {
550                     removeRpcImplementations(implTable);
551                 }
552             };
553         }
554     }
555
556     static final class OperationInvocation {
557         private static final Logger LOG = LoggerFactory.getLogger(OperationInvocation.class);
558
559         static ListenableFuture<? extends DOMActionResult> invoke(final DOMActionRoutingTableEntry entry,
560                 final Absolute type, final DOMDataTreeIdentifier path, final ContainerNode input) {
561             List<DOMActionImplementation> impls = entry.getImplementations(path);
562             if (impls == null) {
563                 impls = entry.getImplementations(
564                     new DOMDataTreeIdentifier(path.getDatastoreType(), YangInstanceIdentifier.empty()));
565                 if (impls == null) {
566                     return Futures.immediateFailedFuture(new DOMActionNotAvailableException(
567                         "No implementation of Action %s available for %s", type, path));
568                 }
569             }
570
571             return impls.get(0).invokeAction(type, path, input);
572         }
573
574         static ListenableFuture<? extends DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
575                 final ContainerNode input) {
576             if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
577                 return Futures.immediateFailedFuture(
578                     new DOMRpcImplementationNotAvailableException("%s is not resolved to an RPC", entry.getType()));
579             } else if (entry instanceof RoutedDOMRpcRoutingTableEntry routed) {
580                 return invokeRoutedRpc(routed, input);
581             } else if (entry instanceof GlobalDOMRpcRoutingTableEntry global) {
582                 return invokeGlobalRpc(global, input);
583             }
584
585             return Futures.immediateFailedFuture(
586                 new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
587         }
588
589         private static ListenableFuture<? extends DOMRpcResult> invokeRoutedRpc(
590                 final RoutedDOMRpcRoutingTableEntry entry, final ContainerNode input) {
591             final Optional<NormalizedNode> maybeKey = NormalizedNodes.findNode(input,
592                 entry.getRpcId().getContextReference());
593
594             // Routing key is present, attempt to deliver as a routed RPC
595             if (maybeKey.isPresent()) {
596                 final NormalizedNode key = maybeKey.orElseThrow();
597                 final Object value = key.body();
598                 if (value instanceof YangInstanceIdentifier iid) {
599                     // Find a DOMRpcImplementation for a specific iid
600                     final List<DOMRpcImplementation> specificImpls = entry.getImplementations(iid);
601                     if (specificImpls != null) {
602                         return specificImpls.get(0)
603                             .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
604                     }
605
606                     LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
607
608                     // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
609                     // implementation this way
610                     final List<DOMRpcImplementation> mayBeRemoteImpls =
611                         entry.getImplementations(YangInstanceIdentifier.empty());
612
613                     if (mayBeRemoteImpls != null) {
614                         return mayBeRemoteImpls.get(0)
615                             .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
616                     }
617
618                 } else {
619                     LOG.warn("Ignoring wrong context value {}", value);
620                 }
621             }
622
623             final List<DOMRpcImplementation> impls = entry.getImplementations(null);
624             if (impls != null) {
625                 return impls.get(0).invokeRpc(entry.getRpcId(), input);
626             }
627
628             return Futures.immediateFailedFuture(
629                 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
630                     entry.getType()));
631         }
632
633         private static ListenableFuture<? extends DOMRpcResult> invokeGlobalRpc(
634                 final GlobalDOMRpcRoutingTableEntry entry, final ContainerNode input) {
635             return entry.getImplementations(YangInstanceIdentifier.empty()).get(0).invokeRpc(entry.getRpcId(), input);
636         }
637     }
638 }