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

©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.