Merge "Refactor snapshot code"
[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
28
29     public RegisterChangeListener(YangInstanceIdentifier path,
30         ActorRef dataChangeListener,
31         AsyncDataBroker.DataChangeScope scope) {
32         this.path = path;
33         this.dataChangeListener = dataChangeListener;
34         this.scope = scope;
35     }
36
37     public YangInstanceIdentifier getPath() {
38         return path;
39     }
40
41
42     public AsyncDataBroker.DataChangeScope getScope() {
43         return scope;
44     }
45
46     public ActorPath getDataChangeListenerPath() {
47         return dataChangeListener.path();
48     }
49
50
51     @Override
52     public ListenerRegistrationMessages.RegisterChangeListener toSerializable() {
53       return ListenerRegistrationMessages.RegisterChangeListener.newBuilder()
54           .setInstanceIdentifierPath(InstanceIdentifierUtils.toSerializable(path))
55           .setDataChangeListenerActorPath(Serialization.serializedActorPath(dataChangeListener))
56           .setDataChangeScope(scope.ordinal()).build();
57     }
58
59   public static RegisterChangeListener fromSerializable(ActorSystem actorSystem, Object serializable){
60     ListenerRegistrationMessages.RegisterChangeListener o = (ListenerRegistrationMessages.RegisterChangeListener) serializable;
61     return new RegisterChangeListener(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPath()),
62                                                 actorSystem.provider().resolveActorRef(o.getDataChangeListenerActorPath()),
63                                               AsyncDataBroker.DataChangeScope.values()[o.getDataChangeScope()]);
64   }
65
66
67 }