blob: 246f8eecf1d5c913721ec9c4dc8d97440602fde1 [file] [log] [blame]
tom73094832014-09-29 13:47:08 -07001package org.onlab.onos.store.cluster.impl;
tomcb43d602014-09-28 22:46:16 -07002
3import org.onlab.nio.AbstractMessage;
4
5import java.util.Objects;
6
7import static com.google.common.base.MoreObjects.toStringHelper;
8
9/**
10 * Base message for cluster-wide communications using TLVs.
11 */
12public class TLVMessage extends AbstractMessage {
13
14 private final int type;
tom73094832014-09-29 13:47:08 -070015 private final byte[] data;
tomcb43d602014-09-28 22:46:16 -070016
17 /**
18 * Creates an immutable TLV message.
19 *
20 * @param type message type
tom73094832014-09-29 13:47:08 -070021 * @param data message data bytes
tomcb43d602014-09-28 22:46:16 -070022 */
tom73094832014-09-29 13:47:08 -070023 public TLVMessage(int type, byte[] data) {
24 this.length = data.length + TLVMessageStream.METADATA_LENGTH;
tomcb43d602014-09-28 22:46:16 -070025 this.type = type;
26 this.data = data;
27 }
28
29 /**
30 * Returns the message type indicator.
31 *
32 * @return message type
33 */
34 public int type() {
35 return type;
36 }
37
38 /**
tom73094832014-09-29 13:47:08 -070039 * Returns the data bytes.
tomcb43d602014-09-28 22:46:16 -070040 *
41 * @return message data
42 */
tom73094832014-09-29 13:47:08 -070043 public byte[] data() {
tomcb43d602014-09-28 22:46:16 -070044 return data;
45 }
46
47 @Override
48 public int hashCode() {
49 return Objects.hash(type, data);
50 }
51
52 @Override
53 public boolean equals(Object obj) {
54 if (this == obj) {
55 return true;
56 }
57 if (obj == null || getClass() != obj.getClass()) {
58 return false;
59 }
60 final TLVMessage other = (TLVMessage) obj;
61 return Objects.equals(this.type, other.type) &&
62 Objects.equals(this.data, other.data);
63 }
64
65 @Override
66 public String toString() {
67 return toStringHelper(this).add("type", type).add("length", length).toString();
68 }
69
70}