blob: 27c8f70df69cce5896f66a5d64522a2bc1c9f654 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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**/
16
17/**
18 *
19 */
20package net.floodlightcontroller.packet;
21
22import java.nio.ByteBuffer;
23import java.util.HashMap;
24import java.util.Map;
25
26/**
27 * @author Shudong Zhou (shudong.zhou@bigswitch.com)
28 *
29 */
30public class BSN extends BasePacket {
31 public static final int BSN_MAGIC = 0x20000604;
32 public static final short BSN_VERSION_CURRENT = 0x0;
33 public static final short BSN_TYPE_PROBE = 0x1;
34 public static final short BSN_TYPE_BDDP = 0x2;
35 public static Map<Short, Class<? extends IPacket>> typeClassMap;
36
37 static {
38 typeClassMap = new HashMap<Short, Class<? extends IPacket>>();
39 typeClassMap.put(BSN_TYPE_PROBE, BSNPROBE.class);
40 typeClassMap.put(BSN_TYPE_BDDP, LLDP.class);
41 }
42
43 protected short type;
44 protected short version;
45
46 public BSN() {
47 version = BSN_VERSION_CURRENT;
48 }
49
50 public BSN(short type) {
51 this.type = type;
52 version = BSN_VERSION_CURRENT;
53 }
54
55 public short getType() {
56 return type;
57 }
58
59 public BSN setType(short type) {
60 this.type = type;
61 return this;
62 }
63
64 public short getVersion() {
65 return version;
66 }
67
68 public BSN setVersion(short version) {
69 this.version = version;
70 return this;
71 }
72
73 @Override
74 public byte[] serialize() {
75 short length = 4 /* magic */ + 2 /* type */ + 2 /* version */;
76
77 byte[] payloadData = null;
78 if (this.payload != null) {
79 payload.setParent(this);
80 payloadData = payload.serialize();
81 length += payloadData.length;
82 }
83
84 byte[] data = new byte[length];
85 ByteBuffer bb = ByteBuffer.wrap(data);
86 bb.putInt(BSN_MAGIC);
87 bb.putShort(this.type);
88 bb.putShort(this.version);
89 if (payloadData != null)
90 bb.put(payloadData);
91
92 if (this.parent != null && this.parent instanceof Ethernet)
93 ((Ethernet)this.parent).setEtherType(Ethernet.TYPE_BSN);
94
95 return data;
96 }
97
98 @Override
99 public IPacket deserialize(byte[] data, int offset, int length) {
100 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
101
102 int magic = bb.getInt();
103 if (magic != BSN_MAGIC) {
104 throw new RuntimeException("Invalid BSN magic " + magic);
105 }
106
107 this.type = bb.getShort();
108 this.version = bb.getShort();
109 if (this.version != BSN_VERSION_CURRENT) {
110 throw new RuntimeException(
111 "Invalid BSN packet version " + this.version + ", should be "
112 + BSN_VERSION_CURRENT);
113 }
114
115 IPacket payload;
116 if (typeClassMap.containsKey(this.type)) {
117 Class<? extends IPacket> clazz = typeClassMap.get(this.type);
118 try {
119 payload = clazz.newInstance();
120 } catch (Exception e) {
121 throw new RuntimeException("Error parsing payload for BSN packet" + e);
122 }
123 } else {
124 payload = new Data();
125 }
126
127 this.payload = new Data();
128 this.payload = payload.deserialize(data, bb.position(), bb.limit() - bb.position());
129 this.payload.setParent(this);
130
131 return this;
132 }
133
134 /* (non-Javadoc)
135 * @see java.lang.Object#hashCode()
136 */
137 @Override
138 public int hashCode() {
139 final int prime = 883;
140 int result = super.hashCode();
141 result = prime * result + version;
142 result = prime * result + type;
143 return result;
144 }
145
146 /* (non-Javadoc)
147 * @see java.lang.Object#equals(java.lang.Object)
148 */
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj)
152 return true;
153 if (!super.equals(obj))
154 return false;
155 if (!(obj instanceof BSN))
156 return false;
157 BSN other = (BSN) obj;
158 return (type == other.type &&
159 version == other.version);
160 }
161
162 public String toString() {
163 StringBuffer sb = new StringBuffer("\n");
164 sb.append("BSN packet");
165 if (typeClassMap.containsKey(this.type))
166 sb.append(" type: " + typeClassMap.get(this.type).getCanonicalName());
167 else
168 sb.append(" type: " + this.type);
169
170 return sb.toString();
171 }
172}