Bug 2486: Get testAbortBeforeFinishCommit working again
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStore.java
1 /*
2  * Copyright (c) 2014 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.cluster.datastore;
10
11 import akka.actor.ActorSystem;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
15 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  *
34  */
35 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
38     public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
39
40     private final ActorContext actorContext;
41
42     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
43             Configuration configuration, DatastoreContext datastoreContext) {
44         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
45         Preconditions.checkNotNull(type, "type should not be null");
46         Preconditions.checkNotNull(cluster, "cluster should not be null");
47         Preconditions.checkNotNull(configuration, "configuration should not be null");
48         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
49
50         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
51
52         LOG.info("Creating ShardManager : {}", shardManagerId);
53
54         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
55                 ShardManager.props(type, cluster, configuration, datastoreContext)
56                     .withMailbox(ActorContext.MAILBOX), shardManagerId ),
57                 cluster, configuration, datastoreContext);
58     }
59
60     public DistributedDataStore(ActorContext actorContext) {
61         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
62     }
63
64     @SuppressWarnings("unchecked")
65     @Override
66     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
67                                               ListenerRegistration<L> registerChangeListener(
68         final YangInstanceIdentifier path, L listener,
69         AsyncDataBroker.DataChangeScope scope) {
70
71         Preconditions.checkNotNull(path, "path should not be null");
72         Preconditions.checkNotNull(listener, "listener should not be null");
73
74         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
75
76         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
77
78         final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
79                 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
80         listenerRegistrationProxy.init(path, scope);
81
82         return listenerRegistrationProxy;
83     }
84
85     @Override
86     public DOMStoreTransactionChain createTransactionChain() {
87         return new TransactionChainProxy(actorContext);
88     }
89
90     @Override
91     public DOMStoreReadTransaction newReadOnlyTransaction() {
92         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
93     }
94
95     @Override
96     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
97         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
98     }
99
100     @Override
101     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
102         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
103     }
104
105     @Override
106     public void onGlobalContextUpdated(SchemaContext schemaContext) {
107         actorContext.setSchemaContext(schemaContext);
108     }
109
110     @Override
111     public void close() throws Exception {
112         actorContext.shutdown();
113     }
114
115     @VisibleForTesting
116     ActorContext getActorContext() {
117         return actorContext;
118     }
119 }