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.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;
26 import java.util.Map.Entry;
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;
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();
54 private final ExecutorService listenerNotifier = Executors.newSingleThreadExecutor(THREAD_FACTORY);
57 private Collection<Registration<?>> listeners = Collections.emptyList();
59 private volatile DOMRpcRoutingTable routingTable = DOMRpcRoutingTable.EMPTY;
61 public static DOMRpcRouter newInstance(final SchemaService schemaService) {
62 final DOMRpcRouter rpcRouter = new DOMRpcRouter();
63 schemaService.registerSchemaContextListener(rpcRouter);
68 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
69 final T implementation, final DOMRpcIdentifier... rpcs) {
70 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
73 private synchronized void removeRpcImplementation(final DOMRpcImplementation implementation,
74 final Set<DOMRpcIdentifier> rpcs) {
75 final DOMRpcRoutingTable oldTable = routingTable;
76 final DOMRpcRoutingTable newTable = oldTable.remove(implementation, rpcs);
77 routingTable = newTable;
79 listenerNotifier.execute(() -> notifyRemoved(newTable, implementation));
83 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
84 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
85 final DOMRpcRoutingTable oldTable = routingTable;
86 final DOMRpcRoutingTable newTable = oldTable.add(implementation, rpcs);
87 routingTable = newTable;
89 listenerNotifier.execute(() -> notifyAdded(newTable, implementation));
91 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
93 protected void removeRegistration() {
94 removeRpcImplementation(getInstance(), rpcs);
100 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
101 final NormalizedNode<?, ?> input) {
102 return routingTable.invokeRpc(type, input);
105 private synchronized void removeListener(final ListenerRegistration<? extends DOMRpcAvailabilityListener> reg) {
106 listeners = ImmutableList.copyOf(Collections2.filter(listeners, i -> !reg.equals(i)));
109 private synchronized void notifyAdded(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
110 for (Registration<?> l : listeners) {
111 l.addRpc(newTable, impl);
115 private synchronized void notifyRemoved(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
116 for (Registration<?> l : listeners) {
117 l.removeRpc(newTable, impl);
122 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
124 final Registration<T> ret = new Registration<>(this, listener, routingTable.getRpcs(listener));
125 final Builder<Registration<?>> b = ImmutableList.builder();
128 listeners = b.build();
130 listenerNotifier.execute(ret::initialTable);
135 public synchronized void onGlobalContextUpdated(final SchemaContext context) {
136 final DOMRpcRoutingTable oldTable = routingTable;
137 final DOMRpcRoutingTable newTable = oldTable.setSchemaContext(context);
138 routingTable = newTable;
142 public void close() {
143 listenerNotifier.shutdown();
146 private static final class Registration<T extends DOMRpcAvailabilityListener>
147 extends AbstractListenerRegistration<T> {
149 private final DOMRpcRouter router;
151 private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
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);
161 protected void removeRegistration() {
162 router.removeListener(this);
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)));
171 if (!added.isEmpty()) {
172 final T l = getInstance();
173 l.onRpcAvailable(added);
177 void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
178 final T l = getInstance();
179 if (!l.acceptsImplementation(impl)) {
183 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = Verify.verifyNotNull(newTable.getRpcs(l));
184 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
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)));
190 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
191 for (YangInstanceIdentifier i : Sets.difference(e.getValue().rightValue(), e.getValue().leftValue())) {
192 added.add(DOMRpcIdentifier.create(e.getKey(), i));
197 if (!added.isEmpty()) {
198 l.onRpcAvailable(added);
202 void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
203 final T l = getInstance();
204 if (!l.acceptsImplementation(impl)) {
208 final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = Verify.verifyNotNull(newTable.getRpcs(l));
209 final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
211 final Collection<DOMRpcIdentifier> removed = new ArrayList<>();
212 for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : diff.entriesOnlyOnLeft().entrySet()) {
213 removed.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
215 for (Entry<SchemaPath, ValueDifference<Set<YangInstanceIdentifier>>> e : diff.entriesDiffering().entrySet()) {
216 for (YangInstanceIdentifier i : Sets.difference(e.getValue().leftValue(), e.getValue().rightValue())) {
217 removed.add(DOMRpcIdentifier.create(e.getKey(), i));
222 if (!removed.isEmpty()) {
223 l.onRpcUnavailable(removed);