alshabib

small fixes to ethtype pattern

Change-Id: Ic58c426821952f66aa21bc828d36fd4f83d8da0d
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
16 16
17 package org.onosproject.aaa.packet; 17 package org.onosproject.aaa.packet;
18 18
19 -import org.onlab.packet.ARP; 19 +import org.onlab.packet.Deserializer;
20 +import org.onlab.packet.EthType;
20 import org.onlab.packet.Ethernet; 21 import org.onlab.packet.Ethernet;
21 -import org.onlab.packet.IPv4; 22 +import org.onlab.packet.IPacket;
22 -import org.onlab.packet.IPv6; 23 +
23 -import org.onlab.packet.LLDP; 24 +import java.util.HashMap;
25 +import java.util.Map;
24 26
25 /** 27 /**
26 * Created by jono on 5/19/15. 28 * Created by jono on 5/19/15.
...@@ -29,17 +31,21 @@ public final class EAPEthernet extends Ethernet { ...@@ -29,17 +31,21 @@ public final class EAPEthernet extends Ethernet {
29 31
30 public static final short TYPE_PAE = (short) 0x888e; 32 public static final short TYPE_PAE = (short) 0x888e;
31 33
34 + private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP =
35 + new HashMap<>();
36 +
32 private EAPEthernet() { 37 private EAPEthernet() {
33 38
34 } 39 }
35 40
36 static { 41 static {
37 - Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_ARP, ARP.class); 42 + for (EthType.EtherType ethType : EthType.EtherType.values()) {
38 - org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_RARP, ARP.class); 43 + if (ethType.deserializer() != null) {
39 - org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_IPV4, IPv4.class); 44 + ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(),
40 - org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_IPV6, IPv6.class); 45 + ethType.deserializer());
41 - org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_LLDP, LLDP.class); 46 + }
42 - org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_BSN, LLDP.class); 47 + }
43 - org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(TYPE_PAE, EAPOL.class); 48 + ETHERTYPE_DESERIALIZER_MAP.put((short) 0x888e, EAPOL.deserializer());
44 } 49 }
50 +
45 } 51 }
......
...@@ -19,12 +19,15 @@ ...@@ -19,12 +19,15 @@
19 package org.onosproject.aaa.packet; 19 package org.onosproject.aaa.packet;
20 20
21 import org.onlab.packet.BasePacket; 21 import org.onlab.packet.BasePacket;
22 +import org.onlab.packet.Deserializer;
22 import org.onlab.packet.Ethernet; 23 import org.onlab.packet.Ethernet;
23 import org.onlab.packet.IPacket; 24 import org.onlab.packet.IPacket;
24 import org.onlab.packet.MacAddress; 25 import org.onlab.packet.MacAddress;
25 26
26 import java.nio.ByteBuffer; 27 import java.nio.ByteBuffer;
27 28
29 +import static org.onlab.packet.PacketUtils.checkInput;
30 +
28 /** 31 /**
29 * 32 *
30 */ 33 */
...@@ -133,29 +136,8 @@ public class EAPOL extends BasePacket { ...@@ -133,29 +136,8 @@ public class EAPOL extends BasePacket {
133 return data; 136 return data;
134 } 137 }
135 138
136 - @Override
137 - public IPacket deserialize(final byte[] data, final int offset,
138 - final int length) {
139 - final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
140 139
141 140
142 - //deserialize the EAPOL header
143 - this.version = bb.get();
144 - this.eapolType = bb.get();
145 - this.packetLength = bb.getShort();
146 -
147 - if (this.packetLength > 0) {
148 - //deserialize the EAP Payload
149 - this.payload = new EAP();
150 -
151 - this.payload = this.payload.deserialize(data, bb.position(), length - 4);
152 - this.payload.setParent(this);
153 - }
154 -
155 -
156 - return this;
157 - }
158 -
159 @Override 141 @Override
160 public int hashCode() { 142 public int hashCode() {
161 final int prime = 3889; 143 final int prime = 3889;
...@@ -196,5 +178,51 @@ public class EAPOL extends BasePacket { ...@@ -196,5 +178,51 @@ public class EAPOL extends BasePacket {
196 eth.setPad(true); 178 eth.setPad(true);
197 return eth; 179 return eth;
198 } 180 }
181 +
182 + public static Deserializer<EAPOL> deserializer() {
183 + return (data, offset, length) -> {
184 + checkInput(data, offset, length, 0);
185 +
186 + EAPOL eapol = new EAPOL();
187 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
188 + eapol.setVersion(bb.get());
189 + eapol.setEapolType(bb.get());
190 + eapol.setPacketLength(bb.getShort());
191 +
192 + if (eapol.packetLength > 0) {
193 + //deserialize the EAP Payload
194 + eapol.payload = new EAP();
195 +
196 + eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
197 + eapol.payload.setParent(eapol);
198 + }
199 + return eapol;
200 + };
201 + }
202 +
203 + @Override
204 + public IPacket deserialize(final byte[] data, final int offset,
205 + final int length) {
206 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
207 +
208 +
209 + //deserialize the EAPOL header
210 + this.version = bb.get();
211 + this.eapolType = bb.get();
212 + this.packetLength = bb.getShort();
213 +
214 + if (this.packetLength > 0) {
215 + //deserialize the EAP Payload
216 + this.payload = new EAP();
217 +
218 + this.payload = this.payload.deserialize(data, bb.position(), length - 4);
219 + this.payload.setParent(this);
220 + }
221 +
222 +
223 + return this;
224 + }
225 +
226 +
199 } 227 }
200 228
......
...@@ -19,6 +19,7 @@ import com.google.common.base.MoreObjects; ...@@ -19,6 +19,7 @@ import com.google.common.base.MoreObjects;
19 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Lists; 20 import com.google.common.collect.Lists;
21 import org.apache.commons.collections.ListUtils; 21 import org.apache.commons.collections.ListUtils;
22 +import org.onlab.packet.EthType;
22 import org.onlab.packet.IpAddress; 23 import org.onlab.packet.IpAddress;
23 import org.onlab.packet.MacAddress; 24 import org.onlab.packet.MacAddress;
24 import org.onlab.packet.MplsLabel; 25 import org.onlab.packet.MplsLabel;
...@@ -309,6 +310,11 @@ public final class DefaultTrafficTreatment implements TrafficTreatment { ...@@ -309,6 +310,11 @@ public final class DefaultTrafficTreatment implements TrafficTreatment {
309 310
310 @Override 311 @Override
311 public Builder popMpls(int etherType) { 312 public Builder popMpls(int etherType) {
313 + return add(Instructions.popMpls(new EthType(etherType)));
314 + }
315 +
316 + @Override
317 + public Builder popMpls(EthType etherType) {
312 return add(Instructions.popMpls(etherType)); 318 return add(Instructions.popMpls(etherType));
313 } 319 }
314 320
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.net.flow; 16 package org.onosproject.net.flow;
17 17
18 +import org.onlab.packet.EthType;
18 import org.onlab.packet.IpAddress; 19 import org.onlab.packet.IpAddress;
19 import org.onlab.packet.MacAddress; 20 import org.onlab.packet.MacAddress;
20 import org.onlab.packet.MplsLabel; 21 import org.onlab.packet.MplsLabel;
...@@ -197,9 +198,18 @@ public interface TrafficTreatment { ...@@ -197,9 +198,18 @@ public interface TrafficTreatment {
197 * @param etherType an ether type 198 * @param etherType an ether type
198 * @return a treatment builder. 199 * @return a treatment builder.
199 */ 200 */
201 + @Deprecated
200 Builder popMpls(int etherType); 202 Builder popMpls(int etherType);
201 203
202 /** 204 /**
205 + * Pops MPLS ether type and set the new ethertype.
206 + *
207 + * @param etherType an ether type
208 + * @return a treatment builder.
209 + */
210 + Builder popMpls(EthType etherType);
211 +
212 + /**
203 * Sets the mpls label. 213 * Sets the mpls label.
204 * 214 *
205 * @param mplsLabel MPLS label. 215 * @param mplsLabel MPLS label.
......
...@@ -25,8 +25,9 @@ import static com.google.common.base.MoreObjects.toStringHelper; ...@@ -25,8 +25,9 @@ import static com.google.common.base.MoreObjects.toStringHelper;
25 * Implementation of Ethernet type criterion (16 bits unsigned integer). 25 * Implementation of Ethernet type criterion (16 bits unsigned integer).
26 */ 26 */
27 public final class EthTypeCriterion implements Criterion { 27 public final class EthTypeCriterion implements Criterion {
28 - private static final int MASK = 0xffff; 28 +
29 - private final EthType ethType; // Ethernet type value: 16 bits 29 +
30 + private final EthType ethType;
30 31
31 /** 32 /**
32 * Constructor. 33 * Constructor.
...@@ -35,7 +36,7 @@ public final class EthTypeCriterion implements Criterion { ...@@ -35,7 +36,7 @@ public final class EthTypeCriterion implements Criterion {
35 * integer) 36 * integer)
36 */ 37 */
37 EthTypeCriterion(int ethType) { 38 EthTypeCriterion(int ethType) {
38 - this.ethType = new EthType(ethType & MASK); 39 + this.ethType = new EthType(ethType);
39 } 40 }
40 41
41 /** 42 /**
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 */ 15 */
16 package org.onosproject.net.flow.instructions; 16 package org.onosproject.net.flow.instructions;
17 17
18 -import org.onlab.packet.Ethernet; 18 +import org.onlab.packet.EthType;
19 import org.onlab.packet.IpAddress; 19 import org.onlab.packet.IpAddress;
20 import org.onlab.packet.MacAddress; 20 import org.onlab.packet.MacAddress;
21 import org.onlab.packet.MplsLabel; 21 import org.onlab.packet.MplsLabel;
...@@ -265,7 +265,7 @@ public final class Instructions { ...@@ -265,7 +265,7 @@ public final class Instructions {
265 public static Instruction pushMpls() { 265 public static Instruction pushMpls() {
266 return new L2ModificationInstruction.PushHeaderInstructions( 266 return new L2ModificationInstruction.PushHeaderInstructions(
267 L2ModificationInstruction.L2SubType.MPLS_PUSH, 267 L2ModificationInstruction.L2SubType.MPLS_PUSH,
268 - Ethernet.MPLS_UNICAST); 268 + EthType.EtherType.MPLS_UNICAST.ethType());
269 } 269 }
270 270
271 /** 271 /**
...@@ -276,7 +276,7 @@ public final class Instructions { ...@@ -276,7 +276,7 @@ public final class Instructions {
276 public static Instruction popMpls() { 276 public static Instruction popMpls() {
277 return new L2ModificationInstruction.PushHeaderInstructions( 277 return new L2ModificationInstruction.PushHeaderInstructions(
278 L2ModificationInstruction.L2SubType.MPLS_POP, 278 L2ModificationInstruction.L2SubType.MPLS_POP,
279 - Ethernet.MPLS_UNICAST); 279 + EthType.EtherType.MPLS_UNICAST.ethType());
280 } 280 }
281 281
282 /** 282 /**
...@@ -285,9 +285,23 @@ public final class Instructions { ...@@ -285,9 +285,23 @@ public final class Instructions {
285 * @param etherType Ethernet type to set 285 * @param etherType Ethernet type to set
286 * @return a L2 modification. 286 * @return a L2 modification.
287 */ 287 */
288 + @Deprecated
288 public static Instruction popMpls(int etherType) { 289 public static Instruction popMpls(int etherType) {
289 checkNotNull(etherType, "Ethernet type cannot be null"); 290 checkNotNull(etherType, "Ethernet type cannot be null");
290 return new L2ModificationInstruction.PushHeaderInstructions( 291 return new L2ModificationInstruction.PushHeaderInstructions(
292 + L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType));
293 + }
294 +
295 +
296 + /**
297 + * Creates a pop MPLS header instruction with a particular ethertype.
298 + *
299 + * @param etherType Ethernet type to set
300 + * @return a L2 modification.
301 + */
302 + public static Instruction popMpls(EthType etherType) {
303 + checkNotNull(etherType, "Ethernet type cannot be null");
304 + return new L2ModificationInstruction.PushHeaderInstructions(
291 L2ModificationInstruction.L2SubType.MPLS_POP, etherType); 305 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
292 } 306 }
293 307
...@@ -308,7 +322,8 @@ public final class Instructions { ...@@ -308,7 +322,8 @@ public final class Instructions {
308 */ 322 */
309 public static Instruction pushVlan() { 323 public static Instruction pushVlan() {
310 return new L2ModificationInstruction.PushHeaderInstructions( 324 return new L2ModificationInstruction.PushHeaderInstructions(
311 - L2ModificationInstruction.L2SubType.VLAN_PUSH, Ethernet.TYPE_VLAN); 325 + L2ModificationInstruction.L2SubType.VLAN_PUSH,
326 + EthType.EtherType.VLAN.ethType());
312 } 327 }
313 328
314 /** 329 /**
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.net.flow.instructions; 16 package org.onosproject.net.flow.instructions;
17 17
18 +import org.onlab.packet.EthType;
18 import org.onlab.packet.MacAddress; 19 import org.onlab.packet.MacAddress;
19 import org.onlab.packet.MplsLabel; 20 import org.onlab.packet.MplsLabel;
20 import org.onlab.packet.VlanId; 21 import org.onlab.packet.VlanId;
...@@ -145,16 +146,16 @@ public abstract class L2ModificationInstruction implements Instruction { ...@@ -145,16 +146,16 @@ public abstract class L2ModificationInstruction implements Instruction {
145 public static final class PushHeaderInstructions extends 146 public static final class PushHeaderInstructions extends
146 L2ModificationInstruction { 147 L2ModificationInstruction {
147 148
148 - private static final int MASK = 0xffff; 149 +
149 private final L2SubType subtype; 150 private final L2SubType subtype;
150 - private final int ethernetType; // Ethernet type value: 16 bits 151 + private final EthType ethernetType; // Ethernet type value: 16 bits
151 152
152 - PushHeaderInstructions(L2SubType subType, int ethernetType) { 153 + PushHeaderInstructions(L2SubType subType, EthType ethernetType) {
153 this.subtype = subType; 154 this.subtype = subType;
154 - this.ethernetType = ethernetType & MASK; 155 + this.ethernetType = ethernetType;
155 } 156 }
156 157
157 - public int ethernetType() { 158 + public EthType ethernetType() {
158 return ethernetType; 159 return ethernetType;
159 } 160 }
160 161
...@@ -166,7 +167,7 @@ public abstract class L2ModificationInstruction implements Instruction { ...@@ -166,7 +167,7 @@ public abstract class L2ModificationInstruction implements Instruction {
166 @Override 167 @Override
167 public String toString() { 168 public String toString() {
168 return toStringHelper(subtype().toString()) 169 return toStringHelper(subtype().toString())
169 - .add("ethernetType", String.format("0x%04x", ethernetType())) 170 + .add("ethernetType", ethernetType())
170 .toString(); 171 .toString();
171 } 172 }
172 173
......
...@@ -119,7 +119,8 @@ public final class EncodeInstructionCodec { ...@@ -119,7 +119,8 @@ public final class EncodeInstructionCodec {
119 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions = 119 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
120 (L2ModificationInstruction.PushHeaderInstructions) instruction; 120 (L2ModificationInstruction.PushHeaderInstructions) instruction;
121 121
122 - result.put(InstructionCodec.ETHERNET_TYPE, pushHeaderInstructions.ethernetType()); 122 + result.put(InstructionCodec.ETHERNET_TYPE,
123 + pushHeaderInstructions.ethernetType().toShort());
123 break; 124 break;
124 125
125 default: 126 default:
......
...@@ -247,15 +247,15 @@ public class FlowRuleCodecTest { ...@@ -247,15 +247,15 @@ public class FlowRuleCodecTest {
247 instruction = getInstruction(Instruction.Type.L2MODIFICATION, 247 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
248 L2ModificationInstruction.L2SubType.MPLS_PUSH.name()); 248 L2ModificationInstruction.L2SubType.MPLS_PUSH.name());
249 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); 249 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
250 - assertThat((short) ((L2ModificationInstruction.PushHeaderInstructions) instruction) 250 + assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction)
251 - .ethernetType(), 251 + .ethernetType().toShort(),
252 is(Ethernet.MPLS_UNICAST)); 252 is(Ethernet.MPLS_UNICAST));
253 253
254 instruction = getInstruction(Instruction.Type.L2MODIFICATION, 254 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
255 L2ModificationInstruction.L2SubType.MPLS_POP.name()); 255 L2ModificationInstruction.L2SubType.MPLS_POP.name());
256 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); 256 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
257 - assertThat((short) ((L2ModificationInstruction.PushHeaderInstructions) instruction) 257 + assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction)
258 - .ethernetType(), 258 + .ethernetType().toShort(),
259 is(Ethernet.MPLS_UNICAST)); 259 is(Ethernet.MPLS_UNICAST));
260 260
261 instruction = getInstruction(Instruction.Type.L2MODIFICATION, 261 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
......
...@@ -66,7 +66,7 @@ public final class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<Json ...@@ -66,7 +66,7 @@ public final class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<Json
66 return false; 66 return false;
67 } 67 }
68 68
69 - if (instructionToMatch.ethernetType() != ethJson.asInt()) { 69 + if (instructionToMatch.ethernetType().toShort() != ethJson.asInt()) {
70 description.appendText("ethernetType was " + ethJson); 70 description.appendText("ethernetType was " + ethJson);
71 return false; 71 return false;
72 } 72 }
......
...@@ -316,12 +316,12 @@ public class FlowModBuilderVer13 extends FlowModBuilder { ...@@ -316,12 +316,12 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
316 PushHeaderInstructions pushHeaderInstructions = 316 PushHeaderInstructions pushHeaderInstructions =
317 (PushHeaderInstructions) l2m; 317 (PushHeaderInstructions) l2m;
318 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions 318 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
319 - .ethernetType())); 319 + .ethernetType().toShort()));
320 case MPLS_POP: 320 case MPLS_POP:
321 PushHeaderInstructions popHeaderInstructions = 321 PushHeaderInstructions popHeaderInstructions =
322 (PushHeaderInstructions) l2m; 322 (PushHeaderInstructions) l2m;
323 return factory().actions().popMpls(EthType.of(popHeaderInstructions 323 return factory().actions().popMpls(EthType.of(popHeaderInstructions
324 - .ethernetType())); 324 + .ethernetType().toShort()));
325 case MPLS_LABEL: 325 case MPLS_LABEL:
326 ModMplsLabelInstruction mplsLabel = 326 ModMplsLabelInstruction mplsLabel =
327 (ModMplsLabelInstruction) l2m; 327 (ModMplsLabelInstruction) l2m;
...@@ -335,7 +335,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder { ...@@ -335,7 +335,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
335 case VLAN_PUSH: 335 case VLAN_PUSH:
336 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m; 336 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
337 return factory().actions().pushVlan( 337 return factory().actions().pushVlan(
338 - EthType.of(pushVlanInstruction.ethernetType())); 338 + EthType.of(pushVlanInstruction.ethernetType().toShort()));
339 default: 339 default:
340 log.warn("Unimplemented action type {}.", l2m.subtype()); 340 log.warn("Unimplemented action type {}.", l2m.subtype());
341 break; 341 break;
......
...@@ -269,17 +269,17 @@ public final class GroupModBuilder { ...@@ -269,17 +269,17 @@ public final class GroupModBuilder {
269 L2ModificationInstruction.PushHeaderInstructions pushVlanInstruction 269 L2ModificationInstruction.PushHeaderInstructions pushVlanInstruction
270 = (L2ModificationInstruction.PushHeaderInstructions) l2m; 270 = (L2ModificationInstruction.PushHeaderInstructions) l2m;
271 return factory.actions().pushVlan( 271 return factory.actions().pushVlan(
272 - EthType.of(pushVlanInstruction.ethernetType())); 272 + EthType.of(pushVlanInstruction.ethernetType().toShort()));
273 case MPLS_PUSH: 273 case MPLS_PUSH:
274 L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions = 274 L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
275 (L2ModificationInstruction.PushHeaderInstructions) l2m; 275 (L2ModificationInstruction.PushHeaderInstructions) l2m;
276 return factory.actions().pushMpls(EthType.of(pushHeaderInstructions 276 return factory.actions().pushMpls(EthType.of(pushHeaderInstructions
277 - .ethernetType())); 277 + .ethernetType().toShort()));
278 case MPLS_POP: 278 case MPLS_POP:
279 L2ModificationInstruction.PushHeaderInstructions popHeaderInstructions = 279 L2ModificationInstruction.PushHeaderInstructions popHeaderInstructions =
280 (L2ModificationInstruction.PushHeaderInstructions) l2m; 280 (L2ModificationInstruction.PushHeaderInstructions) l2m;
281 return factory.actions().popMpls(EthType.of(popHeaderInstructions 281 return factory.actions().popMpls(EthType.of(popHeaderInstructions
282 - .ethernetType())); 282 + .ethernetType().toShort()));
283 case MPLS_LABEL: 283 case MPLS_LABEL:
284 L2ModificationInstruction.ModMplsLabelInstruction mplsLabel = 284 L2ModificationInstruction.ModMplsLabelInstruction mplsLabel =
285 (L2ModificationInstruction.ModMplsLabelInstruction) l2m; 285 (L2ModificationInstruction.ModMplsLabelInstruction) l2m;
......
...@@ -15,52 +15,106 @@ ...@@ -15,52 +15,106 @@
15 */ 15 */
16 package org.onlab.packet; 16 package org.onlab.packet;
17 17
18 -import com.google.common.collect.Maps;
19 -
20 -import java.util.Map;
21 -
22 /** 18 /**
23 * Representation of an Ethertype. 19 * Representation of an Ethertype.
24 */ 20 */
25 public class EthType { 21 public class EthType {
26 22
27 - public static final short ARP = EtherType.ARP.ethType.toShort(); 23 + /**
28 - public static final short RARP = EtherType.RARP.ethType.toShort(); 24 + * A list of known ethertypes. Adding a fully defined enum here will
29 - public static final short VLAN = EtherType.VLAN.ethType.toShort(); 25 + * associated the ethertype with a textual representation and a parsing
30 - public static final short IPV4 = EtherType.IPV4.ethType.toShort(); 26 + * class.
31 - public static final short IPV6 = EtherType.IPV6.ethType.toShort(); 27 + */
32 - public static final short LLDP = EtherType.LLDP.ethType.toShort(); 28 + public enum EtherType {
33 - public static final short BDDP = EtherType.BDDP.ethType.toShort(); 29 +
34 - public static final short MPLS_MULTICAST = EtherType.MPLS_UNICAST.ethType.toShort(); 30 + ARP(0x806, "arp", org.onlab.packet.ARP.deserializer()),
35 - public static final short MPLS_UNICAST = EtherType.MPLS_UNICAST.ethType.toShort(); 31 + RARP(0x8035, "rarp", org.onlab.packet.ARP.deserializer()),
32 + IPV4(0x800, "ipv4", org.onlab.packet.IPv4.deserializer()),
33 + IPV6(0x86dd, "ipv6", org.onlab.packet.IPv6.deserializer()),
34 + LLDP(0x88cc, "lldp", org.onlab.packet.LLDP.deserializer()),
35 + VLAN(0x8100, "vlan", null),
36 + BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()),
37 + MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
38 + MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer());
39 +
40 +
41 +
42 + private final EthType etherType;
43 + private final String type;
44 + private final Deserializer<?> deserializer;
45 +
46 + /**
47 + * Constucts a new ethertype.
48 + *
49 + * @param ethType The actual ethertype
50 + * @param type it's textual representation
51 + * @param deserializer a parser for this ethertype
52 + */
53 + EtherType(int ethType, String type, Deserializer<?> deserializer) {
54 + this.etherType = new EthType(ethType);
55 + this.type = type;
56 + this.deserializer = deserializer;
57 + }
36 58
37 - private short etherType; 59 + public EthType ethType() {
60 + return etherType;
61 + }
38 62
39 - /* 63 + @Override
40 - * Reverse-lookup map for getting a EtherType enum 64 + public String toString() {
41 - */ 65 + return type;
42 - private static final Map<Short, EtherType> LOOKUP = Maps.newHashMap(); 66 + }
43 67
44 - static { 68 + public Deserializer<?> deserializer() {
45 - for (EtherType eth : EtherType.values()) { 69 + return deserializer;
46 - LOOKUP.put(eth.ethType().toShort(), eth);
47 } 70 }
71 +
48 } 72 }
49 73
74 +
75 + private final short etherType;
76 +
77 + /**
78 + * Builds the EthType.
79 + *
80 + * @param etherType an integer representing an ethtype
81 + */
50 public EthType(int etherType) { 82 public EthType(int etherType) {
51 this.etherType = (short) (etherType & 0xFFFF); 83 this.etherType = (short) (etherType & 0xFFFF);
52 } 84 }
53 85
86 + /**
87 + * Builds the EthType.
88 + *
89 + * @param etherType a short representing the ethtype
90 + */
54 public EthType(short etherType) { 91 public EthType(short etherType) {
55 this.etherType = etherType; 92 this.etherType = etherType;
56 } 93 }
57 94
95 + /**
96 + * Returns the short value for this ethtype.
97 + *
98 + * @return a short value
99 + */
58 public short toShort() { 100 public short toShort() {
59 return etherType; 101 return etherType;
60 } 102 }
61 103
62 - public static EtherType lookup(short etherType) { 104 + /**
63 - return LOOKUP.get(etherType); 105 + * Looks up the ethertype by it's numerical representation
106 + * and returns it's textual format.
107 + *
108 + * @param etherType the short value of the ethertype
109 + * @return a textual representation
110 + */
111 + public EtherType lookup(short etherType) {
112 + for (EtherType ethType : EtherType.values()) {
113 + if (ethType.ethType().toShort() == etherType) {
114 + return ethType;
115 + }
116 + }
117 + return null;
64 } 118 }
65 119
66 @Override 120 @Override
...@@ -88,49 +142,8 @@ public class EthType { ...@@ -88,49 +142,8 @@ public class EthType {
88 142
89 public String toString() { 143 public String toString() {
90 EtherType ethType = lookup(this.etherType); 144 EtherType ethType = lookup(this.etherType);
91 - return (ethType == null ? "unknown" : ethType.toString()); 145 + return (ethType == null ? String.format("0x%04x", etherType) :
146 + ethType.toString());
92 } 147 }
93 148
94 - public static enum EtherType {
95 -
96 - ARP(0x806, "arp", ARP.class, org.onlab.packet.ARP.deserializer()),
97 - RARP(0x8035, "rarp", null, org.onlab.packet.ARP.deserializer()),
98 - IPV4(0x800, "ipv4", IPv4.class, org.onlab.packet.IPv4.deserializer()),
99 - IPV6(0x86dd, "ipv6", IPv6.class, org.onlab.packet.IPv6.deserializer()),
100 - LLDP(0x88cc, "lldp", LLDP.class, org.onlab.packet.LLDP.deserializer()),
101 - VLAN(0x8100, "vlan", null, null),
102 - BDDP(0x8942, "bddp", LLDP.class, org.onlab.packet.LLDP.deserializer()),
103 - MPLS_UNICAST(0x8847, "mpls_unicast", null, org.onlab.packet.MPLS.deserializer()),
104 - MPLS_MULTICAST(0x8848, "mpls_unicast", null, org.onlab.packet.MPLS.deserializer());
105 -
106 -
107 - private final Class clazz;
108 - private EthType ethType;
109 - private String type;
110 - private Deserializer<?> deserializer;
111 -
112 - EtherType(int ethType, String type, Class clazz, Deserializer deserializer) {
113 - this.ethType = new EthType(ethType);
114 - this.type = type;
115 - this.clazz = clazz;
116 - this.deserializer = deserializer;
117 - }
118 -
119 - public EthType ethType() {
120 - return ethType;
121 - }
122 -
123 - @Override
124 - public String toString() {
125 - return type;
126 - }
127 -
128 - public Class clazz() {
129 - return clazz;
130 - }
131 -
132 - public Deserializer<?> deserializer() {
133 - return deserializer;
134 - }
135 - }
136 } 149 }
......
...@@ -32,16 +32,17 @@ import static org.onlab.packet.PacketUtils.checkInput; ...@@ -32,16 +32,17 @@ import static org.onlab.packet.PacketUtils.checkInput;
32 */ 32 */
33 public class Ethernet extends BasePacket { 33 public class Ethernet extends BasePacket {
34 private static final String HEXES = "0123456789ABCDEF"; 34 private static final String HEXES = "0123456789ABCDEF";
35 - public static final short TYPE_ARP = EthType.ARP;
36 - public static final short TYPE_RARP = EthType.RARP;
37 - public static final short TYPE_IPV4 = EthType.IPV4;
38 - public static final short TYPE_IPV6 = EthType.IPV6;
39 - public static final short TYPE_LLDP = EthType.LLDP;
40 - public static final short TYPE_VLAN = EthType.VLAN;
41 - public static final short TYPE_BSN = EthType.BDDP;
42 35
43 - public static final short MPLS_UNICAST = EthType.MPLS_UNICAST; 36 + public static final short TYPE_ARP = EthType.EtherType.ARP.ethType().toShort();
44 - public static final short MPLS_MULTICAST = EthType.MPLS_MULTICAST; 37 + public static final short TYPE_RARP = EthType.EtherType.RARP.ethType().toShort();
38 + public static final short TYPE_IPV4 = EthType.EtherType.IPV4.ethType().toShort();
39 + public static final short TYPE_IPV6 = EthType.EtherType.IPV6.ethType().toShort();
40 + public static final short TYPE_LLDP = EthType.EtherType.LLDP.ethType().toShort();
41 + public static final short TYPE_VLAN = EthType.EtherType.VLAN.ethType().toShort();
42 + public static final short TYPE_BSN = EthType.EtherType.BDDP.ethType().toShort();
43 +
44 + public static final short MPLS_UNICAST = EthType.EtherType.MPLS_UNICAST.ethType().toShort();;
45 + public static final short MPLS_MULTICAST = EthType.EtherType.MPLS_MULTICAST.ethType().toShort();
45 46
46 47
47 public static final short VLAN_UNTAGGED = (short) 0xffff; 48 public static final short VLAN_UNTAGGED = (short) 0xffff;
...@@ -56,7 +57,7 @@ public class Ethernet extends BasePacket { ...@@ -56,7 +57,7 @@ public class Ethernet extends BasePacket {
56 57
57 static { 58 static {
58 for (EthType.EtherType ethType : EthType.EtherType.values()) { 59 for (EthType.EtherType ethType : EthType.EtherType.values()) {
59 - if (ethType.clazz() != null) { 60 + if (ethType.deserializer() != null) {
60 ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(), ethType.deserializer()); 61 ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(), ethType.deserializer());
61 } 62 }
62 } 63 }
......