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.collect.Collections2;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableList.Builder;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.MapDifference;
15 import com.google.common.collect.MapDifference.ValueDifference;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Sets;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.ThreadFactoryBuilder;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
24 import java.util.Map.Entry;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ThreadFactory;
29 import javax.annotation.concurrent.GuardedBy;
30 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
31 import org.opendaylight.mdsal.dom.api.DOMRpcException;
32 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
33 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
34 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
35 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
36 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
37 import org.opendaylight.mdsal.dom.api.DOMRpcService;
38 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
39 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcProviderService, SchemaContextListener {
48 private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
49 "DOMRpcRouter-listener-%s").setDaemon(true).build();
51 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
54 private Collection<Registration<?>> listeners = Collections.emptyList();
56 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
59 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
60 final T implementation, final DOMRpcIdentifier... rpcs) {
61 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
65 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T>
66 registerRpcImplementation(final T implementation, final Set<DOMRpcIdentifier> rpcs) {
67 final DOMRpcRoutingTable oldTable = routingTable;
68 final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
69 routingTable = newTable;
71 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
73 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
75 protected void removeRegistration() {
76 removeRpcImplementation(getInstance(), rpcs);
81 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
82 final Set<DOMRpcIdentifier> rpcs) {
83 final DOMRpcRoutingTable oldTable = routingTable;
84 final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
85 routingTable = newTable;
87 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
91 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
92 final NormalizedNode<?, ?> input) {
93 return routingTable.invokeRpc(type, input);
96 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
97 listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
100 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
101 for (Registration<?> l : listeners) {
102 l.addRpc(newTable, impl);
106 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
107 for (Registration<?> l : listeners) {
108 l.removeRpc(newTable, impl);
113 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
115 final Registration<T> ret = new Registration<>(this, listener);
116 final Builder<Registration<?>> b = ImmutableList.builder();
119 listeners = b.build();
121 listenerNotifier.execute(() -> ret.initialTable(routingTable));
126 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
127 final DOMRpcRoutingTable oldTable = routingTable;
128 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
129 routingTable = newTable;
133 public void close() {
134 listenerNotifier.shutdown();
137 private static final class Registration<T extends DOMRpcAvailabilityListener>
138 extends AbstractListenerRegistration<T> {
140 private final DOMRpcRouter router;
142 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
144 Registration(final DOMRpcRouter router, final T listener) {
146 this.router = router;
150 protected void removeRegistration() {
151 router.removeListener(this);
154 void initialTable(final DOMRpcRoutingTable newTable) {
155 final T l = getInstance();
160 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
161 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
162 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : rpcs.entrySet()) {
163 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
166 if (!added.isEmpty()) {
167 l.onRpcAvailable(added);
171 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
172 final T l = getInstance();
173 if (l == null || !l.acceptsImplementation(impl)) {
177 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
178 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
180 final Collection<DOMRpcIdentifier> added = new ArrayList<>();
181 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
182 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
184 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
185 diff.entriesDiffering().entrySet()) {
186 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
187 added.add(DOMRpcIdentifier.create(e.getKey(), i));
192 if (!added.isEmpty()) {
193 l.onRpcAvailable(added);
197 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
198 final T l = getInstance();
199 if (l == null || !l.acceptsImplementation(impl)) {
203 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
204 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
206 final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
207 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
208 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
210 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
211 diff.entriesDiffering().entrySet()) {
212 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
213 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
218 if (!removed.isEmpty()) {
219 l.onRpcUnavailable(removed);