blob: e583b0424ea1b238a4fd337b285a34b35156eb3c [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.util.Arrays;
21
Yuta HIGUCHI498e1532014-08-20 21:30:28 -070022import org.apache.commons.lang3.ArrayUtils;
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070023
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070024// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080025/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026 * @author David Erickson (daviderickson@cs.stanford.edu)
27 */
28public class Data extends BasePacket {
29 protected byte[] data;
30
31 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070032 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080033 */
34 public Data() {
35 }
36
37 /**
38 * @param data
39 */
40 public Data(byte[] data) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070041 this.data = ArrayUtils.clone(data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042 }
43
44 /**
45 * @return the data
46 */
47 public byte[] getData() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070048 return ArrayUtils.clone(this.data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049 }
50
51 /**
52 * @param data the data to set
53 */
54 public Data setData(byte[] data) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070055 this.data = ArrayUtils.clone(data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056 return this;
57 }
58
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070059 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060 public byte[] serialize() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070061 return ArrayUtils.clone(this.data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 }
63
64 @Override
65 public IPacket deserialize(byte[] data, int offset, int length) {
66 this.data = Arrays.copyOfRange(data, offset, data.length);
67 return this;
68 }
69
70 /* (non-Javadoc)
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(data);
78 return result;
79 }
80
81 /* (non-Javadoc)
82 * @see java.lang.Object#equals(java.lang.Object)
83 */
84 @Override
85 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080087 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070088 }
89 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080090 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070091 }
92 if (!(obj instanceof Data)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070094 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095 Data other = (Data) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -070096 if (!Arrays.equals(data, other.data)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070098 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099 return true;
100 }
101}