Merge "Implement DataChangeListener"
[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, AutoCloseable {
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(
60             DataChangeListener.props(listener));
61
62         Object result = actorContext.executeShardOperation(Shard.DEFAULT_NAME,
63             new RegisterChangeListener(path, dataChangeListenerActor.path(),
64                 AsyncDataBroker.DataChangeScope.BASE),
65             ActorContext.ASK_DURATION
66         );
67
68         RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
69         return new DataChangeListenerRegistrationProxy(actorContext.actorSelection(reply.getListenerRegistrationPath()), listener);
70     }
71
72
73
74     @Override
75     public DOMStoreTransactionChain createTransactionChain() {
76         return new TransactionChainProxy(actorContext);
77     }
78
79     @Override
80     public DOMStoreReadTransaction newReadOnlyTransaction() {
81         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
82     }
83
84     @Override
85     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
86         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
87     }
88
89     @Override
90     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
91         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
92     }
93
94     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
95         actorContext.getShardManager().tell(
96             new UpdateSchemaContext(schemaContext), null);
97     }
98
99     @Override public void close() throws Exception {
100         actorContext.shutdown();
101
102     }
103 }