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.controller.md.sal.dom.broker.impl;
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;
25 import java.util.Map.Entry;
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;
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();
53 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
56 private Collection<Registration<?>> listeners = Collections.emptyList();
58 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
60 public static DOMRpcRouter newInstance(final SchemaService schemaService) {
61 final DOMRpcRouter rpcRouter = new DOMRpcRouter();
62 schemaService.registerSchemaContextListener(rpcRouter);
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;
72 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
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;
82 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
84 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
86 protected void removeRegistration() {
87 removeRpcImplementation(getInstance(), rpcs);
93 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
94 final NormalizedNode<?, ?> input) {
95 return routingTable.invokeRpc(type, input);
98 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
99 listeners = ImmutableList.copyOf(Collections2.filter(listeners, i -> !reg.equals(i)));
102 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
103 for (Registration<?> l : listeners) {
104 l.addRpc(newTable, impl);
108 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
109 for (Registration<?> l : listeners) {
110 l.removeRpc(newTable, impl);
115 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
117 final Registration<T> ret = new Registration<>(this, listener, routingTable.getRpcs(listener));
118 final Builder<Registration<?>> b = ImmutableList.builder();
121 listeners = b.build();
123 listenerNotifier.execute(ret::initialTable);
128 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
129 final DOMRpcRoutingTable oldTable = routingTable;
130 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
131 routingTable = newTable;
135 public void close() {
136 listenerNotifier.shutdown();
139 private static final class Registration<T extends DOMRpcAvailabilityListener>
140 extends AbstractListenerRegistration<T> {
142 private final DOMRpcRouter router;
144 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
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);
154 protected void removeRegistration() {
155 router.removeListener(this);
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)));
164 if (!added.isEmpty()) {
165 final T l = getInstance();
166 l.onRpcAvailable(added);
170 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
171 final T l = getInstance();
172 if (!l.acceptsImplementation(impl)) {
176 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = Verify.verifyNotNull(newTable.getRpcs(l));
177 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
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)));
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));
190 if (!added.isEmpty()) {
191 l.onRpcAvailable(added);
195 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
196 final T l = getInstance();
197 if (!l.acceptsImplementation(impl)) {
201 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = Verify.verifyNotNull(newTable.getRpcs(l));
202 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
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)));
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));
215 if (!removed.isEmpty()) {
216 l.onRpcUnavailable(removed);