2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.neutron.ovsdb.util;
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;
22 import com.google.common.base.Optional;
23 import com.google.common.util.concurrent.CheckedFuture;
25 public class DataStore {
26 private static final Logger LOG = LoggerFactory.getLogger(DataStore.class);
27 private static final String HEX = "0x";
29 * Convert an OpenFlow Datapath ID to a Long
31 * @param dpid The OpenFlow Datapath ID
32 * @return The Long representation of the DPID
34 public static Long getLongFromDpid(String dpid) {
35 String[] addressInBytes = dpid.split(":");
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]));
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
51 public static boolean submitToDs(WriteTransaction wTx) {
52 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
54 submitFuture.checkedGet();
56 } catch (TransactionCommitFailedException e) {
57 LOG.warn("Transaction commit failed to DS.", e);
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
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);
71 return resultFuture.checkedGet();
72 } catch (ReadFailedException e) {
73 LOG.warn("Read failed from DS.", e);
74 return Optional.absent();