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