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; |
| 34 | public static Map<Short, Class<? extends IPacket>> typeClassMap; |
| 35 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | static { |
| 37 | typeClassMap = new HashMap<Short, Class<? extends IPacket>>(); |
| 38 | typeClassMap.put(BSN_TYPE_PROBE, BSNPROBE.class); |
| 39 | typeClassMap.put(BSN_TYPE_BDDP, LLDP.class); |
| 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); |
| 88 | if (payloadData != null) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 89 | bb.put(payloadData); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 90 | |
| 91 | if (this.parent != null && this.parent instanceof Ethernet) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 92 | ((Ethernet) this.parent).setEtherType(Ethernet.TYPE_BSN); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 93 | |
| 94 | return data; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 99 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 100 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 101 | int magic = bb.getInt(); |
| 102 | if (magic != BSN_MAGIC) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 103 | throw new RuntimeException("Invalid BSN magic " + magic); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 105 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | this.type = bb.getShort(); |
| 107 | this.version = bb.getShort(); |
| 108 | if (this.version != BSN_VERSION_CURRENT) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 109 | throw new RuntimeException( |
| 110 | "Invalid BSN packet version " + this.version + ", should be " |
| 111 | + BSN_VERSION_CURRENT); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 112 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 113 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 114 | IPacket payload; |
| 115 | if (typeClassMap.containsKey(this.type)) { |
| 116 | Class<? extends IPacket> clazz = typeClassMap.get(this.type); |
| 117 | try { |
| 118 | payload = clazz.newInstance(); |
| 119 | } catch (Exception e) { |
| 120 | throw new RuntimeException("Error parsing payload for BSN packet" + e); |
| 121 | } |
| 122 | } else { |
| 123 | payload = new Data(); |
| 124 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 125 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 126 | this.payload = new Data(); |
| 127 | this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 128 | this.payload.setParent(this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 129 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 130 | return this; |
| 131 | } |
| 132 | |
| 133 | /* (non-Javadoc) |
| 134 | * @see java.lang.Object#hashCode() |
| 135 | */ |
| 136 | @Override |
| 137 | public int hashCode() { |
| 138 | final int prime = 883; |
| 139 | int result = super.hashCode(); |
| 140 | result = prime * result + version; |
| 141 | result = prime * result + type; |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | /* (non-Javadoc) |
| 146 | * @see java.lang.Object#equals(java.lang.Object) |
| 147 | */ |
| 148 | @Override |
| 149 | public boolean equals(Object obj) { |
| 150 | if (this == obj) |
| 151 | return true; |
| 152 | if (!super.equals(obj)) |
| 153 | return false; |
| 154 | if (!(obj instanceof BSN)) |
| 155 | return false; |
| 156 | BSN other = (BSN) obj; |
| 157 | return (type == other.type && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 158 | version == other.version); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 159 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 160 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 161 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 162 | StringBuffer sb = new StringBuffer("\n"); |
| 163 | sb.append("BSN packet"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | if (typeClassMap.containsKey(this.type)) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 165 | sb.append(" type: " + typeClassMap.get(this.type).getCanonicalName()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 166 | else |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 167 | sb.append(" type: " + this.type); |
| 168 | |
| 169 | return sb.toString(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 170 | } |
| 171 | } |