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.collect.ImmutableClassToInstanceMap;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.HashSet;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import java.util.concurrent.atomic.AtomicBoolean;
23 import java.util.concurrent.atomic.AtomicLong;
24 import java.util.concurrent.atomic.AtomicReference;
25 import javax.annotation.PreDestroy;
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.api.RpcProviderService;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.common.api.CommitInfo;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
38 import org.opendaylight.mdsal.eos.binding.api.Entity;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
40 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
41 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohort;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterCommitCohortOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtcl;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterLoggingDtclOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnership;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTest;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTest;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohort;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterCommitCohortOutputBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtcls;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterLoggingDtclsOutputBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnership;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipOutputBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
79 import org.opendaylight.yangtools.concepts.ListenerRegistration;
80 import org.opendaylight.yangtools.concepts.Registration;
81 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
82 import org.opendaylight.yangtools.yang.binding.Rpc;
83 import org.opendaylight.yangtools.yang.common.ErrorType;
84 import org.opendaylight.yangtools.yang.common.RpcResult;
85 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
86 import org.opendaylight.yangtools.yang.common.Uint32;
87 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
88 import org.osgi.service.component.annotations.Activate;
89 import org.osgi.service.component.annotations.Component;
90 import org.osgi.service.component.annotations.Deactivate;
91 import org.osgi.service.component.annotations.Reference;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
96 * Implementation of CarService.
98 * @author Thomas Pantelis
101 @Component(service = { })
102 public final class CarProvider {
103 private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
105 private static final String ENTITY_TYPE = "cars";
106 private static final InstanceIdentifier<Cars> CARS_IID = InstanceIdentifier.builder(Cars.class).build();
107 private static final DataTreeIdentifier<Cars> CARS_DTID = DataTreeIdentifier.create(
108 LogicalDatastoreType.CONFIGURATION, CARS_IID);
110 private final DataBroker dataProvider;
111 private final DOMDataBroker domDataBroker;
112 private final EntityOwnershipService ownershipService;
113 private final AtomicLong succcessCounter = new AtomicLong();
114 private final AtomicLong failureCounter = new AtomicLong();
116 private final EntityOwnershipListener ownershipListener =
117 ownershipChange -> LOG.info("ownershipChanged: {}", ownershipChange);
119 private final AtomicBoolean registeredListener = new AtomicBoolean();
121 private final Set<ListenerRegistration<?>> carsDclRegistrations = ConcurrentHashMap.newKeySet();
123 private final Set<Registration> regs = new HashSet<>();
124 private final Set<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
125 ConcurrentHashMap.newKeySet();
127 private volatile Thread testThread;
128 private volatile boolean stopThread;
129 private final AtomicReference<DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort>> commitCohortReg =
130 new AtomicReference<>();
134 public CarProvider(@Reference final DataBroker dataProvider,
135 @Reference final EntityOwnershipService ownershipService, @Reference final DOMDataBroker domDataBroker,
136 @Reference final RpcProviderService rpcProviderService) {
137 this.dataProvider = dataProvider;
138 this.ownershipService = ownershipService;
139 this.domDataBroker = domDataBroker;
140 regs.add(rpcProviderService.registerRpcImplementations(ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
141 .put(StressTest.class, this::stressTest)
142 .put(StopStressTest.class, this::stopStressTest)
143 .put(RegisterOwnership.class, this::registerOwnership)
144 .put(UnregisterOwnership.class, this::unregisterOwnership)
145 .put(RegisterLoggingDtcl.class, this::registerLoggingDtcl)
146 .put(UnregisterLoggingDtcls.class, this::unregisterLoggingDtcls)
147 .put(RegisterCommitCohort.class, this::registerCommitCohort)
148 .put(UnregisterCommitCohort.class, this::unregisterCommitCohort)
154 public void close() {
156 closeCommitCohortRegistration();
157 regs.forEach(Registration::close);
161 private void stopThread() {
162 if (testThread != null) {
164 testThread.interrupt();
167 } catch (InterruptedException e) {
174 private ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
176 final long inputCount;
178 // If rate is not provided, or given as zero, then just return.
179 if (input.getRate() == null || input.getRate().toJava() == 0) {
180 LOG.info("Exiting stress test as no rate is given.");
181 return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed()
182 .withError(ErrorType.PROTOCOL, "invalid rate")
186 inputRate = input.getRate().toJava();
187 if (input.getCount() != null) {
188 inputCount = input.getCount().toJava();
193 LOG.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
197 succcessCounter.set(0);
198 failureCounter.set(0);
200 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
201 InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
202 tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
204 tx.commit().get(5, TimeUnit.SECONDS);
205 } catch (TimeoutException | InterruptedException | ExecutionException e) {
206 LOG.error("Put Cars failed",e);
207 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
211 final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
212 final Stopwatch sw = Stopwatch.createUnstarted();
213 testThread = new Thread(() -> {
215 AtomicLong count = new AtomicLong();
216 while (!stopThread) {
217 long id = count.incrementAndGet();
218 WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
219 CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
220 tx1.put(LogicalDatastoreType.CONFIGURATION,
221 InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
222 tx1.commit().addCallback(new FutureCallback<CommitInfo>() {
225 public void onSuccess(final CommitInfo result) {
226 // Transaction succeeded
227 succcessCounter.getAndIncrement();
231 public void onFailure(final Throwable ex) {
232 // Transaction failed
233 failureCounter.getAndIncrement();
234 LOG.error("Put Cars failed", ex);
236 }, MoreExecutors.directExecutor());
238 TimeUnit.NANOSECONDS.sleep(sleep);
239 } catch (InterruptedException e) {
243 if (count.get() % 1000 == 0) {
244 LOG.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
247 // Check if a count is specified in input and we have created that many cars.
248 if (inputCount != 0 && count.get() >= inputCount) {
253 LOG.info("Stress test thread stopping after creating {} cars.", count.get());
257 return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
260 private ListenableFuture<RpcResult<StopStressTestOutput>> stopStressTest(final StopStressTestInput input) {
262 StopStressTestOutputBuilder stopStressTestOutput;
263 stopStressTestOutput = new StopStressTestOutputBuilder()
264 .setSuccessCount(Uint32.valueOf(succcessCounter.longValue()))
265 .setFailureCount(Uint32.valueOf(failureCounter.longValue()));
267 final StopStressTestOutput result = stopStressTestOutput.build();
268 LOG.info("Executed Stop Stress test; No. of cars created {}; No. of cars failed {}; ",
269 succcessCounter, failureCounter);
271 succcessCounter.set(0);
272 failureCounter.set(0);
273 return Futures.immediateFuture(RpcResultBuilder.<StopStressTestOutput>success(result).build());
276 private ListenableFuture<RpcResult<RegisterOwnershipOutput>> registerOwnership(final RegisterOwnershipInput input) {
277 if (registeredListener.compareAndSet(false, true)) {
278 ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
281 Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
283 ownershipService.registerCandidate(entity);
284 } catch (CandidateAlreadyRegisteredException e) {
285 return RpcResultBuilder.<RegisterOwnershipOutput>failed().withError(ErrorType.APPLICATION,
286 "Could not register for car " + input.getCarId(), e).buildFuture();
289 return RpcResultBuilder.success(new RegisterOwnershipOutputBuilder().build()).buildFuture();
292 private ListenableFuture<RpcResult<UnregisterOwnershipOutput>> unregisterOwnership(
293 final UnregisterOwnershipInput input) {
294 return RpcResultBuilder.success(new UnregisterOwnershipOutputBuilder().build()).buildFuture();
297 private ListenableFuture<RpcResult<RegisterLoggingDtclOutput>> registerLoggingDtcl(
298 final RegisterLoggingDtclInput input) {
299 LOG.info("Registering a new CarDataTreeChangeListener");
300 final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
301 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
303 carsDtclRegistrations.add(carsDtclRegistration);
304 return RpcResultBuilder.success(new RegisterLoggingDtclOutputBuilder().build()).buildFuture();
307 private ListenableFuture<RpcResult<UnregisterLoggingDtclsOutput>> unregisterLoggingDtcls(
308 final UnregisterLoggingDtclsInput input) {
309 LOG.info("Unregistering the CarDataTreeChangeListener(s)");
310 synchronized (carsDtclRegistrations) {
311 int numListeners = 0;
312 for (ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration : carsDtclRegistrations) {
313 carsDtclRegistration.close();
316 carsDtclRegistrations.clear();
317 LOG.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
319 return RpcResultBuilder.success(new UnregisterLoggingDtclsOutputBuilder().build()).buildFuture();
322 @SuppressWarnings("checkstyle:IllegalCatch")
323 private ListenableFuture<RpcResult<UnregisterCommitCohortOutput>> unregisterCommitCohort(
324 final UnregisterCommitCohortInput input) {
325 closeCommitCohortRegistration();
327 return RpcResultBuilder.success(new UnregisterCommitCohortOutputBuilder().build()).buildFuture();
330 private void closeCommitCohortRegistration() {
331 final DOMDataTreeCommitCohortRegistration<CarEntryDataTreeCommitCohort> reg = commitCohortReg.getAndSet(null);
334 LOG.info("Unregistered commit cohort");
338 private synchronized ListenableFuture<RpcResult<RegisterCommitCohortOutput>> registerCommitCohort(
339 final RegisterCommitCohortInput input) {
340 if (commitCohortReg.get() != null) {
341 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();
344 final DOMDataTreeCommitCohortRegistry commitCohortRegistry = domDataBroker.getExtensions().getInstance(
345 DOMDataTreeCommitCohortRegistry.class);
347 if (commitCohortRegistry == null) {
349 return RpcResultBuilder.<RegisterCommitCohortOutput>failed().withError(ErrorType.APPLICATION,
350 "DOMDataTreeCommitCohortRegistry not found").buildFuture();
353 // Note: it may look strange that we specify the CarEntry.QNAME twice in the path below. This must be done in
354 // order to register the commit cohort for CarEntry instances. In the underlying data tree, a yang list is
355 // represented as a MapNode with MapEntryNodes representing the child list entries. Therefore, in order to
356 // address a list entry, you must specify the path argument for the MapNode and the path argument for the
357 // MapEntryNode. In the path below, the first CarEntry.QNAME argument addresses the MapNode and, since we want
358 // to address all list entries, the second path argument is wild-carded by specifying just the CarEntry.QNAME.
359 final YangInstanceIdentifier carEntryPath = YangInstanceIdentifier.builder(
360 YangInstanceIdentifier.of(Cars.QNAME)).node(CarEntry.QNAME).node(CarEntry.QNAME).build();
361 commitCohortReg.set(commitCohortRegistry.registerCommitCohort(new DOMDataTreeIdentifier(
362 LogicalDatastoreType.CONFIGURATION, carEntryPath), new CarEntryDataTreeCommitCohort()));
364 LOG.info("Registered commit cohort");
366 return RpcResultBuilder.success(new RegisterCommitCohortOutputBuilder().build()).buildFuture();