Added response data to operational datastore, refactored data validation
[packetcable.git] / packetcable-policy-server / src / main / java / org / opendaylight / controller / packetcable / provider / MdsalUtils.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.controller.packetcable.provider;
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.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class MdsalUtils {
25     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
26     private DataBroker databroker = null;
27
28     /**
29      * Class constructor setting the data broker.
30      *
31      * @param dataBroker the {@link DataBroker}
32      */
33     public MdsalUtils(DataBroker dataBroker) {
34         this.databroker = dataBroker;
35     }
36
37     /**
38      * Executes delete as a blocking transaction.
39      *
40      * @param store {@link LogicalDatastoreType} which should be modified
41      * @param path {@link InstanceIdentifier} to read from
42      * @param <D> the data object type
43      * @return the result of the request
44      */
45     public <D extends DataObject> boolean delete(
46             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
47         boolean result = false;
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         return result;
58     }
59
60     /**
61      * Executes merge as a blocking transaction.
62      *
63      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
64      * @param path {@link InstanceIdentifier} for path to read
65      * @param <D> the data object type
66      * @return the result of the request
67      */
68     public <D extends DataObject> boolean merge(
69             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
70         boolean result = false;
71         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
72         transaction.merge(logicalDatastoreType, path, data, true);
73         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
74         try {
75             future.checkedGet();
76             result = true;
77         } catch (TransactionCommitFailedException e) {
78             LOG.warn("Failed to merge {} ", path, e);
79         }
80         return result;
81     }
82
83     /**
84      * Execures a read as a blocking transaction.
85      *
86      * @param logicalDatastoreType which datastore to read from
87      * @param path The path to read
88      * @param <D> the DataObject type
89      * @return an Optional containing the object.
90      */
91     public <D extends DataObject> Optional<D> read(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path) {
92         final ReadOnlyTransaction readOnlyTransaction = databroker.newReadOnlyTransaction();
93
94         CheckedFuture<Optional<D>, ReadFailedException> future = readOnlyTransaction.read(logicalDatastoreType, path);
95
96         try {
97             return future.checkedGet();
98         } catch (ReadFailedException e) {
99             LOG.error("Failed to read {} at path {}", logicalDatastoreType, path, e);
100         }
101
102         return Optional.absent();
103     }
104
105     /**
106      * Executes put as a blocking transaction.
107      *
108      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
109      * @param path {@link InstanceIdentifier} for path to read
110      * @param <D> the data object type
111      * @return the result of the request
112      */
113     public <D extends DataObject> boolean put(
114             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
115         boolean result = false;
116         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
117         transaction.put(logicalDatastoreType, path, data, true);
118         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
119         try {
120             future.checkedGet();
121             result = true;
122         } catch (TransactionCommitFailedException e) {
123             LOG.warn("Failed to merge {} ", path, e);
124         }
125         return result;
126     }
127 }