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.api.DOMSchemaService;
40 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
41 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcProviderService, SchemaContextListener {
50 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
51 "DOMRpcRouter-listener-%s").setDaemon(true).build();
53 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
56 private Collection<Registration<?>> listeners = Collections.emptyList();
58 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
60 private ListenerRegistration<?> listenerRegistration;
62 public static DOMRpcRouter newInstance(final DOMSchemaService schemaService) {
63 final DOMRpcRouter rpcRouter = new DOMRpcRouter();
64 rpcRouter.listenerRegistration = schemaService.registerSchemaContextListener(rpcRouter);
69 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
70 final T implementation, final DOMRpcIdentifier... rpcs) {
71 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
75 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T>
76 registerRpcImplementation(final T implementation, final Set<DOMRpcIdentifier> rpcs) {
77 final DOMRpcRoutingTable oldTable = routingTable;
78 final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
79 routingTable = newTable;
81 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
83 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
85 protected void removeRegistration() {
86 removeRpcImplementation(getInstance(), rpcs);
91 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
92 final Set<DOMRpcIdentifier> rpcs) {
93 final DOMRpcRoutingTable oldTable = routingTable;
94 final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
95 routingTable = newTable;
97 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
101 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
102 final NormalizedNode<?, ?> input) {
103 return routingTable.invokeRpc(type, input);
106 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
107 listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
110 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
111 for (Registration<?> l : listeners) {
112 l.addRpc(newTable, impl);
116 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
117 for (Registration<?> l : listeners) {
118 l.removeRpc(newTable, impl);
123 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
125 final Registration<T> ret = new Registration<>(this, listener);
126 final Builder<Registration<?>> b = ImmutableList.builder();
129 listeners = b.build();
131 listenerNotifier.execute(() -> ret.initialTable(routingTable));
136 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
137 final DOMRpcRoutingTable oldTable = routingTable;
138 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
139 routingTable = newTable;
143 public void close() {
144 listenerNotifier.shutdown();
146 if (listenerRegistration != null) {
147 listenerRegistration.close();
152 Collection<?> listeners() {
157 DOMRpcRoutingTable routingTable() {
161 private static final class Registration<T extends DOMRpcAvailabilityListener>
162 extends AbstractListenerRegistration<T> {
164 private final DOMRpcRouter router;
166 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
168 Registration(final DOMRpcRouter router, final T listener) {
170 this.router = router;
174 protected void removeRegistration() {
175 router.removeListener(this);
178 void initialTable(final DOMRpcRoutingTable newTable) {
179 final T l = getInstance();
184 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
185 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
186 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : rpcs.entrySet()) {
187 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
190 if (!added.isEmpty()) {
191 l.onRpcAvailable(added);
195 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
196 final T l = getInstance();
197 if (l == null || !l.acceptsImplementation(impl)) {
201 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
202 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
204 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
205 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
206 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
208 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
209 diff.entriesDiffering().entrySet()) {
210 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
211 added.add(DOMRpcIdentifier.create(e.getKey(), i));
216 if (!added.isEmpty()) {
217 l.onRpcAvailable(added);
221 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
222 final T l = getInstance();
223 if (l == null || !l.acceptsImplementation(impl)) {
227 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
228 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
230 final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
231 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
232 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
234 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
235 diff.entriesDiffering().entrySet()) {
236 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
237 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
242 if (!removed.isEmpty()) {
243 l.onRpcUnavailable(removed);