blob: 46e3c7e540486e7cf30371bdd3ead12484d09ba1 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
16
17
alshabibc4901cd2014-09-05 16:50:40 -070018
19package org.onlab.packet;
20
21/**
22 *
alshabib638dc712014-09-05 18:03:45 -070023 *
alshabibc4901cd2014-09-05 16:50:40 -070024 */
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 this.parent;
35 }
36
37 /**
38 * @param parent
39 * the parent to set
40 */
41 @Override
42 public IPacket setParent(final IPacket parent) {
43 this.parent = parent;
44 return this;
45 }
46
47 /**
48 * @return the payload
49 */
50 @Override
51 public IPacket getPayload() {
52 return this.payload;
53 }
54
55 /**
56 * @param payload
57 * the payload to set
58 */
59 @Override
60 public IPacket setPayload(final IPacket payload) {
61 this.payload = payload;
62 return this;
63 }
64
65 @Override
66 public void resetChecksum() {
67 if (this.parent != null) {
68 this.parent.resetChecksum();
69 }
70 }
71
72 /*
73 * (non-Javadoc)
74 *
75 * @see java.lang.Object#hashCode()
76 */
77 @Override
78 public int hashCode() {
79 final int prime = 6733;
80 int result = 1;
81 result = prime * result
82 + (this.payload == null ? 0 : this.payload.hashCode());
83 return result;
84 }
85
86 /*
87 * (non-Javadoc)
88 *
89 * @see java.lang.Object#equals(java.lang.Object)
90 */
91 @Override
92 public boolean equals(final Object obj) {
93 if (this == obj) {
94 return true;
95 }
96 if (obj == null) {
97 return false;
98 }
99 if (!(obj instanceof BasePacket)) {
100 return false;
101 }
102 final BasePacket other = (BasePacket) obj;
103 if (this.payload == null) {
104 if (other.payload != null) {
105 return false;
106 }
107 } else if (!this.payload.equals(other.payload)) {
108 return false;
109 }
110 return true;
111 }
112
113 @Override
114 public Object clone() {
115 IPacket pkt;
116 try {
117 pkt = this.getClass().newInstance();
118 } catch (final Exception e) {
119 throw new RuntimeException("Could not clone packet");
120 }
121 // TODO: we are using serialize()/deserialize() to perform the
122 // cloning. Not the most efficient way but simple. We can revisit
123 // if we hit performance problems.
124 final byte[] data = this.serialize();
125 pkt.deserialize(this.serialize(), 0, data.length);
126 pkt.setParent(this.parent);
127 return pkt;
128 }
129}