blob: a6268bb7e4a6b890c2a30bfff252a184b0afdf33 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeyd43fe452015-05-29 09:35:12 -07003 *
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.onosproject.codec.impl;
17
Jian Lidab72562016-04-12 14:10:32 -070018import com.fasterxml.jackson.databind.JsonNode;
Jian Li1ef82db2016-03-03 14:43:21 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lidab72562016-04-12 14:10:32 -070020import org.onlab.osgi.DefaultServiceDirectory;
21import org.onlab.osgi.ServiceDirectory;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050022import org.onlab.packet.EthType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.MplsLabel;
Hyunsun Moonfab29502015-08-25 13:39:16 -070026import org.onlab.packet.TpPort;
Ray Milkeyd43fe452015-05-29 09:35:12 -070027import org.onlab.packet.VlanId;
Yafit Hadar5796d972015-10-15 13:16:11 +030028import org.onlab.util.HexString;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070029import org.onosproject.codec.CodecContext;
Jian Lidab72562016-04-12 14:10:32 -070030import org.onosproject.codec.ExtensionTreatmentCodec;
Jian Lice8c5602016-03-03 21:43:24 -080031import org.onosproject.core.GroupId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070032import org.onosproject.net.ChannelSpacing;
Jian Lidab72562016-04-12 14:10:32 -070033import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070035import org.onosproject.net.GridType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070036import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030037import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070038import org.onosproject.net.PortNumber;
Jian Lidab72562016-04-12 14:10:32 -070039import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.flow.instructions.ExtensionTreatment;
Ray Milkeyd43fe452015-05-29 09:35:12 -070041import org.onosproject.net.flow.instructions.Instruction;
42import org.onosproject.net.flow.instructions.Instructions;
43import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030044import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070045import org.onosproject.net.flow.instructions.L2ModificationInstruction;
46import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070047import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Jian Li47b26232016-03-07 09:59:59 -080048import org.onosproject.net.meter.MeterId;
Jian Lidab72562016-04-12 14:10:32 -070049import org.slf4j.Logger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070050
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050051import java.util.regex.Matcher;
52import java.util.regex.Pattern;
53
Jian Li1ef82db2016-03-03 14:43:21 -080054import static org.onlab.util.Tools.nullIsIllegal;
Jian Lidab72562016-04-12 14:10:32 -070055import static org.slf4j.LoggerFactory.getLogger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070056
57/**
58 * Decoding portion of the instruction codec.
59 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070060public final class DecodeInstructionCodecHelper {
Jian Lidab72562016-04-12 14:10:32 -070061 protected static final Logger log = getLogger(DecodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070062 private final ObjectNode json;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070063 private final CodecContext context;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050064 private static final Pattern ETHTYPE_PATTERN = Pattern.compile("0x([0-9a-fA-F]{4})");
Ray Milkeyd43fe452015-05-29 09:35:12 -070065
66 /**
67 * Creates a decode instruction codec object.
68 *
69 * @param json JSON object to decode
Ray Milkeyef794342016-11-09 16:20:29 -080070 * @param context codec context
Ray Milkeyd43fe452015-05-29 09:35:12 -070071 */
Jonathan Harte3bcfc32016-08-16 17:12:49 -070072 public DecodeInstructionCodecHelper(ObjectNode json, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070073 this.json = json;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070074 this.context = context;
Ray Milkeyd43fe452015-05-29 09:35:12 -070075 }
76
77 /**
78 * Decodes a Layer 2 instruction.
79 *
80 * @return instruction object decoded from the JSON
81 * @throws IllegalArgumentException if the JSON is invalid
82 */
83 private Instruction decodeL2() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +053084 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
85 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -070086
87 if (subType.equals(L2ModificationInstruction.L2SubType.ETH_SRC.name())) {
88 String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
89 InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
90 return Instructions.modL2Src(MacAddress.valueOf(mac));
91 } else if (subType.equals(L2ModificationInstruction.L2SubType.ETH_DST.name())) {
92 String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
93 InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
94 return Instructions.modL2Dst(MacAddress.valueOf(mac));
95 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_ID.name())) {
96 short vlanId = (short) nullIsIllegal(json.get(InstructionCodec.VLAN_ID),
97 InstructionCodec.VLAN_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
98 return Instructions.modVlanId(VlanId.vlanId(vlanId));
99 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PCP.name())) {
100 byte vlanPcp = (byte) nullIsIllegal(json.get(InstructionCodec.VLAN_PCP),
101 InstructionCodec.VLAN_PCP + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
102 return Instructions.modVlanPcp(vlanPcp);
103 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_LABEL.name())) {
104 int label = nullIsIllegal(json.get(InstructionCodec.MPLS_LABEL),
105 InstructionCodec.MPLS_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
106 return Instructions.modMplsLabel(MplsLabel.mplsLabel(label));
107 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_PUSH.name())) {
108 return Instructions.pushMpls();
109 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_POP.name())) {
Sivachidambaram Subramaniandab7f4b2017-05-15 12:28:12 +0530110 if (json.has(InstructionCodec.ETHERNET_TYPE)) {
111 return Instructions.popMpls(getEthType());
112 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700113 return Instructions.popMpls();
114 } else if (subType.equals(L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name())) {
115 return Instructions.decMplsTtl();
116 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_POP.name())) {
117 return Instructions.popVlan();
118 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PUSH.name())) {
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500119 if (json.has(InstructionCodec.ETHERNET_TYPE)) {
120 return Instructions.pushVlan(getEthType());
121 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700122 return Instructions.pushVlan();
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700123 } else if (subType.equals(L2ModificationInstruction.L2SubType.TUNNEL_ID.name())) {
124 long tunnelId = nullIsIllegal(json.get(InstructionCodec.TUNNEL_ID),
125 InstructionCodec.TUNNEL_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
126 return Instructions.modTunnelId(tunnelId);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 }
128 throw new IllegalArgumentException("L2 Instruction subtype "
129 + subType + " is not supported");
130 }
131
132 /**
133 * Decodes a Layer 3 instruction.
134 *
135 * @return instruction object decoded from the JSON
136 * @throws IllegalArgumentException if the JSON is invalid
137 */
138 private Instruction decodeL3() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530139 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
140 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700141
142 if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_SRC.name())) {
143 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
144 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
145 return Instructions.modL3Src(ip);
146 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_DST.name())) {
147 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
148 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
149 return Instructions.modL3Dst(ip);
150 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_SRC.name())) {
151 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
152 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
153 return Instructions.modL3IPv6Src(ip);
154 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_DST.name())) {
155 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
156 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
157 return Instructions.modL3IPv6Dst(ip);
158 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_FLABEL.name())) {
159 int flowLabel = nullIsIllegal(json.get(InstructionCodec.FLOW_LABEL),
160 InstructionCodec.FLOW_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
161 return Instructions.modL3IPv6FlowLabel(flowLabel);
162 }
163 throw new IllegalArgumentException("L3 Instruction subtype "
164 + subType + " is not supported");
165 }
166
167 /**
168 * Decodes a Layer 0 instruction.
169 *
170 * @return instruction object decoded from the JSON
171 * @throws IllegalArgumentException if the JSON is invalid
172 */
173 private Instruction decodeL0() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530174 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
175 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700176
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800177 if (subType.equals(L0ModificationInstruction.L0SubType.OCH.name())) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700178 String gridTypeString = nullIsIllegal(json.get(InstructionCodec.GRID_TYPE),
179 InstructionCodec.GRID_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
180 GridType gridType = GridType.valueOf(gridTypeString);
181 if (gridType == null) {
182 throw new IllegalArgumentException("Unknown grid type "
183 + gridTypeString);
184 }
185 String channelSpacingString = nullIsIllegal(json.get(InstructionCodec.CHANNEL_SPACING),
186 InstructionCodec.CHANNEL_SPACING + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
187 ChannelSpacing channelSpacing = ChannelSpacing.valueOf(channelSpacingString);
188 if (channelSpacing == null) {
189 throw new IllegalArgumentException("Unknown channel spacing "
190 + channelSpacingString);
191 }
192 int spacingMultiplier = nullIsIllegal(json.get(InstructionCodec.SPACING_MULTIPLIER),
193 InstructionCodec.SPACING_MULTIPLIER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
194 int slotGranularity = nullIsIllegal(json.get(InstructionCodec.SLOT_GRANULARITY),
195 InstructionCodec.SLOT_GRANULARITY + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
196 return Instructions.modL0Lambda(new OchSignal(gridType, channelSpacing,
197 spacingMultiplier, slotGranularity));
198 }
199 throw new IllegalArgumentException("L0 Instruction subtype "
200 + subType + " is not supported");
201 }
202
203 /**
Yafit Hadar5796d972015-10-15 13:16:11 +0300204 * Decodes a Layer 1 instruction.
205 *
206 * @return instruction object decoded from the JSON
207 * @throws IllegalArgumentException if the JSON is invalid
208 */
209 private Instruction decodeL1() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530210 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
211 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Yafit Hadar5796d972015-10-15 13:16:11 +0300212 if (subType.equals(L1ModificationInstruction.L1SubType.ODU_SIGID.name())) {
213 int tributaryPortNumber = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_PORT_NUMBER),
214 InstructionCodec.TRIBUTARY_PORT_NUMBER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
215 int tributarySlotLen = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_LEN),
216 InstructionCodec.TRIBUTARY_SLOT_LEN + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
217 byte[] tributarySlotBitmap = null;
218 tributarySlotBitmap = HexString.fromHexString(
219 nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_BITMAP),
220 InstructionCodec.TRIBUTARY_SLOT_BITMAP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
221 return Instructions.modL1OduSignalId(OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen,
222 tributarySlotBitmap));
223 }
224 throw new IllegalArgumentException("L1 Instruction subtype "
225 + subType + " is not supported");
226 }
227
228 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700229 * Decodes a Layer 4 instruction.
230 *
231 * @return instruction object decoded from the JSON
232 * @throws IllegalArgumentException if the JSON is invalid
233 */
234 private Instruction decodeL4() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530235 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
236 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700237
238 if (subType.equals(L4ModificationInstruction.L4SubType.TCP_DST.name())) {
239 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
240 InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
241 return Instructions.modTcpDst(tcpPort);
242 } else if (subType.equals(L4ModificationInstruction.L4SubType.TCP_SRC.name())) {
243 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
244 InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
245 return Instructions.modTcpSrc(tcpPort);
246 } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_DST.name())) {
247 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT),
248 InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
249 return Instructions.modUdpDst(udpPort);
250 } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_SRC.name())) {
251 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT),
252 InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
253 return Instructions.modUdpSrc(udpPort);
254 }
255 throw new IllegalArgumentException("L4 Instruction subtype "
256 + subType + " is not supported");
257 }
258
259 /**
Jian Lidab72562016-04-12 14:10:32 -0700260 * Decodes a extension instruction.
261 *
262 * @return extension treatment
263 */
264 private Instruction decodeExtension() {
265 ObjectNode node = (ObjectNode) json.get(InstructionCodec.EXTENSION);
266 if (node != null) {
267 DeviceId deviceId = getDeviceId();
268
269 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
270 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
271 Device device = deviceService.getDevice(deviceId);
272
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700273 if (device == null) {
274 throw new IllegalArgumentException("Device not found");
275 }
276
Jian Lidab72562016-04-12 14:10:32 -0700277 if (device.is(ExtensionTreatmentCodec.class)) {
278 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700279 ExtensionTreatment treatment = treatmentCodec.decode(node, context);
Jian Lidab72562016-04-12 14:10:32 -0700280 return Instructions.extension(treatment, deviceId);
281 } else {
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700282 throw new IllegalArgumentException(
283 "There is no codec to decode extension for device " + deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700284 }
285 }
286 return null;
287 }
288
289 /**
290 * Returns device identifier.
291 *
292 * @return device identifier
293 * @throws IllegalArgumentException if the JSON is invalid
294 */
295 private DeviceId getDeviceId() {
296 JsonNode deviceIdNode = json.get(InstructionCodec.DEVICE_ID);
297 if (deviceIdNode != null) {
298 return DeviceId.deviceId(deviceIdNode.asText());
299 }
300 throw new IllegalArgumentException("Empty device identifier");
301 }
302
303 /**
Jian Li70dffe42016-03-08 22:23:02 -0800304 * Extracts port number of the given json node.
305 *
306 * @param jsonNode json node
307 * @return port number
308 */
309 private PortNumber getPortNumber(ObjectNode jsonNode) {
310 PortNumber portNumber;
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530311 JsonNode portNode = nullIsIllegal(jsonNode.get(InstructionCodec.PORT),
312 InstructionCodec.PORT + InstructionCodec.ERROR_MESSAGE);
313 if (portNode.isLong() || portNode.isInt()) {
314 portNumber = PortNumber.portNumber(portNode.asLong());
315 } else if (portNode.isTextual()) {
316 portNumber = PortNumber.fromString(portNode.textValue());
Jian Li70dffe42016-03-08 22:23:02 -0800317 } else {
318 throw new IllegalArgumentException("Port value "
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530319 + portNode.toString()
Jian Li70dffe42016-03-08 22:23:02 -0800320 + " is not supported");
321 }
322 return portNumber;
323 }
324
325 /**
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500326 * Returns Ethernet type.
327 *
328 * @return ethernet type
329 * @throws IllegalArgumentException if the JSON is invalid
330 */
331 private EthType getEthType() {
332 String ethTypeStr = nullIsIllegal(json.get(InstructionCodec.ETHERNET_TYPE),
333 InstructionCodec.ETHERNET_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
334 Matcher matcher = ETHTYPE_PATTERN.matcher(ethTypeStr);
335 if (!matcher.matches()) {
336 throw new IllegalArgumentException("ETHERNET_TYPE must be a four digit hex string starting with 0x");
337 }
338 short ethernetType = (short) Integer.parseInt(matcher.group(1), 16);
339 return new EthType(ethernetType);
340 }
341
342 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700343 * Decodes the JSON into an instruction object.
344 *
345 * @return Criterion object
346 * @throws IllegalArgumentException if the JSON is invalid
347 */
348 public Instruction decode() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530349 String type = nullIsIllegal(json.get(InstructionCodec.TYPE),
350 InstructionCodec.TYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700351
352 if (type.equals(Instruction.Type.OUTPUT.name())) {
Jian Li70dffe42016-03-08 22:23:02 -0800353 return Instructions.createOutput(getPortNumber(json));
Ray Milkey2be39ed2016-02-22 15:54:19 -0800354 } else if (type.equals(Instruction.Type.NOACTION.name())) {
355 return Instructions.createNoAction();
Jian Li1ef82db2016-03-03 14:43:21 -0800356 } else if (type.equals(Instruction.Type.TABLE.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530357 return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID),
358 InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Jian Lice8c5602016-03-03 21:43:24 -0800359 } else if (type.equals(Instruction.Type.GROUP.name())) {
Yi Tsengfa394de2017-02-01 11:26:40 -0800360 GroupId groupId = new GroupId(nullIsIllegal(json.get(InstructionCodec.GROUP_ID),
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530361 InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Jian Lice8c5602016-03-03 21:43:24 -0800362 return Instructions.createGroup(groupId);
Jian Li47b26232016-03-07 09:59:59 -0800363 } else if (type.equals(Instruction.Type.METER.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530364 MeterId meterId = MeterId.meterId(nullIsIllegal(json.get(InstructionCodec.METER_ID),
365 InstructionCodec.METER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong());
Jian Li47b26232016-03-07 09:59:59 -0800366 return Instructions.meterTraffic(meterId);
Jian Li70dffe42016-03-08 22:23:02 -0800367 } else if (type.equals(Instruction.Type.QUEUE.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530368 long queueId = nullIsIllegal(json.get(InstructionCodec.QUEUE_ID),
369 InstructionCodec.QUEUE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
ke han74702102016-11-29 14:57:29 +0800370 if (json.get(InstructionCodec.PORT) == null ||
371 json.get(InstructionCodec.PORT).isNull()) {
372 return Instructions.setQueue(queueId, null);
373 } else {
374 return Instructions.setQueue(queueId, getPortNumber(json));
375 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700376 } else if (type.equals(Instruction.Type.L0MODIFICATION.name())) {
377 return decodeL0();
Yafit Hadar5796d972015-10-15 13:16:11 +0300378 } else if (type.equals(Instruction.Type.L1MODIFICATION.name())) {
379 return decodeL1();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700380 } else if (type.equals(Instruction.Type.L2MODIFICATION.name())) {
381 return decodeL2();
382 } else if (type.equals(Instruction.Type.L3MODIFICATION.name())) {
383 return decodeL3();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700384 } else if (type.equals(Instruction.Type.L4MODIFICATION.name())) {
385 return decodeL4();
Jian Lidab72562016-04-12 14:10:32 -0700386 } else if (type.equals(Instruction.Type.EXTENSION.name())) {
387 return decodeExtension();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700388 }
389 throw new IllegalArgumentException("Instruction type "
390 + type + " is not supported");
391 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700392}