73622a072c3bebcd65ea758e28db4ce195b4d90a
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / ClientBackedDataStore.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.databroker;
9
10 import akka.actor.ActorSystem;
11 import com.google.common.annotations.VisibleForTesting;
12 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
13 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
14 import org.opendaylight.controller.cluster.datastore.AbstractDataStore;
15 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
16 import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory;
17 import org.opendaylight.controller.cluster.datastore.config.Configuration;
18 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
24
25 /**
26  * Implements a distributed DOMStore using ClientActor.
27  */
28 public class ClientBackedDataStore extends AbstractDataStore {
29
30     public ClientBackedDataStore(final ActorSystem actorSystem, final ClusterWrapper cluster,
31             final Configuration configuration, final DatastoreContextFactory datastoreContextFactory,
32             final DatastoreSnapshot restoreFromSnapshot) {
33         super(actorSystem, cluster, configuration, datastoreContextFactory, restoreFromSnapshot);
34     }
35
36     @VisibleForTesting
37     ClientBackedDataStore(final ActorContext actorContext, final ClientIdentifier identifier,
38                           final DataStoreClient clientActor) {
39         super(actorContext, identifier, clientActor);
40     }
41
42     @Override
43     public DOMStoreTransactionChain createTransactionChain() {
44         return new ClientBackedTransactionChain(getClient().createLocalHistory(), debugAllocation());
45     }
46
47     @Override
48     public DOMStoreReadTransaction newReadOnlyTransaction() {
49         return new ClientBackedReadTransaction(getClient().createSnapshot(), null, allocationContext());
50     }
51
52     @Override
53     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
54         return new ClientBackedWriteTransaction(getClient().createTransaction(), allocationContext());
55     }
56
57     @Override
58     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
59         return new ClientBackedReadWriteTransaction(getClient().createTransaction(), allocationContext());
60     }
61
62     private boolean debugAllocation() {
63         return getActorContext().getDatastoreContext().isTransactionDebugContextEnabled();
64     }
65
66     private Throwable allocationContext() {
67         return debugAllocation() ? new Throwable("allocated at") : null;
68     }
69 }