Merge "Fixed sal-netconf-connector readConfiguration"
[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     ANNOUNCE((byte) 0),  //TODO: Remove announce, add rpc registration and deregistration
19     HEARTBEAT((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   @Override
81   public String toString() {
82     return "Message{" +
83         "type=" + type +
84         ", sender='" + sender + '\'' +
85         ", recipient='" + recipient + '\'' +
86         ", route=" + route +
87         ", payload=" + payload +
88         '}';
89   }
90
91   /**
92    * Converts any {@link Serializable} object to byte[]
93    *
94    * @param obj
95    * @return
96    * @throws IOException
97    */
98   public static byte[] serialize(Object obj) throws IOException {
99     ByteArrayOutputStream b = new ByteArrayOutputStream();
100     ObjectOutputStream o = new ObjectOutputStream(b);
101     o.writeObject(obj);
102     return b.toByteArray();
103   }
104
105   /**
106    * Converts byte[] to a java object
107    *
108    * @param bytes
109    * @return
110    * @throws IOException
111    * @throws ClassNotFoundException
112    */
113   public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
114     ByteArrayInputStream b = new ByteArrayInputStream(bytes);
115     ObjectInputStream o = new ObjectInputStream(b);
116     return o.readObject();
117   }
118
119   public static class Response extends Message implements RpcRouter.RpcReply {
120     private ResponseCode code; // response code
121
122     public static enum ResponseCode {
123       SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
124
125       private int code;
126
127       ResponseCode(int code) {
128         this.code = code;
129       }
130     }
131
132     public ResponseCode getCode() {
133       return code;
134     }
135
136     public void setCode(ResponseCode code) {
137       this.code = code;
138     }
139   }
140
141   /**
142    * Builds a {@link Message} object
143    */
144   public static class MessageBuilder{
145
146     private Message message;
147
148     public MessageBuilder(){
149       message = new Message();
150     }
151
152
153     public MessageBuilder type(MessageType type){
154       message.setType(type);
155       return this;
156     }
157
158     public MessageBuilder sender(String sender){
159       message.setSender(sender);
160       return this;
161     }
162
163     public MessageBuilder recipient(String recipient){
164       message.setRecipient(recipient);
165       return this;
166     }
167
168     public MessageBuilder route(RpcRouter.RouteIdentifier route){
169       message.setRoute(route);
170       return this;
171     }
172
173     public MessageBuilder payload(Object obj){
174       message.setPayload(obj);
175       return this;
176     }
177
178     public Message build(){
179       return message;
180     }
181   }
182 }
183