Proxy MD-SAL interfaces in DOMMountPointServiceImpl
[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.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.Collections;
15 import java.util.Map;
16 import java.util.Set;
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;
37
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<>());
43
44     private final org.opendaylight.mdsal.dom.api.DOMRpcService delegateRpcService;
45     private final org.opendaylight.mdsal.dom.api.DOMRpcProviderService delegateRpcProviderService;
46
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;
51
52     @VisibleForTesting
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;
58     }
59
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;
65     }
66
67     @Override
68     public <T extends DOMRpcImplementation> DOMRpcImplementationRegistration<T> registerRpcImplementation(
69             final T implementation, final DOMRpcIdentifier... rpcs) {
70         return registerRpcImplementation(implementation, ImmutableSet.copyOf(rpcs));
71     }
72
73     @Override
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() {
78                 @Override
79                 public FluentFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> invokeRpc(
80                         final org.opendaylight.mdsal.dom.api.DOMRpcIdentifier rpc, final NormalizedNode<?, ?> input) {
81                     return new MdsalDOMRpcResultFutureAdapter(implementation.invokeRpc(DOMRpcIdentifier.fromMdsal(rpc),
82                         input));
83                 }
84
85
86                 @Override
87                 public long invocationCost() {
88                     return implementation.invocationCost();
89                 }
90             };
91
92         implMapping.put(delegateImpl, implementation);
93
94         final org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration
95             <org.opendaylight.mdsal.dom.api.DOMRpcImplementation> reg = delegateRpcProviderService
96                 .registerRpcImplementation(delegateImpl, DOMRpcServiceAdapter.convert(rpcs));
97
98         return new AbstractDOMRpcImplementationRegistration<T>(implementation) {
99             @Override
100             protected void removeRegistration() {
101                 reg.close();
102                 implMapping.remove(delegateImpl);
103             }
104         };
105     }
106
107     @Override
108     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
109                                                                   final NormalizedNode<?, ?> input) {
110         final FluentFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> future =
111                 delegateRpcService.invokeRpc(type, input);
112         return future instanceof MdsalDOMRpcResultFutureAdapter ? ((MdsalDOMRpcResultFutureAdapter)future).delegate()
113                 : new LegacyDOMRpcResultFutureAdapter(future);
114     }
115
116     @Override
117     public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
118             final T listener) {
119         final ListenerRegistration<org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener> reg =
120             delegateRpcService.registerRpcListener(new RpcAvailabilityListenerAdapter<T>(listener) {
121                 @Override
122                 public boolean acceptsImplementation(final org.opendaylight.mdsal.dom.api.DOMRpcImplementation impl) {
123                     // If the DOMRpcImplementation wasn't registered thru this interface then the mapping won't be
124                     // present - in this we can't call the listener so just assume acceptance which is the default
125                     // behavior. This should be fine since a legacy listener would not be aware of implementation types
126                     // registered via the new mdsal API.
127                     final DOMRpcImplementation legacyImpl = implMapping.get(impl);
128                     return legacyImpl != null ? delegate().acceptsImplementation(legacyImpl) : true;
129                 }
130             });
131
132         return new AbstractListenerRegistration<T>(listener) {
133             @Override
134             protected void removeRegistration() {
135                 reg.close();
136             }
137         };
138     }
139
140     @Override
141     public void close() {
142     }
143
144     @Override
145     @VisibleForTesting
146     public void onGlobalContextUpdated(final SchemaContext context) {
147         if (delegateSchemaContextListener != null) {
148             delegateSchemaContextListener.onGlobalContextUpdated(context);
149         }
150     }
151 }

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