blob: 578086b049bef43a99681427a7f6cd1cf237baf7 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Ray Milkeyf0c47612017-09-28 11:29:38 -070019import java.lang.reflect.InvocationTargetException;
20import java.lang.reflect.Method;
21
alshabibc4901cd2014-09-05 16:50:40 -070022/**
Jian Li5fc14292015-12-04 11:30:46 -080023 * Base packet class.
alshabibc4901cd2014-09-05 16:50:40 -070024 */
25public abstract class BasePacket implements IPacket {
Jian Li5fc14292015-12-04 11:30:46 -080026
alshabibc4901cd2014-09-05 16:50:40 -070027 protected IPacket parent;
28 protected IPacket payload;
29
alshabibc4901cd2014-09-05 16:50:40 -070030 @Override
31 public IPacket getParent() {
32 return this.parent;
33 }
34
alshabibc4901cd2014-09-05 16:50:40 -070035 @Override
36 public IPacket setParent(final IPacket parent) {
37 this.parent = parent;
38 return this;
39 }
40
alshabibc4901cd2014-09-05 16:50:40 -070041 @Override
42 public IPacket getPayload() {
43 return this.payload;
44 }
45
alshabibc4901cd2014-09-05 16:50:40 -070046 @Override
47 public IPacket setPayload(final IPacket payload) {
48 this.payload = payload;
49 return this;
50 }
51
52 @Override
53 public void resetChecksum() {
54 if (this.parent != null) {
55 this.parent.resetChecksum();
56 }
57 }
58
alshabibc4901cd2014-09-05 16:50:40 -070059 @Override
60 public int hashCode() {
61 final int prime = 6733;
62 int result = 1;
63 result = prime * result
64 + (this.payload == null ? 0 : this.payload.hashCode());
65 return result;
66 }
67
alshabibc4901cd2014-09-05 16:50:40 -070068 @Override
69 public boolean equals(final Object obj) {
70 if (this == obj) {
71 return true;
72 }
73 if (obj == null) {
74 return false;
75 }
76 if (!(obj instanceof BasePacket)) {
77 return false;
78 }
79 final BasePacket other = (BasePacket) obj;
80 if (this.payload == null) {
81 if (other.payload != null) {
82 return false;
83 }
84 } else if (!this.payload.equals(other.payload)) {
85 return false;
86 }
87 return true;
88 }
89
Ray Milkeyf0c47612017-09-28 11:29:38 -070090 /**
91 * This implementation of clone() is here to preserve backwards compatibility. Applications should not
92 * use clone() and instead use the duplicate() methods on the packet classes.
93 *
94 * @return copy of packet
95 */
alshabibc4901cd2014-09-05 16:50:40 -070096 @Override
97 public Object clone() {
Ray Milkeyf0c47612017-09-28 11:29:38 -070098
99 Class<? extends BasePacket> packetClass = this.getClass();
100 Method[] allMethods = packetClass.getDeclaredMethods();
101
102 Method deserializerFactory = null;
103 for (Method m : allMethods) {
104 String mname = m.getName();
105 if (mname.equals("deserializer")) {
106 deserializerFactory = m;
107 break;
108 }
alshabibc4901cd2014-09-05 16:50:40 -0700109 }
alshabib4785eec2014-12-04 16:45:45 -0800110
Ray Milkeyf0c47612017-09-28 11:29:38 -0700111 if (deserializerFactory == null) {
112 throw new IllegalStateException("No Deserializer found for " + packetClass.getName());
113 }
114
115 byte[] data = serialize();
116 try {
117 Deserializer deserializer = (Deserializer) deserializerFactory.invoke(this);
118 return deserializer.deserialize(data, 0, data.length);
119 } catch (IllegalAccessException | InvocationTargetException | DeserializationException ex) {
120 throw new IllegalStateException(ex);
121 }
122
alshabibc4901cd2014-09-05 16:50:40 -0700123 }
124}