Merge "GBP UI fixes"
[groupbasedpolicy.git] / neutron-ovsdb / src / main / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / util / DataStore.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.groupbasedpolicy.neutron.ovsdb.util;
10
11
12 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.base.Optional;
23 import com.google.common.util.concurrent.CheckedFuture;
24
25 public class DataStore {
26     private static final Logger LOG = LoggerFactory.getLogger(DataStore.class);
27     private static final String HEX = "0x";
28     /**
29      * Convert an OpenFlow Datapath ID to a Long
30      *
31      * @param dpid The OpenFlow Datapath ID
32      * @return The Long representation of the DPID
33      */
34     public static Long getLongFromDpid(String dpid) {
35         String[] addressInBytes = dpid.split(":");
36         Long address =
37                 (Long.decode(HEX + addressInBytes[2]) << 40) |
38                 (Long.decode(HEX + addressInBytes[3]) << 32) |
39                 (Long.decode(HEX + addressInBytes[4]) << 24) |
40                 (Long.decode(HEX + addressInBytes[5]) << 16) |
41                 (Long.decode(HEX + addressInBytes[6]) << 8 ) |
42                 (Long.decode(HEX + addressInBytes[7]));
43         return address;
44     }
45
46     /**
47      * Calls {@link WriteTransaction#submit()} on write transaction.
48      * @param wTx write transaction
49      * @return {@code true} if transaction commit was successful; {@code false} otherwise
50      */
51     public static boolean submitToDs(WriteTransaction wTx) {
52         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
53         try {
54             submitFuture.checkedGet();
55             return true;
56         } catch (TransactionCommitFailedException e) {
57             LOG.warn("Transaction commit failed to DS.", e);
58             return false;
59         }
60     }
61
62     /**
63      * Reads data from datastore as synchronous call.
64      * @return {@link Optional#isPresent()} is {@code true} if reading was successful and
65      *         data exists in datastore; {@link Optional#isPresent()} is {@code false} otherwise
66      */
67     public static <T extends DataObject> Optional<T> readFromDs(LogicalDatastoreType store,
68             InstanceIdentifier<T> path, ReadTransaction rTx) {
69         CheckedFuture<Optional<T>, ReadFailedException> resultFuture = rTx.read(store, path);
70         try {
71             return resultFuture.checkedGet();
72         } catch (ReadFailedException e) {
73             LOG.warn("Read failed from DS.", e);
74             return Optional.absent();
75         }
76     }
77 }