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.router.zeromq;
12 import org.codehaus.jackson.map.ObjectMapper;
13 import org.opendaylight.controller.sal.connector.api.RpcRouter;
17 public class Message implements Serializable {
19 public static enum MessageType {
20 ANNOUNCE((byte) 0), //TODO: Remove announce, add rpc registration and deregistration
25 private final byte type;
27 MessageType(byte type) {
31 public byte getType(){
36 private MessageType type;
37 private String sender;
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;
74 public String toString() {
77 ", sender='" + sender + '\'' +
79 ", payload=" + payload +
84 * Converts any {@link Serializable} object to byte[]
90 public static byte[] serialize(Object obj) throws IOException {
91 ByteArrayOutputStream b = new ByteArrayOutputStream();
92 ObjectOutputStream o = new ObjectOutputStream(b);
94 return b.toByteArray();
98 * Converts byte[] to a java object
102 * @throws IOException
103 * @throws ClassNotFoundException
105 public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
106 ByteArrayInputStream b = new ByteArrayInputStream(bytes);
107 ObjectInputStream o = new ObjectInputStream(b);
108 return o.readObject();
111 public static byte[] toJsonBytes(Message m) throws IOException {
112 ObjectMapper o = new ObjectMapper();
113 return o.writeValueAsBytes(m);
116 public static Message fromJsonBytes(byte [] bytes) throws IOException {
118 ObjectMapper o = new ObjectMapper();
119 return o.readValue(bytes, Message.class);
122 public static class Response extends Message implements RpcRouter.RpcReply {
123 private ResponseCode code; // response code
125 public static enum ResponseCode {
126 SUCCESS(200), BADREQUEST(400), TIMEOUT(408), GONE(410), SERVERERROR(500), SERVICEUNAVAILABLE(503);
130 ResponseCode(int code) {
135 public ResponseCode getCode() {
139 public void setCode(ResponseCode code) {
145 * Builds a {@link Message} object
147 public static class MessageBuilder{
149 private Message message;
151 public MessageBuilder(){
152 message = new Message();
156 public MessageBuilder type(MessageType type){
157 message.setType(type);
161 public MessageBuilder sender(String sender){
162 message.setSender(sender);
166 public MessageBuilder route(RpcRouter.RouteIdentifier route){
167 message.setRoute(route);
171 public MessageBuilder payload(Object obj){
172 message.setPayload(obj);
176 public Message build(){