blob: 0de2bcabcc2d86a0a4921c45578d5d3fb19f9a1d [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);
88 if (payloadData != null)
Ray Milkey269ffb92014-04-03 14:43:30 -070089 bb.put(payloadData);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080090
91 if (this.parent != null && this.parent instanceof Ethernet)
Ray Milkey269ffb92014-04-03 14:43:30 -070092 ((Ethernet) this.parent).setEtherType(Ethernet.TYPE_BSN);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093
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 Milkey269ffb92014-04-03 14:43:30 -0700100
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800101 int magic = bb.getInt();
102 if (magic != BSN_MAGIC) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 throw new RuntimeException("Invalid BSN magic " + magic);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700105
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 this.type = bb.getShort();
107 this.version = bb.getShort();
108 if (this.version != BSN_VERSION_CURRENT) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 throw new RuntimeException(
110 "Invalid BSN packet version " + this.version + ", should be "
111 + BSN_VERSION_CURRENT);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800112 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700113
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 IPacket payload;
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700115 if (TYPE_CLASS_MAP.containsKey(this.type)) {
116 Class<? extends IPacket> clazz = TYPE_CLASS_MAP.get(this.type);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800117 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 Milkey269ffb92014-04-03 14:43:30 -0700125
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800126 this.payload = new Data();
127 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
128 this.payload.setParent(this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700129
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800130 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 Milkey269ffb92014-04-03 14:43:30 -0700158 version == other.version);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800159 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700160
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800161 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 StringBuffer sb = new StringBuffer("\n");
163 sb.append("BSN packet");
Pavlin Radoslavov608fac32014-04-09 12:40:24 -0700164 if (TYPE_CLASS_MAP.containsKey(this.type))
165 sb.append(" type: " + TYPE_CLASS_MAP.get(this.type).getCanonicalName());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800166 else
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 sb.append(" type: " + this.type);
168
169 return sb.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800170 }
171}