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.CheckedFuture;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.ThreadFactoryBuilder;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
29 import java.util.Map.Entry;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.ThreadFactory;
34 import javax.annotation.concurrent.GuardedBy;
35 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
36 import org.opendaylight.mdsal.dom.api.DOMRpcException;
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.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
55 public final class DOMRpcRouter extends AbstractRegistration implements DOMRpcService, DOMRpcProviderService,
56 SchemaContextListener {
57 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
58 "DOMRpcRouter-listener-%s").setDaemon(true).build();
60 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
63 private Collection<Registration<?>> listeners = Collections.emptyList();
65 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
67 private ListenerRegistration<?> listenerRegistration;
69 public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
70 final DOMRpcRouter rpcRouter = new DOMRpcRouter();
71 rpcRouter.listenerRegistration = schemaService.registerSchemaContextListener(rpcRouter);
76 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
77 final T implementation, final DOMRpcIdentifier... rpcs) {
78 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
82 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T>
83 registerRpcImplementation(final T implementation, final Set<DOMRpcIdentifier> rpcs) {
84 final DOMRpcRoutingTable oldTable = routingTable;
85 final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
86 routingTable = newTable;
88 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
90 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
92 protected void removeRegistration() {
93 removeRpcImplementation(getInstance(), rpcs);
98 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
99 final Set<DOMRpcIdentifier> rpcs) {
100 final DOMRpcRoutingTable oldTable = routingTable;
101 final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
102 routingTable = newTable;
104 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
108 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
109 final NormalizedNode<?, ?> input) {
110 final AbstractDOMRpcRoutingTableEntry entry = routingTable.getEntry(type);
112 return Futures.immediateFailedCheckedFuture(
113 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
116 return entry.invokeRpc(input);
119 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
120 listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
123 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
124 for (Registration<?> l : listeners) {
125 l.addRpc(newTable, impl);
129 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
130 for (Registration<?> l : listeners) {
131 l.removeRpc(newTable, impl);
136 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
138 final Registration<T> ret = new Registration<>(this, listener, routingTable.getRpcs(listener));
139 final Builder<Registration<?>> b = ImmutableList.builder();
142 listeners = b.build();
144 listenerNotifier.execute(() -> ret.initialTable());
149 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
150 final DOMRpcRoutingTable oldTable = routingTable;
151 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
152 routingTable = newTable;
157 protected void removeRegistration() {
158 if (listenerRegistration != null) {
159 listenerRegistration.close();
160 listenerRegistration = null;
162 listenerNotifier.shutdown();
166 Collection<?> listeners() {
171 DOMRpcRoutingTable routingTable() {
175 private static final class Registration<T extends DOMRpcAvailabilityListener>
176 extends AbstractListenerRegistration<T> {
178 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
179 private DOMRpcRouter router;
181 Registration(final DOMRpcRouter router, final T listener,
182 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs) {
184 this.router = requireNonNull(router);
185 this.prevRpcs = requireNonNull(rpcs);
189 protected void removeRegistration() {
190 router.removeListener(this);
194 void initialTable() {
195 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
196 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
197 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
199 if (!added.isEmpty()) {
200 getInstance().onRpcAvailable(added);
204 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
205 final T l = getInstance();
206 if (!l.acceptsImplementation(impl)) {
210 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getRpcs(l));
211 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
213 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
214 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
215 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
217 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
218 diff.entriesDiffering().entrySet()) {
219 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
220 added.add(DOMRpcIdentifier.create(e.getKey(), i));
225 if (!added.isEmpty()) {
226 l.onRpcAvailable(added);
230 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
231 final T l = getInstance();
232 if (!l.acceptsImplementation(impl)) {
236 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getRpcs(l));
237 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
239 final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
240 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
241 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
243 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
244 diff.entriesDiffering().entrySet()) {
245 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
246 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
251 if (!removed.isEmpty()) {
252 l.onRpcUnavailable(removed);