2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableList.Builder;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.MapDifference;
19 import com.google.common.collect.MapDifference.ValueDifference;
20 import com.google.common.collect.Maps;
21 import com.google.common.collect.Sets;
22 import com.google.common.util.concurrent.FluentFuture;
23 import com.google.common.util.concurrent.ThreadFactoryBuilder;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.Optional;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.ThreadFactory;
35 import javax.annotation.concurrent.GuardedBy;
36 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
37 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
38 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
39 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
40 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
41 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
42 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
43 import org.opendaylight.mdsal.dom.api.DOMRpcService;
44 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
45 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
46 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
47 import org.opendaylight.yangtools.concepts.AbstractRegistration;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
53 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
55 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public final class DOMRpcRouter extends AbstractRegistration implements SchemaContextListener {
60 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
61 "DOMRpcRouter-listener-%s").setDaemon(true).build();
63 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
64 private final DOMRpcProviderService rpcProviderService = new RpcProviderServiceFacade();
65 private final DOMRpcService rpcService = new RpcServiceFacade();
68 private Collection<Registration<?>> listeners = Collections.emptyList();
70 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
72 private ListenerRegistration<?> listenerRegistration;
74 public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
75 final DOMRpcRouter rpcRouter = new DOMRpcRouter();
76 rpcRouter.listenerRegistration = schemaService.registerSchemaContextListener(rpcRouter);
80 public DOMRpcService getRpcService() {
84 public DOMRpcProviderService getRpcProviderService() {
85 return rpcProviderService;
88 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
89 final Set<DOMRpcIdentifier> rpcs) {
90 final DOMRpcRoutingTable oldTable = routingTable;
91 final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
92 routingTable = newTable;
94 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
97 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
98 listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
101 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
102 for (Registration<?> l : listeners) {
103 l.addRpc(newTable, impl);
107 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
108 for (Registration<?> l : listeners) {
109 l.removeRpc(newTable, impl);
114 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
115 final DOMRpcRoutingTable oldTable = routingTable;
116 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
117 routingTable = newTable;
121 protected void removeRegistration() {
122 if (listenerRegistration != null) {
123 listenerRegistration.close();
124 listenerRegistration = null;
126 listenerNotifier.shutdown();
130 Collection<?> listeners() {
135 DOMRpcRoutingTable routingTable() {
139 private static final class Registration<T extends DOMRpcAvailabilityListener>
140 extends AbstractListenerRegistration<T> {
142 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
143 private DOMRpcRouter router;
145 Registration(final DOMRpcRouter router, final T listener,
146 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs) {
148 this.router = requireNonNull(router);
149 this.prevRpcs = requireNonNull(rpcs);
153 protected void removeRegistration() {
154 router.removeListener(this);
158 void initialTable() {
159 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
160 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
161 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
163 if (!added.isEmpty()) {
164 getInstance().onRpcAvailable(added);
168 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
169 final T l = getInstance();
170 if (!l.acceptsImplementation(impl)) {
174 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getRpcs(l));
175 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
177 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
178 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
179 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
181 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
182 diff.entriesDiffering().entrySet()) {
183 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
184 added.add(DOMRpcIdentifier.create(e.getKey(), i));
189 if (!added.isEmpty()) {
190 l.onRpcAvailable(added);
194 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
195 final T l = getInstance();
196 if (!l.acceptsImplementation(impl)) {
200 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getRpcs(l));
201 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
203 final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
204 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
205 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
207 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
208 diff.entriesDiffering().entrySet()) {
209 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
210 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
215 if (!removed.isEmpty()) {
216 l.onRpcUnavailable(removed);
221 private final class RpcServiceFacade implements DOMRpcService {
223 public FluentFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
224 final AbstractDOMRpcRoutingTableEntry entry = routingTable.getEntry(type);
226 return FluentFutures.immediateFailedFluentFuture(
227 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
230 return RpcInvocation.invoke(entry, input);
234 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
235 synchronized (DOMRpcRouter.this) {
236 final Registration<T> ret = new Registration<>(DOMRpcRouter.this, listener,
237 routingTable.getRpcs(listener));
238 final Builder<Registration<?>> b = ImmutableList.builder();
241 listeners = b.build();
243 listenerNotifier.execute(ret::initialTable);
249 private final class RpcProviderServiceFacade implements DOMRpcProviderService {
251 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
252 final T implementation, final DOMRpcIdentifier... rpcs) {
253 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
257 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
258 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
260 synchronized (DOMRpcRouter.this) {
261 final DOMRpcRoutingTable oldTable = routingTable;
262 final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
263 routingTable = newTable;
265 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
268 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
270 protected void removeRegistration() {
271 removeRpcImplementation(getInstance(), rpcs);
277 static final class RpcInvocation {
278 private static final Logger LOG = LoggerFactory.getLogger(RpcInvocation.class);
280 static FluentFuture<DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
281 final NormalizedNode<?, ?> input) {
282 if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
283 return FluentFutures.immediateFailedFluentFuture(
284 new DOMRpcImplementationNotAvailableException("SchemaPath %s is not resolved to an RPC",
285 entry.getSchemaPath()));
286 } else if (entry instanceof RoutedDOMRpcRoutingTableEntry) {
287 return invokeRoutedRpc((RoutedDOMRpcRoutingTableEntry) entry, input);
288 } else if (entry instanceof GlobalDOMRpcRoutingTableEntry) {
289 return invokeGlobalRpc((GlobalDOMRpcRoutingTableEntry) entry, input);
292 return FluentFutures.immediateFailedFluentFuture(
293 new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
296 private static FluentFuture<DOMRpcResult> invokeRoutedRpc(final RoutedDOMRpcRoutingTableEntry entry,
297 final NormalizedNode<?, ?> input) {
298 final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input,
299 entry.getRpcId().getContextReference());
301 // Routing key is present, attempt to deliver as a routed RPC
302 if (maybeKey.isPresent()) {
303 final NormalizedNode<?, ?> key = maybeKey.get();
304 final Object value = key.getValue();
305 if (value instanceof YangInstanceIdentifier) {
306 final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
308 // Find a DOMRpcImplementation for a specific iid
309 final List<DOMRpcImplementation> specificImpls = entry.getImplementations(iid);
310 if (specificImpls != null) {
311 return specificImpls.get(0)
312 .invokeRpc(DOMRpcIdentifier.create(entry.getSchemaPath(), iid), input);
315 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
317 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
318 // implementation this way
319 final List<DOMRpcImplementation> mayBeRemoteImpls =
320 entry.getImplementations(YangInstanceIdentifier.EMPTY);
322 if (mayBeRemoteImpls != null) {
323 return mayBeRemoteImpls.get(0)
324 .invokeRpc(DOMRpcIdentifier.create(entry.getSchemaPath(), iid), input);
328 LOG.warn("Ignoring wrong context value {}", value);
332 final List<DOMRpcImplementation> impls = entry.getImplementations(null);
334 return impls.get(0).invokeRpc(entry.getRpcId(), input);
337 return FluentFutures.immediateFailedFluentFuture(
338 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
339 entry.getSchemaPath()));
342 private static FluentFuture<DOMRpcResult> invokeGlobalRpc(final GlobalDOMRpcRoutingTableEntry entry,
343 final NormalizedNode<?, ?> input) {
344 return entry.getImplementations(YangInstanceIdentifier.EMPTY).get(0).invokeRpc(entry.getRpcId(), input);