Implement SFC integration
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sfcutils / SfcDataStoreHelper.java
1 /*
2  * Copyright (c) 2014 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.renderer.ofoverlay.sfcutils;
10
11 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.CheckedFuture;
23
24 /**
25  * @author Martin Sunal
26  */
27 public class SfcDataStoreHelper {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SfcDataStoreHelper.class);
30
31     /**
32      * Reads data from datastore as synchrone call.
33      * @return {@link Optional#isPresent()} is {@code true} if reading was successful and data exists in datastore; {@link Optional#isPresent()} is {@code false} otherwise
34      */
35     public static <U extends DataObject> U readFromDs(LogicalDatastoreType store,InstanceIdentifier<U> iid, ReadTransaction rTx) {
36         U ret = null;
37         Optional<U> optionalDataObject;
38         CheckedFuture<Optional<U>, ReadFailedException> submitFuture = rTx.read(store, iid);
39         try {
40             optionalDataObject = submitFuture.checkedGet();
41             if (optionalDataObject != null && optionalDataObject.isPresent()) {
42                 ret = optionalDataObject.get();
43             } else {
44                 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
45             }
46         } catch (ReadFailedException e) {
47             LOG.warn("failed to ....", e);
48         }
49         return ret;
50     }
51
52     /**
53      * Calls {@link WriteTransaction#submit()} on write transaction.
54      * @param wTx write transaction
55      * @return {@code true} if transaction commit was successful; {@code false} otherwise
56      */
57     public static boolean submitToDs(WriteTransaction wTx) {
58         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
59         try {
60             submitFuture.checkedGet();
61             return true;
62         } catch (TransactionCommitFailedException e) {
63             LOG.warn("Transaction commit failed to DS.", e);
64             return false;
65         }
66     }
67
68 }