Merge "Snapshot changes"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / example / ExampleActor.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.example;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.japi.Creator;
14 import org.opendaylight.controller.cluster.example.messages.KeyValue;
15 import org.opendaylight.controller.cluster.example.messages.KeyValueSaved;
16 import org.opendaylight.controller.cluster.example.messages.PrintRole;
17 import org.opendaylight.controller.cluster.example.messages.PrintState;
18 import org.opendaylight.controller.cluster.raft.RaftActor;
19 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /**
25  * A sample actor showing how the RaftActor is to be extended
26  */
27 public class ExampleActor extends RaftActor {
28
29     private final Map<String, String> state = new HashMap();
30
31     private long persistIdentifier = 1;
32
33
34     public ExampleActor(String id, Map<String, String> peerAddresses) {
35         super(id, peerAddresses);
36     }
37
38     public static Props props(final String id, final Map<String, String> peerAddresses){
39         return Props.create(new Creator<ExampleActor>(){
40
41             @Override public ExampleActor create() throws Exception {
42                 return new ExampleActor(id, peerAddresses);
43             }
44         });
45     }
46
47     @Override public void onReceiveCommand(Object message){
48         if(message instanceof KeyValue){
49             if(isLeader()) {
50                 String persistId = Long.toString(persistIdentifier++);
51                 persistData(getSender(), persistId, (Payload) message);
52             } else {
53                 if(getLeader() != null) {
54                     getLeader().forward(message, getContext());
55                 }
56             }
57
58         } else if (message instanceof PrintState) {
59             LOG.debug("State of the node:{} has entries={}, {}",
60                 getId(), state.size(), getReplicatedLogState());
61
62         } else if (message instanceof PrintRole) {
63             LOG.debug("{} = {}, Peers={}", getId(), getRaftState(),getPeers());
64
65         } else {
66             super.onReceiveCommand(message);
67         }
68     }
69
70     @Override protected void applyState(ActorRef clientActor, String identifier,
71         Object data) {
72         if(data instanceof KeyValue){
73             KeyValue kv = (KeyValue) data;
74             state.put(kv.getKey(), kv.getValue());
75             if(clientActor != null) {
76                 clientActor.tell(new KeyValueSaved(), getSelf());
77             }
78         }
79     }
80
81     @Override protected Object createSnapshot() {
82         return state;
83     }
84
85     @Override protected void applySnapshot(Object snapshot) {
86         state.clear();
87         state.putAll((HashMap) snapshot);
88         LOG.debug("Snapshot applied to state :" + ((HashMap) snapshot).size());
89     }
90
91     @Override public void onReceiveRecover(Object message) {
92         super.onReceiveRecover(message);
93     }
94
95     @Override public String persistenceId() {
96         return getId();
97     }
98 }