2 * Copyright (c) 2015 Brocade Communications 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.clustering.it.provider;
10 import com.google.common.base.Stopwatch;
11 import com.google.common.collect.Sets;
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.Collection;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import java.util.concurrent.atomic.AtomicBoolean;
20 import java.util.concurrent.atomic.AtomicLong;
21 import java.util.concurrent.atomic.AtomicReference;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeCommitCohortRegistry;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
30 import org.opendaylight.mdsal.eos.binding.api.Entity;
31 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
32 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
33 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
34 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
65 import org.opendaylight.yangtools.concepts.ListenerRegistration;
66 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
67 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
68 import org.opendaylight.yangtools.yang.common.RpcResult;
69 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
75 * Implementation of CarService.
77 * @author Thomas Pantelis
79 public class CarProvider implements CarService {
80 private static final Logger LOG_PURCHASE_CAR = LoggerFactory.getLogger(PurchaseCarProvider.class);
82 private static final Logger LOG_CAR_PROVIDER = LoggerFactory.getLogger(CarProvider.class);
84 private static final String ENTITY_TYPE = "cars";
85 private static final InstanceIdentifier<Cars> CARS_IID = InstanceIdentifier.builder(Cars.class).build();
86 private static final DataTreeIdentifier<Cars> CARS_DTID = new DataTreeIdentifier<>(
87 LogicalDatastoreType.CONFIGURATION, CARS_IID);
89 private final DataBroker dataProvider;
90 private final DOMDataBroker domDataBroker;
91 private final EntityOwnershipService ownershipService;
92 private final AtomicLong succcessCounter = new AtomicLong();
93 private final AtomicLong failureCounter = new AtomicLong();
95 private final CarEntityOwnershipListener ownershipListener = new CarEntityOwnershipListener();
96 private final AtomicBoolean registeredListener = new AtomicBoolean();
98 private final Collection<ListenerRegistration<?>> carsDclRegistrations =
99 Sets.newConcurrentHashSet();
100 private final Collection<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
101 Sets.newConcurrentHashSet();
103 private volatile Thread testThread;
104 private volatile boolean stopThread;
105 private final AtomicReference<DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort>> commitCohortReg =
106 new AtomicReference<>();
108 public CarProvider(final DataBroker dataProvider, final EntityOwnershipService ownershipService,
109 final DOMDataBroker domDataBroker) {
110 this.dataProvider = dataProvider;
111 this.ownershipService = ownershipService;
112 this.domDataBroker = domDataBroker;
115 public void close() {
117 closeCommitCohortRegistration();
120 private void stopThread() {
121 if (testThread != null) {
123 testThread.interrupt();
126 } catch (InterruptedException e) {
134 public ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
136 final long inputCount;
138 // If rate is not provided, or given as zero, then just return.
139 if (input.getRate() == null || input.getRate() == 0) {
140 LOG_PURCHASE_CAR.info("Exiting stress test as no rate is given.");
141 return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed()
142 .withError(ErrorType.PROTOCOL, "invalid rate")
146 inputRate = input.getRate();
147 if (input.getCount() != null) {
148 inputCount = input.getCount();
153 LOG_PURCHASE_CAR.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
157 succcessCounter.set(0);
158 failureCounter.set(0);
160 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
161 InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
162 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
164 tx.submit().checkedGet(5, TimeUnit.SECONDS);
165 } catch (TransactionCommitFailedException | TimeoutException e) {
166 LOG_PURCHASE_CAR.error("Put Cars failed",e);
167 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
171 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
172 final Stopwatch sw = Stopwatch.createUnstarted();
173 testThread = new Thread(() -> {
175 AtomicLong count = new AtomicLong();
176 while (!stopThread) {
177 long id = count.incrementAndGet();
178 WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
179 CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
180 tx1.put(LogicalDatastoreType.CONFIGURATION,
181 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
182 Futures.addCallback(tx1.submit(), new FutureCallback<Void>() {
185 public void onSuccess(final Void result) {
186 // Transaction succeeded
187 succcessCounter.getAndIncrement();
191 public void onFailure(final Throwable ex) {
192 // Transaction failed
193 failureCounter.getAndIncrement();
194 LOG_CAR_PROVIDER.error("Put Cars failed", ex);
196 }, MoreExecutors.directExecutor());
198 TimeUnit.NANOSECONDS.sleep(sleep);
199 } catch (InterruptedException e) {
203 if (count.get() % 1000 == 0) {
204 LOG_PURCHASE_CAR.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
207 // Check if a count is specified in input and we have created that many cars.
208 if (inputCount != 0 && count.get() >= inputCount) {
213 LOG_PURCHASE_CAR.info("Stress test thread stopping after creating {} cars.", count.get());
217 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
221 public ListenableFuture<RpcResult<StopStressTestOutput>> stopStressTest(final StopStressTestInput input) {
223 StopStressTestOutputBuilder stopStressTestOutput;
224 stopStressTestOutput = new StopStressTestOutputBuilder()
225 .setSuccessCount(succcessCounter.longValue())
226 .setFailureCount(failureCounter.longValue());
228 final StopStressTestOutput result = stopStressTestOutput.build();
229 LOG_PURCHASE_CAR.info("Executed Stop Stress test; No. of cars created {}; "
230 + "No. of cars failed {}; ", succcessCounter, failureCounter);
232 succcessCounter.set(0);
233 failureCounter.set(0);
234 return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
239 public ListenableFuture<RpcResult<RegisterOwnershipOutput>> registerOwnership(final RegisterOwnershipInput input) {
240 if (registeredListener.compareAndSet(false, true)) {
241 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
244 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
246 ownershipService.registerCandidate(entity);
247 } catch (CandidateAlreadyRegisteredException e) {
248 return RpcResultBuilder.<RegisterOwnershipOutput>failed().withError(ErrorType.APPLICATION,
249 "Could not register for car " + input.getCarId(), e).buildFuture();
252 return RpcResultBuilder.success(new RegisterOwnershipOutputBuilder().build()).buildFuture();
256 public ListenableFuture<RpcResult<UnregisterOwnershipOutput>> unregisterOwnership(
257 final UnregisterOwnershipInput input) {
258 return RpcResultBuilder.success(new UnregisterOwnershipOutputBuilder().build()).buildFuture();
261 private static class CarEntityOwnershipListener implements EntityOwnershipListener {
263 public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
264 LOG_CAR_PROVIDER.info("ownershipChanged: {}", ownershipChange);
269 public ListenableFuture<RpcResult<RegisterLoggingDtclOutput>> registerLoggingDtcl(
270 final RegisterLoggingDtclInput input) {
271 LOG_CAR_PROVIDER.info("Registering a new CarDataTreeChangeListener");
272 final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
273 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
275 carsDtclRegistrations.add(carsDtclRegistration);
276 return RpcResultBuilder.success(new RegisterLoggingDtclOutputBuilder().build()).buildFuture();
280 public ListenableFuture<RpcResult<UnregisterLoggingDtclsOutput>> unregisterLoggingDtcls(
281 final UnregisterLoggingDtclsInput input) {
282 LOG_CAR_PROVIDER.info("Unregistering the CarDataTreeChangeListener(s)");
283 synchronized (carsDtclRegistrations) {
284 int numListeners = 0;
285 for (ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration : carsDtclRegistrations) {
286 carsDtclRegistration.close();
289 carsDtclRegistrations.clear();
290 LOG_CAR_PROVIDER.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
292 return RpcResultBuilder.success(new UnregisterLoggingDtclsOutputBuilder().build()).buildFuture();
296 @SuppressWarnings("checkstyle:IllegalCatch")
297 public ListenableFuture<RpcResult<UnregisterCommitCohortOutput>> unregisterCommitCohort(
298 final UnregisterCommitCohortInput input) {
299 closeCommitCohortRegistration();
301 return RpcResultBuilder.success(new UnregisterCommitCohortOutputBuilder().build()).buildFuture();
304 private void closeCommitCohortRegistration() {
305 final DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort> reg = commitCohortReg.getAndSet(null);
308 LOG_CAR_PROVIDER.info("Unregistered commit cohort");
313 public synchronized ListenableFuture<RpcResult<RegisterCommitCohortOutput>> registerCommitCohort(
314 final RegisterCommitCohortInput input) {
315 if (commitCohortReg.get() != null) {
316 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
319 final DOMDataTreeCommitCohortRegistry commitCohortRegistry = (DOMDataTreeCommitCohortRegistry)
320 domDataBroker.getSupportedExtensions().get(DOMDataTreeCommitCohortRegistry.class);
322 if (commitCohortRegistry == null) {
324 return RpcResultBuilder.<RegisterCommitCohortOutput>failed().withError(ErrorType.APPLICATION,
325 "DOMDataTreeCommitCohortRegistry not found").buildFuture();
328 // Note: it may look strange that we specify the CarEntry.QNAME twice in the path below. This must be done in
329 // order to register the commit cohort for CarEntry instances. In the underlying data tree, a yang list is
330 // represented as a MapNode with MapEntryNodes representing the child list entries. Therefore, in order to
331 // address a list entry, you must specify the path argument for the MapNode and the path argument for the
332 // MapEntryNode. In the path below, the first CarEntry.QNAME argument addresses the MapNode and, since we want
333 // to address all list entries, the second path argument is wild-carded by specifying just the CarEntry.QNAME.
334 final YangInstanceIdentifier carEntryPath = YangInstanceIdentifier.builder(
335 YangInstanceIdentifier.of(Cars.QNAME)).node(CarEntry.QNAME).node(CarEntry.QNAME).build();
336 commitCohortReg.set(commitCohortRegistry.registerCommitCohort(
337 new org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier(
338 org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
339 carEntryPath), new CarEntryDataTreeCommitCohort()));
341 LOG_CAR_PROVIDER.info("Registered commit cohort");
343 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();