Bug 4735 - null pointer exception in SouthboundImpl
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / MdsalUtils.java
1 /*
2  * Copyright (c) 2015 Red Hat, 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.ovsdb.openstack.netvirt.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class MdsalUtils {
24     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
25     private DataBroker databroker = null;
26
27     /**
28      * Class constructor setting the data broker.
29      *
30      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
31      */
32     public MdsalUtils(DataBroker dataBroker) {
33         this.databroker = dataBroker;
34     }
35
36     /**
37      * Executes delete as a blocking transaction.
38      *
39      * @param store {@link LogicalDatastoreType} which should be modified
40      * @param path {@link InstanceIdentifier} to read from
41      * @param <D> the data object type
42      * @return the result of the request
43      */
44     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
45             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
46         boolean result = false;
47         if(this.read(store,path) != null) {
48             final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
49             transaction.delete(store, path);
50             CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
51             try {
52                 future.checkedGet();
53                 result = true;
54             } catch (TransactionCommitFailedException e) {
55                 LOG.warn("Failed to delete {} ", path, e);
56             }
57         } else {
58             result = true;
59         }
60         return result;
61     }
62
63     /**
64      * Executes merge as a blocking transaction.
65      *
66      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
67      * @param path {@link InstanceIdentifier} for path to read
68      * @param data object of type D
69      * @return the result of the request
70      */
71     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
72             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
73         boolean result = false;
74         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
75         transaction.merge(logicalDatastoreType, path, data, true);
76         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
77         try {
78             future.checkedGet();
79             result = true;
80         } catch (TransactionCommitFailedException e) {
81             LOG.warn("Failed to merge {} ", path, e);
82         }
83         return result;
84     }
85
86     /**
87      * Executes put as a blocking transaction.
88      *
89      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
90      * @param path {@link InstanceIdentifier} for path to read
91      * @param data object of type D
92      * @return the result of the request
93      */
94     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
95             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
96         boolean result = false;
97         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
98         transaction.put(logicalDatastoreType, path, data, true);
99         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
100         try {
101             future.checkedGet();
102             result = true;
103         } catch (TransactionCommitFailedException e) {
104             LOG.warn("Failed to put {} ", path, e);
105         }
106         return result;
107     }
108
109     /**
110      * Executes read as a blocking transaction.
111      *
112      * @param store {@link LogicalDatastoreType} to read
113      * @param path {@link InstanceIdentifier} for path to read
114      * @param <D> the data object type
115      * @return the result as the data object requested
116      */
117     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
118             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
119         D result = null;
120         final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
121         Optional<D> optionalDataObject;
122         CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
123         try {
124             optionalDataObject = future.checkedGet();
125             if (optionalDataObject.isPresent()) {
126                 result = optionalDataObject.get();
127             } else {
128                 LOG.debug("{}: Failed to read {}",
129                         Thread.currentThread().getStackTrace()[1], path);
130             }
131         } catch (ReadFailedException e) {
132             LOG.warn("Failed to read {} ", path, e);
133         }
134         transaction.close();
135         return result;
136     }
137 }