Split out TransactionContext classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerRegistration.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.PoisonPill;
12 import akka.actor.Props;
13 import akka.japi.Creator;
14 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
15
16 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistrationReply;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 public class DataChangeListenerRegistration extends AbstractUntypedActor {
24
25     private final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
26         registration;
27
28     public DataChangeListenerRegistration(
29         ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> registration) {
30         this.registration = registration;
31     }
32
33     @Override
34     public void handleReceive(Object message) throws Exception {
35         if (message.getClass().equals(CloseDataChangeListenerRegistration.SERIALIZABLE_CLASS)) {
36             closeListenerRegistration(
37                 new CloseDataChangeListenerRegistration());
38         }
39     }
40
41     public static Props props(
42         final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> registration) {
43         return Props.create(new DataChangeListenerRegistrationCreator(registration));
44     }
45
46     private void closeListenerRegistration(
47         CloseDataChangeListenerRegistration message) {
48         registration.close();
49         getSender()
50             .tell(new CloseDataChangeListenerRegistrationReply().toSerializable(), getSelf());
51         getSelf().tell(PoisonPill.getInstance(), getSelf());
52     }
53
54     private static class DataChangeListenerRegistrationCreator
55                                             implements Creator<DataChangeListenerRegistration> {
56         private static final long serialVersionUID = 1L;
57         final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
58                                                            NormalizedNode<?, ?>>> registration;
59
60         DataChangeListenerRegistrationCreator(
61                 ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
62                                                              NormalizedNode<?, ?>>> registration) {
63             this.registration = registration;
64         }
65
66         @Override
67         public DataChangeListenerRegistration create() throws Exception {
68             return new DataChangeListenerRegistration(registration);
69         }
70     }
71 }