/* * Copyright © 2018 Orange Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.transportpce.renderer.utils; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.transportpce.common.Timeouts; import org.opendaylight.transportpce.common.device.DeviceTransaction; import org.opendaylight.transportpce.common.device.DeviceTransactionManager; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public final class TransactionUtils { private TransactionUtils() { } @SuppressWarnings({ "unchecked", "deprecation" }) // deviceTx.put needs the "true" boolean parameter at the end in order to not compromise the Junit test suite public static boolean writeTransaction(DeviceTransactionManager deviceTransactionManager, String nodeId, LogicalDatastoreType logicalDatastoreType, InstanceIdentifier instanceIdentifier, DataObject object) throws ExecutionException, InterruptedException { Future> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId); if (!deviceTxFuture.get().isPresent()) { return false; } DeviceTransaction deviceTx = deviceTxFuture.get().get(); deviceTx.merge(logicalDatastoreType, instanceIdentifier, object); deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT).get(); return true; } public static DataObject readTransaction(DeviceTransactionManager deviceTransactionManager, String nodeId, LogicalDatastoreType logicalDatastoreType, InstanceIdentifier instanceIdentifier) throws ExecutionException, InterruptedException { Future> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId); if (!deviceTxFuture.get().isPresent()) { return null; } DeviceTransaction deviceTx = deviceTxFuture.get().get(); Optional readOpt = deviceTx.read(logicalDatastoreType, instanceIdentifier).get(); if (!readOpt.isPresent()) { return null; } return readOpt.get(); } }