Merge "Bump transportpce-models upstream dependency"
[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({ "unchecked", "rawtypes" })
28     // deviceTx.put needs the "true" boolean parameter at the end in order to not compromise the Junit test suite
29     // FIXME check if the InstanceIdentifier raw type can be avoided
30     // Raw types use are discouraged since they lack type safety.
31     // Resulting Problems are observed at run time and not at compile time
32     public static boolean writeTransaction(DeviceTransactionManager deviceTransactionManager,
33                                     String nodeId,
34                                     LogicalDatastoreType logicalDatastoreType,
35                                     InstanceIdentifier instanceIdentifier,
36                                     DataObject object)
37             throws ExecutionException, InterruptedException {
38         Future<Optional<DeviceTransaction>> deviceTxFuture =
39                 deviceTransactionManager.getDeviceTransaction(nodeId);
40         if (!deviceTxFuture.get().isPresent()) {
41             return false;
42         }
43         DeviceTransaction deviceTx = deviceTxFuture.get().orElseThrow();
44         deviceTx.merge(logicalDatastoreType, instanceIdentifier, object);
45         deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT).get();
46         return true;
47     }
48
49     public static DataObject readTransaction(DeviceTransactionManager deviceTransactionManager,
50                                   String nodeId,
51                                   LogicalDatastoreType logicalDatastoreType,
52                                   InstanceIdentifier<? extends DataObject> instanceIdentifier)
53             throws ExecutionException, InterruptedException {
54         Future<Optional<DeviceTransaction>> deviceTxFuture =
55                 deviceTransactionManager.getDeviceTransaction(nodeId);
56         DeviceTransaction deviceTx = deviceTxFuture.get().orElseThrow(null);
57         Optional<? extends DataObject> readOpt
58                 = deviceTx.read(logicalDatastoreType, instanceIdentifier).get();
59         return readOpt.orElseThrow(null);
60     }
61
62 }