blob: 4f52b7d54fe97ec033b32704a61fd8cac7dd7438 [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
96 ARP(0x806, "arp", ARP.class),
97 RARP(0x8035, "rarp", null),
98 IPV4(0x800, "ipv4", IPv4.class),
99 IPV6(0x86dd, "ipv6", IPv6.class),
100 LLDP(0x88cc, "lldp", LLDP.class),
101 VLAN(0x8100, "vlan", null),
102 BDDP(0x8942, "bddp", LLDP.class),
103 MPLS_UNICAST(0x8847, "mpls_unicast", null),
104 MPLS_MULTICAST(0x8848, "mpls_unicast", null);
105
106
107 private final Class clazz;
108 private EthType ethType;
109 private String type;
110
111 EtherType(int ethType, String type, Class clazz) {
112 this.ethType = new EthType(ethType);
113 this.type = type;
114 this.clazz = clazz;
115 }
116
117 public EthType ethType() {
118 return ethType;
119 }
120
121 @Override
122 public String toString() {
123 return type;
124 }
125
126 public Class clazz() {
127 return clazz;
128 }
129
130
131 }
132}