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