Make Raft messages serializable
[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.ActorSystem;
13 import org.opendaylight.controller.cluster.datastore.utils.InstanceIdentifierUtils;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
15 import org.opendaylight.controller.protobuff.messages.registration.ListenerRegistrationMessages;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17
18 public class RegisterChangeListener implements SerializableMessage {
19   public static final Class SERIALIZABLE_CLASS = ListenerRegistrationMessages.RegisterChangeListener.class;
20     private final InstanceIdentifier path;
21     private final ActorPath dataChangeListenerPath;
22     private final AsyncDataBroker.DataChangeScope scope;
23
24
25     public RegisterChangeListener(InstanceIdentifier path,
26         ActorPath dataChangeListenerPath,
27         AsyncDataBroker.DataChangeScope scope) {
28         this.path = path;
29         this.dataChangeListenerPath = dataChangeListenerPath;
30         this.scope = scope;
31     }
32
33     public InstanceIdentifier getPath() {
34         return path;
35     }
36
37
38     public AsyncDataBroker.DataChangeScope getScope() {
39         return scope;
40     }
41
42     public ActorPath getDataChangeListenerPath() {
43         return dataChangeListenerPath;
44     }
45
46
47     @Override
48     public ListenerRegistrationMessages.RegisterChangeListener toSerializable() {
49       return ListenerRegistrationMessages.RegisterChangeListener.newBuilder()
50           .setInstanceIdentifierPath(path.toString())
51           .setDataChangeListenerActorPath(dataChangeListenerPath.toString())
52           .setDataChangeScope(scope.ordinal()).build();
53     }
54
55   public static RegisterChangeListener fromSerializable(ActorSystem actorSystem,Object serializable){
56     ListenerRegistrationMessages.RegisterChangeListener o = (ListenerRegistrationMessages.RegisterChangeListener) serializable;
57     return new RegisterChangeListener(InstanceIdentifierUtils.from(o.getInstanceIdentifierPath()),
58                                                 actorSystem.actorFor(o.getDataChangeListenerActorPath()).path(),
59                                               AsyncDataBroker.DataChangeScope.values()[o.getDataChangeScope()]);
60   }
61
62
63 }