blob: 88e5fe92697b94d42d3b387bdc011415e2a039c1 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07005import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07006import com.google.common.primitives.UnsignedInts;
7
Yotam Harcholf3f11152013-09-05 16:47:16 -07008public class ArpOpcode implements OFValueType<ArpOpcode> {
9
10 final static int LENGTH = 2;
11
12 private static final int ARP_OPCODE_VAL_REQUEST = 1;
13 private static final int ARP_OPCODE_VAL_REPLY = 2;
14 private static final int ARP_OPCODE_VAL_REQUEST_REVERSE = 3;
15 private static final int ARP_OPCODE_VAL_REPLY_REVERSE = 4;
16 private static final int ARP_OPCODE_VAL_DRARP_REQUEST = 5;
17 private static final int ARP_OPCODE_VAL_DRARP_REPLY = 6;
18 private static final int ARP_OPCODE_VAL_DRARP_ERROR = 7;
19 private static final int ARP_OPCODE_VAL_INARP_REQUEST = 8;
20 private static final int ARP_OPCODE_VAL_INARP_REPLY = 9;
21 private static final int ARP_OPCODE_VAL_ARP_NAK = 10;
22 private static final int ARP_OPCODE_VAL_MARS_REQUEST = 11;
23 private static final int ARP_OPCODE_VAL_MARS_MULTI = 12;
24 private static final int ARP_OPCODE_VAL_MARS_MSERV = 13;
25 private static final int ARP_OPCODE_VAL_MARS_JOIN = 14;
26 private static final int ARP_OPCODE_VAL_MARS_LEAVE = 15;
27 private static final int ARP_OPCODE_VAL_MARS_NAK = 16;
28 private static final int ARP_OPCODE_VAL_MARS_UNSERV = 17;
29 private static final int ARP_OPCODE_VAL_MARS_SJOIN = 18;
30 private static final int ARP_OPCODE_VAL_MARS_SLEAVE = 19;
31 private static final int ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST = 20;
32 private static final int ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY = 21;
33 private static final int ARP_OPCODE_VAL_MARS_REDIRECT_MAP = 22;
34 private static final int ARP_OPCODE_VAL_MAPOS_UNARP = 23;
35 private static final int ARP_OPCODE_VAL_OP_EXP1 = 24;
36 private static final int ARP_OPCODE_VAL_OP_EXP2 = 25;
37
38 public static final ArpOpcode ARP_OPCODE_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_REQUEST);
39 public static final ArpOpcode ARP_OPCODE_REPLY = new ArpOpcode(ARP_OPCODE_VAL_REPLY);
40 public static final ArpOpcode ARP_OPCODE_REQUEST_REVERSE = new ArpOpcode(ARP_OPCODE_VAL_REQUEST_REVERSE);
41 public static final ArpOpcode ARP_OPCODE_REPLY_REVERSE = new ArpOpcode(ARP_OPCODE_VAL_REPLY_REVERSE);
42 public static final ArpOpcode ARP_OPCODE_DRARP_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_DRARP_REQUEST);
43 public static final ArpOpcode ARP_OPCODE_DRARP_REPLY = new ArpOpcode(ARP_OPCODE_VAL_DRARP_REPLY);
44 public static final ArpOpcode ARP_OPCODE_DRARP_ERROR = new ArpOpcode(ARP_OPCODE_VAL_DRARP_ERROR);
45 public static final ArpOpcode ARP_OPCODE_INARP_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_INARP_REQUEST);
46 public static final ArpOpcode ARP_OPCODE_INARP_REPLY = new ArpOpcode(ARP_OPCODE_VAL_INARP_REPLY);
47 public static final ArpOpcode ARP_OPCODE_ARP_NAK = new ArpOpcode(ARP_OPCODE_VAL_ARP_NAK);
48 public static final ArpOpcode ARP_OPCODE_MARS_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_MARS_REQUEST);
49 public static final ArpOpcode ARP_OPCODE_MARS_MULTI = new ArpOpcode(ARP_OPCODE_VAL_MARS_MULTI);
50 public static final ArpOpcode ARP_OPCODE_MARS_MSERV = new ArpOpcode(ARP_OPCODE_VAL_MARS_MSERV);
51 public static final ArpOpcode ARP_OPCODE_MARS_JOIN = new ArpOpcode(ARP_OPCODE_VAL_MARS_JOIN);
52 public static final ArpOpcode ARP_OPCODE_MARS_LEAVE = new ArpOpcode(ARP_OPCODE_VAL_MARS_LEAVE);
53 public static final ArpOpcode ARP_OPCODE_MARS_NAK = new ArpOpcode(ARP_OPCODE_VAL_MARS_NAK);
54 public static final ArpOpcode ARP_OPCODE_MARS_UNSERV = new ArpOpcode(ARP_OPCODE_VAL_MARS_UNSERV);
55 public static final ArpOpcode ARP_OPCODE_MARS_SJOIN = new ArpOpcode(ARP_OPCODE_VAL_MARS_SJOIN);
56 public static final ArpOpcode ARP_OPCODE_MARS_SLEAVE = new ArpOpcode(ARP_OPCODE_VAL_MARS_SLEAVE);
57 public static final ArpOpcode ARP_OPCODE_MARS_GROUPLIST_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST);
58 public static final ArpOpcode ARP_OPCODE_MARS_GROUPLIST_REPLY = new ArpOpcode(ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY);
59 public static final ArpOpcode ARP_OPCODE_MARS_REDIRECT_MAP = new ArpOpcode(ARP_OPCODE_VAL_MARS_REDIRECT_MAP);
60 public static final ArpOpcode ARP_OPCODE_MAPOS_UNARP = new ArpOpcode(ARP_OPCODE_VAL_MAPOS_UNARP);
61 public static final ArpOpcode ARP_OPCODE_OP_EXP1 = new ArpOpcode(ARP_OPCODE_VAL_OP_EXP1);
62 public static final ArpOpcode ARP_OPCODE_OP_EXP2 = new ArpOpcode(ARP_OPCODE_VAL_OP_EXP2);
63
64 private static final int MIN_OPCODE = 0;
65 private static final int MAX_OPCODE = 0xFFFF;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070066
67 private static final int NONE_VAL = 0;
68 public static final ArpOpcode NONE = new ArpOpcode(NONE_VAL);
69
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 public static final ArpOpcode NO_MASK = new ArpOpcode(0xFFFFFFFF);
71 public static final ArpOpcode FULL_MASK = new ArpOpcode(0x00000000);
72
73 private final int opcode;
74
75 private ArpOpcode(int opcode) {
76 this.opcode = opcode;
77 }
78
79 @Override
80 public int getLength() {
81 return LENGTH;
82 }
83
84 public int getOpcode() {
85 return this.opcode;
86 }
87
88 public static ArpOpcode of(int opcode) {
89 if (opcode < MIN_OPCODE || opcode > MAX_OPCODE)
90 throw new IllegalArgumentException("Invalid ARP opcode: " + opcode);
91 switch (opcode) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070092 case NONE_VAL:
93 return NONE;
Yotam Harcholf3f11152013-09-05 16:47:16 -070094 case ARP_OPCODE_VAL_REQUEST:
95 return ARP_OPCODE_REQUEST;
96 case ARP_OPCODE_VAL_REPLY:
97 return ARP_OPCODE_REPLY;
98 case ARP_OPCODE_VAL_REQUEST_REVERSE:
99 return ARP_OPCODE_REQUEST_REVERSE;
100 case ARP_OPCODE_VAL_REPLY_REVERSE:
101 return ARP_OPCODE_REPLY_REVERSE;
102 case ARP_OPCODE_VAL_DRARP_REQUEST:
103 return ARP_OPCODE_DRARP_REQUEST;
104 case ARP_OPCODE_VAL_DRARP_REPLY:
105 return ARP_OPCODE_DRARP_REPLY;
106 case ARP_OPCODE_VAL_DRARP_ERROR:
107 return ARP_OPCODE_DRARP_ERROR;
108 case ARP_OPCODE_VAL_INARP_REQUEST:
109 return ARP_OPCODE_INARP_REQUEST;
110 case ARP_OPCODE_VAL_INARP_REPLY:
111 return ARP_OPCODE_INARP_REPLY;
112 case ARP_OPCODE_VAL_ARP_NAK:
113 return ARP_OPCODE_ARP_NAK;
114 case ARP_OPCODE_VAL_MARS_REQUEST:
115 return ARP_OPCODE_MARS_REQUEST;
116 case ARP_OPCODE_VAL_MARS_MULTI:
117 return ARP_OPCODE_MARS_MULTI;
118 case ARP_OPCODE_VAL_MARS_MSERV:
119 return ARP_OPCODE_MARS_MSERV;
120 case ARP_OPCODE_VAL_MARS_JOIN:
121 return ARP_OPCODE_MARS_JOIN;
122 case ARP_OPCODE_VAL_MARS_LEAVE:
123 return ARP_OPCODE_MARS_LEAVE;
124 case ARP_OPCODE_VAL_MARS_NAK:
125 return ARP_OPCODE_MARS_NAK;
126 case ARP_OPCODE_VAL_MARS_UNSERV:
127 return ARP_OPCODE_MARS_UNSERV;
128 case ARP_OPCODE_VAL_MARS_SJOIN:
129 return ARP_OPCODE_MARS_SJOIN;
130 case ARP_OPCODE_VAL_MARS_SLEAVE:
131 return ARP_OPCODE_MARS_SLEAVE;
132 case ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST:
133 return ARP_OPCODE_MARS_GROUPLIST_REQUEST;
134 case ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY:
135 return ARP_OPCODE_MARS_GROUPLIST_REPLY;
136 case ARP_OPCODE_VAL_MARS_REDIRECT_MAP:
137 return ARP_OPCODE_MARS_REDIRECT_MAP;
138 case ARP_OPCODE_VAL_MAPOS_UNARP:
139 return ARP_OPCODE_MAPOS_UNARP;
140 case ARP_OPCODE_VAL_OP_EXP1:
141 return ARP_OPCODE_OP_EXP1;
142 case ARP_OPCODE_VAL_OP_EXP2:
143 return ARP_OPCODE_OP_EXP2;
144 default:
145 return new ArpOpcode(opcode);
146 }
147 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700148
Yotam Harcholf3f11152013-09-05 16:47:16 -0700149 public void write2Bytes(ChannelBuffer c) {
150 c.writeShort(this.opcode);
151 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700152
Yotam Harcholf3f11152013-09-05 16:47:16 -0700153 public static ArpOpcode read2Bytes(ChannelBuffer c) {
154 return ArpOpcode.of(c.readUnsignedShort());
155 }
156
157 @Override
158 public ArpOpcode applyMask(ArpOpcode mask) {
159 return ArpOpcode.of(this.opcode & mask.opcode);
160 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700161
Yotam Harcholf3f11152013-09-05 16:47:16 -0700162 public int getOpCode() {
163 return opcode;
164 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700165
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700166 @Override
167 public int hashCode() {
168 final int prime = 31;
169 int result = 1;
170 result = prime * result + opcode;
171 return result;
172 }
173
174 @Override
175 public boolean equals(Object obj) {
176 if (this == obj)
177 return true;
178 if (obj == null)
179 return false;
180 if (getClass() != obj.getClass())
181 return false;
182 ArpOpcode other = (ArpOpcode) obj;
183 if (opcode != other.opcode)
184 return false;
185 return true;
186 }
187
188 @Override
189 public int compareTo(ArpOpcode o) {
190 return UnsignedInts.compare(opcode, o.opcode);
191 }
192
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700193 @Override
194 public void putTo(PrimitiveSink sink) {
195 sink.putShort((short) this.opcode);
196 }
197
Yotam Harcholf3f11152013-09-05 16:47:16 -0700198}