Merge "Fix checkstyle warnings in netty-event-executor-config."
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / example / messages / KeyValue.java
index 05e9ba7eabdfe7a332096e26cafc62d58a07963a..d2862c2baf219acedc921cf0710f2978f7fe85f7 100644 (file)
@@ -8,11 +8,21 @@
 
 package org.opendaylight.controller.cluster.example.messages;
 
+import com.google.protobuf.GeneratedMessage;
 import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
+import org.opendaylight.controller.protobuff.messages.cluster.example.KeyValueMessages;
+import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
 
-public class KeyValue implements Serializable{
-    private final String key;
-    private final String value;
+public class KeyValue extends Payload implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private String key;
+    private String value;
+
+    public KeyValue() {
+    }
 
     public KeyValue(String key, String value){
         this.key = key;
@@ -26,4 +36,43 @@ public class KeyValue implements Serializable{
     public String getValue() {
         return value;
     }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    @Override public String toString() {
+        return "KeyValue{" +
+            "key='" + key + '\'' +
+            ", value='" + value + '\'' +
+            '}';
+    }
+
+    // override this method to return  the protobuff related extension fields and their values
+    @Override public Map<GeneratedMessage.GeneratedExtension, String> encode() {
+        Map<GeneratedMessage.GeneratedExtension, String> map = new HashMap<>();
+        map.put(KeyValueMessages.key, getKey());
+        map.put(KeyValueMessages.value, getValue());
+        return map;
+    }
+
+    // override this method to assign the values from protobuff
+    @Override public Payload decode(
+        AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payloadProtoBuff) {
+        String key = payloadProtoBuff.getExtension(KeyValueMessages.key);
+        String value = payloadProtoBuff.getExtension(KeyValueMessages.value);
+        this.setKey(key);
+        this.setValue(value);
+        return this;
+    }
+
+    @Override
+    public int size() {
+        return this.value.length() + this.key.length();
+    }
+
 }