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 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;
18 import org.opendaylight.controller.sal.connector.api.RpcRouter;
20 public class Message implements Serializable {
21 private static final long serialVersionUID = 1L;
23 public static enum MessageType {
30 private final byte type;
32 MessageType(byte type) {
36 public byte getType(){
41 private MessageType type;
42 private String sender;
43 private String recipient;
44 private RpcRouter.RouteIdentifier route;
45 private Object payload;
47 public MessageType getType() {
51 public void setType(MessageType type) {
55 public String getSender() {
59 public void setSender(String sender) {
63 public RpcRouter.RouteIdentifier getRoute() {
67 public void setRoute(RpcRouter.RouteIdentifier route) {
71 public Object getPayload() {
75 public void setPayload(Object payload) {
76 this.payload = payload;
79 public String getRecipient() {
83 public void setRecipient(String recipient) {
84 this.recipient = recipient;
88 public String toString() {
91 ", sender='" + sender + '\'' +
92 ", recipient='" + recipient + '\'' +
94 ", payload=" + payload +
99 * Converts any {@link Serializable} object to byte[]
103 * @throws IOException
105 public static byte[] serialize(Object obj) throws IOException {
106 ByteArrayOutputStream b = new ByteArrayOutputStream();
107 ObjectOutputStream o = new ObjectOutputStream(b);
109 return b.toByteArray();
113 * Converts byte[] to a java object
117 * @throws IOException
118 * @throws ClassNotFoundException
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();
126 public static class Response extends Message implements RpcRouter.RpcReply {
127 private static final long serialVersionUID = 1L;
128 private ResponseCode code; // response code
130 public static enum ResponseCode {
131 SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
133 private final int code;
135 ResponseCode(int code) {
140 public ResponseCode getCode() {
144 public void setCode(ResponseCode code) {
150 * Builds a {@link Message} object
152 public static class MessageBuilder{
154 private final Message message;
156 public MessageBuilder(){
157 message = new Message();
161 public MessageBuilder type(MessageType type){
162 message.setType(type);
166 public MessageBuilder sender(String sender){
167 message.setSender(sender);
171 public MessageBuilder recipient(String recipient){
172 message.setRecipient(recipient);
176 public MessageBuilder route(RpcRouter.RouteIdentifier route){
177 message.setRoute(route);
181 public MessageBuilder payload(Object obj){
182 message.setPayload(obj);
186 public Message build(){