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