blob: e02ecc8ab0d7f808d95e0209f9241a44ae265559 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Aaron Kruglikov1b727382016-02-09 16:17:47 -08002 * Copyright 2016 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Aaron Kruglikov1b727382016-02-09 16:17:47 -080016package org.onosproject.store.cluster.messaging.impl;
Yuta HIGUCHI0a0b9e42015-02-11 16:57:12 -080017
18import com.google.common.base.MoreObjects;
Aaron Kruglikov1b727382016-02-09 16:17:47 -080019import org.onlab.util.ByteArraySizeHashPrinter;
20import org.onosproject.store.cluster.messaging.Endpoint;
Yuta HIGUCHI0a0b9e42015-02-11 16:57:12 -080021
Madan Jampaniab6d3112014-10-02 16:30:14 -070022/**
23 * Internal message representation with additional attributes
24 * for supporting, synchronous request/reply behavior.
25 */
Madan Jampanic26eede2015-04-16 11:42:16 -070026public final class InternalMessage {
Madan Jampaniab6d3112014-10-02 16:30:14 -070027
Madan Jampania9e70a62016-03-02 16:28:18 -080028 /**
29 * Message status.
30 */
31 public enum Status {
32 /**
33 * All ok.
34 */
35 OK,
36
37 /**
38 * Response status signifying no registered handler.
39 */
40 ERROR_NO_HANDLER,
41
42 /**
43 * Response status signifying an exception handling the message.
44 */
45 ERROR_HANDLER_EXCEPTION
46
47 // NOTE: For backwards compatibility it important that new enum constants
48 // be appended.
49 // FIXME: We should remove this restriction in the future.
50 }
51
Madan Jampanic26eede2015-04-16 11:42:16 -070052 private final long id;
53 private final Endpoint sender;
54 private final String type;
55 private final byte[] payload;
Madan Jampania9e70a62016-03-02 16:28:18 -080056 private final Status status;
Yuta HIGUCHIc65f5122014-10-07 10:05:59 -070057
Madan Jampanic26eede2015-04-16 11:42:16 -070058 public InternalMessage(long id, Endpoint sender, String type, byte[] payload) {
Madan Jampania9e70a62016-03-02 16:28:18 -080059 this(id, sender, type, payload, Status.OK);
60 }
61
62 public InternalMessage(long id, Endpoint sender, String type, byte[] payload, Status status) {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080063 this.id = id;
64 this.sender = sender;
65 this.type = type;
66 this.payload = payload;
Madan Jampania9e70a62016-03-02 16:28:18 -080067 this.status = status;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080068 }
69
Madan Jampaniab6d3112014-10-02 16:30:14 -070070 public long id() {
71 return id;
72 }
73
Madan Jampani49115e92015-03-14 10:43:33 -070074 public String type() {
Madan Jampaniab6d3112014-10-02 16:30:14 -070075 return type;
76 }
77
78 public Endpoint sender() {
79 return sender;
80 }
81
Madan Jampani53e44e62014-10-07 12:39:51 -070082 public byte[] payload() {
Madan Jampaniab6d3112014-10-02 16:30:14 -070083 return payload;
84 }
85
Madan Jampania9e70a62016-03-02 16:28:18 -080086 public Status status() {
87 return status;
88 }
89
Yuta HIGUCHI0a0b9e42015-02-11 16:57:12 -080090 @Override
91 public String toString() {
92 return MoreObjects.toStringHelper(this)
93 .add("id", id)
94 .add("type", type)
95 .add("sender", sender)
Madan Jampania9e70a62016-03-02 16:28:18 -080096 .add("status", status)
Yuta HIGUCHI0a0b9e42015-02-11 16:57:12 -080097 .add("payload", ByteArraySizeHashPrinter.of(payload))
98 .toString();
99 }
Yuta HIGUCHI92626c02014-10-06 15:46:18 -0700100}