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 2012, Big Switch Networks, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | * not use this file except in compliance with the License. You may obtain |
| 6 | * a copy of the License at |
| 7 | * |
| 8 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 16 | |
| 17 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | */ |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 20 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * @author Shudong Zhou (shudong.zhou@bigswitch.com) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | */ |
| 29 | public class BSN extends BasePacket { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | public static final int BSN_MAGIC = 0x20000604; |
| 31 | public static final short BSN_VERSION_CURRENT = 0x0; |
| 32 | public static final short BSN_TYPE_PROBE = 0x1; |
| 33 | public static final short BSN_TYPE_BDDP = 0x2; |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 34 | public static final Map<Short, Class<? extends IPacket>> TYPE_CLASS_MAP; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | static { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 37 | TYPE_CLASS_MAP = new HashMap<Short, Class<? extends IPacket>>(); |
| 38 | TYPE_CLASS_MAP.put(BSN_TYPE_PROBE, BSNPROBE.class); |
| 39 | TYPE_CLASS_MAP.put(BSN_TYPE_BDDP, LLDP.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | protected short type; |
| 43 | protected short version; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 44 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | public BSN() { |
| 46 | version = BSN_VERSION_CURRENT; |
| 47 | } |
| 48 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 49 | public BSN(short type) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | this.type = type; |
| 51 | version = BSN_VERSION_CURRENT; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public short getType() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | return type; |
| 56 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 57 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | public BSN setType(short type) { |
| 59 | this.type = type; |
| 60 | return this; |
| 61 | } |
| 62 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | public short getVersion() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | return version; |
| 65 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 66 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | public BSN setVersion(short version) { |
| 68 | this.version = version; |
| 69 | return this; |
| 70 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public byte[] serialize() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | short length = 4 /* magic */ + 2 /* type */ + 2 /* version */; |
| 75 | |
| 76 | byte[] payloadData = null; |
| 77 | if (this.payload != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 78 | payload.setParent(this); |
| 79 | payloadData = payload.serialize(); |
| 80 | length += payloadData.length; |
| 81 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 83 | byte[] data = new byte[length]; |
| 84 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 85 | bb.putInt(BSN_MAGIC); |
| 86 | bb.putShort(this.type); |
| 87 | bb.putShort(this.version); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 88 | if (payloadData != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | bb.put(payloadData); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 90 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 91 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 92 | if (this.parent != null && this.parent instanceof Ethernet) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | ((Ethernet) this.parent).setEtherType(Ethernet.TYPE_BSN); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 94 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 95 | |
| 96 | return data; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 101 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 103 | int magic = bb.getInt(); |
| 104 | if (magic != BSN_MAGIC) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | throw new RuntimeException("Invalid BSN magic " + magic); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 108 | this.type = bb.getShort(); |
| 109 | this.version = bb.getShort(); |
| 110 | if (this.version != BSN_VERSION_CURRENT) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | throw new RuntimeException( |
| 112 | "Invalid BSN packet version " + this.version + ", should be " |
| 113 | + BSN_VERSION_CURRENT); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 114 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 116 | IPacket payload; |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 117 | if (TYPE_CLASS_MAP.containsKey(this.type)) { |
| 118 | Class<? extends IPacket> clazz = TYPE_CLASS_MAP.get(this.type); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | try { |
| 120 | payload = clazz.newInstance(); |
| 121 | } catch (Exception e) { |
| 122 | throw new RuntimeException("Error parsing payload for BSN packet" + e); |
| 123 | } |
| 124 | } else { |
| 125 | payload = new Data(); |
| 126 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 128 | this.payload = new Data(); |
| 129 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 130 | this.payload.setParent(this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 132 | return this; |
| 133 | } |
| 134 | |
| 135 | /* (non-Javadoc) |
| 136 | * @see java.lang.Object#hashCode() |
| 137 | */ |
| 138 | @Override |
| 139 | public int hashCode() { |
| 140 | final int prime = 883; |
| 141 | int result = super.hashCode(); |
| 142 | result = prime * result + version; |
| 143 | result = prime * result + type; |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | /* (non-Javadoc) |
| 148 | * @see java.lang.Object#equals(java.lang.Object) |
| 149 | */ |
| 150 | @Override |
| 151 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 152 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 153 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 154 | } |
| 155 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 156 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 157 | } |
| 158 | if (!(obj instanceof BSN)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 159 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 160 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 161 | BSN other = (BSN) obj; |
| 162 | return (type == other.type && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | version == other.version); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 165 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 166 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | StringBuffer sb = new StringBuffer("\n"); |
| 168 | sb.append("BSN packet"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 169 | if (TYPE_CLASS_MAP.containsKey(this.type)) { |
Pavlin Radoslavov | 608fac3 | 2014-04-09 12:40:24 -0700 | [diff] [blame] | 170 | sb.append(" type: " + TYPE_CLASS_MAP.get(this.type).getCanonicalName()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 171 | } else { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | sb.append(" type: " + this.type); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | |
| 176 | return sb.toString(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 177 | } |
| 178 | } |