Adopt odlparent-10.0.0/yangtools-8.0.0-SNAPSHOT
[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.ImmutableMap;
20 import com.google.common.collect.ImmutableSet;
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.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.data.api.schema.NormalizedNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
75 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
76 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
77 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
78 import org.osgi.service.component.annotations.Activate;
79 import org.osgi.service.component.annotations.Component;
80 import org.osgi.service.component.annotations.Deactivate;
81 import org.osgi.service.component.annotations.Reference;
82 import org.osgi.service.component.annotations.RequireServiceComponentRuntime;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85
86 @Singleton
87 @Component(immediate = true, service = DOMRpcRouterServices.class)
88 @RequireServiceComponentRuntime
89 public final class DOMRpcRouter extends AbstractRegistration
90         implements DOMRpcRouterServices, EffectiveModelContextListener {
91     private static final Logger LOG = LoggerFactory.getLogger(DOMRpcRouter.class);
92     private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
93             "DOMRpcRouter-listener-%s").setDaemon(true).build();
94
95     private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
96     private final @NonNull DOMActionProviderService actionProviderService = new ActionProviderServiceFacade();
97     private final @NonNull DOMActionService actionService = new ActionServiceFacade();
98     private final @NonNull DOMRpcProviderService rpcProviderService = new RpcProviderServiceFacade();
99     private final @NonNull DOMRpcService rpcService = new RpcServiceFacade();
100
101     @GuardedBy("this")
102     private ImmutableList<Registration<?>> listeners = ImmutableList.of();
103
104     @GuardedBy("this")
105     private ImmutableList<ActionRegistration<?>> actionListeners = ImmutableList.of();
106
107     private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
108
109     private volatile DOMActionRoutingTable actionRoutingTable = DOMActionRoutingTable.EMPTY;
110
111     private ListenerRegistration<?> listenerRegistration;
112
113     @Deprecated
114     @VisibleForTesting
115     // FIXME: 9.0.0: make this constructor package-private
116     public DOMRpcRouter() {
117
118     }
119
120     @Inject
121     @Activate
122     public DOMRpcRouter(@Reference final DOMSchemaService schemaService) {
123         listenerRegistration = schemaService.registerSchemaContextListener(this);
124         LOG.info("DOM RPC/Action router started");
125     }
126
127     @Deprecated(forRemoval = true)
128     public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
129         return new DOMRpcRouter(schemaService);
130     }
131
132     @PreDestroy
133     @Deactivate
134     public void shutdown() {
135         close();
136     }
137
138     @Override
139     public DOMActionService getActionService() {
140         return actionService;
141     }
142
143     @Override
144     public DOMActionProviderService getActionProviderService() {
145         return actionProviderService;
146     }
147
148     @Override
149     public DOMRpcService getRpcService() {
150         return rpcService;
151     }
152
153     @Override
154     public DOMRpcProviderService getRpcProviderService() {
155         return rpcProviderService;
156     }
157
158     private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
159             final Set<DOMRpcIdentifier> rpcs) {
160         final DOMRpcRoutingTable oldTable = routingTable;
161         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.remove(implementation, rpcs);
162         routingTable = newTable;
163
164         listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
165     }
166
167     private synchronized void removeRpcImplementations(final Map<DOMRpcIdentifier, DOMRpcImplementation> map) {
168         final DOMRpcRoutingTable oldTable = routingTable;
169         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.removeAll(map);
170         routingTable = newTable;
171
172         listenerNotifier.execute(() -> notifyRemoved(newTable, map.values()));
173     }
174
175     private synchronized void removeActionImplementation(final DOMActionImplementation implementation,
176             final Set<DOMActionInstance> actions) {
177         final DOMActionRoutingTable oldTable = actionRoutingTable;
178         final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.remove(implementation, actions);
179         actionRoutingTable = newTable;
180
181         listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
182     }
183
184     private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
185         listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
186     }
187
188     private synchronized void removeActionListener(final ListenerRegistration<? extends AvailabilityListener> reg) {
189         actionListeners = ImmutableList.copyOf(Collections2.filter(actionListeners, input -> !reg.equals(input)));
190     }
191
192     private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
193         for (Registration<?> l : listeners) {
194             l.addRpc(newTable, impl);
195         }
196     }
197
198     private synchronized void notifyAdded(final DOMRpcRoutingTable newTable,
199             final Collection<? extends DOMRpcImplementation> impls) {
200         for (Registration<?> l : listeners) {
201             for (DOMRpcImplementation impl : impls) {
202                 l.addRpc(newTable, impl);
203             }
204         }
205     }
206
207     private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
208         for (Registration<?> l : listeners) {
209             l.removeRpc(newTable, impl);
210         }
211     }
212
213     private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable,
214             final Collection<? extends DOMRpcImplementation> impls) {
215         for (Registration<?> l : listeners) {
216             for (DOMRpcImplementation impl : impls) {
217                 l.removeRpc(newTable, impl);
218             }
219         }
220     }
221
222     private synchronized void notifyActionChanged(final DOMActionRoutingTable newTable,
223             final DOMActionImplementation impl) {
224         for (ActionRegistration<?> l : actionListeners) {
225             l.actionChanged(newTable, impl);
226         }
227     }
228
229     @Override
230     public synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
231         final DOMRpcRoutingTable oldTable = routingTable;
232         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.setSchemaContext(newModelContext);
233         routingTable = newTable;
234
235         final DOMActionRoutingTable oldActionTable = actionRoutingTable;
236         final DOMActionRoutingTable newActionTable =
237                 (DOMActionRoutingTable) oldActionTable.setSchemaContext(newModelContext);
238         actionRoutingTable = newActionTable;
239     }
240
241     @Override
242     protected void removeRegistration() {
243         if (listenerRegistration != null) {
244             listenerRegistration.close();
245             listenerRegistration = null;
246         }
247         listenerNotifier.shutdown();
248         LOG.info("DOM RPC/Action router stopped");
249     }
250
251     @VisibleForTesting
252     synchronized List<?> listeners() {
253         return listeners;
254     }
255
256     @VisibleForTesting
257     synchronized List<?> actionListeners() {
258         return actionListeners;
259     }
260
261     @VisibleForTesting
262     DOMRpcRoutingTable routingTable() {
263         return routingTable;
264     }
265
266     private static final class Registration<T extends DOMRpcAvailabilityListener>
267         extends AbstractListenerRegistration<T> {
268
269         private Map<QName, Set<YangInstanceIdentifier>> prevRpcs;
270         private DOMRpcRouter router;
271
272         Registration(final DOMRpcRouter router, final T listener, final Map<QName, Set<YangInstanceIdentifier>> rpcs) {
273             super(listener);
274             this.router = requireNonNull(router);
275             this.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             this.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
458             synchronized (DOMRpcRouter.this) {
459                 final DOMActionRoutingTable oldTable = actionRoutingTable;
460                 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.add(implementation, instances);
461                 actionRoutingTable = newTable;
462
463                 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
464             }
465
466             return new AbstractObjectRegistration<>(implementation) {
467                 @Override
468                 protected void removeRegistration() {
469                     removeActionImplementation(getInstance(), instances);
470                 }
471             };
472         }
473     }
474
475     private final class RpcServiceFacade implements DOMRpcService {
476         @Override
477         public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
478             final AbstractDOMRpcRoutingTableEntry entry = (AbstractDOMRpcRoutingTableEntry) routingTable.getEntry(type);
479             if (entry == null) {
480                 return Futures.immediateFailedFuture(
481                     new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
482             }
483
484             return OperationInvocation.invoke(entry, requireNonNull(input));
485         }
486
487         @Override
488         public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
489             synchronized (DOMRpcRouter.this) {
490                 final Registration<T> ret = new Registration<>(DOMRpcRouter.this, listener,
491                     routingTable.getOperations(listener));
492                 listeners = ImmutableList.<Registration<?>>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             final ImmutableMap<DOMRpcIdentifier, DOMRpcImplementation> defensive = ImmutableMap.copyOf(map);
531             checkArgument(!map.isEmpty());
532
533             synchronized (DOMRpcRouter.this) {
534                 final DOMRpcRoutingTable oldTable = routingTable;
535                 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.addAll(defensive);
536                 routingTable = newTable;
537
538                 listenerNotifier.execute(() -> notifyAdded(newTable, defensive.values()));
539             }
540
541             return new AbstractRegistration() {
542                 @Override
543                 protected void removeRegistration() {
544                     removeRpcImplementations(defensive);
545                 }
546             };
547         }
548     }
549
550     static final class OperationInvocation {
551         private static final Logger LOG = LoggerFactory.getLogger(OperationInvocation.class);
552
553         static ListenableFuture<? extends DOMActionResult> invoke(final DOMActionRoutingTableEntry entry,
554                 final Absolute type, final DOMDataTreeIdentifier path, final ContainerNode input) {
555             List<DOMActionImplementation> impls = entry.getImplementations(path);
556             if (impls == null) {
557                 impls = entry.getImplementations(
558                     new DOMDataTreeIdentifier(path.getDatastoreType(), YangInstanceIdentifier.empty()));
559                 if (impls == null) {
560                     return Futures.immediateFailedFuture(new DOMActionNotAvailableException(
561                         "No implementation of Action %s available for %s", type, path));
562                 }
563             }
564
565             return impls.get(0).invokeAction(type, path, input);
566         }
567
568         static ListenableFuture<? extends DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
569                 final NormalizedNode input) {
570             if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
571                 return Futures.immediateFailedFuture(
572                     new DOMRpcImplementationNotAvailableException("%s is not resolved to an RPC", entry.getType()));
573             } else if (entry instanceof RoutedDOMRpcRoutingTableEntry) {
574                 return invokeRoutedRpc((RoutedDOMRpcRoutingTableEntry) entry, input);
575             } else if (entry instanceof GlobalDOMRpcRoutingTableEntry) {
576                 return invokeGlobalRpc((GlobalDOMRpcRoutingTableEntry) entry, input);
577             }
578
579             return Futures.immediateFailedFuture(
580                 new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
581         }
582
583         private static ListenableFuture<? extends DOMRpcResult> invokeRoutedRpc(
584                 final RoutedDOMRpcRoutingTableEntry entry, final NormalizedNode input) {
585             final Optional<NormalizedNode> maybeKey = NormalizedNodes.findNode(input,
586                 entry.getRpcId().getContextReference());
587
588             // Routing key is present, attempt to deliver as a routed RPC
589             if (maybeKey.isPresent()) {
590                 final NormalizedNode key = maybeKey.get();
591                 final Object value = key.body();
592                 if (value instanceof YangInstanceIdentifier) {
593                     final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
594
595                     // Find a DOMRpcImplementation for a specific iid
596                     final List<DOMRpcImplementation> specificImpls = entry.getImplementations(iid);
597                     if (specificImpls != null) {
598                         return specificImpls.get(0)
599                             .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
600                     }
601
602                     LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
603
604                     // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
605                     // implementation this way
606                     final List<DOMRpcImplementation> mayBeRemoteImpls =
607                         entry.getImplementations(YangInstanceIdentifier.empty());
608
609                     if (mayBeRemoteImpls != null) {
610                         return mayBeRemoteImpls.get(0)
611                             .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
612                     }
613
614                 } else {
615                     LOG.warn("Ignoring wrong context value {}", value);
616                 }
617             }
618
619             final List<DOMRpcImplementation> impls = entry.getImplementations(null);
620             if (impls != null) {
621                 return impls.get(0).invokeRpc(entry.getRpcId(), input);
622             }
623
624             return Futures.immediateFailedFuture(
625                 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
626                     entry.getType()));
627         }
628
629         private static ListenableFuture<? extends DOMRpcResult> invokeGlobalRpc(
630                 final GlobalDOMRpcRoutingTableEntry entry, final NormalizedNode input) {
631             return entry.getImplementations(YangInstanceIdentifier.empty()).get(0).invokeRpc(entry.getRpcId(), input);
632         }
633     }
634 }