Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 20 | // CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 23 | */ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | public abstract class BasePacket implements IPacket { |
| 25 | protected IPacket parent; |
| 26 | protected IPacket payload; |
| 27 | |
| 28 | /** |
| 29 | * @return the parent |
| 30 | */ |
| 31 | @Override |
| 32 | public IPacket getParent() { |
| 33 | return parent; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param parent the parent to set |
| 38 | */ |
| 39 | @Override |
| 40 | public IPacket setParent(IPacket parent) { |
| 41 | this.parent = parent; |
| 42 | return this; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return the payload |
| 47 | */ |
| 48 | @Override |
| 49 | public IPacket getPayload() { |
| 50 | return payload; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param payload the payload to set |
| 55 | */ |
| 56 | @Override |
| 57 | public IPacket setPayload(IPacket payload) { |
| 58 | this.payload = payload; |
| 59 | return this; |
| 60 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | @Override |
| 63 | public void resetChecksum() { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 64 | if (this.parent != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 65 | this.parent.resetChecksum(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 66 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* (non-Javadoc) |
| 70 | * @see java.lang.Object#hashCode() |
| 71 | */ |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | final int prime = 6733; |
| 75 | int result = 1; |
| 76 | result = prime * result + ((payload == null) ? 0 : payload.hashCode()); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | /* (non-Javadoc) |
| 81 | * @see java.lang.Object#equals(java.lang.Object) |
| 82 | */ |
| 83 | @Override |
| 84 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 85 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 86 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 87 | } |
| 88 | if (obj == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 90 | } |
| 91 | if (!(obj instanceof BasePacket)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 92 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 93 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 94 | BasePacket other = (BasePacket) obj; |
| 95 | if (payload == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 96 | if (other.payload != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 98 | } |
| 99 | } else if (!payload.equals(other.payload)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 100 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 101 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 102 | return true; |
| 103 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 105 | @Override |
| 106 | public Object clone() { |
| 107 | IPacket pkt; |
| 108 | try { |
| 109 | pkt = this.getClass().newInstance(); |
| 110 | } catch (Exception e) { |
| 111 | throw new RuntimeException("Could not clone packet"); |
| 112 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 113 | // TODO: we are using serialize()/deserialize() to perform the |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 114 | // cloning. Not the most efficient way but simple. We can revisit |
| 115 | // if we hit performance problems. |
| 116 | byte[] data = this.serialize(); |
| 117 | pkt.deserialize(this.serialize(), 0, data.length); |
| 118 | pkt.setParent(this.parent); |
| 119 | return pkt; |
| 120 | } |
Ray Milkey | 0f913a0 | 2014-04-07 20:58:17 -0700 | [diff] [blame] | 121 | } |