Bug 4651: Implement handling of ClusteredDOMDataTreeChangeListener in CDS
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / RegisterChangeListener.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.messages;
10
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.serialization.Serialization;
15 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.protobuff.messages.registration.ListenerRegistrationMessages;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19
20 public class RegisterChangeListener implements SerializableMessage, ListenerRegistrationMessage {
21     public static final Class<ListenerRegistrationMessages.RegisterChangeListener> SERIALIZABLE_CLASS =
22             ListenerRegistrationMessages.RegisterChangeListener.class;
23
24     private final YangInstanceIdentifier path;
25     private final ActorRef dataChangeListener;
26     private final AsyncDataBroker.DataChangeScope scope;
27     private final boolean registerOnAllInstances;
28
29
30     public RegisterChangeListener(YangInstanceIdentifier path,
31         ActorRef dataChangeListener,
32         AsyncDataBroker.DataChangeScope scope, boolean registerOnAllInstances) {
33         this.path = path;
34         this.dataChangeListener = dataChangeListener;
35         this.scope = scope;
36         this.registerOnAllInstances = registerOnAllInstances;
37     }
38
39     @Override
40     public YangInstanceIdentifier getPath() {
41         return path;
42     }
43
44
45     public AsyncDataBroker.DataChangeScope getScope() {
46         return scope;
47     }
48
49     public ActorPath getDataChangeListenerPath() {
50         return dataChangeListener.path();
51     }
52
53     @Override
54     public boolean isRegisterOnAllInstances() {
55         return registerOnAllInstances;
56     }
57
58     @Override
59     public ListenerRegistrationMessages.RegisterChangeListener toSerializable() {
60       return ListenerRegistrationMessages.RegisterChangeListener.newBuilder()
61           .setInstanceIdentifierPath(InstanceIdentifierUtils.toSerializable(path))
62           .setDataChangeListenerActorPath(Serialization.serializedActorPath(dataChangeListener))
63           .setDataChangeScope(scope.ordinal()).setRegisterOnAllInstances(registerOnAllInstances).build();
64     }
65
66   public static RegisterChangeListener fromSerializable(ActorSystem actorSystem, Object serializable){
67     ListenerRegistrationMessages.RegisterChangeListener o = (ListenerRegistrationMessages.RegisterChangeListener) serializable;
68     return new RegisterChangeListener(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPath()),
69                                                 actorSystem.provider().resolveActorRef(o.getDataChangeListenerActorPath()),
70                                               AsyncDataBroker.DataChangeScope.values()[o.getDataChangeScope()], o.getRegisterOnAllInstances());
71   }
72
73
74 }