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