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.AtomicLong;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
19 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * @author Thomas Pantelis
43 public class CarProvider implements CarService {
44 private static final Logger log = LoggerFactory.getLogger(PurchaseCarProvider.class);
46 private final DataBroker dataProvider;
47 private final EntityOwnershipService ownershipService;
48 private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
50 private static final String ENTITY_TYPE = "cars";
53 private final CarEntityOwnershipCandidate ownershipCandidate = new CarEntityOwnershipCandidate();
56 private volatile Thread testThread;
57 private volatile boolean stopThread;
59 public CarProvider(DataBroker dataProvider, EntityOwnershipService ownershipService) {
60 this.dataProvider = dataProvider;
61 this.ownershipService = ownershipService;
64 private void stopThread() {
65 if(testThread != null) {
67 testThread.interrupt();
70 } catch (InterruptedException e) {}
76 public Future<RpcResult<Void>> stressTest(StressTestInput input) {
77 final int inputRate, inputCount;
79 // If rate is not provided, or given as zero, then just return.
80 if ((input.getRate() == null) || (input.getRate() == 0)) {
81 log.info("Exiting stress test as no rate is given.");
82 return Futures.immediateFuture(RpcResultBuilder.<Void>failed()
83 .withError(ErrorType.PROTOCOL, "invalid rate")
86 inputRate = input.getRate();
89 if (input.getCount() != null) {
90 inputCount = input.getCount();
95 log.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
99 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
100 InstanceIdentifier<Cars> carsId = InstanceIdentifier.<Cars>builder(Cars.class).build();
101 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
103 tx.submit().checkedGet(5, TimeUnit.SECONDS);
104 } catch (TransactionCommitFailedException | TimeoutException e) {
105 log.error("Put Cars failed",e);
106 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
110 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
111 final Stopwatch sw = Stopwatch.createUnstarted();
112 testThread = new Thread() {
116 AtomicLong count = new AtomicLong();
118 long id = count.incrementAndGet();
119 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
120 CarEntry car = new CarEntryBuilder().setId(new CarId("car"+id)).build();
121 tx.put(LogicalDatastoreType.CONFIGURATION,
122 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.getKey()).build(),
126 TimeUnit.NANOSECONDS.sleep(sleep);
127 } catch (InterruptedException e) {
131 if((count.get() % 1000) == 0) {
132 log.info("Cars created {}, time: {}",count.get(),sw.elapsed(TimeUnit.SECONDS));
135 // Check if a count is specified in input and we have created that many cars.
136 if ((inputCount != 0) && (count.get() >= inputCount)) {
141 log.info("Stress test thread stopping after creating {} cars.", count.get());
146 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
150 public Future<RpcResult<Void>> stopStressTest() {
152 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
157 public Future<RpcResult<Void>> registerOwnership(RegisterOwnershipInput input) {
158 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
160 ownershipService.registerCandidate(entity, ownershipCandidate);
161 } catch (CandidateAlreadyRegisteredException e) {
162 return RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
163 "Could not register for car " + input.getCarId(), e).buildFuture();
166 return RpcResultBuilder.<Void>success().buildFuture();
170 public Future<RpcResult<Void>> unregisterOwnership(UnregisterOwnershipInput input) {
171 return RpcResultBuilder.<Void>success().buildFuture();
174 private static class CarEntityOwnershipCandidate implements EntityOwnershipCandidate {
176 public void ownershipChanged(Entity entity, boolean wasOwner, boolean isOwner) {
177 LOG.info("ownershipChanged: entity: {}, wasOwner: {}, isOwner: ()", entity, wasOwner, isOwner);