e05d4e628ed75a79465191e71c8de93a8d5848e6
[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.SchemaContext;
73 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
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 SchemaContextListener {
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 onGlobalContextUpdated(final SchemaContext context) {
179         final DOMRpcRoutingTable oldTable = routingTable;
180         final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.setSchemaContext(context);
181         routingTable = newTable;
182
183         final DOMActionRoutingTable oldActionTable = actionRoutingTable;
184         final DOMActionRoutingTable newActionTable = (DOMActionRoutingTable) oldActionTable.setSchemaContext(context);
185         actionRoutingTable = newActionTable;
186     }
187
188     @Override
189     protected void removeRegistration() {
190         if (listenerRegistration != null) {
191             listenerRegistration.close();
192             listenerRegistration = null;
193         }
194         listenerNotifier.shutdown();
195     }
196
197     @VisibleForTesting
198     synchronized Collection<?> listeners() {
199         return listeners;
200     }
201
202     @VisibleForTesting
203     DOMRpcRoutingTable routingTable() {
204         return routingTable;
205     }
206
207     private static final class Registration<T extends DOMRpcAvailabilityListener>
208         extends AbstractListenerRegistration<T> {
209
210         private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
211         private DOMRpcRouter router;
212
213         Registration(final DOMRpcRouter router, final T listener,
214                 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs) {
215             super(listener);
216             this.router = requireNonNull(router);
217             this.prevRpcs = requireNonNull(rpcs);
218         }
219
220         @Override
221         protected void removeRegistration() {
222             router.removeListener(this);
223             router = null;
224         }
225
226         void initialTable() {
227             final Collection<DOMRpcIdentifier> added = new ArrayList<>();
228             for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
229                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
230             }
231             if (!added.isEmpty()) {
232                 getInstance().onRpcAvailable(added);
233             }
234         }
235
236         void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
237             final T l = getInstance();
238             if (!l.acceptsImplementation(impl)) {
239                 return;
240             }
241
242             final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
243             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
244
245             final Collection<DOMRpcIdentifier> added = new ArrayList<>();
246             for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
247                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
248             }
249             for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
250                     diff.entriesDiffering().entrySet()) {
251                 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
252                     added.add(DOMRpcIdentifier.create(e.getKey(), i));
253                 }
254             }
255
256             prevRpcs = rpcs;
257             if (!added.isEmpty()) {
258                 l.onRpcAvailable(added);
259             }
260         }
261
262         void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
263             final T l = getInstance();
264             if (!l.acceptsImplementation(impl)) {
265                 return;
266             }
267
268             final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getOperations(l));
269             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
270
271             final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
272             for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
273                 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
274             }
275             for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
276                     diff.entriesDiffering().entrySet()) {
277                 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
278                     removed.add(DOMRpcIdentifier.create(e.getKey(), i));
279                 }
280             }
281
282             prevRpcs = rpcs;
283             if (!removed.isEmpty()) {
284                 l.onRpcUnavailable(removed);
285             }
286         }
287     }
288
289     private static final class ActionRegistration<T extends AvailabilityListener>
290         extends AbstractListenerRegistration<T> {
291
292         private Map<SchemaPath, Set<DOMDataTreeIdentifier>> prevActions;
293         private DOMRpcRouter router;
294
295         ActionRegistration(final DOMRpcRouter router, final T listener,
296                 final Map<SchemaPath, Set<DOMDataTreeIdentifier>> actions) {
297             super(listener);
298             this.router = requireNonNull(router);
299             this.prevActions = requireNonNull(actions);
300         }
301
302         @Override
303         protected void removeRegistration() {
304             router.removeActionListener(this);
305             router = null;
306         }
307
308         void initialTable() {
309             final Collection<DOMActionInstance> added = new ArrayList<>();
310             for (Entry<SchemaPath, Set<DOMDataTreeIdentifier>> e : prevActions.entrySet()) {
311                 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
312             }
313             if (!added.isEmpty()) {
314                 getInstance().onActionsChanged(ImmutableSet.of(), ImmutableSet.copyOf(added));
315             }
316         }
317
318         void actionChanged(final DOMActionRoutingTable newTable, final DOMActionImplementation impl) {
319             final T l = getInstance();
320             if (!l.acceptsImplementation(impl)) {
321                 return;
322             }
323
324             final Map<SchemaPath, Set<DOMDataTreeIdentifier>> actions = verifyNotNull(newTable.getOperations(l));
325             final MapDifference<SchemaPath, Set<DOMDataTreeIdentifier>> diff = Maps.difference(prevActions, actions);
326
327             final Set<DOMActionInstance> removed = new HashSet<>();
328             final Set<DOMActionInstance> added = new HashSet<>();
329
330             for (Entry<SchemaPath, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
331                 removed.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
332             }
333
334             for (Entry<SchemaPath, Set<DOMDataTreeIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
335                 added.addAll(Collections2.transform(e.getValue(), i -> DOMActionInstance.of(e.getKey(), i)));
336             }
337
338             for (Entry<SchemaPath, ValueDifference<Set<DOMDataTreeIdentifier>>> e :
339                 diff.entriesDiffering().entrySet()) {
340                 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
341                     removed.add(DOMActionInstance.of(e.getKey(), i));
342                 }
343
344                 for (DOMDataTreeIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
345                     added.add(DOMActionInstance.of(e.getKey(), i));
346                 }
347             }
348
349             prevActions = actions;
350             if (!removed.isEmpty() || !added.isEmpty()) {
351                 l.onActionsChanged(removed, added);
352             }
353         }
354     }
355
356     @NonNullByDefault
357     private final class ActionAvailabilityFacade implements DOMActionAvailabilityExtension {
358         @Override
359         public <T extends AvailabilityListener> ListenerRegistration<T> registerAvailabilityListener(final T listener) {
360             synchronized (DOMRpcRouter.this) {
361                 final ActionRegistration<T> ret = new ActionRegistration<>(DOMRpcRouter.this,
362                     listener, actionRoutingTable.getOperations(listener));
363                 final Builder<ActionRegistration<?>> b = ImmutableList.builder();
364                 b.addAll(actionListeners);
365                 b.add(ret);
366                 actionListeners = b.build();
367
368                 listenerNotifier.execute(ret::initialTable);
369                 return ret;
370             }
371         }
372     }
373
374     @NonNullByDefault
375     private final class ActionServiceFacade implements DOMActionService {
376         private final ClassToInstanceMap<DOMActionServiceExtension> extensions = ImmutableClassToInstanceMap.of(
377             DOMActionAvailabilityExtension.class, new ActionAvailabilityFacade());
378
379         @Override
380         public ClassToInstanceMap<DOMActionServiceExtension> getExtensions() {
381             return extensions;
382         }
383
384         @Override
385         public ListenableFuture<? extends DOMActionResult> invokeAction(final SchemaPath type,
386                 final DOMDataTreeIdentifier path, final ContainerNode input) {
387             final DOMActionRoutingTableEntry entry = (DOMActionRoutingTableEntry) actionRoutingTable.getEntry(type);
388             if (entry == null) {
389                 return Futures.immediateFailedFuture(
390                     new DOMActionNotAvailableException("No implementation of Action %s available", type));
391             }
392
393             return OperationInvocation.invoke(entry, type, path, input);
394         }
395     }
396
397     @NonNullByDefault
398     private final class ActionProviderServiceFacade implements DOMActionProviderService {
399         @Override
400         public ClassToInstanceMap<DOMActionProviderServiceExtension> getExtensions() {
401             return ImmutableClassToInstanceMap.of();
402         }
403
404         @Override
405         public <T extends DOMActionImplementation> ObjectRegistration<T> registerActionImplementation(
406             final T implementation, final Set<DOMActionInstance> instances) {
407
408             synchronized (DOMRpcRouter.this) {
409                 final DOMActionRoutingTable oldTable = actionRoutingTable;
410                 final DOMActionRoutingTable newTable = (DOMActionRoutingTable) oldTable.add(implementation, instances);
411                 actionRoutingTable = newTable;
412
413                 listenerNotifier.execute(() -> notifyActionChanged(newTable, implementation));
414             }
415
416             return new AbstractObjectRegistration<>(implementation) {
417                 @Override
418                 protected void removeRegistration() {
419                     removeActionImplementation(getInstance(), instances);
420                 }
421             };
422         }
423     }
424
425     private final class RpcServiceFacade implements DOMRpcService {
426         @Override
427         public ListenableFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
428             final AbstractDOMRpcRoutingTableEntry entry = (AbstractDOMRpcRoutingTableEntry) routingTable.getEntry(type);
429             if (entry == null) {
430                 return Futures.immediateFailedFuture(
431                     new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
432             }
433
434             return OperationInvocation.invoke(entry, input);
435         }
436
437         @Override
438         public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
439             synchronized (DOMRpcRouter.this) {
440                 final Registration<T> ret = new Registration<>(DOMRpcRouter.this, listener,
441                     routingTable.getOperations(listener));
442                 final Builder<Registration<?>> b = ImmutableList.builder();
443                 b.addAll(listeners);
444                 b.add(ret);
445                 listeners = b.build();
446
447                 listenerNotifier.execute(ret::initialTable);
448                 return ret;
449             }
450         }
451     }
452
453     private final class RpcProviderServiceFacade implements DOMRpcProviderService {
454         @Override
455         public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
456             final T implementation, final DOMRpcIdentifier... rpcs) {
457             return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
458         }
459
460         @Override
461         public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
462             final T implementation, final Set<DOMRpcIdentifier> rpcs) {
463
464             synchronized (DOMRpcRouter.this) {
465                 final DOMRpcRoutingTable oldTable = routingTable;
466                 final DOMRpcRoutingTable newTable = (DOMRpcRoutingTable) oldTable.add(implementation, rpcs);
467                 routingTable = newTable;
468
469                 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
470             }
471
472             return new AbstractDOMRpcImplementationRegistration<>(implementation) {
473                 @Override
474                 protected void removeRegistration() {
475                     removeRpcImplementation(getInstance(), rpcs);
476                 }
477             };
478         }
479     }
480
481     static final class OperationInvocation {
482         private static final Logger LOG = LoggerFactory.getLogger(OperationInvocation.class);
483
484         static ListenableFuture<? extends DOMActionResult> invoke(final DOMActionRoutingTableEntry entry,
485                 final SchemaPath type, final DOMDataTreeIdentifier path, final ContainerNode input) {
486             return entry.getImplementations(path).get(0).invokeAction(type, path, input);
487         }
488
489         static ListenableFuture<DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
490                 final NormalizedNode<?, ?> input) {
491             if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
492                 return Futures.immediateFailedFuture(
493                     new DOMRpcImplementationNotAvailableException("SchemaPath %s is not resolved to an RPC",
494                         entry.getType()));
495             } else if (entry instanceof RoutedDOMRpcRoutingTableEntry) {
496                 return invokeRoutedRpc((RoutedDOMRpcRoutingTableEntry) entry, input);
497             } else if (entry instanceof GlobalDOMRpcRoutingTableEntry) {
498                 return invokeGlobalRpc((GlobalDOMRpcRoutingTableEntry) entry, input);
499             }
500
501             return Futures.immediateFailedFuture(
502                 new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
503         }
504
505         private static ListenableFuture<DOMRpcResult> invokeRoutedRpc(final RoutedDOMRpcRoutingTableEntry entry,
506                 final NormalizedNode<?, ?> input) {
507             final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input,
508                 entry.getRpcId().getContextReference());
509
510             // Routing key is present, attempt to deliver as a routed RPC
511             if (maybeKey.isPresent()) {
512                 final NormalizedNode<?, ?> key = maybeKey.get();
513                 final Object value = key.getValue();
514                 if (value instanceof YangInstanceIdentifier) {
515                     final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
516
517                     // Find a DOMRpcImplementation for a specific iid
518                     final List<DOMRpcImplementation> specificImpls = entry.getImplementations(iid);
519                     if (specificImpls != null) {
520                         return specificImpls.get(0)
521                             .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
522                     }
523
524                     LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
525
526                     // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
527                     // implementation this way
528                     final List<DOMRpcImplementation> mayBeRemoteImpls =
529                         entry.getImplementations(YangInstanceIdentifier.empty());
530
531                     if (mayBeRemoteImpls != null) {
532                         return mayBeRemoteImpls.get(0)
533                             .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
534                     }
535
536                 } else {
537                     LOG.warn("Ignoring wrong context value {}", value);
538                 }
539             }
540
541             final List<DOMRpcImplementation> impls = entry.getImplementations(null);
542             if (impls != null) {
543                 return impls.get(0).invokeRpc(entry.getRpcId(), input);
544             }
545
546             return Futures.immediateFailedFuture(
547                 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
548                     entry.getType()));
549         }
550
551         private static ListenableFuture<DOMRpcResult> invokeGlobalRpc(final GlobalDOMRpcRoutingTableEntry entry,
552                 final NormalizedNode<?, ?> input) {
553             return entry.getImplementations(YangInstanceIdentifier.empty()).get(0).invokeRpc(entry.getRpcId(), input);
554         }
555     }
556 }