Updated ZeroMQ implementation
[controller.git] / opendaylight / md-sal / sal-zeromq-connector / src / main / java / org / opendaylight / controller / sal / connector / zeromq / Message.java
1 package org.opendaylight.controller.sal.connector.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 import java.util.Arrays;
9
10 public class Message implements Serializable {
11
12  public enum MessageType {
13     ANNOUNCE((byte) 0),
14     HEARTBEAT((byte) 1),
15     REQUEST((byte) 2),
16     RESPONSE((byte) 3);
17
18     private final byte type;
19
20     MessageType(byte type) {
21       this.type = type;
22     }
23
24     public byte getType(){
25       return this.type;
26     }
27   }
28
29   private MessageType type;
30   private String sender;
31   private RpcRouter.RouteIdentifier route;
32   private Object payload;
33
34   public MessageType getType() {
35     return type;
36   }
37
38   public void setType(MessageType type) {
39     this.type = type;
40   }
41
42   public String getSender() {
43     return sender;
44   }
45
46   public void setSender(String sender) {
47     this.sender = sender;
48   }
49
50   public RpcRouter.RouteIdentifier getRoute() {
51     return route;
52   }
53
54   public void setRoute(RpcRouter.RouteIdentifier route) {
55     this.route = route;
56   }
57
58   public Object getPayload() {
59     return payload;
60   }
61
62   public void setPayload(Object payload) {
63     this.payload = payload;
64   }
65
66   @Override
67   public String toString() {
68     return "Message{" +
69         "type=" + type +
70         ", sender='" + sender + '\'' +
71         ", route=" + route +
72         ", payload=" + payload +
73         '}';
74   }
75
76   /**
77    * Converts any {@link Serializable} object to byte[]
78    *
79    * @param obj
80    * @return
81    * @throws IOException
82    */
83   public static byte[] serialize(Object obj) throws IOException {
84     ByteArrayOutputStream b = new ByteArrayOutputStream();
85     ObjectOutputStream o = new ObjectOutputStream(b);
86     o.writeObject(obj);
87     return b.toByteArray();
88   }
89
90   /**
91    * Converts byte[] to a java object
92    *
93    * @param bytes
94    * @return
95    * @throws IOException
96    * @throws ClassNotFoundException
97    */
98   public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
99     ByteArrayInputStream b = new ByteArrayInputStream(bytes);
100     ObjectInputStream o = new ObjectInputStream(b);
101     return o.readObject();
102   }
103
104   public static byte[] toJsonBytes(Message m){
105     ObjectMapper o = new ObjectMapper();
106     try {
107       System.out.println(o.writeValueAsString(m));
108       return o.writeValueAsBytes(m);
109     } catch (IOException e) {
110       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
111     }
112     return null;
113   }
114
115   public static Message fromJsonBytes(byte [] bytes){
116
117     ObjectMapper o = new ObjectMapper();
118     Message m = null;
119     try {
120       m = o.readValue(bytes, Message.class);
121     } catch (IOException e) {
122       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
123     }
124     return m;
125   }
126
127   public static class Response extends Message implements RpcRouter.RpcReply {
128     private ResponseCode code; // response code
129
130     public static enum ResponseCode {
131       SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
132
133       private int code;
134
135       ResponseCode(int code) {
136         this.code = code;
137       }
138     }
139
140     public ResponseCode getCode() {
141       return code;
142     }
143
144     public void setCode(ResponseCode code) {
145       this.code = code;
146     }
147   }
148
149 }
150