blob: cab335ce9580b1ec2a4e8884d955422d2910b145 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 Lice8c5602016-03-03 21:43:24 -080018import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lidab72562016-04-12 14:10:32 -070019import org.onlab.osgi.DefaultServiceDirectory;
20import org.onlab.osgi.ServiceDirectory;
Seyeon Jeong8188da12020-03-03 12:49:48 -080021import org.onlab.packet.EthType;
Yafit Hadar5796d972015-10-15 13:16:11 +030022import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onosproject.codec.CodecContext;
Jian Lidab72562016-04-12 14:10:32 -070024import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070026import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030027import org.onosproject.net.OduSignalId;
Jian Lidab72562016-04-12 14:10:32 -070028import org.onosproject.net.device.DeviceService;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070029import org.onosproject.net.flow.ExtensionTreatmentCodec;
Ray Milkeyd43fe452015-05-29 09:35:12 -070030import org.onosproject.net.flow.instructions.Instruction;
31import org.onosproject.net.flow.instructions.Instructions;
32import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030033import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070034import org.onosproject.net.flow.instructions.L2ModificationInstruction;
35import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070036import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Frank Wang74ce2c12018-04-11 20:26:45 +080037import org.onosproject.net.flow.instructions.PiInstruction;
38import org.onosproject.net.pi.runtime.PiAction;
Frank Wang74ce2c12018-04-11 20:26:45 +080039import org.onosproject.net.pi.runtime.PiActionParam;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070040import org.onosproject.net.pi.runtime.PiActionProfileGroupId;
41import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070042import org.slf4j.Logger;
Jian Lidab72562016-04-12 14:10:32 -070043
44import static org.slf4j.LoggerFactory.getLogger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070045
Ray Milkeyd43fe452015-05-29 09:35:12 -070046/**
47 * JSON encoding of Instructions.
48 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070049public final class EncodeInstructionCodecHelper {
Ray Milkey9c9cde42018-01-12 14:22:06 -080050 private static final Logger log = getLogger(EncodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070051 private final Instruction instruction;
52 private final CodecContext context;
53
54 /**
55 * Creates an instruction object encoder.
56 *
57 * @param instruction instruction to encode
Jian Li47b26232016-03-07 09:59:59 -080058 * @param context codec context for the encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070059 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070060 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070061 this.instruction = instruction;
62 this.context = context;
63 }
64
65
66 /**
67 * Encode an L0 modification instruction.
68 *
69 * @param result json node that the instruction attributes are added to
70 */
71 private void encodeL0(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080072 L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction;
73 result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -070074
Jian Lie0991662016-03-07 11:22:25 -080075 switch (l0Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070076 case OCH:
77 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
Jian Lie0991662016-03-07 11:22:25 -080078 (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070079 OchSignal ochSignal = ochSignalInstruction.lambda();
80 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
81 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
82 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
83 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
84 break;
85
86 default:
Jian Lie0991662016-03-07 11:22:25 -080087 log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -070088 }
89 }
90
91 /**
Yafit Hadar5796d972015-10-15 13:16:11 +030092 * Encode an L1 modification instruction.
93 *
94 * @param result json node that the instruction attributes are added to
Yafit Hadar5796d972015-10-15 13:16:11 +030095 */
96 private void encodeL1(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080097 L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction;
98 result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name());
Yafit Hadar5796d972015-10-15 13:16:11 +030099
Jian Lie0991662016-03-07 11:22:25 -0800100 switch (l1Instruction.subtype()) {
Jian Li47b26232016-03-07 09:59:59 -0800101 case ODU_SIGID:
102 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800103 (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction;
Jian Li47b26232016-03-07 09:59:59 -0800104 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
Yafit Hadar5796d972015-10-15 13:16:11 +0300105
Jian Li47b26232016-03-07 09:59:59 -0800106 ObjectNode child = result.putObject("oduSignalId");
Yafit Hadar5796d972015-10-15 13:16:11 +0300107
Jian Li47b26232016-03-07 09:59:59 -0800108 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
109 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
110 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP,
111 HexString.toHexString(oduSignalId.tributarySlotBitmap()));
112 break;
113 default:
Jian Lie0991662016-03-07 11:22:25 -0800114 log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype());
Jian Li47b26232016-03-07 09:59:59 -0800115 break;
Yafit Hadar5796d972015-10-15 13:16:11 +0300116 }
117 }
118
119 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120 * Encode an L2 modification instruction.
121 *
122 * @param result json node that the instruction attributes are added to
123 */
124 private void encodeL2(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800125 L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction;
126 result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127
Jian Lie0991662016-03-07 11:22:25 -0800128 switch (l2Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700129 case ETH_SRC:
130 case ETH_DST:
131 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800132 (L2ModificationInstruction.ModEtherInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700133 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
134 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700135 case VLAN_ID:
136 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800137 (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700138 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
139 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700140 case VLAN_PCP:
141 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800142 (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
144 break;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500145 case VLAN_PUSH:
146 final L2ModificationInstruction.ModVlanHeaderInstruction pushVlanInstruction =
147 (L2ModificationInstruction.ModVlanHeaderInstruction) l2Instruction;
Seyeon Jeong8188da12020-03-03 12:49:48 -0800148 result.put(InstructionCodec.ETHERNET_TYPE,
149 toHexEthernetType(pushVlanInstruction.ethernetType().toString()));
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500150 break;
Jonghwan Hyun360b02f2017-08-11 14:00:07 -0700151 case VLAN_POP:
152 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700153 case MPLS_LABEL:
154 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800155 (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800156 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700157 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700158 case MPLS_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700159 final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstructions =
160 (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction;
alshabib7b808c52015-06-26 14:22:24 -0700161 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800162 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700163 break;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700164 case TUNNEL_ID:
165 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800166 (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700167 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
168 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700169 case MPLS_BOS:
170 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
171 (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction;
172 result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos());
Jonghwan Hyun360b02f2017-08-11 14:00:07 -0700173 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700174 case MPLS_POP:
Jonathan Hartcc962d82016-08-09 16:52:22 -0700175 final L2ModificationInstruction.ModMplsHeaderInstruction popHeaderInstruction =
176 (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction;
177 result.put(InstructionCodec.ETHERNET_TYPE,
Seyeon Jeong8188da12020-03-03 12:49:48 -0800178 toHexEthernetType(popHeaderInstruction.ethernetType().toString()));
Jonghwan Hyun360b02f2017-08-11 14:00:07 -0700179 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700180 case DEC_MPLS_TTL:
181 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700182 default:
Jian Lie0991662016-03-07 11:22:25 -0800183 log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700184 break;
185 }
186 }
187
188 /**
Seyeon Jeong8188da12020-03-03 12:49:48 -0800189 * Encode ethernet types in the conventional way of 4-digit hex string.
190 * @param ethTypeStr
191 * @return EtherType representation
192 */
193 private String toHexEthernetType(String ethTypeStr) {
194 EthType ethType = EthType.EtherType.valueOf(ethTypeStr.toUpperCase()).ethType();
195 return String.format("0x%04x", ethType.toShort());
196 }
197
198 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700199 * Encode an L3 modification instruction.
200 *
201 * @param result json node that the instruction attributes are added to
202 */
203 private void encodeL3(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800204 L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction;
205 result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name());
206 switch (l3Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700207 case IPV4_SRC:
208 case IPV4_DST:
209 case IPV6_SRC:
210 case IPV6_DST:
211 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800212 (L3ModificationInstruction.ModIPInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700213 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
214 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700215 case IPV6_FLABEL:
216 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
217 modFlowLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800218 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700219 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
220 break;
Rory Savagedaeb9492020-02-18 14:35:50 -0500221 case IP_DSCP:
222 final L3ModificationInstruction.ModDscpInstruction
223 modDscpInstruction =
224 (L3ModificationInstruction.ModDscpInstruction) l3Instruction;
225 result.put(InstructionCodec.IP_DSCP, modDscpInstruction.dscp());
226 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700227 case TTL_IN:
228 case TTL_OUT:
229 case DEC_TTL:
Jonathan Hartcc962d82016-08-09 16:52:22 -0700230 // These instructions have no values to be encoded
Charles Chanfbaabae2016-03-18 10:44:44 -0700231 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700232 default:
Jian Lie0991662016-03-07 11:22:25 -0800233 log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700234 break;
235 }
236 }
237
238 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700239 * Encode a L4 modification instruction.
240 *
241 * @param result json node that the instruction attributes are added to
242 */
243 private void encodeL4(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800244 L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction;
245 result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name());
246 switch (l4Instruction.subtype()) {
Hyunsun Moonfab29502015-08-25 13:39:16 -0700247 case TCP_DST:
248 case TCP_SRC:
249 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800250 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700251 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
252 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700253 case UDP_DST:
254 case UDP_SRC:
255 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800256 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700257 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
258 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700259 default:
Jian Lie0991662016-03-07 11:22:25 -0800260 log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype());
Hyunsun Moonfab29502015-08-25 13:39:16 -0700261 break;
262 }
263 }
264
Frank Wang74ce2c12018-04-11 20:26:45 +0800265 /**
266 * Encode a protocol-independent instruction.
267 *
268 * @param result json node that the instruction attributes are added to
269 */
270 private void encodePi(ObjectNode result) {
271 PiInstruction piInstruction = (PiInstruction) instruction;
272 result.put(InstructionCodec.SUBTYPE, piInstruction.action().type().name());
273 switch (piInstruction.action().type()) {
274 case ACTION:
275 final PiAction piAction = (PiAction) piInstruction.action();
276 result.put(InstructionCodec.PI_ACTION_ID, piAction.id().id());
277 final ObjectNode jsonActionParams = context.mapper().createObjectNode();
278 for (PiActionParam actionParam : piAction.parameters()) {
279 jsonActionParams.put(actionParam.id().id(),
280 HexString.toHexString(actionParam.value().asArray(), null));
281 }
282 result.set(InstructionCodec.PI_ACTION_PARAMS, jsonActionParams);
283 break;
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700284 case ACTION_PROFILE_GROUP_ID:
285 final PiActionProfileGroupId groupId = (PiActionProfileGroupId) piInstruction.action();
286 result.put(InstructionCodec.PI_ACTION_PROFILE_GROUP_ID, groupId.id());
Frank Wang74ce2c12018-04-11 20:26:45 +0800287 break;
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700288 case ACTION_PROFILE_MEMBER_ID:
289 final PiActionProfileMemberId memberId = (PiActionProfileMemberId) piInstruction.action();
290 result.put(InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID, memberId.id());
Frank Wang74ce2c12018-04-11 20:26:45 +0800291 break;
Daniele Morod900fe42021-02-11 16:12:57 +0100292 //TODO: implement JSON encoder for ACTION_SET
Frank Wang74ce2c12018-04-11 20:26:45 +0800293 default:
294 throw new IllegalArgumentException("Cannot convert protocol-independent subtype of" +
295 piInstruction.action().type().name());
296 }
297 }
Jian Lidab72562016-04-12 14:10:32 -0700298
Hyunsun Moonfab29502015-08-25 13:39:16 -0700299 /**
Jian Lidab72562016-04-12 14:10:32 -0700300 * Encodes a extension instruction.
Charles Chanfbaabae2016-03-18 10:44:44 -0700301 *
302 * @param result json node that the instruction attributes are added to
303 */
304 private void encodeExtension(ObjectNode result) {
Jian Lidab72562016-04-12 14:10:32 -0700305 final Instructions.ExtensionInstructionWrapper extensionInstruction =
306 (Instructions.ExtensionInstructionWrapper) instruction;
307
308 DeviceId deviceId = extensionInstruction.deviceId();
309
310 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
311 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
312 Device device = deviceService.getDevice(deviceId);
313
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700314 if (device == null) {
315 throw new IllegalArgumentException("Device not found");
316 }
317
Jian Lidab72562016-04-12 14:10:32 -0700318 if (device.is(ExtensionTreatmentCodec.class)) {
Seyeon Jeong8d3cad22020-02-28 01:17:34 -0800319 // for extension instructions, encoding device id is needed for the corresponding decoder
320 result.put("deviceId", deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700321 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
322 ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context);
323 result.set(InstructionCodec.EXTENSION, node);
324 } else {
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700325 throw new IllegalArgumentException(
326 "There is no codec to encode extension for device " + deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700327 }
Charles Chanfbaabae2016-03-18 10:44:44 -0700328 }
329
330 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700331 * Encodes the given instruction into JSON.
332 *
333 * @return JSON object node representing the instruction
334 */
335 public ObjectNode encode() {
336 final ObjectNode result = context.mapper().createObjectNode()
337 .put(InstructionCodec.TYPE, instruction.type().toString());
338
339 switch (instruction.type()) {
340 case OUTPUT:
341 final Instructions.OutputInstruction outputInstruction =
342 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800343 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700344 break;
345
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700346 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700347 break;
348
Jian Lice8c5602016-03-03 21:43:24 -0800349 case GROUP:
350 final Instructions.GroupInstruction groupInstruction =
351 (Instructions.GroupInstruction) instruction;
Seyeon Jeong8188da12020-03-03 12:49:48 -0800352 // a group id should be an unsigned integer
353 result.put(InstructionCodec.GROUP_ID,
354 Integer.toUnsignedLong(groupInstruction.groupId().id()));
Jian Lice8c5602016-03-03 21:43:24 -0800355 break;
356
Jian Li47b26232016-03-07 09:59:59 -0800357 case METER:
358 final Instructions.MeterInstruction meterInstruction =
359 (Instructions.MeterInstruction) instruction;
360 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
361 break;
362
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -0500363 case TABLE:
364 final Instructions.TableTypeTransition tableTransitionInstruction =
365 (Instructions.TableTypeTransition) instruction;
366 result.put(InstructionCodec.TABLE_ID, tableTransitionInstruction.tableId().toString());
367 break;
368
Jian Li70dffe42016-03-08 22:23:02 -0800369 case QUEUE:
370 final Instructions.SetQueueInstruction setQueueInstruction =
371 (Instructions.SetQueueInstruction) instruction;
372 result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId());
ke han215f2452017-04-28 13:36:05 +0800373 if (setQueueInstruction.port() != null) {
374 result.put(InstructionCodec.PORT, setQueueInstruction.port().toString());
375 }
Jian Li70dffe42016-03-08 22:23:02 -0800376 break;
377
Ray Milkeyd43fe452015-05-29 09:35:12 -0700378 case L0MODIFICATION:
379 encodeL0(result);
380 break;
381
Yafit Hadar5796d972015-10-15 13:16:11 +0300382 case L1MODIFICATION:
383 encodeL1(result);
384 break;
385
Ray Milkeyd43fe452015-05-29 09:35:12 -0700386 case L2MODIFICATION:
387 encodeL2(result);
388 break;
389
390 case L3MODIFICATION:
391 encodeL3(result);
392 break;
393
Hyunsun Moonfab29502015-08-25 13:39:16 -0700394 case L4MODIFICATION:
395 encodeL4(result);
396 break;
397
Frank Wang74ce2c12018-04-11 20:26:45 +0800398 case PROTOCOL_INDEPENDENT:
399 encodePi(result);
400 break;
401
Charles Chanfbaabae2016-03-18 10:44:44 -0700402 case EXTENSION:
403 encodeExtension(result);
404 break;
405
Ray Milkeyd43fe452015-05-29 09:35:12 -0700406 default:
407 log.info("Cannot convert instruction type of {}", instruction.type());
408 break;
409 }
410 return result;
411 }
412
413}