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 {
18 ANNOUNCE((byte) 0), //TODO: Remove announce, add rpc registration and deregistration
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;
81 public String toString() {
84 ", sender='" + sender + '\'' +
85 ", recipient='" + recipient + '\'' +
87 ", payload=" + payload +
92 * Converts any {@link Serializable} object to byte[]
98 public static byte[] serialize(Object obj) throws IOException {
99 ByteArrayOutputStream b = new ByteArrayOutputStream();
100 ObjectOutputStream o = new ObjectOutputStream(b);
102 return b.toByteArray();
106 * Converts byte[] to a java object
110 * @throws IOException
111 * @throws ClassNotFoundException
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();
119 public static class Response extends Message implements RpcRouter.RpcReply {
120 private ResponseCode code; // response code
122 public static enum ResponseCode {
123 SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
127 ResponseCode(int code) {
132 public ResponseCode getCode() {
136 public void setCode(ResponseCode code) {
142 * Builds a {@link Message} object
144 public static class MessageBuilder{
146 private Message message;
148 public MessageBuilder(){
149 message = new Message();
153 public MessageBuilder type(MessageType type){
154 message.setType(type);
158 public MessageBuilder sender(String sender){
159 message.setSender(sender);
163 public MessageBuilder recipient(String recipient){
164 message.setRecipient(recipient);
168 public MessageBuilder route(RpcRouter.RouteIdentifier route){
169 message.setRoute(route);
173 public MessageBuilder payload(Object obj){
174 message.setPayload(obj);
178 public Message build(){