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.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import java.util.Collection;
16 import java.util.concurrent.Future;
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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
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.clustering.CandidateAlreadyRegisteredException;
26 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
27 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
28 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
30 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * @author Thomas Pantelis
55 public class CarProvider implements CarService {
56 private static final Logger log = LoggerFactory.getLogger(PurchaseCarProvider.class);
58 private final DataBroker dataProvider;
59 private final EntityOwnershipService ownershipService;
60 private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
62 private static final String ENTITY_TYPE = "cars";
64 private AtomicLong succcessCounter = new AtomicLong();
65 private AtomicLong failureCounter = new AtomicLong();
67 private final CarEntityOwnershipListener ownershipListener = new CarEntityOwnershipListener();
68 private final AtomicBoolean registeredListener = new AtomicBoolean();
70 private volatile Thread testThread;
71 private volatile boolean stopThread;
73 private static final InstanceIdentifier CARS_IID = InstanceIdentifier.builder(Cars.class).build();
74 private static final DataTreeIdentifier<Cars> CARS_DTID = new DataTreeIdentifier<>(
75 LogicalDatastoreType.CONFIGURATION, CARS_IID);
77 private Collection<ListenerRegistration<DataChangeListener>> carsDclRegistrations =
78 Sets.newConcurrentHashSet();
79 private Collection<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
80 Sets.newConcurrentHashSet();
82 public CarProvider(DataBroker dataProvider, EntityOwnershipService ownershipService) {
83 this.dataProvider = dataProvider;
84 this.ownershipService = ownershipService;
87 private void stopThread() {
88 if(testThread != null) {
90 testThread.interrupt();
93 } catch (InterruptedException e) {}
99 public Future<RpcResult<Void>> stressTest(StressTestInput input) {
101 final long inputCount;
103 // If rate is not provided, or given as zero, then just return.
104 if ((input.getRate() == null) || (input.getRate() == 0)) {
105 log.info("Exiting stress test as no rate is given.");
106 return Futures.immediateFuture(RpcResultBuilder.<Void>failed()
107 .withError(ErrorType.PROTOCOL, "invalid rate")
110 inputRate = input.getRate();
113 if (input.getCount() != null) {
114 inputCount = input.getCount();
119 log.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
123 succcessCounter.set(0);
124 failureCounter.set(0);
126 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
127 InstanceIdentifier<Cars> carsId = InstanceIdentifier.<Cars>builder(Cars.class).build();
128 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
130 tx.submit().checkedGet(5, TimeUnit.SECONDS);
131 } catch (TransactionCommitFailedException | TimeoutException e) {
132 log.error("Put Cars failed",e);
133 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
137 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
138 final Stopwatch sw = Stopwatch.createUnstarted();
139 testThread = new Thread() {
143 AtomicLong count = new AtomicLong();
145 long id = count.incrementAndGet();
146 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
147 CarEntry car = new CarEntryBuilder().setId(new CarId("car"+id)).build();
148 tx.put(LogicalDatastoreType.CONFIGURATION,
149 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.getKey()).build(),
151 CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
152 Futures.addCallback(future, new FutureCallback<Void>() {
155 public void onSuccess(final Void result) {
156 // Transaction succeeded
157 succcessCounter.getAndIncrement();
161 public void onFailure(final Throwable t) {
162 // Transaction failed
163 failureCounter.getAndIncrement();
164 LOG.error("Put Cars failed", t);
168 TimeUnit.NANOSECONDS.sleep(sleep);
169 } catch (InterruptedException e) {
173 if((count.get() % 1000) == 0) {
174 log.info("Cars created {}, time: {}",count.get(),sw.elapsed(TimeUnit.SECONDS));
177 // Check if a count is specified in input and we have created that many cars.
178 if ((inputCount != 0) && (count.get() >= inputCount)) {
183 log.info("Stress test thread stopping after creating {} cars.", count.get());
188 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
192 public Future<RpcResult<StopStressTestOutput>> stopStressTest() {
194 StopStressTestOutputBuilder stopStressTestOutput;
195 stopStressTestOutput = new StopStressTestOutputBuilder()
196 .setSuccessCount(succcessCounter.longValue())
197 .setFailureCount(failureCounter.longValue());
199 StopStressTestOutput result = stopStressTestOutput.build();
200 log.info("Executed Stop Stress test; No. of cars created {}; " +
201 "No. of cars failed {}; ", succcessCounter, failureCounter);
203 succcessCounter.set(0);
204 failureCounter.set(0);
205 return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
210 public Future<RpcResult<Void>> registerOwnership(RegisterOwnershipInput input) {
211 if(registeredListener.compareAndSet(false, true)) {
212 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
215 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
217 ownershipService.registerCandidate(entity);
218 } catch (CandidateAlreadyRegisteredException e) {
219 return RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
220 "Could not register for car " + input.getCarId(), e).buildFuture();
223 return RpcResultBuilder.<Void>success().buildFuture();
227 public Future<RpcResult<Void>> unregisterOwnership(UnregisterOwnershipInput input) {
228 return RpcResultBuilder.<Void>success().buildFuture();
231 private static class CarEntityOwnershipListener implements EntityOwnershipListener {
233 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
234 LOG.info("ownershipChanged: {}", ownershipChange);
239 public Future<RpcResult<java.lang.Void>> registerLoggingDcl() {
240 LOG.info("Registering a new CarDataChangeListener");
241 final ListenerRegistration carsDclRegistration = dataProvider.registerDataChangeListener(
242 LogicalDatastoreType.CONFIGURATION, CARS_IID, new CarDataChangeListener(),
243 AsyncDataBroker.DataChangeScope.SUBTREE);
245 if (carsDclRegistration != null) {
246 carsDclRegistrations.add(carsDclRegistration);
247 return RpcResultBuilder.<Void>success().buildFuture();
249 return RpcResultBuilder.<Void>failed().buildFuture();
253 public Future<RpcResult<java.lang.Void>> registerLoggingDtcl() {
254 LOG.info("Registering a new CarDataTreeChangeListener");
255 final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
256 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
258 if (carsDtclRegistration != null) {
259 carsDtclRegistrations.add(carsDtclRegistration);
260 return RpcResultBuilder.<Void>success().buildFuture();
262 return RpcResultBuilder.<Void>failed().buildFuture();
266 public Future<RpcResult<java.lang.Void>> unregisterLoggingDcls() {
267 LOG.info("Unregistering the CarDataChangeListener(s)");
268 synchronized (carsDclRegistrations) {
269 int numListeners = 0;
270 for (ListenerRegistration<DataChangeListener> carsDclRegistration : carsDclRegistrations) {
271 carsDclRegistration.close();
274 carsDclRegistrations.clear();
275 LOG.info("Unregistered {} CarDataChangeListener(s)", numListeners);
277 return RpcResultBuilder.<Void>success().buildFuture();
281 public Future<RpcResult<java.lang.Void>> unregisterLoggingDtcls() {
282 LOG.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.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
292 return RpcResultBuilder.<Void>success().buildFuture();