Merge "Implement TransactionProxy#read"
[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 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
14 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
15 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
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.InstanceIdentifier;
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 {
36
37     private static final Logger
38         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
39
40
41     private final String type;
42     private final ActorContext actorContext;
43
44     public DistributedDataStore(ActorSystem actorSystem, String type) {
45         this(new ActorContext(actorSystem, actorSystem.actorOf(ShardManager.props(type))), type);
46     }
47
48     public DistributedDataStore(ActorContext actorContext, String type) {
49         this.type = type;
50         this.actorContext = actorContext;
51     }
52
53
54     @Override
55     public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
56         InstanceIdentifier path, L listener,
57         AsyncDataBroker.DataChangeScope scope) {
58
59         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(DataChangeListener.props());
60
61         Object result = actorContext.executeShardOperation(Shard.DEFAULT_NAME,
62             new RegisterChangeListener(path, dataChangeListenerActor.path(),
63                 AsyncDataBroker.DataChangeScope.BASE),
64             ActorContext.ASK_DURATION);
65
66         RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
67         return new ListenerRegistrationProxy(reply.getListenerRegistrationPath());
68     }
69
70
71
72     @Override
73     public DOMStoreTransactionChain createTransactionChain() {
74         return new TransactionChainProxy(actorContext);
75     }
76
77     @Override
78     public DOMStoreReadTransaction newReadOnlyTransaction() {
79         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
80     }
81
82     @Override
83     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
84         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
85     }
86
87     @Override
88     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
89         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
90     }
91
92     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
93         actorContext.getShardManager().tell(new UpdateSchemaContext(schemaContext), null);
94     }
95 }