2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.sal.connector.remoterpc.dto;
11 import org.opendaylight.controller.sal.connector.api.RpcRouter;
15 public class Message implements Serializable {
17 public static enum MessageType {
24 private final byte type;
26 MessageType(byte type) {
30 public byte getType(){
35 private MessageType type;
36 private String sender;
37 private String recipient;
38 private RpcRouter.RouteIdentifier route;
39 private Object payload;
41 public MessageType getType() {
45 public void setType(MessageType type) {
49 public String getSender() {
53 public void setSender(String sender) {
57 public RpcRouter.RouteIdentifier getRoute() {
61 public void setRoute(RpcRouter.RouteIdentifier route) {
65 public Object getPayload() {
69 public void setPayload(Object payload) {
70 this.payload = payload;
73 public String getRecipient() {
77 public void setRecipient(String recipient) {
78 this.recipient = recipient;
82 public String toString() {
85 ", sender='" + sender + '\'' +
86 ", recipient='" + recipient + '\'' +
88 ", payload=" + payload +
93 * Converts any {@link Serializable} object to byte[]
99 public static byte[] serialize(Object obj) throws IOException {
100 ByteArrayOutputStream b = new ByteArrayOutputStream();
101 ObjectOutputStream o = new ObjectOutputStream(b);
103 return b.toByteArray();
107 * Converts byte[] to a java object
111 * @throws IOException
112 * @throws ClassNotFoundException
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();
120 public static class Response extends Message implements RpcRouter.RpcReply {
121 private ResponseCode code; // response code
123 public static enum ResponseCode {
124 SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
128 ResponseCode(int code) {
133 public ResponseCode getCode() {
137 public void setCode(ResponseCode code) {
143 * Builds a {@link Message} object
145 public static class MessageBuilder{
147 private Message message;
149 public MessageBuilder(){
150 message = new Message();
154 public MessageBuilder type(MessageType type){
155 message.setType(type);
159 public MessageBuilder sender(String sender){
160 message.setSender(sender);
164 public MessageBuilder recipient(String recipient){
165 message.setRecipient(recipient);
169 public MessageBuilder route(RpcRouter.RouteIdentifier route){
170 message.setRoute(route);
174 public MessageBuilder payload(Object obj){
175 message.setPayload(obj);
179 public Message build(){