blob: 6f15afdafc24ad23002e313384b389e114352e43 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
alshabibc4901cd2014-09-05 16:50:40 -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
alshabibc4901cd2014-09-05 16:50:40 -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 */
16
17
alshabibc4901cd2014-09-05 16:50:40 -070018
19package org.onlab.packet;
20
21import java.util.Arrays;
22
Jian Li5fc14292015-12-04 11:30:46 -080023import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070024import static org.onlab.packet.PacketUtils.*;
25
alshabibc4901cd2014-09-05 16:50:40 -070026/**
27 *
alshabibc4901cd2014-09-05 16:50:40 -070028 */
29public class Data extends BasePacket {
30 protected byte[] data;
31
32 /**
33 *
34 */
35 public Data() {
Jonathan Hart2a655752015-04-07 16:46:33 -070036 data = new byte[0];
alshabibc4901cd2014-09-05 16:50:40 -070037 }
38
39 /**
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080040 * @param data the data
alshabibc4901cd2014-09-05 16:50:40 -070041 */
42 public Data(final byte[] data) {
43 this.data = data;
44 }
45
46 /**
47 * @return the data
48 */
49 public byte[] getData() {
50 return this.data;
51 }
52
53 /**
54 * @param data
55 * the data to set
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080056 * @return self
alshabibc4901cd2014-09-05 16:50:40 -070057 */
58 public Data setData(final byte[] data) {
59 this.data = data;
60 return this;
61 }
62
63 @Override
64 public byte[] serialize() {
65 return this.data;
66 }
67
alshabibc4901cd2014-09-05 16:50:40 -070068 /*
69 * (non-Javadoc)
70 *
71 * @see java.lang.Object#hashCode()
72 */
73 @Override
74 public int hashCode() {
75 final int prime = 1571;
76 int result = super.hashCode();
77 result = prime * result + Arrays.hashCode(this.data);
78 return result;
79 }
80
81 /*
82 * (non-Javadoc)
83 *
84 * @see java.lang.Object#equals(java.lang.Object)
85 */
86 @Override
87 public boolean equals(final Object obj) {
88 if (this == obj) {
89 return true;
90 }
91 if (!super.equals(obj)) {
92 return false;
93 }
94 if (!(obj instanceof Data)) {
95 return false;
96 }
97 final Data other = (Data) obj;
98 if (!Arrays.equals(this.data, other.data)) {
99 return false;
100 }
101 return true;
102 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700103
104 /**
105 * Deserializer function for generic payload data.
106 *
107 * @return deserializer function
108 */
109 public static Deserializer<Data> deserializer() {
110 return (data, offset, length) -> {
111 // Allow zero-length data for now
112 if (length == 0) {
113 return new Data();
114 }
115
116 checkInput(data, offset, length, 1);
117
118 Data dataObject = new Data();
119
Dmytro Titova1010de2019-06-10 18:07:52 +0500120 dataObject.data = Arrays.copyOfRange(data, offset, offset + length);
Jonathan Hart2a655752015-04-07 16:46:33 -0700121
122 return dataObject;
123 };
124 }
125
Jian Li5fc14292015-12-04 11:30:46 -0800126 @Override
127 public String toString() {
128 return toStringHelper(getClass())
129 .add("data", Arrays.toString(data))
130 .toString();
131 }
alshabibc4901cd2014-09-05 16:50:40 -0700132}