Fix ietf-yang-types version
[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.collect.Sets;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collection;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import java.util.concurrent.atomic.AtomicBoolean;
18 import java.util.concurrent.atomic.AtomicLong;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
24 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
25 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
26 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
27 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.RegisterOwnershipInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.UnregisterOwnershipInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * @author Thomas Pantelis
50  */
51 public class CarProvider implements CarService {
52     private static final Logger log = LoggerFactory.getLogger(PurchaseCarProvider.class);
53
54     private final DataBroker dataProvider;
55     private final EntityOwnershipService ownershipService;
56     private static final Logger LOG = LoggerFactory.getLogger(CarProvider.class);
57
58     private static final String ENTITY_TYPE = "cars";
59
60     private final CarEntityOwnershipListener ownershipListener = new CarEntityOwnershipListener();
61     private final AtomicBoolean registeredListener = new AtomicBoolean();
62
63     private volatile Thread testThread;
64     private volatile boolean stopThread;
65
66     private static final InstanceIdentifier CARS_IID = InstanceIdentifier.builder(Cars.class).build();
67     private static final DataTreeIdentifier<Cars> CARS_DTID = new DataTreeIdentifier<>(
68             LogicalDatastoreType.CONFIGURATION, CARS_IID);
69
70     private Collection<ListenerRegistration<DataChangeListener>> carsDclRegistrations =
71             Sets.newConcurrentHashSet();
72     private Collection<ListenerRegistration<CarDataTreeChangeListener>> carsDtclRegistrations =
73             Sets.newConcurrentHashSet();
74
75     public CarProvider(DataBroker dataProvider, EntityOwnershipService ownershipService) {
76         this.dataProvider = dataProvider;
77         this.ownershipService = ownershipService;
78     }
79
80     private void stopThread() {
81         if(testThread != null) {
82             stopThread = true;
83             testThread.interrupt();
84             try {
85                 testThread.join();
86             } catch (InterruptedException e) {}
87             testThread = null;
88         }
89     }
90
91     @Override
92     public Future<RpcResult<Void>> stressTest(StressTestInput input) {
93         final int inputRate;
94         final long inputCount;
95
96         // If rate is not provided, or given as zero, then just return.
97         if ((input.getRate() == null) || (input.getRate() == 0)) {
98             log.info("Exiting stress test as no rate is given.");
99             return Futures.immediateFuture(RpcResultBuilder.<Void>failed()
100                                            .withError(ErrorType.PROTOCOL, "invalid rate")
101                                            .build());
102         } else {
103             inputRate = input.getRate();
104         }
105
106         if (input.getCount() != null) {
107             inputCount = input.getCount();
108         } else {
109             inputCount = 0;
110         }
111
112         log.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
113
114         stopThread();
115
116         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
117         InstanceIdentifier<Cars> carsId = InstanceIdentifier.<Cars>builder(Cars.class).build();
118         tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
119         try {
120             tx.submit().checkedGet(5, TimeUnit.SECONDS);
121         } catch (TransactionCommitFailedException | TimeoutException e) {
122             log.error("Put Cars failed",e);
123             return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
124         }
125
126         stopThread = false;
127         final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
128         final Stopwatch sw = Stopwatch.createUnstarted();
129         testThread = new Thread() {
130             @Override
131             public void run() {
132                 sw.start();
133                 AtomicLong count = new AtomicLong();
134                 while(!stopThread) {
135                     long id = count.incrementAndGet();
136                     WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
137                     CarEntry car = new CarEntryBuilder().setId(new CarId("car"+id)).build();
138                     tx.put(LogicalDatastoreType.CONFIGURATION,
139                             InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.getKey()).build(),
140                             car);
141                     tx.submit();
142                     try {
143                         TimeUnit.NANOSECONDS.sleep(sleep);
144                     } catch (InterruptedException e) {
145                         break;
146                     }
147
148                     if((count.get() % 1000) == 0) {
149                         log.info("Cars created {}, time: {}",count.get(),sw.elapsed(TimeUnit.SECONDS));
150                     }
151
152                     // Check if a count is specified in input and we have created that many cars.
153                     if ((inputCount != 0) && (count.get() >= inputCount)) {
154                         stopThread = true;
155                     }
156                 }
157
158                 log.info("Stress test thread stopping after creating {} cars.", count.get());
159             }
160         };
161         testThread.start();
162
163         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
164     }
165
166     @Override
167     public Future<RpcResult<Void>> stopStressTest() {
168         stopThread();
169         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
170     }
171
172
173     @Override
174     public Future<RpcResult<Void>> registerOwnership(RegisterOwnershipInput input) {
175         if(registeredListener.compareAndSet(false, true)) {
176             ownershipService.registerListener(ENTITY_TYPE, ownershipListener);
177         }
178
179         Entity entity = new Entity(ENTITY_TYPE, input.getCarId());
180         try {
181             ownershipService.registerCandidate(entity);
182         } catch (CandidateAlreadyRegisteredException e) {
183             return RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
184                     "Could not register for car " + input.getCarId(), e).buildFuture();
185         }
186
187         return RpcResultBuilder.<Void>success().buildFuture();
188     }
189
190     @Override
191     public Future<RpcResult<Void>> unregisterOwnership(UnregisterOwnershipInput input) {
192         return RpcResultBuilder.<Void>success().buildFuture();
193     }
194
195     private static class CarEntityOwnershipListener implements EntityOwnershipListener {
196         @Override
197         public void ownershipChanged(EntityOwnershipChange ownershipChange) {
198             LOG.info("ownershipChanged: {}", ownershipChange);
199         }
200     }
201
202     @Override
203     public Future<RpcResult<java.lang.Void>> registerLoggingDcl() {
204         LOG.info("Registering a new CarDataChangeListener");
205         final ListenerRegistration carsDclRegistration = dataProvider.registerDataChangeListener(
206                 LogicalDatastoreType.CONFIGURATION, CARS_IID, new CarDataChangeListener(),
207                 AsyncDataBroker.DataChangeScope.SUBTREE);
208
209         if (carsDclRegistration != null) {
210             carsDclRegistrations.add(carsDclRegistration);
211             return RpcResultBuilder.<Void>success().buildFuture();
212         }
213         return RpcResultBuilder.<Void>failed().buildFuture();
214     }
215
216     @Override
217     public Future<RpcResult<java.lang.Void>> registerLoggingDtcl() {
218         LOG.info("Registering a new CarDataTreeChangeListener");
219         final ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration =
220                 dataProvider.registerDataTreeChangeListener(CARS_DTID, new CarDataTreeChangeListener());
221
222         if (carsDtclRegistration != null) {
223             carsDtclRegistrations.add(carsDtclRegistration);
224             return RpcResultBuilder.<Void>success().buildFuture();
225         }
226         return RpcResultBuilder.<Void>failed().buildFuture();
227     }
228
229     @Override
230     public Future<RpcResult<java.lang.Void>> unregisterLoggingDcls() {
231         LOG.info("Unregistering the CarDataChangeListener(s)");
232         synchronized (carsDclRegistrations) {
233             int numListeners = 0;
234             for (ListenerRegistration<DataChangeListener> carsDclRegistration : carsDclRegistrations) {
235                 carsDclRegistration.close();
236                 numListeners++;
237             }
238             carsDclRegistrations.clear();
239             LOG.info("Unregistered {} CarDataChangeListener(s)", numListeners);
240         }
241         return RpcResultBuilder.<Void>success().buildFuture();
242     }
243
244     @Override
245     public Future<RpcResult<java.lang.Void>> unregisterLoggingDtcls() {
246         LOG.info("Unregistering the CarDataTreeChangeListener(s)");
247         synchronized (carsDtclRegistrations) {
248             int numListeners = 0;
249             for (ListenerRegistration<CarDataTreeChangeListener> carsDtclRegistration : carsDtclRegistrations) {
250                 carsDtclRegistration.close();
251                 numListeners++;
252             }
253             carsDtclRegistrations.clear();
254             LOG.info("Unregistered {} CaraDataTreeChangeListener(s)", numListeners);
255         }
256         return RpcResultBuilder.<Void>success().buildFuture();
257     }
258 }