Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 21 | import org.onlab.util.HexString; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 22 | import org.onosproject.codec.CodecContext; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 23 | import org.onosproject.codec.ExtensionTreatmentCodec; |
| 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; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.instructions.Instruction; |
| 30 | import org.onosproject.net.flow.instructions.Instructions; |
| 31 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.L1ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 34 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 37 | |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 39 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 40 | /** |
| 41 | * JSON encoding of Instructions. |
| 42 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 43 | public final class EncodeInstructionCodecHelper { |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 44 | protected static final Logger log = getLogger(EncodeInstructionCodecHelper.class); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | private final Instruction instruction; |
| 46 | private final CodecContext context; |
| 47 | |
| 48 | /** |
| 49 | * Creates an instruction object encoder. |
| 50 | * |
| 51 | * @param instruction instruction to encode |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 52 | * @param context codec context for the encoding |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 53 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 54 | public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 55 | this.instruction = instruction; |
| 56 | this.context = context; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Encode an L0 modification instruction. |
| 62 | * |
| 63 | * @param result json node that the instruction attributes are added to |
| 64 | */ |
| 65 | private void encodeL0(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 66 | L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction; |
| 67 | result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 68 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 69 | switch (l0Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 70 | case OCH: |
| 71 | L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 72 | (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 73 | OchSignal ochSignal = ochSignalInstruction.lambda(); |
| 74 | result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name()); |
| 75 | result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name()); |
| 76 | result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier()); |
| 77 | result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity()); |
| 78 | break; |
| 79 | |
| 80 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 81 | log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 86 | * Encode an L1 modification instruction. |
| 87 | * |
| 88 | * @param result json node that the instruction attributes are added to |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 89 | */ |
| 90 | private void encodeL1(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 91 | L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction; |
| 92 | result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name()); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 93 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 94 | switch (l1Instruction.subtype()) { |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 95 | case ODU_SIGID: |
| 96 | final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 97 | (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction; |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 98 | OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId(); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 99 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 100 | ObjectNode child = result.putObject("oduSignalId"); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 101 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 102 | child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber()); |
| 103 | child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength()); |
| 104 | child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP, |
| 105 | HexString.toHexString(oduSignalId.tributarySlotBitmap())); |
| 106 | break; |
| 107 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 108 | log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype()); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 109 | break; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 114 | * Encode an L2 modification instruction. |
| 115 | * |
| 116 | * @param result json node that the instruction attributes are added to |
| 117 | */ |
| 118 | private void encodeL2(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 119 | L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction; |
| 120 | result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 121 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 122 | switch (l2Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 123 | case ETH_SRC: |
| 124 | case ETH_DST: |
| 125 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 126 | (L2ModificationInstruction.ModEtherInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 127 | result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString()); |
| 128 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 129 | case VLAN_ID: |
| 130 | final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 131 | (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 132 | result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort()); |
| 133 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 134 | case VLAN_PCP: |
| 135 | final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 136 | (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 137 | result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp()); |
| 138 | break; |
Konstantinos Kanonakis | 9215ff2 | 2016-11-04 13:28:11 -0500 | [diff] [blame] | 139 | case VLAN_PUSH: |
| 140 | final L2ModificationInstruction.ModVlanHeaderInstruction pushVlanInstruction = |
| 141 | (L2ModificationInstruction.ModVlanHeaderInstruction) l2Instruction; |
| 142 | result.put(InstructionCodec.ETHERNET_TYPE, pushVlanInstruction.ethernetType().toString()); |
| 143 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 144 | case MPLS_LABEL: |
| 145 | final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 146 | (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction; |
Ray Milkey | 125572b | 2016-02-22 16:48:17 -0800 | [diff] [blame] | 147 | result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 148 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 149 | case MPLS_PUSH: |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 150 | final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstructions = |
| 151 | (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction; |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 152 | result.put(InstructionCodec.ETHERNET_TYPE, |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 153 | pushHeaderInstructions.ethernetType().toShort()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 154 | break; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 155 | case TUNNEL_ID: |
| 156 | final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 157 | (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 158 | result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId()); |
| 159 | break; |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 160 | case MPLS_BOS: |
| 161 | final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction = |
| 162 | (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction; |
| 163 | result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos()); |
| 164 | case MPLS_POP: |
| 165 | case DEC_MPLS_TTL: |
| 166 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 167 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 168 | log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Encode an L3 modification instruction. |
| 175 | * |
| 176 | * @param result json node that the instruction attributes are added to |
| 177 | */ |
| 178 | private void encodeL3(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 179 | L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction; |
| 180 | result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name()); |
| 181 | switch (l3Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 182 | case IPV4_SRC: |
| 183 | case IPV4_DST: |
| 184 | case IPV6_SRC: |
| 185 | case IPV6_DST: |
| 186 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 187 | (L3ModificationInstruction.ModIPInstruction) l3Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 188 | result.put(InstructionCodec.IP, modIPInstruction.ip().toString()); |
| 189 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 190 | case IPV6_FLABEL: |
| 191 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction |
| 192 | modFlowLabelInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 193 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 194 | result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel()); |
| 195 | break; |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 196 | case TTL_IN: |
| 197 | case TTL_OUT: |
| 198 | case DEC_TTL: |
| 199 | break; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 200 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 201 | log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 207 | * Encode a L4 modification instruction. |
| 208 | * |
| 209 | * @param result json node that the instruction attributes are added to |
| 210 | */ |
| 211 | private void encodeL4(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 212 | L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction; |
| 213 | result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name()); |
| 214 | switch (l4Instruction.subtype()) { |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 215 | case TCP_DST: |
| 216 | case TCP_SRC: |
| 217 | final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 218 | (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 219 | result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt()); |
| 220 | break; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 221 | case UDP_DST: |
| 222 | case UDP_SRC: |
| 223 | final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 224 | (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 225 | result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt()); |
| 226 | break; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 227 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame] | 228 | log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype()); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 233 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 234 | /** |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 235 | * Encodes a extension instruction. |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 236 | * |
| 237 | * @param result json node that the instruction attributes are added to |
| 238 | */ |
| 239 | private void encodeExtension(ObjectNode result) { |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 240 | final Instructions.ExtensionInstructionWrapper extensionInstruction = |
| 241 | (Instructions.ExtensionInstructionWrapper) instruction; |
| 242 | |
| 243 | DeviceId deviceId = extensionInstruction.deviceId(); |
| 244 | |
| 245 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 246 | DeviceService deviceService = serviceDirectory.get(DeviceService.class); |
| 247 | Device device = deviceService.getDevice(deviceId); |
| 248 | |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 249 | if (device == null) { |
| 250 | throw new IllegalArgumentException("Device not found"); |
| 251 | } |
| 252 | |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 253 | if (device.is(ExtensionTreatmentCodec.class)) { |
| 254 | ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class); |
| 255 | ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context); |
| 256 | result.set(InstructionCodec.EXTENSION, node); |
| 257 | } else { |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 258 | throw new IllegalArgumentException( |
| 259 | "There is no codec to encode extension for device " + deviceId.toString()); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 260 | } |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 264 | * Encodes the given instruction into JSON. |
| 265 | * |
| 266 | * @return JSON object node representing the instruction |
| 267 | */ |
| 268 | public ObjectNode encode() { |
| 269 | final ObjectNode result = context.mapper().createObjectNode() |
| 270 | .put(InstructionCodec.TYPE, instruction.type().toString()); |
| 271 | |
| 272 | switch (instruction.type()) { |
| 273 | case OUTPUT: |
| 274 | final Instructions.OutputInstruction outputInstruction = |
| 275 | (Instructions.OutputInstruction) instruction; |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 276 | result.put(InstructionCodec.PORT, outputInstruction.port().toString()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 277 | break; |
| 278 | |
Brian O'Connor | 7664e7d | 2015-10-09 00:57:58 -0700 | [diff] [blame] | 279 | case NOACTION: |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 280 | break; |
| 281 | |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 282 | case GROUP: |
| 283 | final Instructions.GroupInstruction groupInstruction = |
| 284 | (Instructions.GroupInstruction) instruction; |
| 285 | result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString()); |
| 286 | break; |
| 287 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 288 | case METER: |
| 289 | final Instructions.MeterInstruction meterInstruction = |
| 290 | (Instructions.MeterInstruction) instruction; |
| 291 | result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString()); |
| 292 | break; |
| 293 | |
Konstantinos Kanonakis | c5d93e6 | 2016-04-12 18:51:20 -0500 | [diff] [blame] | 294 | case TABLE: |
| 295 | final Instructions.TableTypeTransition tableTransitionInstruction = |
| 296 | (Instructions.TableTypeTransition) instruction; |
| 297 | result.put(InstructionCodec.TABLE_ID, tableTransitionInstruction.tableId().toString()); |
| 298 | break; |
| 299 | |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 300 | case QUEUE: |
| 301 | final Instructions.SetQueueInstruction setQueueInstruction = |
| 302 | (Instructions.SetQueueInstruction) instruction; |
| 303 | result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId()); |
| 304 | result.put(InstructionCodec.PORT, setQueueInstruction.port().toString()); |
| 305 | break; |
| 306 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 307 | case L0MODIFICATION: |
| 308 | encodeL0(result); |
| 309 | break; |
| 310 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 311 | case L1MODIFICATION: |
| 312 | encodeL1(result); |
| 313 | break; |
| 314 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 315 | case L2MODIFICATION: |
| 316 | encodeL2(result); |
| 317 | break; |
| 318 | |
| 319 | case L3MODIFICATION: |
| 320 | encodeL3(result); |
| 321 | break; |
| 322 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 323 | case L4MODIFICATION: |
| 324 | encodeL4(result); |
| 325 | break; |
| 326 | |
Charles Chan | fbaabae | 2016-03-18 10:44:44 -0700 | [diff] [blame] | 327 | case EXTENSION: |
| 328 | encodeExtension(result); |
| 329 | break; |
| 330 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 331 | default: |
| 332 | log.info("Cannot convert instruction type of {}", instruction.type()); |
| 333 | break; |
| 334 | } |
| 335 | return result; |
| 336 | } |
| 337 | |
| 338 | } |