acf5397478a5ff520d963086ce15a14923d55e4e
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / MdsalLowLevelTestProvider.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.controller.clustering.it.provider;
10
11 import com.google.common.util.concurrent.Futures;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.clustering.it.provider.impl.GetConstantService;
16 import org.opendaylight.controller.clustering.it.provider.impl.RoutedGetConstantService;
17 import org.opendaylight.controller.clustering.it.provider.impl.SingletonGetConstantService;
18 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
19 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodecFactory;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.controller.sal.core.api.model.SchemaService;
25 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
27 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
28 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.AddShardReplicaInput;
29 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomeModuleLeaderInput;
30 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomePrefixLeaderInput;
31 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.IsClientAbortedOutput;
32 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.OdlMdsalLowlevelControlService;
33 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsInput;
34 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.PublishNotificationsInput;
35 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RegisterBoundConstantInput;
36 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RegisterConstantInput;
37 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RegisterDefaultConstantInput;
38 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RegisterSingletonConstantInput;
39 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemoveShardReplicaInput;
40 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.SubscribeYnlInput;
41 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnregisterBoundConstantInput;
42 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnregisterFlappingSingletonOutput;
43 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeDdtlOutput;
44 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeDtclOutput;
45 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlInput;
46 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlOutput;
47 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsInput;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.RpcError;
52 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService {
60
61     private static final Logger LOG = LoggerFactory.getLogger(MdsalLowLevelTestProvider.class);
62
63     private final RpcProviderRegistry rpcRegistry;
64     private final BindingAwareBroker.RpcRegistration<OdlMdsalLowlevelControlService> registration;
65     private final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
66     private final SchemaService schemaService;
67     private final ClusterSingletonServiceProvider singletonService;
68     private final DOMRpcProviderService domRpcService;
69
70     private Map<InstanceIdentifier<?>, DOMRpcImplementationRegistration<RoutedGetConstantService>> routedRegistrations =
71             new HashMap<>();
72
73     private DOMRpcImplementationRegistration<GetConstantService> globalGetConstantRegistration = null;
74     private ClusterSingletonServiceRegistration getSingletonConstantRegistration;
75
76     public MdsalLowLevelTestProvider(final RpcProviderRegistry rpcRegistry,
77                                      final DOMRpcProviderService domRpcService,
78                                      final ClusterSingletonServiceProvider singletonService,
79                                      final SchemaService schemaService,
80                                      final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
81         this.rpcRegistry = rpcRegistry;
82         this.domRpcService = domRpcService;
83         this.singletonService = singletonService;
84         this.schemaService = schemaService;
85         this.bindingNormalizedNodeSerializer = bindingNormalizedNodeSerializer;
86
87         registration = rpcRegistry.addRpcImplementation(OdlMdsalLowlevelControlService.class, this);
88     }
89
90     @Override
91     public Future<RpcResult<Void>> unregisterSingletonConstant() {
92         LOG.debug("unregister-singleton-constant");
93
94         if (getSingletonConstantRegistration == null) {
95             LOG.debug("No get-singleton-constant registration present.");
96             final RpcError rpcError = RpcResultBuilder
97                     .newError(ErrorType.APPLICATION, "missing-registration", "No get-singleton-constant rpc registration present.");
98             final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
99             return Futures.immediateFuture(result);
100         }
101
102         try {
103             getSingletonConstantRegistration.close();
104             getSingletonConstantRegistration = null;
105
106             return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
107         } catch (final Exception e) {
108             LOG.debug("There was a problem closing the singleton constant service", e);
109             final RpcError rpcError = RpcResultBuilder
110                     .newError(ErrorType.APPLICATION, "error-closing", "There was a problem closing get-singleton-constant");
111             final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
112             return Futures.immediateFuture(result);
113         }
114     }
115
116     @Override
117     public Future<RpcResult<Void>> publishNotifications(PublishNotificationsInput input) {
118         return null;
119     }
120
121     @Override
122     public Future<RpcResult<Void>> subscribeDtcl() {
123         return null;
124     }
125
126     @Override
127     public Future<RpcResult<Void>> writeTransactions(WriteTransactionsInput input) {
128         return null;
129     }
130
131     @Override
132     public Future<RpcResult<IsClientAbortedOutput>> isClientAborted() {
133         return null;
134     }
135
136     @Override
137     public Future<RpcResult<Void>> becomeModuleLeader(BecomeModuleLeaderInput input) {
138         return null;
139     }
140
141     @Override
142     public Future<RpcResult<Void>> removeShardReplica(RemoveShardReplicaInput input) {
143         return null;
144     }
145
146     @Override
147     public Future<RpcResult<Void>> subscribeYnl(SubscribeYnlInput input) {
148         return null;
149     }
150
151     @Override
152     public Future<RpcResult<Void>> becomePrefixLeader(BecomePrefixLeaderInput input) {
153         return null;
154     }
155
156     @Override
157     public Future<RpcResult<Void>> unregisterBoundConstant(final UnregisterBoundConstantInput input) {
158         LOG.debug("unregister-bound-constant, {}", input);
159
160         final DOMRpcImplementationRegistration<RoutedGetConstantService> registration =
161                 routedRegistrations.remove(input.getContext());
162
163         if (registration == null) {
164             LOG.debug("No get-contexted-constant registration for context: {}", input.getContext());
165             final RpcError rpcError = RpcResultBuilder
166                     .newError(ErrorType.APPLICATION, "missing-registration", "No get-constant rpc registration present.");
167             final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
168             return Futures.immediateFuture(result);
169         }
170
171         registration.close();
172         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
173     }
174
175     @Override
176     public Future<RpcResult<Void>> registerSingletonConstant(final RegisterSingletonConstantInput input) {
177
178         LOG.debug("Received register-singleton-constant rpc, input: {}", input);
179
180         if (input.getConstant() == null) {
181             final RpcError error = RpcResultBuilder.newError(
182                     ErrorType.RPC, "Invalid input.", "Constant value is null");
183             return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
184         }
185
186         getSingletonConstantRegistration =
187                 SingletonGetConstantService.registerNew(singletonService, domRpcService, input.getConstant());
188
189         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
190     }
191
192     @Override
193     public Future<RpcResult<Void>> registerDefaultConstant(RegisterDefaultConstantInput input) {
194         return null;
195     }
196
197     @Override
198     public Future<RpcResult<Void>> unregisterConstant() {
199
200         if (globalGetConstantRegistration == null) {
201             final RpcError rpcError = RpcResultBuilder
202                     .newError(ErrorType.APPLICATION, "missing-registration", "No get-constant rpc registration present.");
203             final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
204             return Futures.immediateFuture(result);
205         }
206
207         globalGetConstantRegistration.close();
208         globalGetConstantRegistration = null;
209
210         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
211     }
212
213     @Override
214     public Future<RpcResult<UnregisterFlappingSingletonOutput>> unregisterFlappingSingleton() {
215         return null;
216     }
217
218     @Override
219     public Future<RpcResult<Void>> addShardReplica(AddShardReplicaInput input) {
220         return null;
221     }
222
223     @Override
224     public Future<RpcResult<Void>> subscribeDdtl() {
225         return null;
226     }
227
228     @Override
229     public Future<RpcResult<Void>> registerBoundConstant(final RegisterBoundConstantInput input) {
230         LOG.debug("register-bound-constant: {}", input);
231
232         if (input.getContext() == null) {
233             final RpcError error = RpcResultBuilder.newError(
234                     ErrorType.RPC, "Invalid input.", "Context value is null");
235             return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
236         }
237
238         if (input.getConstant() == null) {
239             final RpcError error = RpcResultBuilder.newError(
240                     ErrorType.RPC, "Invalid input.", "Constant value is null");
241             return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
242         }
243
244         if (routedRegistrations.containsKey(input.getContext())) {
245             final RpcError error = RpcResultBuilder.newError(ErrorType.RPC, "Registration present.",
246                     "There is already a rpc registered for context: " + input.getContext());
247             return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
248         }
249
250         final DOMRpcImplementationRegistration<RoutedGetConstantService> registration =
251                 RoutedGetConstantService.registerNew(bindingNormalizedNodeSerializer, domRpcService,
252                         input.getConstant(), input.getContext());
253
254         routedRegistrations.put(input.getContext(), registration);
255         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
256     }
257
258     @Override
259     public Future<RpcResult<Void>> registerFlappingSingleton() {
260         return null;
261     }
262
263     @Override
264     public Future<RpcResult<UnsubscribeDtclOutput>> unsubscribeDtcl() {
265         return null;
266     }
267
268     @Override
269     public Future<RpcResult<Void>> deconfigureIdIntsShard() {
270         return null;
271     }
272
273     @Override
274     public Future<RpcResult<UnsubscribeYnlOutput>> unsubscribeYnl(UnsubscribeYnlInput input) {
275         return null;
276     }
277
278     @Override
279     public Future<RpcResult<Void>> produceTransactions(ProduceTransactionsInput input) {
280         return null;
281     }
282
283     @Override
284     public Future<RpcResult<Void>> registerConstant(final RegisterConstantInput input) {
285
286         LOG.debug("Received register-constant rpc, input: {}", input);
287
288         if (input.getConstant() == null) {
289             final RpcError error = RpcResultBuilder.newError(
290                     ErrorType.RPC, "Invalid input.", "Constant value is null");
291             return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
292         }
293
294         if (globalGetConstantRegistration != null) {
295             final RpcError error = RpcResultBuilder.newError(ErrorType.RPC, "Registration present.",
296                     "There is already a get-constant rpc registered.");
297             return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
298         }
299
300         globalGetConstantRegistration = GetConstantService.registerNew(domRpcService, input.getConstant());
301         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
302     }
303
304     @Override
305     public Future<RpcResult<Void>> unregisterDefaultConstant() {
306         return null;
307     }
308
309     @Override
310     public Future<RpcResult<UnsubscribeDdtlOutput>> unsubscribeDdtl() {
311         return null;
312     }
313 }

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