Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.messaging; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 17 | |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Objects; |
| 21 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 22 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 25 | import com.google.common.base.Charsets; |
| 26 | import com.google.common.base.MoreObjects; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 27 | |
Yuta HIGUCHI | 971addc | 2014-10-07 23:23:17 -0700 | [diff] [blame] | 28 | // TODO: Should payload type be ByteBuffer? |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Base message for cluster-wide communications. |
| 31 | */ |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 32 | public class ClusterMessage { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 33 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 34 | private final NodeId sender; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 35 | private final MessageSubject subject; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 36 | private final byte[] payload; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 37 | private transient byte[] response; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Creates a cluster message. |
| 41 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 42 | * @param sender message sender |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 43 | * @param subject message subject |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 44 | * @param payload message payload |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 45 | */ |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 46 | public ClusterMessage(NodeId sender, MessageSubject subject, byte[] payload) { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 47 | this.sender = sender; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 48 | this.subject = subject; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 49 | this.payload = payload; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 53 | * Returns the id of the controller sending this message. |
| 54 | * |
| 55 | * @return message sender id. |
| 56 | */ |
| 57 | public NodeId sender() { |
| 58 | return sender; |
| 59 | } |
| 60 | |
| 61 | /** |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 62 | * Returns the message subject indicator. |
| 63 | * |
| 64 | * @return message subject |
| 65 | */ |
| 66 | public MessageSubject subject() { |
| 67 | return subject; |
| 68 | } |
| 69 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Returns the message payload. |
| 72 | * |
| 73 | * @return message payload. |
| 74 | */ |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 75 | public byte[] payload() { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 76 | return payload; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 77 | } |
Madan Jampani | 8a89509 | 2014-10-17 16:55:50 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 80 | * Records the response to be sent to the sender. |
Madan Jampani | 8a89509 | 2014-10-17 16:55:50 -0700 | [diff] [blame] | 81 | * |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 82 | * @param data response payload |
Madan Jampani | 8a89509 | 2014-10-17 16:55:50 -0700 | [diff] [blame] | 83 | */ |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 84 | public void respond(byte[] data) { |
| 85 | response = data; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the response to be sent to the sender. |
| 90 | * |
| 91 | * @return response bytes |
| 92 | */ |
| 93 | public byte[] response() { |
| 94 | return response; |
Madan Jampani | 8a89509 | 2014-10-17 16:55:50 -0700 | [diff] [blame] | 95 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return MoreObjects.toStringHelper(getClass()) |
| 100 | .add("sender", sender) |
| 101 | .add("subject", subject) |
| 102 | .add("payload", ByteArraySizeHashPrinter.of(payload)) |
| 103 | .toString(); |
| 104 | } |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public boolean equals(Object o) { |
| 108 | if (!(o instanceof ClusterMessage)) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | ClusterMessage that = (ClusterMessage) o; |
| 113 | |
| 114 | return Objects.equals(this.sender, that.sender) && |
| 115 | Objects.equals(this.subject, that.subject) && |
| 116 | Arrays.equals(this.payload, that.payload); |
| 117 | } |
| 118 | |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Serializes this instance. |
| 121 | * @return bytes |
| 122 | */ |
| 123 | public byte[] getBytes() { |
| 124 | byte[] senderBytes = sender.toString().getBytes(Charsets.UTF_8); |
| 125 | byte[] subjectBytes = subject.value().getBytes(Charsets.UTF_8); |
| 126 | int capacity = 12 + senderBytes.length + subjectBytes.length + payload.length; |
| 127 | ByteBuffer buffer = ByteBuffer.allocate(capacity); |
| 128 | buffer.putInt(senderBytes.length); |
| 129 | buffer.put(senderBytes); |
| 130 | buffer.putInt(subjectBytes.length); |
| 131 | buffer.put(subjectBytes); |
| 132 | buffer.putInt(payload.length); |
| 133 | buffer.put(payload); |
| 134 | return buffer.array(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Decodes a new ClusterMessage from raw bytes. |
| 139 | * @param bytes raw bytes |
| 140 | * @return cluster message |
| 141 | */ |
| 142 | public static ClusterMessage fromBytes(byte[] bytes) { |
| 143 | ByteBuffer buffer = ByteBuffer.wrap(bytes); |
| 144 | byte[] senderBytes = new byte[buffer.getInt()]; |
| 145 | buffer.get(senderBytes); |
| 146 | byte[] subjectBytes = new byte[buffer.getInt()]; |
| 147 | buffer.get(subjectBytes); |
| 148 | byte[] payloadBytes = new byte[buffer.getInt()]; |
| 149 | buffer.get(payloadBytes); |
| 150 | |
| 151 | return new ClusterMessage(new NodeId(new String(senderBytes, Charsets.UTF_8)), |
Thomas Vachuska | ff96523 | 2015-03-17 14:10:52 -0700 | [diff] [blame] | 152 | new MessageSubject(new String(subjectBytes, Charsets.UTF_8)), |
Madan Jampani | ba47223 | 2015-03-04 13:00:50 -0800 | [diff] [blame] | 153 | payloadBytes); |
| 154 | } |
| 155 | |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public int hashCode() { |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 158 | return Objects.hash(sender, subject, Arrays.hashCode(payload)); |
Jonathan Hart | 584d2f3 | 2015-01-27 19:46:14 -0800 | [diff] [blame] | 159 | } |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 160 | } |