fix mdsal WriteOperations deprecated warnings
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / utils / TransactionUtils.java
1 /*
2  * Copyright © 2018 Orange 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
9 package org.opendaylight.transportpce.renderer.utils;
10
11 import java.util.Optional;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.transportpce.common.Timeouts;
16 import org.opendaylight.transportpce.common.device.DeviceTransaction;
17 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20
21 public final class TransactionUtils {
22
23     private TransactionUtils() {
24
25     }
26
27     @SuppressWarnings("deprecation")
28     // deviceTx.put needs the "true" boolean parameter at the end in order to not compromise the Junit test suite
29     public static boolean writeTransaction(DeviceTransactionManager deviceTransactionManager,
30                                     String nodeId,
31                                     LogicalDatastoreType logicalDatastoreType,
32                                     InstanceIdentifier instanceIdentifier,
33                                     DataObject object)
34             throws ExecutionException, InterruptedException {
35         Future<Optional<DeviceTransaction>> deviceTxFuture =
36                 deviceTransactionManager.getDeviceTransaction(nodeId);
37         if (!deviceTxFuture.get().isPresent()) {
38             return false;
39         }
40         DeviceTransaction deviceTx = deviceTxFuture.get().get();
41         deviceTx.put(logicalDatastoreType, instanceIdentifier, object, true);
42         deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT).get();
43         return true;
44     }
45
46     public static DataObject readTransaction(DeviceTransactionManager deviceTransactionManager,
47                                   String nodeId,
48                                   LogicalDatastoreType logicalDatastoreType,
49                                   InstanceIdentifier<? extends DataObject> instanceIdentifier)
50             throws ExecutionException, InterruptedException {
51         Future<Optional<DeviceTransaction>> deviceTxFuture =
52                 deviceTransactionManager.getDeviceTransaction(nodeId);
53         if (!deviceTxFuture.get().isPresent()) {
54             return null;
55         }
56         DeviceTransaction deviceTx = deviceTxFuture.get().get();
57         Optional<? extends DataObject> readOpt
58                 = deviceTx.read(logicalDatastoreType, instanceIdentifier).get();
59         if (!readOpt.isPresent()) {
60             return null;
61         }
62         return readOpt.get();
63     }
64
65 }