blob: cadcfa26f70aefbb99a40a28e81538d88147fb34 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * 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
7 *
8 * 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.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.cluster.messaging;
tom1d416c52014-09-29 20:55:24 -070017
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080018import com.google.common.base.MoreObjects;
Jonathan Hart584d2f32015-01-27 19:46:14 -080019import org.onlab.util.ByteArraySizeHashPrinter;
20import org.onosproject.cluster.NodeId;
21
22import java.io.IOException;
23import java.util.Arrays;
24import java.util.Objects;
tom1d416c52014-09-29 20:55:24 -070025
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070026// TODO: Should payload type be ByteBuffer?
tom1d416c52014-09-29 20:55:24 -070027/**
28 * Base message for cluster-wide communications.
29 */
Madan Jampani890bc352014-10-01 22:35:29 -070030public class ClusterMessage {
tom1d416c52014-09-29 20:55:24 -070031
Madan Jampani890bc352014-10-01 22:35:29 -070032 private final NodeId sender;
tom1d416c52014-09-29 20:55:24 -070033 private final MessageSubject subject;
Madan Jampani53e44e62014-10-07 12:39:51 -070034 private final byte[] payload;
tom1d416c52014-09-29 20:55:24 -070035
36 /**
37 * Creates a cluster message.
38 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080039 * @param sender message sender
tom1d416c52014-09-29 20:55:24 -070040 * @param subject message subject
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080041 * @param payload message payload
tom1d416c52014-09-29 20:55:24 -070042 */
Madan Jampani53e44e62014-10-07 12:39:51 -070043 public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) {
Madan Jampani890bc352014-10-01 22:35:29 -070044 this.sender = sender;
tom1d416c52014-09-29 20:55:24 -070045 this.subject = subject;
Madan Jampani890bc352014-10-01 22:35:29 -070046 this.payload = payload;
tom1d416c52014-09-29 20:55:24 -070047 }
48
49 /**
Madan Jampani890bc352014-10-01 22:35:29 -070050 * Returns the id of the controller sending this message.
51 *
52 * @return message sender id.
53 */
54 public NodeId sender() {
55 return sender;
56 }
57
58 /**
tom1d416c52014-09-29 20:55:24 -070059 * Returns the message subject indicator.
60 *
61 * @return message subject
62 */
63 public MessageSubject subject() {
64 return subject;
65 }
66
Madan Jampani890bc352014-10-01 22:35:29 -070067 /**
68 * Returns the message payload.
69 *
70 * @return message payload.
71 */
Madan Jampani53e44e62014-10-07 12:39:51 -070072 public byte[] payload() {
Madan Jampani890bc352014-10-01 22:35:29 -070073 return payload;
tom1d416c52014-09-29 20:55:24 -070074 }
Madan Jampani8a895092014-10-17 16:55:50 -070075
76 /**
77 * Sends a response to the sender.
78 *
79 * @param data payload response.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080080 * @throws IOException when I/O exception of some sort has occurred
Madan Jampani8a895092014-10-17 16:55:50 -070081 */
82 public void respond(byte[] data) throws IOException {
Jonathan Hart584d2f32015-01-27 19:46:14 -080083 throw new IllegalStateException("One can only respond to message received from others.");
Madan Jampani8a895092014-10-17 16:55:50 -070084 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080085
86 @Override
87 public String toString() {
88 return MoreObjects.toStringHelper(getClass())
89 .add("sender", sender)
90 .add("subject", subject)
91 .add("payload", ByteArraySizeHashPrinter.of(payload))
92 .toString();
93 }
Jonathan Hart584d2f32015-01-27 19:46:14 -080094
95 @Override
96 public boolean equals(Object o) {
97 if (!(o instanceof ClusterMessage)) {
98 return false;
99 }
100
101 ClusterMessage that = (ClusterMessage) o;
102
103 return Objects.equals(this.sender, that.sender) &&
104 Objects.equals(this.subject, that.subject) &&
105 Arrays.equals(this.payload, that.payload);
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(sender, subject, payload);
111 }
tom1d416c52014-09-29 20:55:24 -0700112}