2 * Copyright (c) 2013 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.controller.mdsal;
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;
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
29 public void onFailure(Throwable t) {
30 // TODO: Implement default behaviour
34 private final MdSAL mdSAL;
36 public DataStore(MdSAL mdSAL) {
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);
47 public <T extends DataObject> void asyncPUT(LogicalDatastoreType datastoreType,
48 InstanceIdentifier<T> path,
50 asyncPUT(datastoreType, path, data, DEFAULT_CALLBACK);
53 public <T extends DataObject> void asyncPUT(LogicalDatastoreType datastoreType,
54 InstanceIdentifier<T> path,
56 FutureCallback<Void> callback) {
57 WriteTransaction tx = mdSAL.getDataBroker().newWriteOnlyTransaction();
58 tx.put(datastoreType, path, data, true);
59 execPut(tx, callback);
62 public <T extends DataObject> T read(LogicalDatastoreType datastoreType,
63 InstanceIdentifier<T> path) {
65 ReadOnlyTransaction tx = mdSAL.getDataBroker().newReadOnlyTransaction();
69 result = tx.read(datastoreType, path).get().get();
70 } catch (Exception e) {
71 throw new RuntimeException(e);
77 private static void execPut(WriteTransaction tx, FutureCallback<Void> callback) {
78 Futures.addCallback(tx.submit(), callback);