Deprecate DOMDataTreeProducer-related classes
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / ProduceTransactionsHandler.java
1 /*
2  * Copyright (c) 2017 Cisco 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.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.SplittableRandom;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
32 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsInput;
33 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutput;
34 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutputBuilder;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 @Deprecated(forRemoval = true)
47 public final class ProduceTransactionsHandler extends AbstractTransactionHandler {
48     private static final Logger LOG = LoggerFactory.getLogger(ProduceTransactionsHandler.class);
49
50     private final SettableFuture<RpcResult<ProduceTransactionsOutput>> future = SettableFuture.create();
51     private final SplittableRandom random = new SplittableRandom();
52     private final Set<Integer> usedValues = new HashSet<>();
53     private final DOMDataTreeIdentifier idListItem;
54     private final DOMDataTreeProducer itemProducer;
55
56     private long insertTx = 0;
57     private long deleteTx = 0;
58
59     private ProduceTransactionsHandler(final DOMDataTreeProducer producer, final DOMDataTreeIdentifier idListItem,
60             final ProduceTransactionsInput input) {
61         super(input);
62         this.itemProducer = requireNonNull(producer);
63         this.idListItem = requireNonNull(idListItem);
64     }
65
66     public static ListenableFuture<RpcResult<ProduceTransactionsOutput>> start(
67             final DOMDataTreeService domDataTreeService, final ProduceTransactionsInput input) {
68         final String id = input.getId();
69         LOG.debug("Filling the item list {} with initial values.", id);
70
71         final YangInstanceIdentifier idListWithKey = ID_INT_YID.node(NodeIdentifierWithPredicates.of(ID_INT, ID, id));
72
73         final DOMDataTreeProducer itemProducer = domDataTreeService.createProducer(
74             Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey)));
75
76         final DOMDataTreeCursorAwareTransaction tx = itemProducer.createTransaction(false);
77         final DOMDataTreeWriteCursor cursor =
78                 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey));
79
80         final MapNode list = ImmutableNodes.mapNodeBuilder(ITEM).build();
81         cursor.write(list.getIdentifier(), list);
82         cursor.close();
83
84         try {
85             tx.commit().get(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
86         } catch (InterruptedException | ExecutionException | TimeoutException e) {
87             LOG.warn("Unable to fill the initial item list.", e);
88             closeProducer(itemProducer);
89
90             return Futures.immediateFuture(RpcResultBuilder.<ProduceTransactionsOutput>failed()
91                 .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
92         }
93
94         final ProduceTransactionsHandler handler = new ProduceTransactionsHandler(itemProducer,
95             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey.node(list.getIdentifier())
96                 .toOptimized()), input);
97         // It is handler's responsibility to close itemProducer when the work is finished.
98         handler.doStart();
99         return handler.future;
100     }
101
102     private static void closeProducer(final DOMDataTreeProducer producer) {
103         try {
104             producer.close();
105         } catch (final DOMDataTreeProducerException exception) {
106             LOG.warn("Failure while closing producer.", exception);
107         }
108     }
109
110     @Override
111     FluentFuture<? extends @NonNull CommitInfo> execWrite(final long txId) {
112         final int i = random.nextInt(MAX_ITEM + 1);
113         final DOMDataTreeCursorAwareTransaction tx = itemProducer.createTransaction(false);
114         final DOMDataTreeWriteCursor cursor = tx.createCursor(idListItem);
115
116         final NodeIdentifierWithPredicates entryId = NodeIdentifierWithPredicates.of(ITEM, NUMBER, i);
117         if (usedValues.contains(i)) {
118             LOG.debug("Deleting item: {}", i);
119             deleteTx++;
120             cursor.delete(entryId);
121             usedValues.remove(i);
122
123         } else {
124             LOG.debug("Inserting item: {}", i);
125             insertTx++;
126
127             final MapEntryNode entry = ImmutableNodes.mapEntryBuilder().withNodeIdentifier(entryId)
128                     .withChild(ImmutableNodes.leafNode(NUMBER, i)).build();
129             cursor.write(entryId, entry);
130             usedValues.add(i);
131         }
132
133         cursor.close();
134
135         return tx.commit();
136     }
137
138     @Override
139     void runFailed(final Throwable cause, final long txId) {
140         closeProducer(itemProducer);
141         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
142             .withError(RpcError.ErrorType.APPLICATION, "Commit failed for tx # " + txId, cause).build());
143     }
144
145     @Override
146     void runSuccessful(final long allTx) {
147         closeProducer(itemProducer);
148         final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder()
149                 .setAllTx(allTx)
150                 .setInsertTx(insertTx)
151                 .setDeleteTx(deleteTx)
152                 .build();
153         future.set(RpcResultBuilder.<ProduceTransactionsOutput>success()
154                 .withResult(output).build());
155     }
156
157     @Override
158     void runTimedOut(final String cause) {
159         closeProducer(itemProducer);
160         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
161             .withError(RpcError.ErrorType.APPLICATION, cause).build());
162     }
163 }