Add type of transaction being created to log message
[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.ActorRef;
12 import akka.actor.ActorSystem;
13
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;
36
37 /**
38  *
39  */
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43
44     private final ActorContext actorContext;
45
46     private SchemaContext schemaContext;
47
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");
54
55
56         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
57
58         LOG.info("Creating ShardManager : {}", shardManagerId);
59
60         this.actorContext = new ActorContext(actorSystem, actorSystem
61             .actorOf(ShardManager.props(type, cluster, configuration, dataStoreProperties),
62                 shardManagerId ), cluster, configuration);
63     }
64
65     public DistributedDataStore(ActorContext actorContext) {
66         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
67     }
68
69
70     @SuppressWarnings("unchecked")
71     @Override
72     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
73                                               ListenerRegistration<L> registerChangeListener(
74         YangInstanceIdentifier path, L listener,
75         AsyncDataBroker.DataChangeScope scope) {
76
77         Preconditions.checkNotNull(path, "path should not be null");
78         Preconditions.checkNotNull(listener, "listener should not be null");
79
80         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
81
82         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
83             DataChangeListener.props(schemaContext,listener,path ));
84
85         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
86
87         Object result = actorContext.executeLocalShardOperation(shardName,
88             new RegisterChangeListener(path, dataChangeListenerActor.path(), scope),
89             ActorContext.ASK_DURATION);
90
91         if (result != null) {
92             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
93             return new DataChangeListenerRegistrationProxy(actorContext
94                 .actorSelection(reply.getListenerRegistrationPath()), listener,
95                 dataChangeListenerActor);
96         }
97
98         LOG.debug(
99             "No local shard for shardName {} was found so returning a noop registration",
100             shardName);
101
102         return new NoOpDataChangeListenerRegistration(listener);
103     }
104
105     @Override
106     public DOMStoreTransactionChain createTransactionChain() {
107         return new TransactionChainProxy(actorContext, schemaContext);
108     }
109
110     @Override
111     public DOMStoreReadTransaction newReadOnlyTransaction() {
112         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
113             schemaContext);
114     }
115
116     @Override
117     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
118         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
119             schemaContext);
120     }
121
122     @Override
123     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
124         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
125             schemaContext);
126     }
127
128     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
129         this.schemaContext = schemaContext;
130         actorContext.getShardManager().tell(
131             new UpdateSchemaContext(schemaContext), null);
132     }
133
134     @Override public void close() throws Exception {
135         actorContext.shutdown();
136
137     }
138 }