492fd4abfbd8854b6a3381d62c24710a178636ef
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMRpcRouter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
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;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Set;
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;
47
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();
51
52     private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
53
54     @GuardedBy("this")
55     private Collection<Registration<?>> listeners = Collections.emptyList();
56
57     private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
58
59     @Override
60     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
61             final T implementation, final DOMRpcIdentifier... rpcs) {
62         return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
63     }
64
65     @Override
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;
71
72         listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
73
74         return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
75             @Override
76             protected void removeRegistration() {
77                 removeRpcImplementation(getInstance(), rpcs);
78             }
79         };
80     }
81
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;
87
88         listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
89     }
90
91     @Override
92     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
93             final NormalizedNode<?, ?> input) {
94         return routingTable.invokeRpc(type, input);
95     }
96
97     private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
98         listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
99     }
100
101     private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
102         for (Registration<?> l : listeners) {
103             l.addRpc(newTable, impl);
104         }
105     }
106
107     private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
108         for (Registration<?> l : listeners) {
109             l.removeRpc(newTable, impl);
110         }
111     }
112
113     @Override
114     public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
115             final T listener) {
116         final Registration<T> ret = new Registration<>(this, listener);
117         final Builder<Registration<?>> b = ImmutableList.builder();
118         b.addAll(listeners);
119         b.add(ret);
120         listeners = b.build();
121
122         listenerNotifier.execute(() -> ret.initialTable(routingTable));
123         return ret;
124     }
125
126     @Override
127     public synchronized void onGlobalContextUpdated(final SchemaContext context) {
128         final DOMRpcRoutingTable oldTable = routingTable;
129         final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
130         routingTable = newTable;
131     }
132
133     @Override
134     public void close() {
135         listenerNotifier.shutdown();
136     }
137
138     @VisibleForTesting
139     Collection<?> listeners() {
140         return listeners;
141     }
142
143     @VisibleForTesting
144     DOMRpcRoutingTable routingTable() {
145         return routingTable;
146     }
147
148     private static final class Registration<T extends DOMRpcAvailabilityListener>
149         extends AbstractListenerRegistration<T> {
150
151         private final DOMRpcRouter router;
152
153         private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
154
155         Registration(final DOMRpcRouter router, final T listener) {
156             super(listener);
157             this.router = router;
158         }
159
160         @Override
161         protected void removeRegistration() {
162             router.removeListener(this);
163         }
164
165         void initialTable(final DOMRpcRoutingTable newTable) {
166             final T l = getInstance();
167             if (l == null) {
168                 return;
169             }
170
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)));
175             }
176             prevRpcs = rpcs;
177             if (!added.isEmpty()) {
178                 l.onRpcAvailable(added);
179             }
180         }
181
182         void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
183             final T l = getInstance();
184             if (l == null || !l.acceptsImplementation(impl)) {
185                 return;
186             }
187
188             final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
189             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
190
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)));
194             }
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));
199                 }
200             }
201
202             prevRpcs = rpcs;
203             if (!added.isEmpty()) {
204                 l.onRpcAvailable(added);
205             }
206         }
207
208         void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
209             final T l = getInstance();
210             if (l == null || !l.acceptsImplementation(impl)) {
211                 return;
212             }
213
214             final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
215             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
216
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)));
220             }
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));
225                 }
226             }
227
228             prevRpcs = rpcs;
229             if (!removed.isEmpty()) {
230                 l.onRpcUnavailable(removed);
231             }
232         }
233     }
234 }