blob: a47fcfcb9db779d1506f6552485241a3564a67d4 [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
20
21/**
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() {
64 if (this.parent != null)
65 this.parent.resetChecksum();
66 }
67
68 /* (non-Javadoc)
69 * @see java.lang.Object#hashCode()
70 */
71 @Override
72 public int hashCode() {
73 final int prime = 6733;
74 int result = 1;
75 result = prime * result + ((payload == null) ? 0 : payload.hashCode());
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) {
84 if (this == obj)
85 return true;
86 if (obj == null)
87 return false;
88 if (!(obj instanceof BasePacket))
89 return false;
90 BasePacket other = (BasePacket) obj;
91 if (payload == null) {
92 if (other.payload != null)
93 return false;
94 } else if (!payload.equals(other.payload))
95 return false;
96 return true;
97 }
Ray Milkey269ffb92014-04-03 14:43:30 -070098
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099 @Override
100 public Object clone() {
101 IPacket pkt;
102 try {
103 pkt = this.getClass().newInstance();
104 } catch (Exception e) {
105 throw new RuntimeException("Could not clone packet");
106 }
107 // TODO: we are using serialize()/deserialize() to perform the
108 // cloning. Not the most efficient way but simple. We can revisit
109 // if we hit performance problems.
110 byte[] data = this.serialize();
111 pkt.deserialize(this.serialize(), 0, data.length);
112 pkt.setParent(this.parent);
113 return pkt;
114 }
Ray Milkey0f913a02014-04-07 20:58:17 -0700115}