blob: 90439eb4e312552ed26ba118a06a747e639c72d3 [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()),
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;
alshabibcaf1ca22015-06-25 15:18:16 -070057 }
alshabib7b808c52015-06-26 14:22:24 -070058
59 public EthType ethType() {
60 return etherType;
61 }
62
63 @Override
64 public String toString() {
65 return type;
66 }
67
68 public Deserializer<?> deserializer() {
69 return deserializer;
70 }
71
alshabibcaf1ca22015-06-25 15:18:16 -070072 }
73
alshabib7b808c52015-06-26 14:22:24 -070074
75 private final short etherType;
76
77 /**
78 * Builds the EthType.
79 *
80 * @param etherType an integer representing an ethtype
81 */
alshabibcaf1ca22015-06-25 15:18:16 -070082 public EthType(int etherType) {
83 this.etherType = (short) (etherType & 0xFFFF);
84 }
85
alshabib7b808c52015-06-26 14:22:24 -070086 /**
87 * Builds the EthType.
88 *
89 * @param etherType a short representing the ethtype
90 */
alshabibcaf1ca22015-06-25 15:18:16 -070091 public EthType(short etherType) {
92 this.etherType = etherType;
93 }
94
alshabib7b808c52015-06-26 14:22:24 -070095 /**
96 * Returns the short value for this ethtype.
97 *
98 * @return a short value
99 */
alshabibcaf1ca22015-06-25 15:18:16 -0700100 public short toShort() {
101 return etherType;
102 }
103
alshabib7b808c52015-06-26 14:22:24 -0700104 /**
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;
alshabibcaf1ca22015-06-25 15:18:16 -0700118 }
119
120 @Override
121 public boolean equals(Object o) {
122 if (this == o) {
123 return true;
124 }
125 if (o == null || getClass() != o.getClass()) {
126 return false;
127 }
128
129 EthType ethType = (EthType) o;
130
131 if (etherType != ethType.etherType) {
132 return false;
133 }
134
135 return true;
136 }
137
138 @Override
139 public int hashCode() {
140 return (int) etherType;
141 }
142
143 public String toString() {
144 EtherType ethType = lookup(this.etherType);
alshabib7b808c52015-06-26 14:22:24 -0700145 return (ethType == null ? String.format("0x%04x", etherType) :
146 ethType.toString());
alshabibcaf1ca22015-06-25 15:18:16 -0700147 }
148
alshabibcaf1ca22015-06-25 15:18:16 -0700149}