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.util.concurrent.Futures;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicLong;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * @author Thomas Pantelis
45 public class CarProvider implements CarService {
46 private static final Logger log = LoggerFactory.getLogger(PurchaseCarProvider.class);
48 private final DataBroker dataProvider;
49 private final EntityOwnershipService ownershipService;
50 private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
52 private static final String ENTITY_TYPE = "cars";
54 private final CarEntityOwnershipListener ownershipListener = new CarEntityOwnershipListener();
55 private final AtomicBoolean registeredListener = new AtomicBoolean();
57 private volatile Thread testThread;
58 private volatile boolean stopThread;
60 public CarProvider(DataBroker dataProvider, EntityOwnershipService ownershipService) {
61 this.dataProvider = dataProvider;
62 this.ownershipService = ownershipService;
65 private void stopThread() {
66 if(testThread != null) {
68 testThread.interrupt();
71 } catch (InterruptedException e) {}
77 public Future<RpcResult<Void>> stressTest(StressTestInput input) {
79 final long inputCount;
81 // If rate is not provided, or given as zero, then just return.
82 if ((input.getRate() == null) || (input.getRate() == 0)) {
83 log.info("Exiting stress test as no rate is given.");
84 return Futures.immediateFuture(RpcResultBuilder.<Void>failed()
85 .withError(ErrorType.PROTOCOL, "invalid rate")
88 inputRate = input.getRate();
91 if (input.getCount() != null) {
92 inputCount = input.getCount();
97 log.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
101 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
102 InstanceIdentifier<Cars> carsId = InstanceIdentifier.<Cars>builder(Cars.class).build();
103 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
105 tx.submit().checkedGet(5, TimeUnit.SECONDS);
106 } catch (TransactionCommitFailedException | TimeoutException e) {
107 log.error("Put Cars failed",e);
108 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
112 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
113 final Stopwatch sw = Stopwatch.createUnstarted();
114 testThread = new Thread() {
118 AtomicLong count = new AtomicLong();
120 long id = count.incrementAndGet();
121 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
122 CarEntry car = new CarEntryBuilder().setId(new CarId("car"+id)).build();
123 tx.put(LogicalDatastoreType.CONFIGURATION,
124 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.getKey()).build(),
128 TimeUnit.NANOSECONDS.sleep(sleep);
129 } catch (InterruptedException e) {
133 if((count.get() % 1000) == 0) {
134 log.info("Cars created {}, time: {}",count.get(),sw.elapsed(TimeUnit.SECONDS));
137 // Check if a count is specified in input and we have created that many cars.
138 if ((inputCount != 0) && (count.get() >= inputCount)) {
143 log.info("Stress test thread stopping after creating {} cars.", count.get());
148 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
152 public Future<RpcResult<Void>> stopStressTest() {
154 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
159 public Future<RpcResult<Void>> registerOwnership(RegisterOwnershipInput input) {
160 if(registeredListener.compareAndSet(false, true)) {
161 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
164 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
166 ownershipService.registerCandidate(entity);
167 } catch (CandidateAlreadyRegisteredException e) {
168 return RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
169 "Could not register for car " + input.getCarId(), e).buildFuture();
172 return RpcResultBuilder.<Void>success().buildFuture();
176 public Future<RpcResult<Void>> unregisterOwnership(UnregisterOwnershipInput input) {
177 return RpcResultBuilder.<Void>success().buildFuture();
180 private static class CarEntityOwnershipListener implements EntityOwnershipListener {
182 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
183 LOG.info("ownershipChanged: {}", ownershipChange);