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