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 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
20 import java.util.HashMap;
21 import java.util.Map;
22
23 /**
24  * A sample actor showing how the RaftActor is to be extended
25  */
26 public class ExampleActor extends RaftActor {
27
28     private final Map<String, String> state = new HashMap();
29
30     private long persistIdentifier = 1;
31
32
33     public ExampleActor(String id, Map<String, String> peerAddresses) {
34         super(id, peerAddresses);
35     }
36
37     public static Props props(final String id, final Map<String, String> peerAddresses){
38         return Props.create(new Creator<ExampleActor>(){
39
40             @Override public ExampleActor create() throws Exception {
41                 return new ExampleActor(id, peerAddresses);
42             }
43         });
44     }
45
46     @Override public void onReceiveCommand(Object message){
47         if(message instanceof KeyValue){
48
49             if(isLeader()) {
50                 String persistId = Long.toString(persistIdentifier++);
51                 persistData(getSender(), persistId, 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:"+getId() + " is="+state.size());
60
61         } else if (message instanceof PrintRole) {
62             LOG.debug(getId() + " = " + getRaftState());
63         }
64         super.onReceiveCommand(message);
65     }
66
67     @Override protected void applyState(ActorRef clientActor, String identifier,
68         Object data) {
69         if(data instanceof KeyValue){
70             KeyValue kv = (KeyValue) data;
71             state.put(kv.getKey(), kv.getValue());
72             if(clientActor != null) {
73                 clientActor.tell(new KeyValueSaved(), getSelf());
74             }
75         }
76     }
77
78     @Override protected Object createSnapshot() {
79         return state;
80     }
81
82     @Override protected void applySnapshot(Object snapshot) {
83         state.clear();
84         state.putAll((HashMap) snapshot);
85     }
86
87     @Override public void onReceiveRecover(Object message) {
88         super.onReceiveRecover(message);
89     }
90
91     @Override public String persistenceId() {
92         return getId();
93     }
94 }