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.annotations.VisibleForTesting;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Collection;
15 import java.util.Collections;
18 import java.util.WeakHashMap;
19 import java.util.stream.Collectors;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
28 import org.opendaylight.controller.md.sal.dom.spi.AbstractDOMRpcImplementationRegistration;
29 import org.opendaylight.controller.sal.core.compat.LegacyDOMRpcResultFutureAdapter;
30 import org.opendaylight.controller.sal.core.compat.MdsalDOMRpcResultFutureAdapter;
31 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcProviderService, SchemaContextListener {
39 // This mapping is used to translate mdsal DOMRpcImplementations to their corresponding legacy
40 // DOMRpcImplementations registered thru this interface when invoking a DOMRpcAvailabilityListener.
41 private final Map<org.opendaylight.mdsal.dom.api.DOMRpcImplementation, DOMRpcImplementation> implMapping =
42 Collections.synchronizedMap(new WeakHashMap<>());
44 private final org.opendaylight.mdsal.dom.api.DOMRpcService delegateRpcService;
45 private final org.opendaylight.mdsal.dom.api.DOMRpcProviderService delegateRpcProviderService;
47 // Note - this is only used for backward compatibility for UTs that use the empty constructor which creates
48 // a local mdsal DOMRpcRouter that needs to be updated with the SchemaContext. In production, the mdsal API
49 // services are passed via the constructor and are set up externally with the SchemaContext.
50 private final SchemaContextListener delegateSchemaContextListener;
53 public DOMRpcRouter() {
54 org.opendaylight.mdsal.dom.broker.DOMRpcRouter delegate = new org.opendaylight.mdsal.dom.broker.DOMRpcRouter();
55 this.delegateRpcService = delegate.getRpcService();
56 this.delegateRpcProviderService = delegate.getRpcProviderService();
57 this.delegateSchemaContextListener = delegate;
60 public DOMRpcRouter(final org.opendaylight.mdsal.dom.api.DOMRpcService delegateRpcService,
61 final org.opendaylight.mdsal.dom.api.DOMRpcProviderService delegateRpcProviderService) {
62 this.delegateRpcService = delegateRpcService;
63 this.delegateRpcProviderService = delegateRpcProviderService;
64 this.delegateSchemaContextListener = null;
68 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
69 final T implementation, final DOMRpcIdentifier... rpcs) {
70 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
74 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
75 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
76 org.opendaylight.mdsal.dom.api.DOMRpcImplementation delegateImpl =
77 new org.opendaylight.mdsal.dom.api.DOMRpcImplementation() {
79 public FluentFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> invokeRpc(
80 org.opendaylight.mdsal.dom.api.DOMRpcIdentifier rpc, NormalizedNode<?, ?> input) {
81 return new MdsalDOMRpcResultFutureAdapter(implementation.invokeRpc(convert(rpc), input));
85 public long invocationCost() {
86 return implementation.invocationCost();
90 implMapping.put(delegateImpl, implementation);
92 final org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration
93 <org.opendaylight.mdsal.dom.api.DOMRpcImplementation> reg = delegateRpcProviderService
94 .registerRpcImplementation(delegateImpl,
95 rpcs.stream().map(DOMRpcRouter::convert).collect(Collectors.toSet()));
97 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
99 protected void removeRegistration() {
101 implMapping.remove(delegateImpl);
106 private static org.opendaylight.mdsal.dom.api.DOMRpcIdentifier convert(DOMRpcIdentifier from) {
107 return org.opendaylight.mdsal.dom.api.DOMRpcIdentifier.create(from.getType(), from.getContextReference());
110 private static DOMRpcIdentifier convert(org.opendaylight.mdsal.dom.api.DOMRpcIdentifier from) {
111 return DOMRpcIdentifier.create(from.getType(), from.getContextReference());
114 private static Collection<DOMRpcIdentifier> convert(
115 Collection<org.opendaylight.mdsal.dom.api.DOMRpcIdentifier> from) {
116 return from.stream().map(DOMRpcRouter::convert).collect(Collectors.toList());
120 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
121 final NormalizedNode<?, ?> input) {
122 final FluentFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> future =
123 delegateRpcService.invokeRpc(type, input);
124 return future instanceof MdsalDOMRpcResultFutureAdapter ? ((MdsalDOMRpcResultFutureAdapter)future).delegate()
125 : new LegacyDOMRpcResultFutureAdapter(future);
129 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
131 final ListenerRegistration<org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener> reg =
132 delegateRpcService.registerRpcListener(new org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener() {
134 public void onRpcAvailable(Collection<org.opendaylight.mdsal.dom.api.DOMRpcIdentifier> rpcs) {
135 listener.onRpcAvailable(convert(rpcs));
139 public void onRpcUnavailable(Collection<org.opendaylight.mdsal.dom.api.DOMRpcIdentifier> rpcs) {
140 listener.onRpcUnavailable(convert(rpcs));
144 public boolean acceptsImplementation(final org.opendaylight.mdsal.dom.api.DOMRpcImplementation impl) {
145 // If the DOMRpcImplementation wasn't registered thru this interface then the mapping won't be
146 // present - in this we can't call the listener so just assume acceptance which is the default
147 // behavior. This should be fine since a legacy listener would not be aware of implementation types
148 // registered via the new mdsal API.
149 final DOMRpcImplementation legacyImpl = implMapping.get(impl);
150 return legacyImpl != null ? listener.acceptsImplementation(legacyImpl) : true;
154 return new AbstractListenerRegistration<T>(listener) {
156 protected void removeRegistration() {
163 public void close() {
168 public void onGlobalContextUpdated(final SchemaContext context) {
169 if (delegateSchemaContextListener != null) {
170 delegateSchemaContextListener.onGlobalContextUpdated(context);