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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableList.Builder;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.MapDifference;
16 import com.google.common.collect.MapDifference.ValueDifference;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Sets;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.ThreadFactoryBuilder;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
25 import java.util.Map.Entry;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.ThreadFactory;
30 import javax.annotation.concurrent.GuardedBy;
31 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
32 import org.opendaylight.mdsal.dom.api.DOMRpcException;
33 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
34 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
35 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
36 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
37 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
38 import org.opendaylight.mdsal.dom.api.DOMRpcService;
39 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
40 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48 public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcProviderService, SchemaContextListener {
49 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
50 "DOMRpcRouter-listener-%s").setDaemon(true).build();
52 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
55 private Collection<Registration<?>> listeners = Collections.emptyList();
57 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
60 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
61 final T implementation, final DOMRpcIdentifier... rpcs) {
62 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
66 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T>
67 registerRpcImplementation(final T implementation, final Set<DOMRpcIdentifier> rpcs) {
68 final DOMRpcRoutingTable oldTable = routingTable;
69 final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
70 routingTable = newTable;
72 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
74 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
76 protected void removeRegistration() {
77 removeRpcImplementation(getInstance(), rpcs);
82 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
83 final Set<DOMRpcIdentifier> rpcs) {
84 final DOMRpcRoutingTable oldTable = routingTable;
85 final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
86 routingTable = newTable;
88 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
92 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
93 final NormalizedNode<?, ?> input) {
94 return routingTable.invokeRpc(type, input);
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 <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
116 final Registration<T> ret = new Registration<>(this, listener);
117 final Builder<Registration<?>> b = ImmutableList.builder();
120 listeners = b.build();
122 listenerNotifier.execute(() -> ret.initialTable(routingTable));
127 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
128 final DOMRpcRoutingTable oldTable = routingTable;
129 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
130 routingTable = newTable;
134 public void close() {
135 listenerNotifier.shutdown();
139 Collection<?> listeners() {
144 DOMRpcRoutingTable routingTable() {
148 private static final class Registration<T extends DOMRpcAvailabilityListener>
149 extends AbstractListenerRegistration<T> {
151 private final DOMRpcRouter router;
153 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
155 Registration(final DOMRpcRouter router, final T listener) {
157 this.router = router;
161 protected void removeRegistration() {
162 router.removeListener(this);
165 void initialTable(final DOMRpcRoutingTable newTable) {
166 final T l = getInstance();
171 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
172 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
173 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : rpcs.entrySet()) {
174 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
177 if (!added.isEmpty()) {
178 l.onRpcAvailable(added);
182 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
183 final T l = getInstance();
184 if (l == null || !l.acceptsImplementation(impl)) {
188 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
189 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
191 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
192 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
193 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
195 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
196 diff.entriesDiffering().entrySet()) {
197 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
198 added.add(DOMRpcIdentifier.create(e.getKey(), i));
203 if (!added.isEmpty()) {
204 l.onRpcAvailable(added);
208 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
209 final T l = getInstance();
210 if (l == null || !l.acceptsImplementation(impl)) {
214 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
215 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
217 final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
218 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
219 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
221 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
222 diff.entriesDiffering().entrySet()) {
223 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
224 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
229 if (!removed.isEmpty()) {
230 l.onRpcUnavailable(removed);