11d443be79b29e7af5c5e86eafa81bdab2697bd3
[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     public static boolean writeTransaction(DeviceTransactionManager deviceTransactionManager,
28                                     String nodeId,
29                                     LogicalDatastoreType logicalDatastoreType,
30                                     InstanceIdentifier instanceIdentifier,
31                                     DataObject object)
32             throws ExecutionException, InterruptedException {
33         Future<Optional<DeviceTransaction>> deviceTxFuture =
34                 deviceTransactionManager.getDeviceTransaction(nodeId);
35         if (!deviceTxFuture.get().isPresent()) {
36             return false;
37         }
38         DeviceTransaction deviceTx = deviceTxFuture.get().get();
39         deviceTx.put(logicalDatastoreType, instanceIdentifier, object, true);
40         deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT).get();
41         return true;
42     }
43
44     public static DataObject readTransaction(DeviceTransactionManager deviceTransactionManager,
45                                   String nodeId,
46                                   LogicalDatastoreType logicalDatastoreType,
47                                   InstanceIdentifier<? extends DataObject> instanceIdentifier)
48             throws ExecutionException, InterruptedException {
49         Future<Optional<DeviceTransaction>> deviceTxFuture =
50                 deviceTransactionManager.getDeviceTransaction(nodeId);
51         if (!deviceTxFuture.get().isPresent()) {
52             return null;
53         }
54         DeviceTransaction deviceTx = deviceTxFuture.get().get();
55         Optional<? extends DataObject> readOpt
56                 = deviceTx.read(logicalDatastoreType, instanceIdentifier).get();
57         if (!readOpt.isPresent()) {
58             return null;
59         }
60         return readOpt.get();
61     }
62
63 }