blob: 2917afd1b9e5a5fc63af7d9113dfedbaa6100983 [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.DefaultGroupId;
32import org.onosproject.core.GroupId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070033import org.onosproject.net.ChannelSpacing;
Jian Lidab72562016-04-12 14:10:32 -070034import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070036import org.onosproject.net.GridType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070037import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030038import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070039import org.onosproject.net.PortNumber;
Jian Lidab72562016-04-12 14:10:32 -070040import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.flow.instructions.ExtensionTreatment;
Ray Milkeyd43fe452015-05-29 09:35:12 -070042import org.onosproject.net.flow.instructions.Instruction;
43import org.onosproject.net.flow.instructions.Instructions;
44import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030045import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070046import org.onosproject.net.flow.instructions.L2ModificationInstruction;
47import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070048import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Jian Li47b26232016-03-07 09:59:59 -080049import org.onosproject.net.meter.MeterId;
Jian Lidab72562016-04-12 14:10:32 -070050import org.slf4j.Logger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070051
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050052import java.util.regex.Matcher;
53import java.util.regex.Pattern;
54
Jian Li1ef82db2016-03-03 14:43:21 -080055import static org.onlab.util.Tools.nullIsIllegal;
Jian Lidab72562016-04-12 14:10:32 -070056import static org.slf4j.LoggerFactory.getLogger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070057
58/**
59 * Decoding portion of the instruction codec.
60 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070061public final class DecodeInstructionCodecHelper {
Jian Lidab72562016-04-12 14:10:32 -070062 protected static final Logger log = getLogger(DecodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070063 private final ObjectNode json;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070064 private final CodecContext context;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050065 private static final Pattern ETHTYPE_PATTERN = Pattern.compile("0x([0-9a-fA-F]{4})");
Ray Milkeyd43fe452015-05-29 09:35:12 -070066
67 /**
68 * Creates a decode instruction codec object.
69 *
70 * @param json JSON object to decode
Ray Milkeyef794342016-11-09 16:20:29 -080071 * @param context codec context
Ray Milkeyd43fe452015-05-29 09:35:12 -070072 */
Jonathan Harte3bcfc32016-08-16 17:12:49 -070073 public DecodeInstructionCodecHelper(ObjectNode json, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070074 this.json = json;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070075 this.context = context;
Ray Milkeyd43fe452015-05-29 09:35:12 -070076 }
77
78 /**
79 * Decodes a Layer 2 instruction.
80 *
81 * @return instruction object decoded from the JSON
82 * @throws IllegalArgumentException if the JSON is invalid
83 */
84 private Instruction decodeL2() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +053085 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
86 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -070087
88 if (subType.equals(L2ModificationInstruction.L2SubType.ETH_SRC.name())) {
89 String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
90 InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
91 return Instructions.modL2Src(MacAddress.valueOf(mac));
92 } else if (subType.equals(L2ModificationInstruction.L2SubType.ETH_DST.name())) {
93 String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
94 InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
95 return Instructions.modL2Dst(MacAddress.valueOf(mac));
96 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_ID.name())) {
97 short vlanId = (short) nullIsIllegal(json.get(InstructionCodec.VLAN_ID),
98 InstructionCodec.VLAN_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
99 return Instructions.modVlanId(VlanId.vlanId(vlanId));
100 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PCP.name())) {
101 byte vlanPcp = (byte) nullIsIllegal(json.get(InstructionCodec.VLAN_PCP),
102 InstructionCodec.VLAN_PCP + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
103 return Instructions.modVlanPcp(vlanPcp);
104 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_LABEL.name())) {
105 int label = nullIsIllegal(json.get(InstructionCodec.MPLS_LABEL),
106 InstructionCodec.MPLS_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
107 return Instructions.modMplsLabel(MplsLabel.mplsLabel(label));
108 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_PUSH.name())) {
109 return Instructions.pushMpls();
110 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_POP.name())) {
111 return Instructions.popMpls();
112 } else if (subType.equals(L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name())) {
113 return Instructions.decMplsTtl();
114 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_POP.name())) {
115 return Instructions.popVlan();
116 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PUSH.name())) {
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500117 if (json.has(InstructionCodec.ETHERNET_TYPE)) {
118 return Instructions.pushVlan(getEthType());
119 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120 return Instructions.pushVlan();
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700121 } else if (subType.equals(L2ModificationInstruction.L2SubType.TUNNEL_ID.name())) {
122 long tunnelId = nullIsIllegal(json.get(InstructionCodec.TUNNEL_ID),
123 InstructionCodec.TUNNEL_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
124 return Instructions.modTunnelId(tunnelId);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700125 }
126 throw new IllegalArgumentException("L2 Instruction subtype "
127 + subType + " is not supported");
128 }
129
130 /**
131 * Decodes a Layer 3 instruction.
132 *
133 * @return instruction object decoded from the JSON
134 * @throws IllegalArgumentException if the JSON is invalid
135 */
136 private Instruction decodeL3() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530137 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
138 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700139
140 if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_SRC.name())) {
141 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
142 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
143 return Instructions.modL3Src(ip);
144 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_DST.name())) {
145 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
146 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
147 return Instructions.modL3Dst(ip);
148 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_SRC.name())) {
149 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
150 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
151 return Instructions.modL3IPv6Src(ip);
152 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_DST.name())) {
153 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
154 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
155 return Instructions.modL3IPv6Dst(ip);
156 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_FLABEL.name())) {
157 int flowLabel = nullIsIllegal(json.get(InstructionCodec.FLOW_LABEL),
158 InstructionCodec.FLOW_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
159 return Instructions.modL3IPv6FlowLabel(flowLabel);
160 }
161 throw new IllegalArgumentException("L3 Instruction subtype "
162 + subType + " is not supported");
163 }
164
165 /**
166 * Decodes a Layer 0 instruction.
167 *
168 * @return instruction object decoded from the JSON
169 * @throws IllegalArgumentException if the JSON is invalid
170 */
171 private Instruction decodeL0() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530172 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
173 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700174
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800175 if (subType.equals(L0ModificationInstruction.L0SubType.OCH.name())) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700176 String gridTypeString = nullIsIllegal(json.get(InstructionCodec.GRID_TYPE),
177 InstructionCodec.GRID_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
178 GridType gridType = GridType.valueOf(gridTypeString);
179 if (gridType == null) {
180 throw new IllegalArgumentException("Unknown grid type "
181 + gridTypeString);
182 }
183 String channelSpacingString = nullIsIllegal(json.get(InstructionCodec.CHANNEL_SPACING),
184 InstructionCodec.CHANNEL_SPACING + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
185 ChannelSpacing channelSpacing = ChannelSpacing.valueOf(channelSpacingString);
186 if (channelSpacing == null) {
187 throw new IllegalArgumentException("Unknown channel spacing "
188 + channelSpacingString);
189 }
190 int spacingMultiplier = nullIsIllegal(json.get(InstructionCodec.SPACING_MULTIPLIER),
191 InstructionCodec.SPACING_MULTIPLIER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
192 int slotGranularity = nullIsIllegal(json.get(InstructionCodec.SLOT_GRANULARITY),
193 InstructionCodec.SLOT_GRANULARITY + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
194 return Instructions.modL0Lambda(new OchSignal(gridType, channelSpacing,
195 spacingMultiplier, slotGranularity));
196 }
197 throw new IllegalArgumentException("L0 Instruction subtype "
198 + subType + " is not supported");
199 }
200
201 /**
Yafit Hadar5796d972015-10-15 13:16:11 +0300202 * Decodes a Layer 1 instruction.
203 *
204 * @return instruction object decoded from the JSON
205 * @throws IllegalArgumentException if the JSON is invalid
206 */
207 private Instruction decodeL1() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530208 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
209 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Yafit Hadar5796d972015-10-15 13:16:11 +0300210 if (subType.equals(L1ModificationInstruction.L1SubType.ODU_SIGID.name())) {
211 int tributaryPortNumber = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_PORT_NUMBER),
212 InstructionCodec.TRIBUTARY_PORT_NUMBER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
213 int tributarySlotLen = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_LEN),
214 InstructionCodec.TRIBUTARY_SLOT_LEN + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
215 byte[] tributarySlotBitmap = null;
216 tributarySlotBitmap = HexString.fromHexString(
217 nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_BITMAP),
218 InstructionCodec.TRIBUTARY_SLOT_BITMAP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
219 return Instructions.modL1OduSignalId(OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen,
220 tributarySlotBitmap));
221 }
222 throw new IllegalArgumentException("L1 Instruction subtype "
223 + subType + " is not supported");
224 }
225
226 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700227 * Decodes a Layer 4 instruction.
228 *
229 * @return instruction object decoded from the JSON
230 * @throws IllegalArgumentException if the JSON is invalid
231 */
232 private Instruction decodeL4() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530233 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
234 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700235
236 if (subType.equals(L4ModificationInstruction.L4SubType.TCP_DST.name())) {
237 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
238 InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
239 return Instructions.modTcpDst(tcpPort);
240 } else if (subType.equals(L4ModificationInstruction.L4SubType.TCP_SRC.name())) {
241 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
242 InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
243 return Instructions.modTcpSrc(tcpPort);
244 } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_DST.name())) {
245 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT),
246 InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
247 return Instructions.modUdpDst(udpPort);
248 } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_SRC.name())) {
249 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT),
250 InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
251 return Instructions.modUdpSrc(udpPort);
252 }
253 throw new IllegalArgumentException("L4 Instruction subtype "
254 + subType + " is not supported");
255 }
256
257 /**
Jian Lidab72562016-04-12 14:10:32 -0700258 * Decodes a extension instruction.
259 *
260 * @return extension treatment
261 */
262 private Instruction decodeExtension() {
263 ObjectNode node = (ObjectNode) json.get(InstructionCodec.EXTENSION);
264 if (node != null) {
265 DeviceId deviceId = getDeviceId();
266
267 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
268 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
269 Device device = deviceService.getDevice(deviceId);
270
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700271 if (device == null) {
272 throw new IllegalArgumentException("Device not found");
273 }
274
Jian Lidab72562016-04-12 14:10:32 -0700275 if (device.is(ExtensionTreatmentCodec.class)) {
276 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700277 ExtensionTreatment treatment = treatmentCodec.decode(node, context);
Jian Lidab72562016-04-12 14:10:32 -0700278 return Instructions.extension(treatment, deviceId);
279 } else {
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700280 throw new IllegalArgumentException(
281 "There is no codec to decode extension for device " + deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700282 }
283 }
284 return null;
285 }
286
287 /**
288 * Returns device identifier.
289 *
290 * @return device identifier
291 * @throws IllegalArgumentException if the JSON is invalid
292 */
293 private DeviceId getDeviceId() {
294 JsonNode deviceIdNode = json.get(InstructionCodec.DEVICE_ID);
295 if (deviceIdNode != null) {
296 return DeviceId.deviceId(deviceIdNode.asText());
297 }
298 throw new IllegalArgumentException("Empty device identifier");
299 }
300
301 /**
Jian Li70dffe42016-03-08 22:23:02 -0800302 * Extracts port number of the given json node.
303 *
304 * @param jsonNode json node
305 * @return port number
306 */
307 private PortNumber getPortNumber(ObjectNode jsonNode) {
308 PortNumber portNumber;
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530309 JsonNode portNode = nullIsIllegal(jsonNode.get(InstructionCodec.PORT),
310 InstructionCodec.PORT + InstructionCodec.ERROR_MESSAGE);
311 if (portNode.isLong() || portNode.isInt()) {
312 portNumber = PortNumber.portNumber(portNode.asLong());
313 } else if (portNode.isTextual()) {
314 portNumber = PortNumber.fromString(portNode.textValue());
Jian Li70dffe42016-03-08 22:23:02 -0800315 } else {
316 throw new IllegalArgumentException("Port value "
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530317 + portNode.toString()
Jian Li70dffe42016-03-08 22:23:02 -0800318 + " is not supported");
319 }
320 return portNumber;
321 }
322
323 /**
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500324 * Returns Ethernet type.
325 *
326 * @return ethernet type
327 * @throws IllegalArgumentException if the JSON is invalid
328 */
329 private EthType getEthType() {
330 String ethTypeStr = nullIsIllegal(json.get(InstructionCodec.ETHERNET_TYPE),
331 InstructionCodec.ETHERNET_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
332 Matcher matcher = ETHTYPE_PATTERN.matcher(ethTypeStr);
333 if (!matcher.matches()) {
334 throw new IllegalArgumentException("ETHERNET_TYPE must be a four digit hex string starting with 0x");
335 }
336 short ethernetType = (short) Integer.parseInt(matcher.group(1), 16);
337 return new EthType(ethernetType);
338 }
339
340 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700341 * Decodes the JSON into an instruction object.
342 *
343 * @return Criterion object
344 * @throws IllegalArgumentException if the JSON is invalid
345 */
346 public Instruction decode() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530347 String type = nullIsIllegal(json.get(InstructionCodec.TYPE),
348 InstructionCodec.TYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700349
350 if (type.equals(Instruction.Type.OUTPUT.name())) {
Jian Li70dffe42016-03-08 22:23:02 -0800351 return Instructions.createOutput(getPortNumber(json));
Ray Milkey2be39ed2016-02-22 15:54:19 -0800352 } else if (type.equals(Instruction.Type.NOACTION.name())) {
353 return Instructions.createNoAction();
Jian Li1ef82db2016-03-03 14:43:21 -0800354 } else if (type.equals(Instruction.Type.TABLE.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530355 return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID),
356 InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Jian Lice8c5602016-03-03 21:43:24 -0800357 } else if (type.equals(Instruction.Type.GROUP.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530358 GroupId groupId = new DefaultGroupId(nullIsIllegal(json.get(InstructionCodec.GROUP_ID),
359 InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Jian Lice8c5602016-03-03 21:43:24 -0800360 return Instructions.createGroup(groupId);
Jian Li47b26232016-03-07 09:59:59 -0800361 } else if (type.equals(Instruction.Type.METER.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530362 MeterId meterId = MeterId.meterId(nullIsIllegal(json.get(InstructionCodec.METER_ID),
363 InstructionCodec.METER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong());
Jian Li47b26232016-03-07 09:59:59 -0800364 return Instructions.meterTraffic(meterId);
Jian Li70dffe42016-03-08 22:23:02 -0800365 } else if (type.equals(Instruction.Type.QUEUE.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530366 long queueId = nullIsIllegal(json.get(InstructionCodec.QUEUE_ID),
367 InstructionCodec.QUEUE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
ke han74702102016-11-29 14:57:29 +0800368 if (json.get(InstructionCodec.PORT) == null ||
369 json.get(InstructionCodec.PORT).isNull()) {
370 return Instructions.setQueue(queueId, null);
371 } else {
372 return Instructions.setQueue(queueId, getPortNumber(json));
373 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700374 } else if (type.equals(Instruction.Type.L0MODIFICATION.name())) {
375 return decodeL0();
Yafit Hadar5796d972015-10-15 13:16:11 +0300376 } else if (type.equals(Instruction.Type.L1MODIFICATION.name())) {
377 return decodeL1();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700378 } else if (type.equals(Instruction.Type.L2MODIFICATION.name())) {
379 return decodeL2();
380 } else if (type.equals(Instruction.Type.L3MODIFICATION.name())) {
381 return decodeL3();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700382 } else if (type.equals(Instruction.Type.L4MODIFICATION.name())) {
383 return decodeL4();
Jian Lidab72562016-04-12 14:10:32 -0700384 } else if (type.equals(Instruction.Type.EXTENSION.name())) {
385 return decodeExtension();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700386 }
387 throw new IllegalArgumentException("Instruction type "
388 + type + " is not supported");
389 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700390}