f7a51a93ffe173b755cdd5bd2d59ccb5cacb19ef
[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 {
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     public YangInstanceIdentifier getPath() {
40         return path;
41     }
42
43
44     public AsyncDataBroker.DataChangeScope getScope() {
45         return scope;
46     }
47
48     public ActorPath getDataChangeListenerPath() {
49         return dataChangeListener.path();
50     }
51
52     public boolean isRegisterOnAllInstances() {
53         return registerOnAllInstances;
54     }
55
56     @Override
57     public ListenerRegistrationMessages.RegisterChangeListener toSerializable() {
58       return ListenerRegistrationMessages.RegisterChangeListener.newBuilder()
59           .setInstanceIdentifierPath(InstanceIdentifierUtils.toSerializable(path))
60           .setDataChangeListenerActorPath(Serialization.serializedActorPath(dataChangeListener))
61           .setDataChangeScope(scope.ordinal()).setRegisterOnAllInstances(registerOnAllInstances).build();
62     }
63
64   public static RegisterChangeListener fromSerializable(ActorSystem actorSystem, Object serializable){
65     ListenerRegistrationMessages.RegisterChangeListener o = (ListenerRegistrationMessages.RegisterChangeListener) serializable;
66     return new RegisterChangeListener(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPath()),
67                                                 actorSystem.provider().resolveActorRef(o.getDataChangeListenerActorPath()),
68                                               AsyncDataBroker.DataChangeScope.values()[o.getDataChangeScope()], o.getRegisterOnAllInstances());
69   }
70
71
72 }