blob: c893ef00378b965f1e00035a809c51516cddb80b [file] [log] [blame]
alshabibc4901cd2014-09-05 16:50:40 -07001/*******************************************************************************
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16/**
17 * Copyright 2011, Big Switch Networks, Inc.
18 * Originally created by David Erickson, Stanford University
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License. You may obtain
22 * a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 * License for the specific language governing permissions and limitations
30 * under the License.
31 **/
32
33package org.onlab.packet;
34
35/**
36 *
alshabib638dc712014-09-05 18:03:45 -070037 *
alshabibc4901cd2014-09-05 16:50:40 -070038 */
39public abstract class BasePacket implements IPacket {
40 protected IPacket parent;
41 protected IPacket payload;
42
43 /**
44 * @return the parent
45 */
46 @Override
47 public IPacket getParent() {
48 return this.parent;
49 }
50
51 /**
52 * @param parent
53 * the parent to set
54 */
55 @Override
56 public IPacket setParent(final IPacket parent) {
57 this.parent = parent;
58 return this;
59 }
60
61 /**
62 * @return the payload
63 */
64 @Override
65 public IPacket getPayload() {
66 return this.payload;
67 }
68
69 /**
70 * @param payload
71 * the payload to set
72 */
73 @Override
74 public IPacket setPayload(final IPacket payload) {
75 this.payload = payload;
76 return this;
77 }
78
79 @Override
80 public void resetChecksum() {
81 if (this.parent != null) {
82 this.parent.resetChecksum();
83 }
84 }
85
86 /*
87 * (non-Javadoc)
88 *
89 * @see java.lang.Object#hashCode()
90 */
91 @Override
92 public int hashCode() {
93 final int prime = 6733;
94 int result = 1;
95 result = prime * result
96 + (this.payload == null ? 0 : this.payload.hashCode());
97 return result;
98 }
99
100 /*
101 * (non-Javadoc)
102 *
103 * @see java.lang.Object#equals(java.lang.Object)
104 */
105 @Override
106 public boolean equals(final Object obj) {
107 if (this == obj) {
108 return true;
109 }
110 if (obj == null) {
111 return false;
112 }
113 if (!(obj instanceof BasePacket)) {
114 return false;
115 }
116 final BasePacket other = (BasePacket) obj;
117 if (this.payload == null) {
118 if (other.payload != null) {
119 return false;
120 }
121 } else if (!this.payload.equals(other.payload)) {
122 return false;
123 }
124 return true;
125 }
126
127 @Override
128 public Object clone() {
129 IPacket pkt;
130 try {
131 pkt = this.getClass().newInstance();
132 } catch (final Exception e) {
133 throw new RuntimeException("Could not clone packet");
134 }
135 // TODO: we are using serialize()/deserialize() to perform the
136 // cloning. Not the most efficient way but simple. We can revisit
137 // if we hit performance problems.
138 final byte[] data = this.serialize();
139 pkt.deserialize(this.serialize(), 0, data.length);
140 pkt.setParent(this.parent);
141 return pkt;
142 }
143}