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