Switch to MD-SAL APIs
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / TestProviderTransactionUtil.java
1 /*
2  * Copyright (c) 2015 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.openflowplugin.test;
9
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.mdsal.binding.api.ReadTransaction;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Created by Martin Bobak mbobak@cisco.com on 8/20/14.
21  */
22 public final class TestProviderTransactionUtil {
23
24     private static final Logger LOG = LoggerFactory.getLogger(TestProviderTransactionUtil.class);
25
26     private TestProviderTransactionUtil() {
27         throw new AssertionError("TestProviderTransactionUtil was not meant to be instantiated.");
28     }
29
30     public static <T extends DataObject> T getDataObject(ReadTransaction readOnlyTransaction,
31             InstanceIdentifier<T> identifier) {
32         Optional<T> optionalData = null;
33         try {
34             optionalData = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, identifier).get();
35             if (optionalData.isPresent()) {
36                 return optionalData.get();
37             }
38         } catch (ExecutionException | InterruptedException e) {
39             LOG.error("Read transaction for identifier {} failed.", identifier, e);
40         }
41         return null;
42     }
43 }