bd06589f3e371a416eedf9ed6cb771376882cd03
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / CarProvider.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.clustering.it.provider;
9
10 import com.google.common.base.Stopwatch;
11 import com.google.common.collect.ImmutableClassToInstanceMap;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.HashSet;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import java.util.concurrent.atomic.AtomicBoolean;
23 import java.util.concurrent.atomic.AtomicLong;
24 import java.util.concurrent.atomic.AtomicReference;
25 import javax.annotation.PreDestroy;
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.api.RpcProviderService;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.common.api.CommitInfo;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
38 import org.opendaylight.mdsal.eos.binding.api.Entity;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
40 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
41 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohort;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtcl;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnership;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTest;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTest;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohort;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutputBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtcls;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutputBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnership;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutputBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
79 import org.opendaylight.yangtools.concepts.ListenerRegistration;
80 import org.opendaylight.yangtools.concepts.Registration;
81 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
82 import org.opendaylight.yangtools.yang.binding.Rpc;
83 import org.opendaylight.yangtools.yang.common.ErrorType;
84 import org.opendaylight.yangtools.yang.common.RpcResult;
85 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
86 import org.opendaylight.yangtools.yang.common.Uint32;
87 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
88 import org.osgi.service.component.annotations.Activate;
89 import org.osgi.service.component.annotations.Component;
90 import org.osgi.service.component.annotations.Deactivate;
91 import org.osgi.service.component.annotations.Reference;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
94
95 /**
96  * Implementation of CarService.
97  *
98  * @author Thomas Pantelis
99  */
100 @Singleton
101 @Component(service = { })
102 public final class CarProvider {
103     private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
104
105     private static final String ENTITY_TYPE = "cars";
106     private static final InstanceIdentifier<Cars> CARS_IID = InstanceIdentifier.builder(Cars.class).build();
107     private static final DataTreeIdentifier<Cars> CARS_DTID = DataTreeIdentifier.create(
108             LogicalDatastoreType.CONFIGURATION, CARS_IID);
109
110     private final DataBroker dataProvider;
111     private final DOMDataBroker domDataBroker;
112     private final EntityOwnershipService ownershipService;
113     private final AtomicLong succcessCounter = new AtomicLong();
114     private final AtomicLong failureCounter = new AtomicLong();
115
116     private final EntityOwnershipListener ownershipListener =
117         ownershipChange -> LOG.info("ownershipChanged: {}", ownershipChange);
118
119     private final AtomicBoolean registeredListener = new AtomicBoolean();
120
121     private final Set<ListenerRegistration<?>> carsDclRegistrations = ConcurrentHashMap.newKeySet();
122
123     private final Set<Registration> regs = new HashSet<>();
124     private final Set<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
125             ConcurrentHashMap.newKeySet();
126
127     private volatile Thread testThread;
128     private volatile boolean stopThread;
129     private final AtomicReference<DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort>> commitCohortReg =
130             new AtomicReference<>();
131
132     @Inject
133     @Activate
134     public CarProvider(@Reference final DataBroker dataProvider,
135             @Reference final EntityOwnershipService ownershipService, @Reference final DOMDataBroker domDataBroker,
136             @Reference final RpcProviderService rpcProviderService) {
137         this.dataProvider = dataProvider;
138         this.ownershipService = ownershipService;
139         this.domDataBroker = domDataBroker;
140         regs.add(rpcProviderService.registerRpcImplementations(ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
141             .put(StressTest.class, this::stressTest)
142             .put(StopStressTest.class, this::stopStressTest)
143             .put(RegisterOwnership.class, this::registerOwnership)
144             .put(UnregisterOwnership.class, this::unregisterOwnership)
145             .put(RegisterLoggingDtcl.class, this::registerLoggingDtcl)
146             .put(UnregisterLoggingDtcls.class, this::unregisterLoggingDtcls)
147             .put(RegisterCommitCohort.class, this::registerCommitCohort)
148             .put(UnregisterCommitCohort.class, this::unregisterCommitCohort)
149             .build()));
150     }
151
152     @PreDestroy
153     @Deactivate
154     public void close() {
155         stopThread();
156         closeCommitCohortRegistration();
157         regs.forEach(Registration::close);
158         regs.clear();
159     }
160
161     private void stopThread() {
162         if (testThread != null) {
163             stopThread = true;
164             testThread.interrupt();
165             try {
166                 testThread.join();
167             } catch (InterruptedException e) {
168                 // don't care
169             }
170             testThread = null;
171         }
172     }
173
174     private ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
175         final int inputRate;
176         final long inputCount;
177
178         // If rate is not provided, or given as zero, then just return.
179         if (input.getRate() == null || input.getRate().toJava() == 0) {
180             LOG.info("Exiting stress test as no rate is given.");
181             return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed()
182                     .withError(ErrorType.PROTOCOL, "invalid rate")
183                     .build());
184         }
185
186         inputRate = input.getRate().toJava();
187         if (input.getCount() != null) {
188             inputCount = input.getCount().toJava();
189         } else {
190             inputCount = 0;
191         }
192
193         LOG.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
194
195         stopThread();
196         // clear counters
197         succcessCounter.set(0);
198         failureCounter.set(0);
199
200         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
201         InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
202         tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
203         try {
204             tx.commit().get(5, TimeUnit.SECONDS);
205         } catch (TimeoutException | InterruptedException | ExecutionException e) {
206             LOG.error("Put Cars failed",e);
207             return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
208         }
209
210         stopThread = false;
211         final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
212         final Stopwatch sw = Stopwatch.createUnstarted();
213         testThread = new Thread(() -> {
214             sw.start();
215             AtomicLong count = new AtomicLong();
216             while (!stopThread) {
217                 long id = count.incrementAndGet();
218                 WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
219                 CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
220                 tx1.put(LogicalDatastoreType.CONFIGURATION,
221                         InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
222                 tx1.commit().addCallback(new FutureCallback<CommitInfo>() {
223
224                     @Override
225                     public void onSuccess(final CommitInfo result) {
226                         // Transaction succeeded
227                         succcessCounter.getAndIncrement();
228                     }
229
230                     @Override
231                     public void onFailure(final Throwable ex) {
232                         // Transaction failed
233                         failureCounter.getAndIncrement();
234                         LOG.error("Put Cars failed", ex);
235                     }
236                 }, MoreExecutors.directExecutor());
237                 try {
238                     TimeUnit.NANOSECONDS.sleep(sleep);
239                 } catch (InterruptedException e) {
240                     break;
241                 }
242
243                 if (count.get() % 1000 == 0) {
244                     LOG.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
245                 }
246
247                 // Check if a count is specified in input and we have created that many cars.
248                 if (inputCount != 0 && count.get() >= inputCount) {
249                     stopThread = true;
250                 }
251             }
252
253             LOG.info("Stress test thread stopping after creating {} cars.", count.get());
254         });
255         testThread.start();
256
257         return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
258     }
259
260     private ListenableFuture<RpcResult<StopStressTestOutput>> stopStressTest(final StopStressTestInput input) {
261         stopThread();
262         StopStressTestOutputBuilder stopStressTestOutput;
263         stopStressTestOutput = new StopStressTestOutputBuilder()
264                 .setSuccessCount(Uint32.valueOf(succcessCounter.longValue()))
265                 .setFailureCount(Uint32.valueOf(failureCounter.longValue()));
266
267         final StopStressTestOutput result = stopStressTestOutput.build();
268         LOG.info("Executed Stop Stress test; No. of cars created {}; No. of cars failed {}; ",
269             succcessCounter, failureCounter);
270         // clear counters
271         succcessCounter.set(0);
272         failureCounter.set(0);
273         return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
274     }
275
276     private ListenableFuture<RpcResult<RegisterOwnershipOutput>> registerOwnership(final RegisterOwnershipInput input) {
277         if (registeredListener.compareAndSet(false, true)) {
278             ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
279         }
280
281         Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
282         try {
283             ownershipService.registerCandidate(entity);
284         } catch (CandidateAlreadyRegisteredException e) {
285             return RpcResultBuilder.<RegisterOwnershipOutput>failed().withError(ErrorType.APPLICATION,
286                     "Could not register for car " + input.getCarId(), e).buildFuture();
287         }
288
289         return RpcResultBuilder.success(new RegisterOwnershipOutputBuilder().build()).buildFuture();
290     }
291
292     private ListenableFuture<RpcResult<UnregisterOwnershipOutput>> unregisterOwnership(
293             final UnregisterOwnershipInput input) {
294         return RpcResultBuilder.success(new UnregisterOwnershipOutputBuilder().build()).buildFuture();
295     }
296
297     private ListenableFuture<RpcResult<RegisterLoggingDtclOutput>> registerLoggingDtcl(
298             final RegisterLoggingDtclInput input) {
299         LOG.info("Registering a new CarDataTreeChangeListener");
300         final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
301                 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
302
303         carsDtclRegistrations.add(carsDtclRegistration);
304         return RpcResultBuilder.success(new RegisterLoggingDtclOutputBuilder().build()).buildFuture();
305     }
306
307     private ListenableFuture<RpcResult<UnregisterLoggingDtclsOutput>> unregisterLoggingDtcls(
308             final UnregisterLoggingDtclsInput input) {
309         LOG.info("Unregistering the CarDataTreeChangeListener(s)");
310         synchronized (carsDtclRegistrations) {
311             int numListeners = 0;
312             for (ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration : carsDtclRegistrations) {
313                 carsDtclRegistration.close();
314                 numListeners++;
315             }
316             carsDtclRegistrations.clear();
317             LOG.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
318         }
319         return RpcResultBuilder.success(new UnregisterLoggingDtclsOutputBuilder().build()).buildFuture();
320     }
321
322     @SuppressWarnings("checkstyle:IllegalCatch")
323     private ListenableFuture<RpcResult<UnregisterCommitCohortOutput>> unregisterCommitCohort(
324             final UnregisterCommitCohortInput input) {
325         closeCommitCohortRegistration();
326
327         return RpcResultBuilder.success(new UnregisterCommitCohortOutputBuilder().build()).buildFuture();
328     }
329
330     private void closeCommitCohortRegistration() {
331         final DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort> reg = commitCohortReg.getAndSet(null);
332         if (reg != null) {
333             reg.close();
334             LOG.info("Unregistered commit cohort");
335         }
336     }
337
338     private synchronized ListenableFuture<RpcResult<RegisterCommitCohortOutput>> registerCommitCohort(
339             final RegisterCommitCohortInput input) {
340         if (commitCohortReg.get() != null) {
341             return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
342         }
343
344         final DOMDataTreeCommitCohortRegistry commitCohortRegistry = domDataBroker.getExtensions().getInstance(
345             DOMDataTreeCommitCohortRegistry.class);
346
347         if (commitCohortRegistry == null) {
348             // Shouldn't happen
349             return RpcResultBuilder.<RegisterCommitCohortOutput>failed().withError(ErrorType.APPLICATION,
350                     "DOMDataTreeCommitCohortRegistry not found").buildFuture();
351         }
352
353         // Note: it may look strange that we specify the CarEntry.QNAME twice in the path below. This must be done in
354         // order to register the commit cohort for CarEntry instances. In the underlying data tree, a yang list is
355         // represented as a MapNode with MapEntryNodes representing the child list entries. Therefore, in order to
356         // address a list entry, you must specify the path argument for the MapNode and the path argument for the
357         // MapEntryNode. In the path below, the first CarEntry.QNAME argument addresses the MapNode and, since we want
358         // to address all list entries, the second path argument is wild-carded by specifying just the CarEntry.QNAME.
359         final YangInstanceIdentifier carEntryPath = YangInstanceIdentifier.builder(
360                 YangInstanceIdentifier.of(Cars.QNAME)).node(CarEntry.QNAME).node(CarEntry.QNAME).build();
361         commitCohortReg.set(commitCohortRegistry.registerCommitCohort(new DOMDataTreeIdentifier(
362             LogicalDatastoreType.CONFIGURATION, carEntryPath), new CarEntryDataTreeCommitCohort()));
363
364         LOG.info("Registered commit cohort");
365
366         return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
367     }
368 }