blob: f63306bb196e9b6660157ef511663739ed1b0946 [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
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070022import org.apache.commons.lang.ArrayUtils;
23
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080025 * @author David Erickson (daviderickson@cs.stanford.edu)
26 */
27public class Data extends BasePacket {
28 protected byte[] data;
29
30 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070031 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080032 */
33 public Data() {
34 }
35
36 /**
37 * @param data
38 */
39 public Data(byte[] data) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070040 this.data = ArrayUtils.clone(data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041 }
42
43 /**
44 * @return the data
45 */
46 public byte[] getData() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070047 return ArrayUtils.clone(this.data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048 }
49
50 /**
51 * @param data the data to set
52 */
53 public Data setData(byte[] data) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070054 this.data = ArrayUtils.clone(data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055 return this;
56 }
57
58 public byte[] serialize() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070059 return ArrayUtils.clone(this.data);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060 }
61
62 @Override
63 public IPacket deserialize(byte[] data, int offset, int length) {
64 this.data = Arrays.copyOfRange(data, offset, data.length);
65 return this;
66 }
67
68 /* (non-Javadoc)
69 * @see java.lang.Object#hashCode()
70 */
71 @Override
72 public int hashCode() {
73 final int prime = 1571;
74 int result = super.hashCode();
75 result = prime * result + Arrays.hashCode(data);
76 return result;
77 }
78
79 /* (non-Javadoc)
80 * @see java.lang.Object#equals(java.lang.Object)
81 */
82 @Override
83 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070084 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 }
87 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070089 }
90 if (!(obj instanceof Data)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070092 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093 Data other = (Data) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -070094 if (!Arrays.equals(data, other.data)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070096 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097 return true;
98 }
99}