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.ActorRef;
12 import akka.actor.ActorSystem;
14 import com.google.common.base.Preconditions;
15 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
17 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
18 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
19 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
23 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
44 private final ActorContext actorContext;
46 private SchemaContext schemaContext;
48 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
49 Configuration configuration, InMemoryDOMDataStoreConfigProperties dataStoreProperties) {
50 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
51 Preconditions.checkNotNull(type, "type should not be null");
52 Preconditions.checkNotNull(cluster, "cluster should not be null");
53 Preconditions.checkNotNull(configuration, "configuration should not be null");
56 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
58 LOG.info("Creating ShardManager : {}", shardManagerId);
60 this.actorContext = new ActorContext(actorSystem, actorSystem
61 .actorOf(ShardManager.props(type, cluster, configuration, dataStoreProperties),
62 shardManagerId ), cluster, configuration);
65 public DistributedDataStore(ActorContext actorContext) {
66 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
70 @SuppressWarnings("unchecked")
72 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
73 ListenerRegistration<L> registerChangeListener(
74 YangInstanceIdentifier path, L listener,
75 AsyncDataBroker.DataChangeScope scope) {
77 Preconditions.checkNotNull(path, "path should not be null");
78 Preconditions.checkNotNull(listener, "listener should not be null");
80 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
82 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
83 DataChangeListener.props(schemaContext,listener,path ));
85 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
87 Object result = actorContext.executeLocalShardOperation(shardName,
88 new RegisterChangeListener(path, dataChangeListenerActor.path(), scope),
89 ActorContext.ASK_DURATION);
92 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
93 return new DataChangeListenerRegistrationProxy(actorContext
94 .actorSelection(reply.getListenerRegistrationPath()), listener,
95 dataChangeListenerActor);
99 "No local shard for shardName {} was found so returning a noop registration",
102 return new NoOpDataChangeListenerRegistration(listener);
106 public DOMStoreTransactionChain createTransactionChain() {
107 return new TransactionChainProxy(actorContext, schemaContext);
111 public DOMStoreReadTransaction newReadOnlyTransaction() {
112 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
117 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
118 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
123 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
124 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
128 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
129 this.schemaContext = schemaContext;
130 actorContext.getShardManager().tell(
131 new UpdateSchemaContext(schemaContext), null);
134 @Override public void close() throws Exception {
135 actorContext.shutdown();