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 |
| 45 | * @param context codec context for the encoding |
| 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) { |
| 59 | L0ModificationInstruction instruction = |
| 60 | (L0ModificationInstruction) this.instruction; |
| 61 | result.put(InstructionCodec.SUBTYPE, instruction.subtype().name()); |
| 62 | |
| 63 | switch (instruction.subtype()) { |
| 64 | case LAMBDA: |
| 65 | final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction = |
| 66 | (L0ModificationInstruction.ModLambdaInstruction) instruction; |
| 67 | result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda()); |
| 68 | break; |
| 69 | |
| 70 | case OCH: |
| 71 | L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction = |
| 72 | (L0ModificationInstruction.ModOchSignalInstruction) instruction; |
| 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: |
| 81 | log.info("Cannot convert L0 subtype of {}", instruction.subtype()); |
| 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) { |
| 91 | L1ModificationInstruction instruction = |
| 92 | (L1ModificationInstruction) this.instruction; |
| 93 | result.put(InstructionCodec.SUBTYPE, instruction.subtype().name()); |
| 94 | |
| 95 | switch (instruction.subtype()) { |
| 96 | case ODU_SIGID: |
| 97 | final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction = |
| 98 | (L1ModificationInstruction.ModOduSignalIdInstruction) instruction; |
| 99 | OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId(); |
| 100 | |
| 101 | ObjectNode child = result.putObject("oduSignalId"); |
| 102 | |
| 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, HexString.toHexString(oduSignalId.tributarySlotBitmap())); |
| 106 | break; |
| 107 | default: |
| 108 | log.info("Cannot convert L1 subtype of {}", instruction.subtype()); |
| 109 | break; |
| 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) { |
| 119 | L2ModificationInstruction instruction = |
| 120 | (L2ModificationInstruction) this.instruction; |
| 121 | result.put(InstructionCodec.SUBTYPE, instruction.subtype().name()); |
| 122 | |
| 123 | switch (instruction.subtype()) { |
| 124 | case ETH_SRC: |
| 125 | case ETH_DST: |
| 126 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
| 127 | (L2ModificationInstruction.ModEtherInstruction) instruction; |
| 128 | result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString()); |
| 129 | break; |
| 130 | |
| 131 | case VLAN_ID: |
| 132 | final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction = |
| 133 | (L2ModificationInstruction.ModVlanIdInstruction) instruction; |
| 134 | result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort()); |
| 135 | break; |
| 136 | |
| 137 | case VLAN_PCP: |
| 138 | final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction = |
| 139 | (L2ModificationInstruction.ModVlanPcpInstruction) instruction; |
| 140 | result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp()); |
| 141 | break; |
| 142 | |
| 143 | case MPLS_LABEL: |
| 144 | final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction = |
| 145 | (L2ModificationInstruction.ModMplsLabelInstruction) instruction; |
Ray Milkey | 125572b | 2016-02-22 16:48:17 -0800 | [diff] [blame] | 146 | result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 147 | break; |
| 148 | |
| 149 | case MPLS_PUSH: |
| 150 | final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions = |
| 151 | (L2ModificationInstruction.PushHeaderInstructions) instruction; |
| 152 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 153 | result.put(InstructionCodec.ETHERNET_TYPE, |
| 154 | pushHeaderInstructions.ethernetType().toShort()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 155 | break; |
| 156 | |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 157 | case TUNNEL_ID: |
| 158 | final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction = |
| 159 | (L2ModificationInstruction.ModTunnelIdInstruction) instruction; |
| 160 | result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId()); |
| 161 | break; |
| 162 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 163 | default: |
| 164 | log.info("Cannot convert L2 subtype of {}", instruction.subtype()); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Encode an L3 modification instruction. |
| 171 | * |
| 172 | * @param result json node that the instruction attributes are added to |
| 173 | */ |
| 174 | private void encodeL3(ObjectNode result) { |
| 175 | L3ModificationInstruction instruction = |
| 176 | (L3ModificationInstruction) this.instruction; |
| 177 | result.put(InstructionCodec.SUBTYPE, instruction.subtype().name()); |
| 178 | switch (instruction.subtype()) { |
| 179 | case IPV4_SRC: |
| 180 | case IPV4_DST: |
| 181 | case IPV6_SRC: |
| 182 | case IPV6_DST: |
| 183 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
| 184 | (L3ModificationInstruction.ModIPInstruction) instruction; |
| 185 | result.put(InstructionCodec.IP, modIPInstruction.ip().toString()); |
| 186 | break; |
| 187 | |
| 188 | case IPV6_FLABEL: |
| 189 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction |
| 190 | modFlowLabelInstruction = |
| 191 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction; |
| 192 | result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel()); |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | log.info("Cannot convert L3 subtype of {}", instruction.subtype()); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 202 | * Encode a L4 modification instruction. |
| 203 | * |
| 204 | * @param result json node that the instruction attributes are added to |
| 205 | */ |
| 206 | private void encodeL4(ObjectNode result) { |
| 207 | L4ModificationInstruction instruction = |
| 208 | (L4ModificationInstruction) this.instruction; |
| 209 | result.put(InstructionCodec.SUBTYPE, instruction.subtype().name()); |
| 210 | switch (instruction.subtype()) { |
| 211 | case TCP_DST: |
| 212 | case TCP_SRC: |
| 213 | final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction = |
| 214 | (L4ModificationInstruction.ModTransportPortInstruction) instruction; |
| 215 | result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt()); |
| 216 | break; |
| 217 | |
| 218 | case UDP_DST: |
| 219 | case UDP_SRC: |
| 220 | final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction = |
| 221 | (L4ModificationInstruction.ModTransportPortInstruction) instruction; |
| 222 | result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt()); |
| 223 | break; |
| 224 | |
| 225 | default: |
| 226 | log.info("Cannot convert L4 subtype of {}", instruction.subtype()); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 232 | * Encodes the given instruction into JSON. |
| 233 | * |
| 234 | * @return JSON object node representing the instruction |
| 235 | */ |
| 236 | public ObjectNode encode() { |
| 237 | final ObjectNode result = context.mapper().createObjectNode() |
| 238 | .put(InstructionCodec.TYPE, instruction.type().toString()); |
| 239 | |
| 240 | switch (instruction.type()) { |
| 241 | case OUTPUT: |
| 242 | final Instructions.OutputInstruction outputInstruction = |
| 243 | (Instructions.OutputInstruction) instruction; |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 244 | result.put(InstructionCodec.PORT, outputInstruction.port().toString()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case DROP: |
Brian O'Connor | 7664e7d | 2015-10-09 00:57:58 -0700 | [diff] [blame] | 248 | case NOACTION: |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 249 | break; |
| 250 | |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame^] | 251 | case GROUP: |
| 252 | final Instructions.GroupInstruction groupInstruction = |
| 253 | (Instructions.GroupInstruction) instruction; |
| 254 | result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString()); |
| 255 | break; |
| 256 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 257 | case L0MODIFICATION: |
| 258 | encodeL0(result); |
| 259 | break; |
| 260 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 261 | case L1MODIFICATION: |
| 262 | encodeL1(result); |
| 263 | break; |
| 264 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 265 | case L2MODIFICATION: |
| 266 | encodeL2(result); |
| 267 | break; |
| 268 | |
| 269 | case L3MODIFICATION: |
| 270 | encodeL3(result); |
| 271 | break; |
| 272 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 273 | case L4MODIFICATION: |
| 274 | encodeL4(result); |
| 275 | break; |
| 276 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 277 | default: |
| 278 | log.info("Cannot convert instruction type of {}", instruction.type()); |
| 279 | break; |
| 280 | } |
| 281 | return result; |
| 282 | } |
| 283 | |
| 284 | } |