Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 19 | import org.onlab.util.HexString; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.net.OchSignal; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 22 | import org.onosproject.net.OduSignalId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.instructions.Instruction; |
| 24 | import org.onosproject.net.flow.instructions.Instructions; |
| 25 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.L1ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 27 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 28 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 33 | /** |
| 34 | * JSON encoding of Instructions. |
| 35 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 36 | public final class EncodeInstructionCodecHelper { |
| 37 | protected static final Logger log = LoggerFactory.getLogger(EncodeInstructionCodecHelper.class); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 38 | private final Instruction instruction; |
| 39 | private final CodecContext context; |
| 40 | |
| 41 | /** |
| 42 | * Creates an instruction object encoder. |
| 43 | * |
| 44 | * @param instruction instruction to encode |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 45 | * @param context codec context for the encoding |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 46 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 47 | public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 48 | this.instruction = instruction; |
| 49 | this.context = context; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Encode an L0 modification instruction. |
| 55 | * |
| 56 | * @param result json node that the instruction attributes are added to |
| 57 | */ |
| 58 | private void encodeL0(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 59 | L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction; |
| 60 | result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 61 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 62 | switch (l0Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 63 | case LAMBDA: |
| 64 | final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 65 | (L0ModificationInstruction.ModLambdaInstruction) l0Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 66 | result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda()); |
| 67 | break; |
| 68 | |
| 69 | case OCH: |
| 70 | L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 71 | (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 72 | OchSignal ochSignal = ochSignalInstruction.lambda(); |
| 73 | result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name()); |
| 74 | result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name()); |
| 75 | result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier()); |
| 76 | result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity()); |
| 77 | break; |
| 78 | |
| 79 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 80 | log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 85 | * Encode an L1 modification instruction. |
| 86 | * |
| 87 | * @param result json node that the instruction attributes are added to |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 88 | */ |
| 89 | private void encodeL1(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 90 | L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction; |
| 91 | result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name()); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 92 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 93 | switch (l1Instruction.subtype()) { |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 94 | case ODU_SIGID: |
| 95 | final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 96 | (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction; |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 97 | OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId(); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 98 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 99 | ObjectNode child = result.putObject("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 | child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber()); |
| 102 | child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength()); |
| 103 | child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP, |
| 104 | HexString.toHexString(oduSignalId.tributarySlotBitmap())); |
| 105 | break; |
| 106 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 107 | log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype()); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 108 | break; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 113 | * Encode an L2 modification instruction. |
| 114 | * |
| 115 | * @param result json node that the instruction attributes are added to |
| 116 | */ |
| 117 | private void encodeL2(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 118 | L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction; |
| 119 | result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 120 | |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 121 | switch (l2Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 122 | case ETH_SRC: |
| 123 | case ETH_DST: |
| 124 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 125 | (L2ModificationInstruction.ModEtherInstruction) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 126 | result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString()); |
| 127 | break; |
| 128 | |
| 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; |
| 134 | |
| 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; |
| 140 | |
| 141 | case MPLS_LABEL: |
| 142 | final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 143 | (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction; |
Ray Milkey | 125572b | 2016-02-22 16:48:17 -0800 | [diff] [blame] | 144 | result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 145 | break; |
| 146 | |
| 147 | case MPLS_PUSH: |
| 148 | final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 149 | (L2ModificationInstruction.PushHeaderInstructions) l2Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 150 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 151 | result.put(InstructionCodec.ETHERNET_TYPE, |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 152 | pushHeaderInstructions.ethernetType().toShort()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 153 | break; |
| 154 | |
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; |
| 160 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 161 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 162 | log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Encode an L3 modification instruction. |
| 169 | * |
| 170 | * @param result json node that the instruction attributes are added to |
| 171 | */ |
| 172 | private void encodeL3(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 173 | L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction; |
| 174 | result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name()); |
| 175 | switch (l3Instruction.subtype()) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 176 | case IPV4_SRC: |
| 177 | case IPV4_DST: |
| 178 | case IPV6_SRC: |
| 179 | case IPV6_DST: |
| 180 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 181 | (L3ModificationInstruction.ModIPInstruction) l3Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 182 | result.put(InstructionCodec.IP, modIPInstruction.ip().toString()); |
| 183 | break; |
| 184 | |
| 185 | case IPV6_FLABEL: |
| 186 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction |
| 187 | modFlowLabelInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 188 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 189 | result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel()); |
| 190 | break; |
| 191 | |
| 192 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 193 | log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 199 | * Encode a L4 modification instruction. |
| 200 | * |
| 201 | * @param result json node that the instruction attributes are added to |
| 202 | */ |
| 203 | private void encodeL4(ObjectNode result) { |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 204 | L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction; |
| 205 | result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name()); |
| 206 | switch (l4Instruction.subtype()) { |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 207 | case TCP_DST: |
| 208 | case TCP_SRC: |
| 209 | final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 210 | (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 211 | result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt()); |
| 212 | break; |
| 213 | |
| 214 | case UDP_DST: |
| 215 | case UDP_SRC: |
| 216 | final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction = |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 217 | (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 218 | result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt()); |
| 219 | break; |
| 220 | |
| 221 | default: |
Jian Li | e099166 | 2016-03-07 11:22:25 -0800 | [diff] [blame^] | 222 | log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype()); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 228 | * Encodes the given instruction into JSON. |
| 229 | * |
| 230 | * @return JSON object node representing the instruction |
| 231 | */ |
| 232 | public ObjectNode encode() { |
| 233 | final ObjectNode result = context.mapper().createObjectNode() |
| 234 | .put(InstructionCodec.TYPE, instruction.type().toString()); |
| 235 | |
| 236 | switch (instruction.type()) { |
| 237 | case OUTPUT: |
| 238 | final Instructions.OutputInstruction outputInstruction = |
| 239 | (Instructions.OutputInstruction) instruction; |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 240 | result.put(InstructionCodec.PORT, outputInstruction.port().toString()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | case DROP: |
Brian O'Connor | 7664e7d | 2015-10-09 00:57:58 -0700 | [diff] [blame] | 244 | case NOACTION: |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 245 | break; |
| 246 | |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 247 | case GROUP: |
| 248 | final Instructions.GroupInstruction groupInstruction = |
| 249 | (Instructions.GroupInstruction) instruction; |
| 250 | result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString()); |
| 251 | break; |
| 252 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 253 | case METER: |
| 254 | final Instructions.MeterInstruction meterInstruction = |
| 255 | (Instructions.MeterInstruction) instruction; |
| 256 | result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString()); |
| 257 | break; |
| 258 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 259 | case L0MODIFICATION: |
| 260 | encodeL0(result); |
| 261 | break; |
| 262 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 263 | case L1MODIFICATION: |
| 264 | encodeL1(result); |
| 265 | break; |
| 266 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 267 | case L2MODIFICATION: |
| 268 | encodeL2(result); |
| 269 | break; |
| 270 | |
| 271 | case L3MODIFICATION: |
| 272 | encodeL3(result); |
| 273 | break; |
| 274 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 275 | case L4MODIFICATION: |
| 276 | encodeL4(result); |
| 277 | break; |
| 278 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 279 | default: |
| 280 | log.info("Cannot convert instruction type of {}", instruction.type()); |
| 281 | break; |
| 282 | } |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | } |