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.ListenableFuture;
14 import java.util.Collections;
17 import java.util.WeakHashMap;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.md.sal.dom.spi.AbstractDOMRpcImplementationRegistration;
27 import org.opendaylight.controller.sal.core.compat.DOMRpcServiceAdapter;
28 import org.opendaylight.controller.sal.core.compat.LegacyDOMRpcResultFutureAdapter;
29 import org.opendaylight.controller.sal.core.compat.MdsalDOMRpcResultFutureAdapter;
30 import org.opendaylight.controller.sal.core.compat.RpcAvailabilityListenerAdapter;
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;
39 public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcProviderService, SchemaContextListener {
40 // This mapping is used to translate mdsal DOMRpcImplementations to their corresponding legacy
41 // DOMRpcImplementations registered thru this interface when invoking a DOMRpcAvailabilityListener.
42 private final Map<org.opendaylight.mdsal.dom.api.DOMRpcImplementation, DOMRpcImplementation> implMapping =
43 Collections.synchronizedMap(new WeakHashMap<>());
45 private final org.opendaylight.mdsal.dom.api.DOMRpcService delegateRpcService;
46 private final org.opendaylight.mdsal.dom.api.DOMRpcProviderService delegateRpcProviderService;
48 // Note - this is only used for backward compatibility for UTs that use the empty constructor which creates
49 // a local mdsal DOMRpcRouter that needs to be updated with the SchemaContext. In production, the mdsal API
50 // services are passed via the constructor and are set up externally with the SchemaContext.
51 private final SchemaContextListener delegateSchemaContextListener;
54 public DOMRpcRouter() {
55 org.opendaylight.mdsal.dom.broker.DOMRpcRouter delegate = new org.opendaylight.mdsal.dom.broker.DOMRpcRouter();
56 this.delegateRpcService = delegate.getRpcService();
57 this.delegateRpcProviderService = delegate.getRpcProviderService();
58 this.delegateSchemaContextListener = delegate;
61 public DOMRpcRouter(final org.opendaylight.mdsal.dom.api.DOMRpcService delegateRpcService,
62 final org.opendaylight.mdsal.dom.api.DOMRpcProviderService delegateRpcProviderService) {
63 this.delegateRpcService = delegateRpcService;
64 this.delegateRpcProviderService = delegateRpcProviderService;
65 this.delegateSchemaContextListener = null;
69 public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
70 final T implementation, final DOMRpcIdentifier... rpcs) {
71 return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
75 public synchronized <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
76 final T implementation, final Set<DOMRpcIdentifier> rpcs) {
77 org.opendaylight.mdsal.dom.api.DOMRpcImplementation delegateImpl =
78 new org.opendaylight.mdsal.dom.api.DOMRpcImplementation() {
80 public ListenableFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> invokeRpc(
81 final org.opendaylight.mdsal.dom.api.DOMRpcIdentifier rpc, final NormalizedNode<?, ?> input) {
82 return new MdsalDOMRpcResultFutureAdapter(implementation.invokeRpc(DOMRpcIdentifier.fromMdsal(rpc),
88 public long invocationCost() {
89 return implementation.invocationCost();
93 implMapping.put(delegateImpl, implementation);
95 final org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration
96 <org.opendaylight.mdsal.dom.api.DOMRpcImplementation> reg = delegateRpcProviderService
97 .registerRpcImplementation(delegateImpl, DOMRpcServiceAdapter.convert(rpcs));
99 return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
101 protected void removeRegistration() {
103 implMapping.remove(delegateImpl);
109 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
110 final NormalizedNode<?, ?> input) {
111 final ListenableFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> future =
112 delegateRpcService.invokeRpc(type, input);
113 return future instanceof MdsalDOMRpcResultFutureAdapter ? ((MdsalDOMRpcResultFutureAdapter)future).delegate()
114 : new LegacyDOMRpcResultFutureAdapter(future);
118 public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
120 final ListenerRegistration<org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener> reg =
121 delegateRpcService.registerRpcListener(new RpcAvailabilityListenerAdapter<T>(listener) {
123 public boolean acceptsImplementation(final org.opendaylight.mdsal.dom.api.DOMRpcImplementation impl) {
124 // If the DOMRpcImplementation wasn't registered thru this interface then the mapping won't be
125 // present - in this we can't call the listener so just assume acceptance which is the default
126 // behavior. This should be fine since a legacy listener would not be aware of implementation types
127 // registered via the new mdsal API.
128 final DOMRpcImplementation legacyImpl = implMapping.get(impl);
129 return legacyImpl != null ? delegate().acceptsImplementation(legacyImpl) : true;
133 return new AbstractListenerRegistration<T>(listener) {
135 protected void removeRegistration() {
142 public void close() {
147 public void onGlobalContextUpdated(final SchemaContext context) {
148 if (delegateSchemaContextListener != null) {
149 delegateSchemaContextListener.onGlobalContextUpdated(context);