blob: e05740c91aa51dc74392387c3a4452bcacc2178a [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
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070020// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080021/**
Ray Milkey269ffb92014-04-03 14:43:30 -070022 * @author David Erickson (daviderickson@cs.stanford.edu)
23 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024public abstract class BasePacket implements IPacket {
25 protected IPacket parent;
26 protected IPacket payload;
27
28 /**
29 * @return the parent
30 */
31 @Override
32 public IPacket getParent() {
33 return parent;
34 }
35
36 /**
37 * @param parent the parent to set
38 */
39 @Override
40 public IPacket setParent(IPacket parent) {
41 this.parent = parent;
42 return this;
43 }
44
45 /**
46 * @return the payload
47 */
48 @Override
49 public IPacket getPayload() {
50 return payload;
51 }
52
53 /**
54 * @param payload the payload to set
55 */
56 @Override
57 public IPacket setPayload(IPacket payload) {
58 this.payload = payload;
59 return this;
60 }
Ray Milkey269ffb92014-04-03 14:43:30 -070061
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080062 @Override
63 public void resetChecksum() {
Ray Milkeyb29e6262014-04-09 16:02:14 -070064 if (this.parent != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080065 this.parent.resetChecksum();
Ray Milkeyb29e6262014-04-09 16:02:14 -070066 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080067 }
68
69 /* (non-Javadoc)
70 * @see java.lang.Object#hashCode()
71 */
72 @Override
73 public int hashCode() {
74 final int prime = 6733;
75 int result = 1;
76 result = prime * result + ((payload == null) ? 0 : payload.hashCode());
77 return result;
78 }
79
80 /* (non-Javadoc)
81 * @see java.lang.Object#equals(java.lang.Object)
82 */
83 @Override
84 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070085 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080086 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070087 }
88 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080089 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070090 }
91 if (!(obj instanceof BasePacket)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070093 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080094 BasePacket other = (BasePacket) obj;
95 if (payload == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070096 if (other.payload != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070098 }
99 } else if (!payload.equals(other.payload)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800100 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 return true;
103 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700104
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 @Override
106 public Object clone() {
107 IPacket pkt;
108 try {
109 pkt = this.getClass().newInstance();
110 } catch (Exception e) {
111 throw new RuntimeException("Could not clone packet");
112 }
Ray Milkey5d406012014-04-08 14:44:41 -0700113 // TODO: we are using serialize()/deserialize() to perform the
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 // cloning. Not the most efficient way but simple. We can revisit
115 // if we hit performance problems.
116 byte[] data = this.serialize();
117 pkt.deserialize(this.serialize(), 0, data.length);
118 pkt.setParent(this.parent);
119 return pkt;
120 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700121}