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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.