BUG-972: correct Precondition
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / main / java / org / opendaylight / controller / sal / connector / remoterpc / dto / 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.dto;
10
11 import org.opendaylight.controller.sal.connector.api.RpcRouter;
12
13 import java.io.*;
14
15 public class Message implements Serializable {
16
17  public static enum MessageType {
18     PING((byte) 0),
19     PONG((byte) 1),
20     REQUEST((byte) 2),
21     RESPONSE((byte) 3),
22     ERROR((byte)4);
23
24     private final byte type;
25
26     MessageType(byte type) {
27       this.type = type;
28     }
29
30     public byte getType(){
31       return this.type;
32     }
33   }
34
35   private MessageType type;
36   private String sender;
37   private String recipient;
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   public String getRecipient() {
74     return recipient;
75   }
76
77   public void setRecipient(String recipient) {
78     this.recipient = recipient;
79   }
80
81   @Override
82   public String toString() {
83     return "Message{" +
84         "type=" + type +
85         ", sender='" + sender + '\'' +
86         ", recipient='" + recipient + '\'' +
87         ", route=" + route +
88         ", payload=" + payload +
89         '}';
90   }
91
92   /**
93    * Converts any {@link Serializable} object to byte[]
94    *
95    * @param obj
96    * @return
97    * @throws IOException
98    */
99   public static byte[] serialize(Object obj) throws IOException {
100     ByteArrayOutputStream b = new ByteArrayOutputStream();
101     ObjectOutputStream o = new ObjectOutputStream(b);
102     o.writeObject(obj);
103     return b.toByteArray();
104   }
105
106   /**
107    * Converts byte[] to a java object
108    *
109    * @param bytes
110    * @return
111    * @throws IOException
112    * @throws ClassNotFoundException
113    */
114   public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
115     ByteArrayInputStream b = new ByteArrayInputStream(bytes);
116     ObjectInputStream o = new ObjectInputStream(b);
117     return o.readObject();
118   }
119
120   public static class Response extends Message implements RpcRouter.RpcReply {
121     private ResponseCode code; // response code
122
123     public static enum ResponseCode {
124       SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
125
126       private int code;
127
128       ResponseCode(int code) {
129         this.code = code;
130       }
131     }
132
133     public ResponseCode getCode() {
134       return code;
135     }
136
137     public void setCode(ResponseCode code) {
138       this.code = code;
139     }
140   }
141
142   /**
143    * Builds a {@link Message} object
144    */
145   public static class MessageBuilder{
146
147     private Message message;
148
149     public MessageBuilder(){
150       message = new Message();
151     }
152
153
154     public MessageBuilder type(MessageType type){
155       message.setType(type);
156       return this;
157     }
158
159     public MessageBuilder sender(String sender){
160       message.setSender(sender);
161       return this;
162     }
163
164     public MessageBuilder recipient(String recipient){
165       message.setRecipient(recipient);
166       return this;
167     }
168
169     public MessageBuilder route(RpcRouter.RouteIdentifier route){
170       message.setRoute(route);
171       return this;
172     }
173
174     public MessageBuilder payload(Object obj){
175       message.setPayload(obj);
176       return this;
177     }
178
179     public Message build(){
180       return message;
181     }
182   }
183 }
184