Merge "Bug 2538: Remove redundant Augmentation checks and tests"
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / mdsal / DataStore.java
1 /*
2  * Copyright (c) 2013 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.controller.mdsal;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
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.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21
22 public class DataStore {
23     private static final FutureCallback<Void> DEFAULT_CALLBACK =
24             new FutureCallback<Void>() {
25                 public void onSuccess(Void result) {
26                     // TODO: Implement default behaviour
27                 }
28
29                 public void onFailure(Throwable t) {
30                     // TODO: Implement default behaviour
31                 };
32             };
33
34     private final MdSAL mdSAL;
35
36     public DataStore(MdSAL mdSAL) {
37         this.mdSAL = mdSAL;
38     }
39
40     public ListenerRegistration<DataChangeListener> registerDataChangeListener(LogicalDatastoreType store,
41                                                                                InstanceIdentifier<?> path,
42                                                                                DataChangeListener listener,
43                                                                                AsyncDataBroker.DataChangeScope triggeringScope) {
44         return mdSAL.getDataBroker().registerDataChangeListener(store, path, listener, triggeringScope);
45     }
46
47     public <T extends DataObject> void asyncPUT(LogicalDatastoreType datastoreType,
48                                                 InstanceIdentifier<T> path,
49                                                 T data) {
50         asyncPUT(datastoreType, path, data, DEFAULT_CALLBACK);
51     }
52
53     public <T extends DataObject> void asyncPUT(LogicalDatastoreType datastoreType,
54                                                 InstanceIdentifier<T> path,
55                                                 T data,
56                                                 FutureCallback<Void> callback) {
57         WriteTransaction tx = mdSAL.getDataBroker().newWriteOnlyTransaction();
58         tx.put(datastoreType, path, data, true);
59         execPut(tx, callback);
60     }
61
62     public <T extends DataObject> T read(LogicalDatastoreType datastoreType,
63                                          InstanceIdentifier<T> path) {
64
65         ReadOnlyTransaction tx = mdSAL.getDataBroker().newReadOnlyTransaction();
66         T result = null;
67
68         try {
69             result = tx.read(datastoreType, path).get().get();
70         } catch (Exception e) {
71             throw new RuntimeException(e);
72         }
73
74         return result;
75     }
76
77     private static void execPut(WriteTransaction tx, FutureCallback<Void> callback) {
78         Futures.addCallback(tx.submit(), callback);
79     }
80 }