blob: 561c9307f8f59f2b4cb6577f620d0e6fbb3aa3bf [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
alshabibcaf1ca22015-06-25 15:18:16 -070018/**
19 * Representation of an Ethertype.
20 */
21public class EthType {
22
alshabib7b808c52015-06-26 14:22:24 -070023 /**
24 * A list of known ethertypes. Adding a fully defined enum here will
25 * associated the ethertype with a textual representation and a parsing
26 * class.
alshabibcaf1ca22015-06-25 15:18:16 -070027 */
alshabib7b808c52015-06-26 14:22:24 -070028 public enum EtherType {
alshabibcaf1ca22015-06-25 15:18:16 -070029
alshabib7b808c52015-06-26 14:22:24 -070030 ARP(0x806, "arp", org.onlab.packet.ARP.deserializer()),
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()),
Jonathan Hart4a60bb32015-06-30 15:31:20 -070038 MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
39 EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()),
40 UNKNOWN(0, "unknown", null);
alshabib7b808c52015-06-26 14:22:24 -070041
42
43 private final EthType etherType;
44 private final String type;
45 private final Deserializer<?> deserializer;
46
47 /**
48 * Constucts a new ethertype.
49 *
50 * @param ethType The actual ethertype
51 * @param type it's textual representation
52 * @param deserializer a parser for this ethertype
53 */
54 EtherType(int ethType, String type, Deserializer<?> deserializer) {
55 this.etherType = new EthType(ethType);
56 this.type = type;
57 this.deserializer = deserializer;
alshabibcaf1ca22015-06-25 15:18:16 -070058 }
alshabib7b808c52015-06-26 14:22:24 -070059
60 public EthType ethType() {
61 return etherType;
62 }
63
64 @Override
65 public String toString() {
66 return type;
67 }
68
69 public Deserializer<?> deserializer() {
70 return deserializer;
71 }
72
Jonathan Hart4a60bb32015-06-30 15:31:20 -070073 public static EtherType lookup(short etherType) {
74 for (EtherType ethType : EtherType.values()) {
75 if (ethType.ethType().toShort() == etherType) {
76 return ethType;
77 }
78 }
79 return UNKNOWN;
80 }
81
alshabibcaf1ca22015-06-25 15:18:16 -070082 }
83
alshabib7b808c52015-06-26 14:22:24 -070084
85 private final short etherType;
86
87 /**
88 * Builds the EthType.
89 *
90 * @param etherType an integer representing an ethtype
91 */
alshabibcaf1ca22015-06-25 15:18:16 -070092 public EthType(int etherType) {
93 this.etherType = (short) (etherType & 0xFFFF);
94 }
95
alshabib7b808c52015-06-26 14:22:24 -070096 /**
97 * Builds the EthType.
98 *
99 * @param etherType a short representing the ethtype
100 */
alshabibcaf1ca22015-06-25 15:18:16 -0700101 public EthType(short etherType) {
102 this.etherType = etherType;
103 }
104
alshabib7b808c52015-06-26 14:22:24 -0700105 /**
106 * Returns the short value for this ethtype.
107 *
108 * @return a short value
109 */
alshabibcaf1ca22015-06-25 15:18:16 -0700110 public short toShort() {
111 return etherType;
112 }
113
alshabib7b808c52015-06-26 14:22:24 -0700114 /**
115 * Looks up the ethertype by it's numerical representation
116 * and returns it's textual format.
117 *
118 * @param etherType the short value of the ethertype
119 * @return a textual representation
120 */
121 public EtherType lookup(short etherType) {
122 for (EtherType ethType : EtherType.values()) {
123 if (ethType.ethType().toShort() == etherType) {
124 return ethType;
125 }
126 }
127 return null;
alshabibcaf1ca22015-06-25 15:18:16 -0700128 }
129
130 @Override
131 public boolean equals(Object o) {
132 if (this == o) {
133 return true;
134 }
135 if (o == null || getClass() != o.getClass()) {
136 return false;
137 }
138
139 EthType ethType = (EthType) o;
140
141 if (etherType != ethType.etherType) {
142 return false;
143 }
144
145 return true;
146 }
147
148 @Override
149 public int hashCode() {
150 return (int) etherType;
151 }
152
153 public String toString() {
154 EtherType ethType = lookup(this.etherType);
alshabib7b808c52015-06-26 14:22:24 -0700155 return (ethType == null ? String.format("0x%04x", etherType) :
156 ethType.toString());
alshabibcaf1ca22015-06-25 15:18:16 -0700157 }
158
alshabibcaf1ca22015-06-25 15:18:16 -0700159}