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.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21 import java.util.concurrent.atomic.AtomicBoolean;
22 import java.util.concurrent.atomic.AtomicLong;
23 import java.util.concurrent.atomic.AtomicReference;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
33 import org.opendaylight.mdsal.eos.binding.api.Entity;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
37 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutputBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
68 import org.opendaylight.yangtools.concepts.ListenerRegistration;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.common.ErrorType;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
72 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
73 import org.opendaylight.yangtools.yang.common.Uint32;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
79 * Implementation of CarService.
81 * @author Thomas Pantelis
83 @SuppressFBWarnings("SLF4J_ILLEGAL_PASSED_CLASS")
84 public class CarProvider implements CarService {
85 private static final Logger LOG_PURCHASE_CAR = LoggerFactory.getLogger(PurchaseCarProvider.class);
87 private static final Logger LOG_CAR_PROVIDER = LoggerFactory.getLogger(CarProvider.class);
89 private static final String ENTITY_TYPE = "cars";
90 private static final InstanceIdentifier<Cars> CARS_IID = InstanceIdentifier.builder(Cars.class).build();
91 private static final DataTreeIdentifier<Cars> CARS_DTID = DataTreeIdentifier.create(
92 LogicalDatastoreType.CONFIGURATION, CARS_IID);
94 private final DataBroker dataProvider;
95 private final DOMDataBroker domDataBroker;
96 private final EntityOwnershipService ownershipService;
97 private final AtomicLong succcessCounter = new AtomicLong();
98 private final AtomicLong failureCounter = new AtomicLong();
100 private final CarEntityOwnershipListener ownershipListener = new CarEntityOwnershipListener();
101 private final AtomicBoolean registeredListener = new AtomicBoolean();
103 private final Set<ListenerRegistration<?>> carsDclRegistrations = ConcurrentHashMap.newKeySet();
104 private final Set<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
105 ConcurrentHashMap.newKeySet();
107 private volatile Thread testThread;
108 private volatile boolean stopThread;
109 private final AtomicReference<DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort>> commitCohortReg =
110 new AtomicReference<>();
112 public CarProvider(final DataBroker dataProvider, final EntityOwnershipService ownershipService,
113 final DOMDataBroker domDataBroker) {
114 this.dataProvider = dataProvider;
115 this.ownershipService = ownershipService;
116 this.domDataBroker = domDataBroker;
119 public void close() {
121 closeCommitCohortRegistration();
124 private void stopThread() {
125 if (testThread != null) {
127 testThread.interrupt();
130 } catch (InterruptedException e) {
138 public ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
140 final long inputCount;
142 // If rate is not provided, or given as zero, then just return.
143 if (input.getRate() == null || input.getRate().toJava() == 0) {
144 LOG_PURCHASE_CAR.info("Exiting stress test as no rate is given.");
145 return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed()
146 .withError(ErrorType.PROTOCOL, "invalid rate")
150 inputRate = input.getRate().toJava();
151 if (input.getCount() != null) {
152 inputCount = input.getCount().toJava();
157 LOG_PURCHASE_CAR.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
161 succcessCounter.set(0);
162 failureCounter.set(0);
164 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
165 InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
166 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
168 tx.commit().get(5, TimeUnit.SECONDS);
169 } catch (TimeoutException | InterruptedException | ExecutionException e) {
170 LOG_PURCHASE_CAR.error("Put Cars failed",e);
171 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
175 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
176 final Stopwatch sw = Stopwatch.createUnstarted();
177 testThread = new Thread(() -> {
179 AtomicLong count = new AtomicLong();
180 while (!stopThread) {
181 long id = count.incrementAndGet();
182 WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
183 CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
184 tx1.put(LogicalDatastoreType.CONFIGURATION,
185 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
186 tx1.commit().addCallback(new FutureCallback<CommitInfo>() {
189 public void onSuccess(final CommitInfo result) {
190 // Transaction succeeded
191 succcessCounter.getAndIncrement();
195 public void onFailure(final Throwable ex) {
196 // Transaction failed
197 failureCounter.getAndIncrement();
198 LOG_CAR_PROVIDER.error("Put Cars failed", ex);
200 }, MoreExecutors.directExecutor());
202 TimeUnit.NANOSECONDS.sleep(sleep);
203 } catch (InterruptedException e) {
207 if (count.get() % 1000 == 0) {
208 LOG_PURCHASE_CAR.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
211 // Check if a count is specified in input and we have created that many cars.
212 if (inputCount != 0 && count.get() >= inputCount) {
217 LOG_PURCHASE_CAR.info("Stress test thread stopping after creating {} cars.", count.get());
221 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
225 public ListenableFuture<RpcResult<StopStressTestOutput>> stopStressTest(final StopStressTestInput input) {
227 StopStressTestOutputBuilder stopStressTestOutput;
228 stopStressTestOutput = new StopStressTestOutputBuilder()
229 .setSuccessCount(Uint32.valueOf(succcessCounter.longValue()))
230 .setFailureCount(Uint32.valueOf(failureCounter.longValue()));
232 final StopStressTestOutput result = stopStressTestOutput.build();
233 LOG_PURCHASE_CAR.info("Executed Stop Stress test; No. of cars created {}; "
234 + "No. of cars failed {}; ", succcessCounter, failureCounter);
236 succcessCounter.set(0);
237 failureCounter.set(0);
238 return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
243 public ListenableFuture<RpcResult<RegisterOwnershipOutput>> registerOwnership(final RegisterOwnershipInput input) {
244 if (registeredListener.compareAndSet(false, true)) {
245 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
248 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
250 ownershipService.registerCandidate(entity);
251 } catch (CandidateAlreadyRegisteredException e) {
252 return RpcResultBuilder.<RegisterOwnershipOutput>failed().withError(ErrorType.APPLICATION,
253 "Could not register for car " + input.getCarId(), e).buildFuture();
256 return RpcResultBuilder.success(new RegisterOwnershipOutputBuilder().build()).buildFuture();
260 public ListenableFuture<RpcResult<UnregisterOwnershipOutput>> unregisterOwnership(
261 final UnregisterOwnershipInput input) {
262 return RpcResultBuilder.success(new UnregisterOwnershipOutputBuilder().build()).buildFuture();
265 private static class CarEntityOwnershipListener implements EntityOwnershipListener {
267 public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
268 LOG_CAR_PROVIDER.info("ownershipChanged: {}", ownershipChange);
273 public ListenableFuture<RpcResult<RegisterLoggingDtclOutput>> registerLoggingDtcl(
274 final RegisterLoggingDtclInput input) {
275 LOG_CAR_PROVIDER.info("Registering a new CarDataTreeChangeListener");
276 final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
277 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
279 carsDtclRegistrations.add(carsDtclRegistration);
280 return RpcResultBuilder.success(new RegisterLoggingDtclOutputBuilder().build()).buildFuture();
284 public ListenableFuture<RpcResult<UnregisterLoggingDtclsOutput>> unregisterLoggingDtcls(
285 final UnregisterLoggingDtclsInput input) {
286 LOG_CAR_PROVIDER.info("Unregistering the CarDataTreeChangeListener(s)");
287 synchronized (carsDtclRegistrations) {
288 int numListeners = 0;
289 for (ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration : carsDtclRegistrations) {
290 carsDtclRegistration.close();
293 carsDtclRegistrations.clear();
294 LOG_CAR_PROVIDER.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
296 return RpcResultBuilder.success(new UnregisterLoggingDtclsOutputBuilder().build()).buildFuture();
300 @SuppressWarnings("checkstyle:IllegalCatch")
301 public ListenableFuture<RpcResult<UnregisterCommitCohortOutput>> unregisterCommitCohort(
302 final UnregisterCommitCohortInput input) {
303 closeCommitCohortRegistration();
305 return RpcResultBuilder.success(new UnregisterCommitCohortOutputBuilder().build()).buildFuture();
308 private void closeCommitCohortRegistration() {
309 final DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort> reg = commitCohortReg.getAndSet(null);
312 LOG_CAR_PROVIDER.info("Unregistered commit cohort");
317 public synchronized ListenableFuture<RpcResult<RegisterCommitCohortOutput>> registerCommitCohort(
318 final RegisterCommitCohortInput input) {
319 if (commitCohortReg.get() != null) {
320 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
323 final DOMDataTreeCommitCohortRegistry commitCohortRegistry = domDataBroker.getExtensions().getInstance(
324 DOMDataTreeCommitCohortRegistry.class);
326 if (commitCohortRegistry == null) {
328 return RpcResultBuilder.<RegisterCommitCohortOutput>failed().withError(ErrorType.APPLICATION,
329 "DOMDataTreeCommitCohortRegistry not found").buildFuture();
332 // Note: it may look strange that we specify the CarEntry.QNAME twice in the path below. This must be done in
333 // order to register the commit cohort for CarEntry instances. In the underlying data tree, a yang list is
334 // represented as a MapNode with MapEntryNodes representing the child list entries. Therefore, in order to
335 // address a list entry, you must specify the path argument for the MapNode and the path argument for the
336 // MapEntryNode. In the path below, the first CarEntry.QNAME argument addresses the MapNode and, since we want
337 // to address all list entries, the second path argument is wild-carded by specifying just the CarEntry.QNAME.
338 final YangInstanceIdentifier carEntryPath = YangInstanceIdentifier.builder(
339 YangInstanceIdentifier.of(Cars.QNAME)).node(CarEntry.QNAME).node(CarEntry.QNAME).build();
340 commitCohortReg.set(commitCohortRegistry.registerCommitCohort(new DOMDataTreeIdentifier(
341 LogicalDatastoreType.CONFIGURATION, carEntryPath), new CarEntryDataTreeCommitCohort()));
343 LOG_CAR_PROVIDER.info("Registered commit cohort");
345 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();