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.eos.binding.api.Entity;
33 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
36 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortInput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsInput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutputBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
67 import org.opendaylight.yangtools.concepts.ListenerRegistration;
68 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
69 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
70 import org.opendaylight.yangtools.yang.common.RpcResult;
71 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
72 import org.opendaylight.yangtools.yang.common.Uint32;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
78 * Implementation of CarService.
80 * @author Thomas Pantelis
82 @SuppressFBWarnings("SLF4J_ILLEGAL_PASSED_CLASS")
83 public class CarProvider implements CarService {
84 private static final Logger LOG_PURCHASE_CAR = LoggerFactory.getLogger(PurchaseCarProvider.class);
86 private static final Logger LOG_CAR_PROVIDER = LoggerFactory.getLogger(CarProvider.class);
88 private static final String ENTITY_TYPE = "cars";
89 private static final InstanceIdentifier<Cars> CARS_IID = InstanceIdentifier.builder(Cars.class).build();
90 private static final DataTreeIdentifier<Cars> CARS_DTID = DataTreeIdentifier.create(
91 LogicalDatastoreType.CONFIGURATION, CARS_IID);
93 private final DataBroker dataProvider;
94 private final DOMDataBroker domDataBroker;
95 private final EntityOwnershipService ownershipService;
96 private final AtomicLong succcessCounter = new AtomicLong();
97 private final AtomicLong failureCounter = new AtomicLong();
99 private final CarEntityOwnershipListener ownershipListener = new CarEntityOwnershipListener();
100 private final AtomicBoolean registeredListener = new AtomicBoolean();
102 private final Set<ListenerRegistration<?>> carsDclRegistrations = ConcurrentHashMap.newKeySet();
103 private final Set<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
104 ConcurrentHashMap.newKeySet();
106 private volatile Thread testThread;
107 private volatile boolean stopThread;
108 private final AtomicReference<DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort>> commitCohortReg =
109 new AtomicReference<>();
111 public CarProvider(final DataBroker dataProvider, final EntityOwnershipService ownershipService,
112 final DOMDataBroker domDataBroker) {
113 this.dataProvider = dataProvider;
114 this.ownershipService = ownershipService;
115 this.domDataBroker = domDataBroker;
118 public void close() {
120 closeCommitCohortRegistration();
123 private void stopThread() {
124 if (testThread != null) {
126 testThread.interrupt();
129 } catch (InterruptedException e) {
137 public ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
139 final long inputCount;
141 // If rate is not provided, or given as zero, then just return.
142 if (input.getRate() == null || input.getRate().toJava() == 0) {
143 LOG_PURCHASE_CAR.info("Exiting stress test as no rate is given.");
144 return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed()
145 .withError(ErrorType.PROTOCOL, "invalid rate")
149 inputRate = input.getRate().toJava();
150 if (input.getCount() != null) {
151 inputCount = input.getCount().toJava();
156 LOG_PURCHASE_CAR.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
160 succcessCounter.set(0);
161 failureCounter.set(0);
163 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
164 InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
165 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
167 tx.commit().get(5, TimeUnit.SECONDS);
168 } catch (TimeoutException | InterruptedException | ExecutionException e) {
169 LOG_PURCHASE_CAR.error("Put Cars failed",e);
170 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
174 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
175 final Stopwatch sw = Stopwatch.createUnstarted();
176 testThread = new Thread(() -> {
178 AtomicLong count = new AtomicLong();
179 while (!stopThread) {
180 long id = count.incrementAndGet();
181 WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
182 CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
183 tx1.put(LogicalDatastoreType.CONFIGURATION,
184 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
185 tx1.commit().addCallback(new FutureCallback<CommitInfo>() {
188 public void onSuccess(final CommitInfo result) {
189 // Transaction succeeded
190 succcessCounter.getAndIncrement();
194 public void onFailure(final Throwable ex) {
195 // Transaction failed
196 failureCounter.getAndIncrement();
197 LOG_CAR_PROVIDER.error("Put Cars failed", ex);
199 }, MoreExecutors.directExecutor());
201 TimeUnit.NANOSECONDS.sleep(sleep);
202 } catch (InterruptedException e) {
206 if (count.get() % 1000 == 0) {
207 LOG_PURCHASE_CAR.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
210 // Check if a count is specified in input and we have created that many cars.
211 if (inputCount != 0 && count.get() >= inputCount) {
216 LOG_PURCHASE_CAR.info("Stress test thread stopping after creating {} cars.", count.get());
220 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
224 public ListenableFuture<RpcResult<StopStressTestOutput>> stopStressTest(final StopStressTestInput input) {
226 StopStressTestOutputBuilder stopStressTestOutput;
227 stopStressTestOutput = new StopStressTestOutputBuilder()
228 .setSuccessCount(Uint32.valueOf(succcessCounter.longValue()))
229 .setFailureCount(Uint32.valueOf(failureCounter.longValue()));
231 final StopStressTestOutput result = stopStressTestOutput.build();
232 LOG_PURCHASE_CAR.info("Executed Stop Stress test; No. of cars created {}; "
233 + "No. of cars failed {}; ", succcessCounter, failureCounter);
235 succcessCounter.set(0);
236 failureCounter.set(0);
237 return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
242 public ListenableFuture<RpcResult<RegisterOwnershipOutput>> registerOwnership(final RegisterOwnershipInput input) {
243 if (registeredListener.compareAndSet(false, true)) {
244 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
247 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
249 ownershipService.registerCandidate(entity);
250 } catch (CandidateAlreadyRegisteredException e) {
251 return RpcResultBuilder.<RegisterOwnershipOutput>failed().withError(ErrorType.APPLICATION,
252 "Could not register for car " + input.getCarId(), e).buildFuture();
255 return RpcResultBuilder.success(new RegisterOwnershipOutputBuilder().build()).buildFuture();
259 public ListenableFuture<RpcResult<UnregisterOwnershipOutput>> unregisterOwnership(
260 final UnregisterOwnershipInput input) {
261 return RpcResultBuilder.success(new UnregisterOwnershipOutputBuilder().build()).buildFuture();
264 private static class CarEntityOwnershipListener implements EntityOwnershipListener {
266 public void ownershipChanged(final EntityOwnershipChange ownershipChange) {
267 LOG_CAR_PROVIDER.info("ownershipChanged: {}", ownershipChange);
272 public ListenableFuture<RpcResult<RegisterLoggingDtclOutput>> registerLoggingDtcl(
273 final RegisterLoggingDtclInput input) {
274 LOG_CAR_PROVIDER.info("Registering a new CarDataTreeChangeListener");
275 final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
276 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
278 carsDtclRegistrations.add(carsDtclRegistration);
279 return RpcResultBuilder.success(new RegisterLoggingDtclOutputBuilder().build()).buildFuture();
283 public ListenableFuture<RpcResult<UnregisterLoggingDtclsOutput>> unregisterLoggingDtcls(
284 final UnregisterLoggingDtclsInput input) {
285 LOG_CAR_PROVIDER.info("Unregistering the CarDataTreeChangeListener(s)");
286 synchronized (carsDtclRegistrations) {
287 int numListeners = 0;
288 for (ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration : carsDtclRegistrations) {
289 carsDtclRegistration.close();
292 carsDtclRegistrations.clear();
293 LOG_CAR_PROVIDER.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
295 return RpcResultBuilder.success(new UnregisterLoggingDtclsOutputBuilder().build()).buildFuture();
299 @SuppressWarnings("checkstyle:IllegalCatch")
300 public ListenableFuture<RpcResult<UnregisterCommitCohortOutput>> unregisterCommitCohort(
301 final UnregisterCommitCohortInput input) {
302 closeCommitCohortRegistration();
304 return RpcResultBuilder.success(new UnregisterCommitCohortOutputBuilder().build()).buildFuture();
307 private void closeCommitCohortRegistration() {
308 final DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort> reg = commitCohortReg.getAndSet(null);
311 LOG_CAR_PROVIDER.info("Unregistered commit cohort");
316 public synchronized ListenableFuture<RpcResult<RegisterCommitCohortOutput>> registerCommitCohort(
317 final RegisterCommitCohortInput input) {
318 if (commitCohortReg.get() != null) {
319 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
322 final DOMDataTreeCommitCohortRegistry commitCohortRegistry = domDataBroker.getExtensions().getInstance(
323 DOMDataTreeCommitCohortRegistry.class);
325 if (commitCohortRegistry == null) {
327 return RpcResultBuilder.<RegisterCommitCohortOutput>failed().withError(ErrorType.APPLICATION,
328 "DOMDataTreeCommitCohortRegistry not found").buildFuture();
331 // Note: it may look strange that we specify the CarEntry.QNAME twice in the path below. This must be done in
332 // order to register the commit cohort for CarEntry instances. In the underlying data tree, a yang list is
333 // represented as a MapNode with MapEntryNodes representing the child list entries. Therefore, in order to
334 // address a list entry, you must specify the path argument for the MapNode and the path argument for the
335 // MapEntryNode. In the path below, the first CarEntry.QNAME argument addresses the MapNode and, since we want
336 // to address all list entries, the second path argument is wild-carded by specifying just the CarEntry.QNAME.
337 final YangInstanceIdentifier carEntryPath = YangInstanceIdentifier.builder(
338 YangInstanceIdentifier.of(Cars.QNAME)).node(CarEntry.QNAME).node(CarEntry.QNAME).build();
339 commitCohortReg.set(commitCohortRegistry.registerCommitCohort(
340 new org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier(
341 org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
342 carEntryPath), new CarEntryDataTreeCommitCohort()));
344 LOG_CAR_PROVIDER.info("Registered commit cohort");
346 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();