9a251cd64586741de2ec69ca288dc21189ca027e
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / protobuff / client / messages / Payload.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.raft.protobuff.client.messages;
10
11
12 import com.google.protobuf.GeneratedMessage;
13 import org.opendaylight.controller.cluster.raft.protobuff.messages.AppendEntriesMessages;
14
15 import java.util.Map;
16
17 /**
18  * An instance of a Payload class is meant to be used as the Payload for
19  * AppendEntries.
20  * <p>
21  *
22  * When an actor which is derived from RaftActor attempts to persistData it
23  * must pass an instance of the Payload class. Similarly when state needs to
24  * be applied to the derived RaftActor it will be passed an instance of the
25  * Payload class.
26  * <p>
27  *
28  * To define your own payload do the following,
29  * <ol>
30  *     <li>Create your own protocol buffer message which extends the AppendEntries Payload</li>
31  *     <li>Extend this Payload class</li>
32  *     <li>Implement encode</li>
33  *     <li>Implement decode</li>
34  * </ol>
35  *
36  * Your own protocol buffer message can be create like so, <br/>
37  * <pre>
38  * {@code
39  *
40  * import "AppendEntriesMessages.proto";
41  *
42  * package org.opendaylight.controller.cluster.raft;
43  *
44  * option java_package = "org.opendaylight.controller.cluster.raft.protobuff.messages";
45  * option java_outer_classname = "MockPayloadMessages";
46  *
47  * extend AppendEntries.ReplicatedLogEntry.Payload {
48  *      optional string value = 2;
49  * }
50  * }
51  * </pre>
52  *
53  */
54 public abstract class Payload {
55     private String clientPayloadClassName;
56
57     public String getClientPayloadClassName() {
58         return this.getClass().getName();
59     }
60
61     public void setClientPayloadClassName(String clientPayloadClassName) {
62         this.clientPayloadClassName = clientPayloadClassName;
63     }
64
65     /**
66      * Encode the payload data as a protocol buffer extension.
67      * <p>
68      * TODO: Add more meat in here
69      * @param <T>
70      * @return Map of <GeneratedMessage.GeneratedExtension, T>
71      */
72     public abstract <T extends Object> Map<GeneratedMessage.GeneratedExtension, T> encode();
73
74     /**
75      * Decode the protocol buffer payload into a specific Payload as defined
76      * by the class extending RaftActor
77      *
78      * @param payload The payload in protocol buffer format
79      * @return
80      */
81     public abstract Payload decode(
82         AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payload);
83
84
85
86 }