Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 19 | import org.onlab.osgi.DefaultServiceDirectory; |
| 20 | import org.onlab.osgi.ServiceDirectory; |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 21 | import org.onlab.packet.EthType; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 22 | import org.onlab.util.HexString; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 23 | import org.onosproject.codec.CodecContext; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 26 | import org.onosproject.net.OchSignal; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 27 | import org.onosproject.net.OduSignalId; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.ExtensionTreatmentCodec; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.instructions.Instruction; |
| 31 | import org.onosproject.net.flow.instructions.Instructions; |
| 32 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.L1ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 35 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.instructions.PiInstruction; |
| 38 | import org.onosproject.net.pi.runtime.PiAction; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiActionProfileGroupId; |
| 41 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 43 | |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 46 | /** |
| 47 | * JSON encoding of Instructions. |
| 48 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 49 | public final class EncodeInstructionCodecHelper { |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 50 | private static final Logger log = getLogger(EncodeInstructionCodecHelper.class); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 51 | 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 Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 58 | * @param context codec context for the encoding |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 59 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 60 | public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 61 | 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 Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 72 | L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction; |
| 73 | result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 74 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 75 | switch (l0Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 76 | case OCH: |
| 77 | L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 78 | (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 79 | 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 Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 87 | log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 92 | * Encode an L1 modification instruction. |
| 93 | * |
| 94 | * @param result json node that the instruction attributes are added to |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 95 | */ |
| 96 | private void encodeL1(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 97 | L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction; |
| 98 | result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name()); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 99 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 100 | switch (l1Instruction.subtype()) { |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 101 | case ODU_SIGID: |
| 102 | final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 103 | (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction; |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 104 | OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId(); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 105 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 106 | ObjectNode child = result.putObject("oduSignalId"); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 107 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 108 | 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 Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 114 | log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype()); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 115 | break; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 120 | * 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 Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 125 | L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction; |
| 126 | result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 127 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 128 | switch (l2Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 129 | case ETH_SRC: |
| 130 | case ETH_DST: |
| 131 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 132 | (L2ModificationInstruction.ModEtherInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 133 | result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString()); |
| 134 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 135 | case VLAN_ID: |
| 136 | final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 137 | (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 138 | result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort()); |
| 139 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 140 | case VLAN_PCP: |
| 141 | final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 142 | (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 143 | result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp()); |
| 144 | break; |
Konstantinos Kanonakis | 9215ff2 | 2016-11-04 13:28:11 -0500 | [diff] [blame] | 145 | case VLAN_PUSH: |
| 146 | final L2ModificationInstruction.ModVlanHeaderInstruction pushVlanInstruction = |
| 147 | (L2ModificationInstruction.ModVlanHeaderInstruction) l2Instruction; |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 148 | result.put(InstructionCodec.ETHERNET_TYPE, |
| 149 | toHexEthernetType(pushVlanInstruction.ethernetType().toString())); |
Konstantinos Kanonakis | 9215ff2 | 2016-11-04 13:28:11 -0500 | [diff] [blame] | 150 | break; |
Jonghwan Hyun | 360b02f | 2017-08-11 14:00:07 -0700 | [diff] [blame] | 151 | case VLAN_POP: |
| 152 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 153 | case MPLS_LABEL: |
| 154 | final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 155 | (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction; |
Ray Milkey | 125572b | 2016-02-22 16:48:17 -0800 | [diff] [blame] | 156 | result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 157 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 158 | case MPLS_PUSH: |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 159 | final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstructions = |
| 160 | (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction; |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 161 | result.put(InstructionCodec.ETHERNET_TYPE, |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 162 | pushHeaderInstructions.ethernetType().toShort()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 163 | break; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 164 | case TUNNEL_ID: |
| 165 | final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 166 | (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 167 | result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId()); |
| 168 | break; |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 169 | case MPLS_BOS: |
| 170 | final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction = |
| 171 | (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction; |
| 172 | result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos()); |
Jonghwan Hyun | 360b02f | 2017-08-11 14:00:07 -0700 | [diff] [blame] | 173 | break; |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 174 | case MPLS_POP: |
Jonathan Hart | cc962d8 | 2016-08-09 16:52:22 -0700 | [diff] [blame] | 175 | final L2ModificationInstruction.ModMplsHeaderInstruction popHeaderInstruction = |
| 176 | (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction; |
| 177 | result.put(InstructionCodec.ETHERNET_TYPE, |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 178 | toHexEthernetType(popHeaderInstruction.ethernetType().toString())); |
Jonghwan Hyun | 360b02f | 2017-08-11 14:00:07 -0700 | [diff] [blame] | 179 | break; |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 180 | case DEC_MPLS_TTL: |
| 181 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 182 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 183 | log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 189 | * 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 Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 199 | * 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 Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 204 | L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction; |
| 205 | result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name()); |
| 206 | switch (l3Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 207 | case IPV4_SRC: |
| 208 | case IPV4_DST: |
| 209 | case IPV6_SRC: |
| 210 | case IPV6_DST: |
| 211 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 212 | (L3ModificationInstruction.ModIPInstruction) l3Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 213 | result.put(InstructionCodec.IP, modIPInstruction.ip().toString()); |
| 214 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 215 | case IPV6_FLABEL: |
| 216 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction |
| 217 | modFlowLabelInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 218 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 219 | result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel()); |
| 220 | break; |
Rory Savage | daeb949 | 2020-02-18 14:35:50 -0500 | [diff] [blame] | 221 | case IP_DSCP: |
| 222 | final L3ModificationInstruction.ModDscpInstruction |
| 223 | modDscpInstruction = |
| 224 | (L3ModificationInstruction.ModDscpInstruction) l3Instruction; |
| 225 | result.put(InstructionCodec.IP_DSCP, modDscpInstruction.dscp()); |
| 226 | break; |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 227 | case TTL_IN: |
| 228 | case TTL_OUT: |
| 229 | case DEC_TTL: |
Jonathan Hart | cc962d8 | 2016-08-09 16:52:22 -0700 | [diff] [blame] | 230 | // These instructions have no values to be encoded |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 231 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 232 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 233 | log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 239 | * 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 Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 244 | L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction; |
| 245 | result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name()); |
| 246 | switch (l4Instruction.subtype()) { |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 247 | case TCP_DST: |
| 248 | case TCP_SRC: |
| 249 | final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 250 | (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 251 | result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt()); |
| 252 | break; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 253 | case UDP_DST: |
| 254 | case UDP_SRC: |
| 255 | final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 256 | (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 257 | result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt()); |
| 258 | break; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 259 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 260 | log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype()); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 265 | /** |
| 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 Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 284 | case ACTION_PROFILE_GROUP_ID: |
| 285 | final PiActionProfileGroupId groupId = (PiActionProfileGroupId) piInstruction.action(); |
| 286 | result.put(InstructionCodec.PI_ACTION_PROFILE_GROUP_ID, groupId.id()); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 287 | break; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 288 | case ACTION_PROFILE_MEMBER_ID: |
| 289 | final PiActionProfileMemberId memberId = (PiActionProfileMemberId) piInstruction.action(); |
| 290 | result.put(InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID, memberId.id()); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 291 | break; |
| 292 | default: |
| 293 | throw new IllegalArgumentException("Cannot convert protocol-independent subtype of" + |
| 294 | piInstruction.action().type().name()); |
| 295 | } |
| 296 | } |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 297 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 298 | /** |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 299 | * Encodes a extension instruction. |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 300 | * |
| 301 | * @param result json node that the instruction attributes are added to |
| 302 | */ |
| 303 | private void encodeExtension(ObjectNode result) { |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 304 | final Instructions.ExtensionInstructionWrapper extensionInstruction = |
| 305 | (Instructions.ExtensionInstructionWrapper) instruction; |
| 306 | |
| 307 | DeviceId deviceId = extensionInstruction.deviceId(); |
| 308 | |
| 309 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 310 | DeviceService deviceService = serviceDirectory.get(DeviceService.class); |
| 311 | Device device = deviceService.getDevice(deviceId); |
| 312 | |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 313 | if (device == null) { |
| 314 | throw new IllegalArgumentException("Device not found"); |
| 315 | } |
| 316 | |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 317 | if (device.is(ExtensionTreatmentCodec.class)) { |
Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 318 | // for extension instructions, encoding device id is needed for the corresponding decoder |
| 319 | result.put("deviceId", deviceId.toString()); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 320 | ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class); |
| 321 | ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context); |
| 322 | result.set(InstructionCodec.EXTENSION, node); |
| 323 | } else { |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 324 | throw new IllegalArgumentException( |
| 325 | "There is no codec to encode extension for device " + deviceId.toString()); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 326 | } |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 330 | * Encodes the given instruction into JSON. |
| 331 | * |
| 332 | * @return JSON object node representing the instruction |
| 333 | */ |
| 334 | public ObjectNode encode() { |
| 335 | final ObjectNode result = context.mapper().createObjectNode() |
| 336 | .put(InstructionCodec.TYPE, instruction.type().toString()); |
| 337 | |
| 338 | switch (instruction.type()) { |
| 339 | case OUTPUT: |
| 340 | final Instructions.OutputInstruction outputInstruction = |
| 341 | (Instructions.OutputInstruction) instruction; |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 342 | result.put(InstructionCodec.PORT, outputInstruction.port().toString()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 343 | break; |
| 344 | |
Brian O'Connor | 7664e7d | 2015-10-09 00:57:58 -0700 | [diff] [blame] | 345 | case NOACTION: |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 346 | break; |
| 347 | |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 348 | case GROUP: |
| 349 | final Instructions.GroupInstruction groupInstruction = |
| 350 | (Instructions.GroupInstruction) instruction; |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 351 | // a group id should be an unsigned integer |
| 352 | result.put(InstructionCodec.GROUP_ID, |
| 353 | Integer.toUnsignedLong(groupInstruction.groupId().id())); |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 354 | break; |
| 355 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 356 | case METER: |
| 357 | final Instructions.MeterInstruction meterInstruction = |
| 358 | (Instructions.MeterInstruction) instruction; |
| 359 | result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString()); |
| 360 | break; |
| 361 | |
Konstantinos Kanonakis | c5d93e6 | 2016-04-12 18:51:20 -0500 | [diff] [blame] | 362 | case TABLE: |
| 363 | final Instructions.TableTypeTransition tableTransitionInstruction = |
| 364 | (Instructions.TableTypeTransition) instruction; |
| 365 | result.put(InstructionCodec.TABLE_ID, tableTransitionInstruction.tableId().toString()); |
| 366 | break; |
| 367 | |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 368 | case QUEUE: |
| 369 | final Instructions.SetQueueInstruction setQueueInstruction = |
| 370 | (Instructions.SetQueueInstruction) instruction; |
| 371 | result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId()); |
ke han | 215f245 | 2017-04-28 13:36:05 +0800 | [diff] [blame] | 372 | if (setQueueInstruction.port() != null) { |
| 373 | result.put(InstructionCodec.PORT, setQueueInstruction.port().toString()); |
| 374 | } |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 375 | break; |
| 376 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 377 | case L0MODIFICATION: |
| 378 | encodeL0(result); |
| 379 | break; |
| 380 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 381 | case L1MODIFICATION: |
| 382 | encodeL1(result); |
| 383 | break; |
| 384 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 385 | case L2MODIFICATION: |
| 386 | encodeL2(result); |
| 387 | break; |
| 388 | |
| 389 | case L3MODIFICATION: |
| 390 | encodeL3(result); |
| 391 | break; |
| 392 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 393 | case L4MODIFICATION: |
| 394 | encodeL4(result); |
| 395 | break; |
| 396 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 397 | case PROTOCOL_INDEPENDENT: |
| 398 | encodePi(result); |
| 399 | break; |
| 400 | |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 401 | case EXTENSION: |
| 402 | encodeExtension(result); |
| 403 | break; |
| 404 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 405 | default: |
| 406 | log.info("Cannot convert instruction type of {}", instruction.type()); |
| 407 | break; |
| 408 | } |
| 409 | return result; |
| 410 | } |
| 411 | |
| 412 | } |