BUG-7697: add defences against nulls
[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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
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.ThreadFactoryBuilder;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.ThreadFactory;
33 import javax.annotation.concurrent.GuardedBy;
34 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
35 import org.opendaylight.mdsal.dom.api.DOMRpcException;
36 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
37 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
38 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
39 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
40 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
41 import org.opendaylight.mdsal.dom.api.DOMRpcService;
42 import org.opendaylight.mdsal.dom.spi.AbstractDOMRpcImplementationRegistration;
43 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50
51 public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcProviderService, SchemaContextListener {
52     private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
53             "DOMRpcRouter-listener-%s").setDaemon(true).build();
54
55     private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
56
57     @GuardedBy("this")
58     private Collection<Registration<?>> listeners = Collections.emptyList();
59
60     private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
61
62     @Override
63     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
64             final T implementation, final DOMRpcIdentifier... rpcs) {
65         return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
66     }
67
68     @Override
69     public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T>
70             registerRpcImplementation(final T implementation, final Set<DOMRpcIdentifier> rpcs) {
71         final DOMRpcRoutingTable oldTable = routingTable;
72         final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
73         routingTable = newTable;
74
75         listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
76
77         return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
78             @Override
79             protected void removeRegistration() {
80                 removeRpcImplementation(getInstance(), rpcs);
81             }
82         };
83     }
84
85     private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
86             final Set<DOMRpcIdentifier> rpcs) {
87         final DOMRpcRoutingTable oldTable = routingTable;
88         final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
89         routingTable = newTable;
90
91         listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
92     }
93
94     @Override
95     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
96             final NormalizedNode<?, ?> input) {
97         return routingTable.invokeRpc(type, input);
98     }
99
100     private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
101         listeners = ImmutableList.copyOf(Collections2.filter(listeners, input -> !reg.equals(input)));
102     }
103
104     private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
105         for (Registration<?> l : listeners) {
106             l.addRpc(newTable, impl);
107         }
108     }
109
110     private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
111         for (Registration<?> l : listeners) {
112             l.removeRpc(newTable, impl);
113         }
114     }
115
116     @Override
117     public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
118             final T listener) {
119         final Registration<T> ret = new Registration<>(this, listener, routingTable.getRpcs(listener));
120         final Builder<Registration<?>> b = ImmutableList.builder();
121         b.addAll(listeners);
122         b.add(ret);
123         listeners = b.build();
124
125         listenerNotifier.execute(() -> ret.initialTable());
126         return ret;
127     }
128
129     @Override
130     public synchronized void onGlobalContextUpdated(final SchemaContext context) {
131         final DOMRpcRoutingTable oldTable = routingTable;
132         final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
133         routingTable = newTable;
134     }
135
136     @Override
137     public void close() {
138         listenerNotifier.shutdown();
139     }
140
141     @VisibleForTesting
142     Collection<?> listeners() {
143         return listeners;
144     }
145
146     @VisibleForTesting
147     DOMRpcRoutingTable routingTable() {
148         return routingTable;
149     }
150
151     private static final class Registration<T extends DOMRpcAvailabilityListener>
152         extends AbstractListenerRegistration<T> {
153
154         private final DOMRpcRouter router;
155
156         private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
157
158         Registration(final DOMRpcRouter router, final T listener,
159                 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs) {
160             super(listener);
161             this.router = requireNonNull(router);
162             this.prevRpcs = requireNonNull(rpcs);
163         }
164
165         @Override
166         protected void removeRegistration() {
167             router.removeListener(this);
168         }
169
170         void initialTable() {
171             final Collection<DOMRpcIdentifier> added = new ArrayList<>();
172             for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
173                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
174             }
175             if (!added.isEmpty()) {
176                 getInstance().onRpcAvailable(added);
177             }
178         }
179
180         void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
181             final T l = getInstance();
182             if (!l.acceptsImplementation(impl)) {
183                 return;
184             }
185
186             final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getRpcs(l));
187             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
188
189             final Collection<DOMRpcIdentifier> added = new ArrayList<>();
190             for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnRight().entrySet()) {
191                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
192             }
193             for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
194                     diff.entriesDiffering().entrySet()) {
195                 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
196                     added.add(DOMRpcIdentifier.create(e.getKey(), i));
197                 }
198             }
199
200             prevRpcs = rpcs;
201             if (!added.isEmpty()) {
202                 l.onRpcAvailable(added);
203             }
204         }
205
206         void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
207             final T l = getInstance();
208             if (!l.acceptsImplementation(impl)) {
209                 return;
210             }
211
212             final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = verifyNotNull(newTable.getRpcs(l));
213             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
214
215             final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
216             for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
217                 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
218             }
219             for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e :
220                     diff.entriesDiffering().entrySet()) {
221                 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
222                     removed.add(DOMRpcIdentifier.create(e.getKey(), i));
223                 }
224             }
225
226             prevRpcs = rpcs;
227             if (!removed.isEmpty()) {
228                 l.onRpcUnavailable(removed);
229             }
230         }
231     }
232 }