blob: d1380288b7bfd4159b715167b426aae38f6b6ebc [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
alshabibc4901cd2014-09-05 16:50:40 -070017package org.onlab.packet;
18
19/**
Jian Li5fc14292015-12-04 11:30:46 -080020 * Base packet class.
alshabibc4901cd2014-09-05 16:50:40 -070021 */
22public abstract class BasePacket implements IPacket {
Jian Li5fc14292015-12-04 11:30:46 -080023
alshabibc4901cd2014-09-05 16:50:40 -070024 protected IPacket parent;
25 protected IPacket payload;
26
alshabibc4901cd2014-09-05 16:50:40 -070027 @Override
28 public IPacket getParent() {
29 return this.parent;
30 }
31
alshabibc4901cd2014-09-05 16:50:40 -070032 @Override
33 public IPacket setParent(final IPacket parent) {
34 this.parent = parent;
35 return this;
36 }
37
alshabibc4901cd2014-09-05 16:50:40 -070038 @Override
39 public IPacket getPayload() {
40 return this.payload;
41 }
42
alshabibc4901cd2014-09-05 16:50:40 -070043 @Override
44 public IPacket setPayload(final IPacket payload) {
45 this.payload = payload;
46 return this;
47 }
48
49 @Override
50 public void resetChecksum() {
51 if (this.parent != null) {
52 this.parent.resetChecksum();
53 }
54 }
55
alshabibc4901cd2014-09-05 16:50:40 -070056 @Override
57 public int hashCode() {
58 final int prime = 6733;
59 int result = 1;
60 result = prime * result
61 + (this.payload == null ? 0 : this.payload.hashCode());
62 return result;
63 }
64
alshabibc4901cd2014-09-05 16:50:40 -070065 @Override
66 public boolean equals(final Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (obj == null) {
71 return false;
72 }
73 if (!(obj instanceof BasePacket)) {
74 return false;
75 }
76 final BasePacket other = (BasePacket) obj;
77 if (this.payload == null) {
78 if (other.payload != null) {
79 return false;
80 }
81 } else if (!this.payload.equals(other.payload)) {
82 return false;
83 }
84 return true;
85 }
86
87 @Override
88 public Object clone() {
89 IPacket pkt;
90 try {
91 pkt = this.getClass().newInstance();
92 } catch (final Exception e) {
93 throw new RuntimeException("Could not clone packet");
94 }
alshabib4785eec2014-12-04 16:45:45 -080095
alshabibc4901cd2014-09-05 16:50:40 -070096 final byte[] data = this.serialize();
97 pkt.deserialize(this.serialize(), 0, data.length);
98 pkt.setParent(this.parent);
99 return pkt;
100 }
101}