Bug-4234 - Add count field to cars stress-test RPC.
[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.util.concurrent.Futures;
12
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import java.util.concurrent.atomic.AtomicLong;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author Thomas Pantelis
38  */
39 public class CarProvider implements CarService {
40     private static final Logger log = LoggerFactory.getLogger(PurchaseCarProvider.class);
41
42     private final DataBroker dataProvider;
43
44     private volatile Thread testThread;
45     private volatile boolean stopThread;
46
47     public CarProvider(DataBroker dataProvider) {
48         this.dataProvider = dataProvider;
49     }
50
51     private void stopThread() {
52         if(testThread != null) {
53             stopThread = true;
54             testThread.interrupt();
55             try {
56                 testThread.join();
57             } catch (InterruptedException e) {}
58             testThread = null;
59         }
60     }
61
62     @Override
63     public Future<RpcResult<Void>> stressTest(StressTestInput input) {
64         final int inputRate, inputCount;
65
66         // If rate is not provided, or given as zero, then just return.
67         if ((input.getRate() == null) || (input.getRate() == 0)) {
68             log.info("Exiting stress test as no rate is given.");
69             return Futures.immediateFuture(RpcResultBuilder.<Void>failed()
70                                            .withError(ErrorType.PROTOCOL, "invalid rate")
71                                            .build());
72         } else {
73             inputRate = input.getRate();
74         }
75
76         if (input.getCount() != null) {
77             inputCount = input.getCount();
78         } else {
79             inputCount = 0;
80         }
81
82         log.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
83
84         stopThread();
85
86         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
87         InstanceIdentifier<Cars> carsId = InstanceIdentifier.<Cars>builder(Cars.class).build();
88         tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
89         try {
90             tx.submit().checkedGet(5, TimeUnit.SECONDS);
91         } catch (TransactionCommitFailedException | TimeoutException e) {
92             log.error("Put Cars failed",e);
93             return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
94         }
95
96         stopThread = false;
97         final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
98         final Stopwatch sw = Stopwatch.createUnstarted();
99         testThread = new Thread() {
100             @Override
101             public void run() {
102                 sw.start();
103                 AtomicLong count = new AtomicLong();
104                 while(!stopThread) {
105                     long id = count.incrementAndGet();
106                     WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
107                     CarEntry car = new CarEntryBuilder().setId(new CarId("car"+id)).build();
108                     tx.put(LogicalDatastoreType.CONFIGURATION,
109                             InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.getKey()).build(),
110                             car);
111                     tx.submit();
112                     try {
113                         TimeUnit.NANOSECONDS.sleep(sleep);
114                     } catch (InterruptedException e) {
115                         break;
116                     }
117
118                     if((count.get() % 1000) == 0) {
119                         log.info("Cars created {}, time: {}",count.get(),sw.elapsed(TimeUnit.SECONDS));
120                     }
121
122                     // Check if a count is specified in input and we have created that many cars.
123                     if ((inputCount != 0) && (count.get() >= inputCount)) {
124                         stopThread = true;
125                     }
126                 }
127
128                 log.info("Stress test thread stopping after creating {} cars.", count.get());
129             }
130         };
131         testThread.start();
132
133         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
134     }
135
136     @Override
137     public Future<RpcResult<Void>> stopStressTest() {
138         stopThread();
139         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
140     }
141
142 }