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