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 java.util.HashSet;
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 javax.annotation.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
28 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.mdsal.binding.api.RpcProviderService;
30 import org.opendaylight.mdsal.binding.api.WriteTransaction;
31 import org.opendaylight.mdsal.common.api.CommitInfo;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
34 import org.opendaylight.mdsal.dom.api.DOMDataBroker.CommitCohortExtension;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
36 import org.opendaylight.mdsal.eos.binding.api.Entity;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
39 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohort;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtcl;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnership;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTest;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestInput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTest;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohort;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutputBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtcls;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsInput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutputBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnership;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutputBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
77 import org.opendaylight.yangtools.concepts.ObjectRegistration;
78 import org.opendaylight.yangtools.concepts.Registration;
79 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
80 import org.opendaylight.yangtools.yang.common.ErrorType;
81 import org.opendaylight.yangtools.yang.common.RpcResult;
82 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
83 import org.opendaylight.yangtools.yang.common.Uint32;
84 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
85 import org.osgi.service.component.annotations.Activate;
86 import org.osgi.service.component.annotations.Component;
87 import org.osgi.service.component.annotations.Deactivate;
88 import org.osgi.service.component.annotations.Reference;
89 import org.slf4j.Logger;
90 import org.slf4j.LoggerFactory;
93 * Implementation of CarService.
95 * @author Thomas Pantelis
98 @Component(service = { })
99 public final class CarProvider {
100 private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
102 private static final String ENTITY_TYPE = "cars";
103 private static final InstanceIdentifier<Cars> CARS_IID = InstanceIdentifier.builder(Cars.class).build();
104 private static final DataTreeIdentifier<Cars> CARS_DTID = DataTreeIdentifier.of(
105 LogicalDatastoreType.CONFIGURATION, CARS_IID);
107 private final DataBroker dataProvider;
108 private final DOMDataBroker domDataBroker;
109 private final EntityOwnershipService ownershipService;
110 private final AtomicLong succcessCounter = new AtomicLong();
111 private final AtomicLong failureCounter = new AtomicLong();
113 private final EntityOwnershipListener ownershipListener = (entity, change, inJeopardy) ->
114 LOG.info("ownershipChanged: entity={} change={} inJeopardy={}", entity, change, inJeopardy);
116 private final AtomicBoolean registeredListener = new AtomicBoolean();
117 private final AtomicReference<Registration> commitCohortReg = new AtomicReference<>();
118 private final Set<ObjectRegistration<?>> carsDclRegistrations = ConcurrentHashMap.newKeySet();
119 private final Set<Registration> regs = new HashSet<>();
120 private final Set<Registration> carsDtclRegistrations = ConcurrentHashMap.newKeySet();
122 private volatile Thread testThread;
123 private volatile boolean stopThread;
127 public CarProvider(@Reference final DataBroker dataProvider,
128 @Reference final EntityOwnershipService ownershipService, @Reference final DOMDataBroker domDataBroker,
129 @Reference final RpcProviderService rpcProviderService) {
130 this.dataProvider = dataProvider;
131 this.ownershipService = ownershipService;
132 this.domDataBroker = domDataBroker;
133 regs.add(rpcProviderService.registerRpcImplementations(
134 (StressTest) this::stressTest,
135 (StopStressTest) this::stopStressTest,
136 (RegisterOwnership) this::registerOwnership,
137 (UnregisterOwnership) this::unregisterOwnership,
138 (RegisterLoggingDtcl) this::registerLoggingDtcl,
139 (UnregisterLoggingDtcls) this::unregisterLoggingDtcls,
140 (RegisterCommitCohort) this::registerCommitCohort,
141 (UnregisterCommitCohort) this::unregisterCommitCohort));
146 public void close() {
148 closeCommitCohortRegistration();
149 regs.forEach(Registration::close);
153 private void stopThread() {
154 if (testThread != null) {
156 testThread.interrupt();
159 } catch (InterruptedException e) {
166 private ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
168 final long inputCount;
170 // If rate is not provided, or given as zero, then just return.
171 if (input.getRate() == null || input.getRate().toJava() == 0) {
172 LOG.info("Exiting stress test as no rate is given.");
173 return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed()
174 .withError(ErrorType.PROTOCOL, "invalid rate")
178 inputRate = input.getRate().toJava();
179 if (input.getCount() != null) {
180 inputCount = input.getCount().toJava();
185 LOG.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
189 succcessCounter.set(0);
190 failureCounter.set(0);
192 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
193 InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
194 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
196 tx.commit().get(5, TimeUnit.SECONDS);
197 } catch (TimeoutException | InterruptedException | ExecutionException e) {
198 LOG.error("Put Cars failed",e);
199 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
203 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
204 final Stopwatch sw = Stopwatch.createUnstarted();
205 testThread = new Thread(() -> {
207 AtomicLong count = new AtomicLong();
208 while (!stopThread) {
209 long id = count.incrementAndGet();
210 WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
211 CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
212 tx1.put(LogicalDatastoreType.CONFIGURATION,
213 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
214 tx1.commit().addCallback(new FutureCallback<CommitInfo>() {
217 public void onSuccess(final CommitInfo result) {
218 // Transaction succeeded
219 succcessCounter.getAndIncrement();
223 public void onFailure(final Throwable ex) {
224 // Transaction failed
225 failureCounter.getAndIncrement();
226 LOG.error("Put Cars failed", ex);
228 }, MoreExecutors.directExecutor());
230 TimeUnit.NANOSECONDS.sleep(sleep);
231 } catch (InterruptedException e) {
235 if (count.get() % 1000 == 0) {
236 LOG.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
239 // Check if a count is specified in input and we have created that many cars.
240 if (inputCount != 0 && count.get() >= inputCount) {
245 LOG.info("Stress test thread stopping after creating {} cars.", count.get());
249 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
252 private ListenableFuture<RpcResult<StopStressTestOutput>> stopStressTest(final StopStressTestInput input) {
254 StopStressTestOutputBuilder stopStressTestOutput;
255 stopStressTestOutput = new StopStressTestOutputBuilder()
256 .setSuccessCount(Uint32.valueOf(succcessCounter.longValue()))
257 .setFailureCount(Uint32.valueOf(failureCounter.longValue()));
259 final StopStressTestOutput result = stopStressTestOutput.build();
260 LOG.info("Executed Stop Stress test; No. of cars created {}; No. of cars failed {}; ",
261 succcessCounter, failureCounter);
263 succcessCounter.set(0);
264 failureCounter.set(0);
265 return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
268 private ListenableFuture<RpcResult<RegisterOwnershipOutput>> registerOwnership(final RegisterOwnershipInput input) {
269 if (registeredListener.compareAndSet(false, true)) {
270 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
273 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
275 ownershipService.registerCandidate(entity);
276 } catch (CandidateAlreadyRegisteredException e) {
277 return RpcResultBuilder.<RegisterOwnershipOutput>failed().withError(ErrorType.APPLICATION,
278 "Could not register for car " + input.getCarId(), e).buildFuture();
281 return RpcResultBuilder.success(new RegisterOwnershipOutputBuilder().build()).buildFuture();
284 private ListenableFuture<RpcResult<UnregisterOwnershipOutput>> unregisterOwnership(
285 final UnregisterOwnershipInput input) {
286 return RpcResultBuilder.success(new UnregisterOwnershipOutputBuilder().build()).buildFuture();
289 private ListenableFuture<RpcResult<RegisterLoggingDtclOutput>> registerLoggingDtcl(
290 final RegisterLoggingDtclInput input) {
291 LOG.info("Registering a new CarDataTreeChangeListener");
292 final var reg = dataProvider.registerTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
293 carsDtclRegistrations.add(reg);
294 return RpcResultBuilder.success(new RegisterLoggingDtclOutputBuilder().build()).buildFuture();
297 private ListenableFuture<RpcResult<UnregisterLoggingDtclsOutput>> unregisterLoggingDtcls(
298 final UnregisterLoggingDtclsInput input) {
299 LOG.info("Unregistering the CarDataTreeChangeListener(s)");
300 synchronized (carsDtclRegistrations) {
301 int numListeners = 0;
302 for (var carsDtclRegistration : carsDtclRegistrations) {
303 carsDtclRegistration.close();
306 carsDtclRegistrations.clear();
307 LOG.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
309 return RpcResultBuilder.success(new UnregisterLoggingDtclsOutputBuilder().build()).buildFuture();
312 @SuppressWarnings("checkstyle:IllegalCatch")
313 private ListenableFuture<RpcResult<UnregisterCommitCohortOutput>> unregisterCommitCohort(
314 final UnregisterCommitCohortInput input) {
315 closeCommitCohortRegistration();
317 return RpcResultBuilder.success(new UnregisterCommitCohortOutputBuilder().build()).buildFuture();
320 private void closeCommitCohortRegistration() {
321 final var reg = commitCohortReg.getAndSet(null);
324 LOG.info("Unregistered commit cohort");
328 private synchronized ListenableFuture<RpcResult<RegisterCommitCohortOutput>> registerCommitCohort(
329 final RegisterCommitCohortInput input) {
330 if (commitCohortReg.get() != null) {
331 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
334 final var commitCohortRegistry = domDataBroker.extension(CommitCohortExtension.class);
335 if (commitCohortRegistry == null) {
337 return RpcResultBuilder.<RegisterCommitCohortOutput>failed().withError(ErrorType.APPLICATION,
338 "DOMDataTreeCommitCohortRegistry not found").buildFuture();
341 // Note: it may look strange that we specify the CarEntry.QNAME twice in the path below. This must be done in
342 // order to register the commit cohort for CarEntry instances. In the underlying data tree, a yang list is
343 // represented as a MapNode with MapEntryNodes representing the child list entries. Therefore, in order to
344 // address a list entry, you must specify the path argument for the MapNode and the path argument for the
345 // MapEntryNode. In the path below, the first CarEntry.QNAME argument addresses the MapNode and, since we want
346 // to address all list entries, the second path argument is wild-carded by specifying just the CarEntry.QNAME.
347 final YangInstanceIdentifier carEntryPath = YangInstanceIdentifier.builder(
348 YangInstanceIdentifier.of(Cars.QNAME)).node(CarEntry.QNAME).node(CarEntry.QNAME).build();
349 commitCohortReg.set(commitCohortRegistry.registerCommitCohort(DOMDataTreeIdentifier.of(
350 LogicalDatastoreType.CONFIGURATION, carEntryPath), new CarEntryDataTreeCommitCohort()));
352 LOG.info("Registered commit cohort");
354 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();