500f7d562bd85ca993528cf28f15adeea2d92bca
[controller.git] / opendaylight / md-sal / sal-zeromq-connector / src / main / java / org / opendaylight / controller / sal / connector / remoterpc / router / zeromq / Message.java
1 package org.opendaylight.controller.sal.connector.remoterpc.router.zeromq;
2
3
4 import org.codehaus.jackson.map.ObjectMapper;
5 import org.opendaylight.controller.sal.connector.api.RpcRouter;
6
7 import java.io.*;
8
9 public class Message implements Serializable {
10
11  public static enum MessageType {
12     ANNOUNCE((byte) 0),  //TODO: Remove announce, add rpc registration and deregistration
13     HEARTBEAT((byte) 1),
14     REQUEST((byte) 2),
15     RESPONSE((byte) 3);
16
17     private final byte type;
18
19     MessageType(byte type) {
20       this.type = type;
21     }
22
23     public byte getType(){
24       return this.type;
25     }
26   }
27
28   private MessageType type;
29   private String sender;
30   private RpcRouter.RouteIdentifier route;
31   private Object payload;
32
33   public MessageType getType() {
34     return type;
35   }
36
37   public void setType(MessageType type) {
38     this.type = type;
39   }
40
41   public String getSender() {
42     return sender;
43   }
44
45   public void setSender(String sender) {
46     this.sender = sender;
47   }
48
49   public RpcRouter.RouteIdentifier getRoute() {
50     return route;
51   }
52
53   public void setRoute(RpcRouter.RouteIdentifier route) {
54     this.route = route;
55   }
56
57   public Object getPayload() {
58     return payload;
59   }
60
61   public void setPayload(Object payload) {
62     this.payload = payload;
63   }
64
65   @Override
66   public String toString() {
67     return "Message{" +
68         "type=" + type +
69         ", sender='" + sender + '\'' +
70         ", route=" + route +
71         ", payload=" + payload +
72         '}';
73   }
74
75   /**
76    * Converts any {@link Serializable} object to byte[]
77    *
78    * @param obj
79    * @return
80    * @throws IOException
81    */
82   public static byte[] serialize(Object obj) throws IOException {
83     ByteArrayOutputStream b = new ByteArrayOutputStream();
84     ObjectOutputStream o = new ObjectOutputStream(b);
85     o.writeObject(obj);
86     return b.toByteArray();
87   }
88
89   /**
90    * Converts byte[] to a java object
91    *
92    * @param bytes
93    * @return
94    * @throws IOException
95    * @throws ClassNotFoundException
96    */
97   public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
98     ByteArrayInputStream b = new ByteArrayInputStream(bytes);
99     ObjectInputStream o = new ObjectInputStream(b);
100     return o.readObject();
101   }
102
103   public static byte[] toJsonBytes(Message m) throws IOException {
104     ObjectMapper o = new ObjectMapper();
105     return o.writeValueAsBytes(m);
106   }
107
108   public static Message fromJsonBytes(byte [] bytes) throws IOException {
109
110     ObjectMapper o = new ObjectMapper();
111     return o.readValue(bytes, Message.class);
112   }
113
114   public static class Response extends Message implements RpcRouter.RpcReply {
115     private ResponseCode code; // response code
116
117     public static enum ResponseCode {
118       SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
119
120       private int code;
121
122       ResponseCode(int code) {
123         this.code = code;
124       }
125     }
126
127     public ResponseCode getCode() {
128       return code;
129     }
130
131     public void setCode(ResponseCode code) {
132       this.code = code;
133     }
134   }
135
136   /**
137    * Builds a {@link Message} object
138    */
139   public static class MessageBuilder{
140
141     private Message message;
142
143     public MessageBuilder(){
144       message = new Message();
145     }
146
147
148     public MessageBuilder type(MessageType type){
149       message.setType(type);
150       return this;
151     }
152
153     public MessageBuilder sender(String sender){
154       message.setSender(sender);
155       return this;
156     }
157
158     public MessageBuilder route(RpcRouter.RouteIdentifier route){
159       message.setRoute(route);
160       return this;
161     }
162
163     public MessageBuilder payload(Object obj){
164       message.setPayload(obj);
165       return this;
166     }
167
168     public Message build(){
169       return message;
170     }
171   }
172 }
173