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