2 * Copyright (c) 2014 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.cluster.datastore;
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;
35 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
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
40 private final ActorContext actorContext;
42 public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
43 Configuration configuration, DatastoreContext datastoreContext) {
44 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
45 Preconditions.checkNotNull(cluster, "cluster should not be null");
46 Preconditions.checkNotNull(configuration, "configuration should not be null");
47 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
49 String type = datastoreContext.getDataStoreType();
51 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
53 LOG.info("Creating ShardManager : {}", shardManagerId);
55 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
56 ShardManager.props(cluster, configuration, datastoreContext)
57 .withMailbox(ActorContext.MAILBOX), shardManagerId ),
58 cluster, configuration, datastoreContext);
61 public DistributedDataStore(ActorContext actorContext) {
62 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
65 @SuppressWarnings("unchecked")
67 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
68 ListenerRegistration<L> registerChangeListener(
69 final YangInstanceIdentifier path, L listener,
70 AsyncDataBroker.DataChangeScope scope) {
72 Preconditions.checkNotNull(path, "path should not be null");
73 Preconditions.checkNotNull(listener, "listener should not be null");
75 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
77 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
79 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
80 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
81 listenerRegistrationProxy.init(path, scope);
83 return listenerRegistrationProxy;
87 public DOMStoreTransactionChain createTransactionChain() {
88 return new TransactionChainProxy(actorContext);
92 public DOMStoreReadTransaction newReadOnlyTransaction() {
93 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
97 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
98 actorContext.acquireTxCreationPermit();
99 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
103 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
104 actorContext.acquireTxCreationPermit();
105 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
109 public void onGlobalContextUpdated(SchemaContext schemaContext) {
110 actorContext.setSchemaContext(schemaContext);
114 public void close() throws Exception {
115 actorContext.shutdown();
119 ActorContext getActorContext() {