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