blob: 654da68cc26105a2445c3fbf736a2dbe664a8316 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080016
17/**
Ray Milkey269ffb92014-04-03 14:43:30 -070018 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019 */
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070020package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080021
22import java.nio.ByteBuffer;
23import java.util.HashMap;
24import java.util.Map;
25
26/**
27 * @author Shudong Zhou (shudong.zhou@bigswitch.com)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028 */
29public class BSN extends BasePacket {
Ray Milkey269ffb92014-04-03 14:43:30 -070030 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 Radoslavov608fac32014-04-09 12:40:24 -070034 public static final Map<Short, Class<? extends IPacket>> TYPE_CLASS_MAP;
Ray Milkey269ffb92014-04-03 14:43:30 -070035
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 static {
Pavlin Radoslavov608fac32014-04-09 12:40:24 -070037 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 Krishnaswamy345ee992012-12-13 20:29:48 -080040 }
41
Ray Milkey269ffb92014-04-03 14:43:30 -070042 protected short type;
43 protected short version;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 public BSN() {
46 version = BSN_VERSION_CURRENT;
47 }
48
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080049 public BSN(short type) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 this.type = type;
51 version = BSN_VERSION_CURRENT;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052 }
53
54 public short getType() {
Ray Milkey269ffb92014-04-03 14:43:30 -070055 return type;
56 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 public BSN setType(short type) {
59 this.type = type;
60 return this;
61 }
62
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 public short getVersion() {
Ray Milkey269ffb92014-04-03 14:43:30 -070064 return version;
65 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 public BSN setVersion(short version) {
68 this.version = version;
69 return this;
70 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071
72 @Override
73 public byte[] serialize() {
Ray Milkey269ffb92014-04-03 14:43:30 -070074 short length = 4 /* magic */ + 2 /* type */ + 2 /* version */;
75
76 byte[] payloadData = null;
77 if (this.payload != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080078 payload.setParent(this);
79 payloadData = payload.serialize();
80 length += payloadData.length;
81 }
Ray Milkey269ffb92014-04-03 14:43:30 -070082
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083 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 Milkeyb29e6262014-04-09 16:02:14 -070088 if (payloadData != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070089 bb.put(payloadData);
Ray Milkeyb29e6262014-04-09 16:02:14 -070090 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091
Ray Milkeyb29e6262014-04-09 16:02:14 -070092 if (this.parent != null && this.parent instanceof Ethernet) {
Ray Milkey269ffb92014-04-03 14:43:30 -070093 ((Ethernet) this.parent).setEtherType(Ethernet.TYPE_BSN);
Ray Milkeyb29e6262014-04-09 16:02:14 -070094 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095
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 Milkey269ffb92014-04-03 14:43:30 -0700102
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 int magic = bb.getInt();
104 if (magic != BSN_MAGIC) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 throw new RuntimeException("Invalid BSN magic " + magic);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700107
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 this.type = bb.getShort();
109 this.version = bb.getShort();
110 if (this.version != BSN_VERSION_CURRENT) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 throw new RuntimeException(
112 "Invalid BSN packet version " + this.version + ", should be "
113 + BSN_VERSION_CURRENT);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700115
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800116 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700117 if (TYPE_CLASS_MAP.containsKey(this.type)) {
118 Class<? extends IPacket> clazz = TYPE_CLASS_MAP.get(this.type);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800119 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 Milkey269ffb92014-04-03 14:43:30 -0700127
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800128 this.payload = new Data();
129 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
130 this.payload.setParent(this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700131
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800132 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 Milkeyb29e6262014-04-09 16:02:14 -0700152 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800153 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700154 }
155 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800156 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700157 }
158 if (!(obj instanceof BSN)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800159 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700160 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800161 BSN other = (BSN) obj;
162 return (type == other.type &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700163 version == other.version);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700165
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800166 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 StringBuffer sb = new StringBuffer("\n");
168 sb.append("BSN packet");
Ray Milkeyb29e6262014-04-09 16:02:14 -0700169 if (TYPE_CLASS_MAP.containsKey(this.type)) {
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700170 sb.append(" type: " + TYPE_CLASS_MAP.get(this.type).getCanonicalName());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700171 } else {
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 sb.append(" type: " + this.type);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700173 }
174
Ray Milkey269ffb92014-04-03 14:43:30 -0700175
176 return sb.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800177 }
178}