blob: d3b382bec7e1aa78b0fb69afe44d16b2e35bd460 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* 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**/
17
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20
21/**
22*
23* @author David Erickson (daviderickson@cs.stanford.edu)
24*/
25public abstract class BasePacket implements IPacket {
26 protected IPacket parent;
27 protected IPacket payload;
28
29 /**
30 * @return the parent
31 */
32 @Override
33 public IPacket getParent() {
34 return parent;
35 }
36
37 /**
38 * @param parent the parent to set
39 */
40 @Override
41 public IPacket setParent(IPacket parent) {
42 this.parent = parent;
43 return this;
44 }
45
46 /**
47 * @return the payload
48 */
49 @Override
50 public IPacket getPayload() {
51 return payload;
52 }
53
54 /**
55 * @param payload the payload to set
56 */
57 @Override
58 public IPacket setPayload(IPacket payload) {
59 this.payload = payload;
60 return this;
61 }
62
63 @Override
64 public void resetChecksum() {
65 if (this.parent != null)
66 this.parent.resetChecksum();
67 }
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) {
85 if (this == obj)
86 return true;
87 if (obj == null)
88 return false;
89 if (!(obj instanceof BasePacket))
90 return false;
91 BasePacket other = (BasePacket) obj;
92 if (payload == null) {
93 if (other.payload != null)
94 return false;
95 } else if (!payload.equals(other.payload))
96 return false;
97 return true;
98 }
99
100 @Override
101 public Object clone() {
102 IPacket pkt;
103 try {
104 pkt = this.getClass().newInstance();
105 } catch (Exception e) {
106 throw new RuntimeException("Could not clone packet");
107 }
108 // TODO: we are using serialize()/deserialize() to perform the
109 // cloning. Not the most efficient way but simple. We can revisit
110 // if we hit performance problems.
111 byte[] data = this.serialize();
112 pkt.deserialize(this.serialize(), 0, data.length);
113 pkt.setParent(this.parent);
114 return pkt;
115 }
116}