blob: 7573ae896d530bbc11428c8f640f43ba9e394e58 [file] [log] [blame]
alshabibcaf1ca22015-06-25 15:18:16 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.packet;
17
18import com.google.common.collect.Maps;
19
20import java.util.Map;
21
22/**
23 * Representation of an Ethertype.
24 */
25public class EthType {
26
27 public static final short ARP = EtherType.ARP.ethType.toShort();
28 public static final short RARP = EtherType.RARP.ethType.toShort();
29 public static final short VLAN = EtherType.VLAN.ethType.toShort();
30 public static final short IPV4 = EtherType.IPV4.ethType.toShort();
31 public static final short IPV6 = EtherType.IPV6.ethType.toShort();
32 public static final short LLDP = EtherType.LLDP.ethType.toShort();
33 public static final short BDDP = EtherType.BDDP.ethType.toShort();
34 public static final short MPLS_MULTICAST = EtherType.MPLS_UNICAST.ethType.toShort();
35 public static final short MPLS_UNICAST = EtherType.MPLS_UNICAST.ethType.toShort();
36
37 private short etherType;
38
39 /*
40 * Reverse-lookup map for getting a EtherType enum
41 */
42 private static final Map<Short, EtherType> LOOKUP = Maps.newHashMap();
43
44 static {
45 for (EtherType eth : EtherType.values()) {
46 LOOKUP.put(eth.ethType().toShort(), eth);
47 }
48 }
49
50 public EthType(int etherType) {
51 this.etherType = (short) (etherType & 0xFFFF);
52 }
53
54 public EthType(short etherType) {
55 this.etherType = etherType;
56 }
57
58 public short toShort() {
59 return etherType;
60 }
61
62 public static EtherType lookup(short etherType) {
63 return LOOKUP.get(etherType);
64 }
65
66 @Override
67 public boolean equals(Object o) {
68 if (this == o) {
69 return true;
70 }
71 if (o == null || getClass() != o.getClass()) {
72 return false;
73 }
74
75 EthType ethType = (EthType) o;
76
77 if (etherType != ethType.etherType) {
78 return false;
79 }
80
81 return true;
82 }
83
84 @Override
85 public int hashCode() {
86 return (int) etherType;
87 }
88
89 public String toString() {
90 EtherType ethType = lookup(this.etherType);
91 return (ethType == null ? "unknown" : ethType.toString());
92 }
93
94 public static enum EtherType {
95
Jonathan Hart2a655752015-04-07 16:46:33 -070096 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());
alshabibcaf1ca22015-06-25 15:18:16 -0700105
106
107 private final Class clazz;
108 private EthType ethType;
109 private String type;
Jonathan Hart2a655752015-04-07 16:46:33 -0700110 private Deserializer<?> deserializer;
alshabibcaf1ca22015-06-25 15:18:16 -0700111
Jonathan Hart2a655752015-04-07 16:46:33 -0700112 EtherType(int ethType, String type, Class clazz, Deserializer deserializer) {
alshabibcaf1ca22015-06-25 15:18:16 -0700113 this.ethType = new EthType(ethType);
114 this.type = type;
115 this.clazz = clazz;
Jonathan Hart2a655752015-04-07 16:46:33 -0700116 this.deserializer = deserializer;
alshabibcaf1ca22015-06-25 15:18:16 -0700117 }
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
Jonathan Hart2a655752015-04-07 16:46:33 -0700132 public Deserializer<?> deserializer() {
133 return deserializer;
134 }
alshabibcaf1ca22015-06-25 15:18:16 -0700135 }
136}