Introducing the Modification classses
[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 java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.io.Serializable;
17
18 import org.opendaylight.controller.sal.connector.api.RpcRouter;
19
20 public class Message implements Serializable {
21     private static final long serialVersionUID = 1L;
22
23  public static enum MessageType {
24     PING((byte) 0),
25     PONG((byte) 1),
26     REQUEST((byte) 2),
27     RESPONSE((byte) 3),
28     ERROR((byte)4);
29
30     private final byte type;
31
32     MessageType(byte type) {
33       this.type = type;
34     }
35
36     public byte getType(){
37       return this.type;
38     }
39   }
40
41   private MessageType type;
42   private String sender;
43   private String recipient;
44   private RpcRouter.RouteIdentifier<?, ?, ?> route;
45   private Object payload;
46
47   public MessageType getType() {
48     return type;
49   }
50
51   public void setType(MessageType type) {
52     this.type = type;
53   }
54
55   public String getSender() {
56     return sender;
57   }
58
59   public void setSender(String sender) {
60     this.sender = sender;
61   }
62
63   public RpcRouter.RouteIdentifier<?, ?, ?> getRoute() {
64     return route;
65   }
66
67   public void setRoute(RpcRouter.RouteIdentifier<?, ?, ?> route) {
68     this.route = route;
69   }
70
71   public Object getPayload() {
72     return payload;
73   }
74
75   public void setPayload(Object payload) {
76     this.payload = payload;
77   }
78
79   public String getRecipient() {
80     return recipient;
81   }
82
83   public void setRecipient(String recipient) {
84     this.recipient = recipient;
85   }
86
87   @Override
88   public String toString() {
89     return "Message{" +
90         "type=" + type +
91         ", sender='" + sender + '\'' +
92         ", recipient='" + recipient + '\'' +
93         ", route=" + route +
94         ", payload=" + payload +
95         '}';
96   }
97
98   /**
99    * Converts any {@link Serializable} object to byte[]
100    *
101    * @param obj
102    * @return
103    * @throws IOException
104    */
105   public static byte[] serialize(Object obj) throws IOException {
106     ByteArrayOutputStream b = new ByteArrayOutputStream();
107     ObjectOutputStream o = new ObjectOutputStream(b);
108     o.writeObject(obj);
109     return b.toByteArray();
110   }
111
112   /**
113    * Converts byte[] to a java object
114    *
115    * @param bytes
116    * @return
117    * @throws IOException
118    * @throws ClassNotFoundException
119    */
120   public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
121     ByteArrayInputStream b = new ByteArrayInputStream(bytes);
122     ObjectInputStream o = new ObjectInputStream(b);
123     return o.readObject();
124   }
125
126   public static class Response extends Message implements RpcRouter.RpcReply<Object> {
127     private static final long serialVersionUID = 1L;
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 final 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    * Builds a {@link Message} object
151    */
152   public static class MessageBuilder{
153
154     private final Message message;
155
156     public MessageBuilder(){
157       message = new Message();
158     }
159
160
161     public MessageBuilder type(MessageType type){
162       message.setType(type);
163       return this;
164     }
165
166     public MessageBuilder sender(String sender){
167       message.setSender(sender);
168       return this;
169     }
170
171     public MessageBuilder recipient(String recipient){
172       message.setRecipient(recipient);
173       return this;
174     }
175
176     public MessageBuilder route(RpcRouter.RouteIdentifier<?, ?, ?> route){
177       message.setRoute(route);
178       return this;
179     }
180
181     public MessageBuilder payload(Object obj){
182       message.setPayload(obj);
183       return this;
184     }
185
186     public Message build(){
187       return message;
188     }
189   }
190 }
191