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