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 | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 1ef82db | 2016-03-03 14:43:21 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.MplsLabel; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 25 | import org.onlab.packet.TpPort; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 26 | import org.onlab.packet.VlanId; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 27 | import org.onlab.util.HexString; |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 28 | import org.onosproject.codec.CodecContext; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 29 | import org.onosproject.codec.ExtensionTreatmentCodec; |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 30 | import org.onosproject.core.DefaultGroupId; |
| 31 | import org.onosproject.core.GroupId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 32 | import org.onosproject.net.ChannelSpacing; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 35 | import org.onosproject.net.GridType; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 36 | import org.onosproject.net.OchSignal; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 37 | import org.onosproject.net.OduSignalId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 38 | import org.onosproject.net.PortNumber; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.instructions.Instruction; |
| 42 | import org.onosproject.net.flow.instructions.Instructions; |
| 43 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 44 | import org.onosproject.net.flow.instructions.L1ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 46 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 48 | import org.onosproject.net.meter.MeterId; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 50 | |
Jian Li | 1ef82db | 2016-03-03 14:43:21 -0800 | [diff] [blame] | 51 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Decoding portion of the instruction codec. |
| 56 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 57 | public final class DecodeInstructionCodecHelper { |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 58 | protected static final Logger log = getLogger(DecodeInstructionCodecHelper.class); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 59 | private final ObjectNode json; |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 60 | private final CodecContext context; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Creates a decode instruction codec object. |
| 64 | * |
| 65 | * @param json JSON object to decode |
| 66 | */ |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 67 | public DecodeInstructionCodecHelper(ObjectNode json, CodecContext context) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 68 | this.json = json; |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 69 | this.context = context; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Decodes a Layer 2 instruction. |
| 74 | * |
| 75 | * @return instruction object decoded from the JSON |
| 76 | * @throws IllegalArgumentException if the JSON is invalid |
| 77 | */ |
| 78 | private Instruction decodeL2() { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 79 | String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), |
| 80 | InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 81 | |
| 82 | if (subType.equals(L2ModificationInstruction.L2SubType.ETH_SRC.name())) { |
| 83 | String mac = nullIsIllegal(json.get(InstructionCodec.MAC), |
| 84 | InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText(); |
| 85 | return Instructions.modL2Src(MacAddress.valueOf(mac)); |
| 86 | } else if (subType.equals(L2ModificationInstruction.L2SubType.ETH_DST.name())) { |
| 87 | String mac = nullIsIllegal(json.get(InstructionCodec.MAC), |
| 88 | InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText(); |
| 89 | return Instructions.modL2Dst(MacAddress.valueOf(mac)); |
| 90 | } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_ID.name())) { |
| 91 | short vlanId = (short) nullIsIllegal(json.get(InstructionCodec.VLAN_ID), |
| 92 | InstructionCodec.VLAN_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 93 | return Instructions.modVlanId(VlanId.vlanId(vlanId)); |
| 94 | } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PCP.name())) { |
| 95 | byte vlanPcp = (byte) nullIsIllegal(json.get(InstructionCodec.VLAN_PCP), |
| 96 | InstructionCodec.VLAN_PCP + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 97 | return Instructions.modVlanPcp(vlanPcp); |
| 98 | } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_LABEL.name())) { |
| 99 | int label = nullIsIllegal(json.get(InstructionCodec.MPLS_LABEL), |
| 100 | InstructionCodec.MPLS_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 101 | return Instructions.modMplsLabel(MplsLabel.mplsLabel(label)); |
| 102 | } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_PUSH.name())) { |
| 103 | return Instructions.pushMpls(); |
| 104 | } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_POP.name())) { |
| 105 | return Instructions.popMpls(); |
| 106 | } else if (subType.equals(L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name())) { |
| 107 | return Instructions.decMplsTtl(); |
| 108 | } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_POP.name())) { |
| 109 | return Instructions.popVlan(); |
| 110 | } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PUSH.name())) { |
| 111 | return Instructions.pushVlan(); |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 112 | } else if (subType.equals(L2ModificationInstruction.L2SubType.TUNNEL_ID.name())) { |
| 113 | long tunnelId = nullIsIllegal(json.get(InstructionCodec.TUNNEL_ID), |
| 114 | InstructionCodec.TUNNEL_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong(); |
| 115 | return Instructions.modTunnelId(tunnelId); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 116 | } |
| 117 | throw new IllegalArgumentException("L2 Instruction subtype " |
| 118 | + subType + " is not supported"); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Decodes a Layer 3 instruction. |
| 123 | * |
| 124 | * @return instruction object decoded from the JSON |
| 125 | * @throws IllegalArgumentException if the JSON is invalid |
| 126 | */ |
| 127 | private Instruction decodeL3() { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 128 | String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), |
| 129 | InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 130 | |
| 131 | if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_SRC.name())) { |
| 132 | IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP), |
| 133 | InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText()); |
| 134 | return Instructions.modL3Src(ip); |
| 135 | } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_DST.name())) { |
| 136 | IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP), |
| 137 | InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText()); |
| 138 | return Instructions.modL3Dst(ip); |
| 139 | } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_SRC.name())) { |
| 140 | IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP), |
| 141 | InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText()); |
| 142 | return Instructions.modL3IPv6Src(ip); |
| 143 | } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_DST.name())) { |
| 144 | IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP), |
| 145 | InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText()); |
| 146 | return Instructions.modL3IPv6Dst(ip); |
| 147 | } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_FLABEL.name())) { |
| 148 | int flowLabel = nullIsIllegal(json.get(InstructionCodec.FLOW_LABEL), |
| 149 | InstructionCodec.FLOW_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 150 | return Instructions.modL3IPv6FlowLabel(flowLabel); |
| 151 | } |
| 152 | throw new IllegalArgumentException("L3 Instruction subtype " |
| 153 | + subType + " is not supported"); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Decodes a Layer 0 instruction. |
| 158 | * |
| 159 | * @return instruction object decoded from the JSON |
| 160 | * @throws IllegalArgumentException if the JSON is invalid |
| 161 | */ |
| 162 | private Instruction decodeL0() { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 163 | String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), |
| 164 | InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 165 | |
Sho SHIMIZU | cc137a9 | 2016-03-11 15:10:54 -0800 | [diff] [blame] | 166 | if (subType.equals(L0ModificationInstruction.L0SubType.OCH.name())) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 167 | String gridTypeString = nullIsIllegal(json.get(InstructionCodec.GRID_TYPE), |
| 168 | InstructionCodec.GRID_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText(); |
| 169 | GridType gridType = GridType.valueOf(gridTypeString); |
| 170 | if (gridType == null) { |
| 171 | throw new IllegalArgumentException("Unknown grid type " |
| 172 | + gridTypeString); |
| 173 | } |
| 174 | String channelSpacingString = nullIsIllegal(json.get(InstructionCodec.CHANNEL_SPACING), |
| 175 | InstructionCodec.CHANNEL_SPACING + InstructionCodec.MISSING_MEMBER_MESSAGE).asText(); |
| 176 | ChannelSpacing channelSpacing = ChannelSpacing.valueOf(channelSpacingString); |
| 177 | if (channelSpacing == null) { |
| 178 | throw new IllegalArgumentException("Unknown channel spacing " |
| 179 | + channelSpacingString); |
| 180 | } |
| 181 | int spacingMultiplier = nullIsIllegal(json.get(InstructionCodec.SPACING_MULTIPLIER), |
| 182 | InstructionCodec.SPACING_MULTIPLIER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 183 | int slotGranularity = nullIsIllegal(json.get(InstructionCodec.SLOT_GRANULARITY), |
| 184 | InstructionCodec.SLOT_GRANULARITY + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 185 | return Instructions.modL0Lambda(new OchSignal(gridType, channelSpacing, |
| 186 | spacingMultiplier, slotGranularity)); |
| 187 | } |
| 188 | throw new IllegalArgumentException("L0 Instruction subtype " |
| 189 | + subType + " is not supported"); |
| 190 | } |
| 191 | |
| 192 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 193 | * Decodes a Layer 1 instruction. |
| 194 | * |
| 195 | * @return instruction object decoded from the JSON |
| 196 | * @throws IllegalArgumentException if the JSON is invalid |
| 197 | */ |
| 198 | private Instruction decodeL1() { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 199 | String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), |
| 200 | InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText(); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 201 | if (subType.equals(L1ModificationInstruction.L1SubType.ODU_SIGID.name())) { |
| 202 | int tributaryPortNumber = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_PORT_NUMBER), |
| 203 | InstructionCodec.TRIBUTARY_PORT_NUMBER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 204 | int tributarySlotLen = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_LEN), |
| 205 | InstructionCodec.TRIBUTARY_SLOT_LEN + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); |
| 206 | byte[] tributarySlotBitmap = null; |
| 207 | tributarySlotBitmap = HexString.fromHexString( |
| 208 | nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_BITMAP), |
| 209 | InstructionCodec.TRIBUTARY_SLOT_BITMAP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText()); |
| 210 | return Instructions.modL1OduSignalId(OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, |
| 211 | tributarySlotBitmap)); |
| 212 | } |
| 213 | throw new IllegalArgumentException("L1 Instruction subtype " |
| 214 | + subType + " is not supported"); |
| 215 | } |
| 216 | |
| 217 | /** |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 218 | * Decodes a Layer 4 instruction. |
| 219 | * |
| 220 | * @return instruction object decoded from the JSON |
| 221 | * @throws IllegalArgumentException if the JSON is invalid |
| 222 | */ |
| 223 | private Instruction decodeL4() { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 224 | String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), |
| 225 | InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText(); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 226 | |
| 227 | if (subType.equals(L4ModificationInstruction.L4SubType.TCP_DST.name())) { |
| 228 | TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT), |
| 229 | InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt()); |
| 230 | return Instructions.modTcpDst(tcpPort); |
| 231 | } else if (subType.equals(L4ModificationInstruction.L4SubType.TCP_SRC.name())) { |
| 232 | TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT), |
| 233 | InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt()); |
| 234 | return Instructions.modTcpSrc(tcpPort); |
| 235 | } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_DST.name())) { |
| 236 | TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT), |
| 237 | InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt()); |
| 238 | return Instructions.modUdpDst(udpPort); |
| 239 | } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_SRC.name())) { |
| 240 | TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT), |
| 241 | InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt()); |
| 242 | return Instructions.modUdpSrc(udpPort); |
| 243 | } |
| 244 | throw new IllegalArgumentException("L4 Instruction subtype " |
| 245 | + subType + " is not supported"); |
| 246 | } |
| 247 | |
| 248 | /** |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 249 | * Decodes a extension instruction. |
| 250 | * |
| 251 | * @return extension treatment |
| 252 | */ |
| 253 | private Instruction decodeExtension() { |
| 254 | ObjectNode node = (ObjectNode) json.get(InstructionCodec.EXTENSION); |
| 255 | if (node != null) { |
| 256 | DeviceId deviceId = getDeviceId(); |
| 257 | |
| 258 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 259 | DeviceService deviceService = serviceDirectory.get(DeviceService.class); |
| 260 | Device device = deviceService.getDevice(deviceId); |
| 261 | |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 262 | if (device == null) { |
| 263 | throw new IllegalArgumentException("Device not found"); |
| 264 | } |
| 265 | |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 266 | if (device.is(ExtensionTreatmentCodec.class)) { |
| 267 | ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class); |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 268 | ExtensionTreatment treatment = treatmentCodec.decode(node, context); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 269 | return Instructions.extension(treatment, deviceId); |
| 270 | } else { |
Jonathan Hart | e3bcfc3 | 2016-08-16 17:12:49 -0700 | [diff] [blame] | 271 | throw new IllegalArgumentException( |
| 272 | "There is no codec to decode extension for device " + deviceId.toString()); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | return null; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns device identifier. |
| 280 | * |
| 281 | * @return device identifier |
| 282 | * @throws IllegalArgumentException if the JSON is invalid |
| 283 | */ |
| 284 | private DeviceId getDeviceId() { |
| 285 | JsonNode deviceIdNode = json.get(InstructionCodec.DEVICE_ID); |
| 286 | if (deviceIdNode != null) { |
| 287 | return DeviceId.deviceId(deviceIdNode.asText()); |
| 288 | } |
| 289 | throw new IllegalArgumentException("Empty device identifier"); |
| 290 | } |
| 291 | |
| 292 | /** |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 293 | * Extracts port number of the given json node. |
| 294 | * |
| 295 | * @param jsonNode json node |
| 296 | * @return port number |
| 297 | */ |
| 298 | private PortNumber getPortNumber(ObjectNode jsonNode) { |
| 299 | PortNumber portNumber; |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 300 | JsonNode portNode = nullIsIllegal(jsonNode.get(InstructionCodec.PORT), |
| 301 | InstructionCodec.PORT + InstructionCodec.ERROR_MESSAGE); |
| 302 | if (portNode.isLong() || portNode.isInt()) { |
| 303 | portNumber = PortNumber.portNumber(portNode.asLong()); |
| 304 | } else if (portNode.isTextual()) { |
| 305 | portNumber = PortNumber.fromString(portNode.textValue()); |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 306 | } else { |
| 307 | throw new IllegalArgumentException("Port value " |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 308 | + portNode.toString() |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 309 | + " is not supported"); |
| 310 | } |
| 311 | return portNumber; |
| 312 | } |
| 313 | |
| 314 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 315 | * Decodes the JSON into an instruction object. |
| 316 | * |
| 317 | * @return Criterion object |
| 318 | * @throws IllegalArgumentException if the JSON is invalid |
| 319 | */ |
| 320 | public Instruction decode() { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 321 | String type = nullIsIllegal(json.get(InstructionCodec.TYPE), |
| 322 | InstructionCodec.TYPE + InstructionCodec.ERROR_MESSAGE).asText(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 323 | |
| 324 | if (type.equals(Instruction.Type.OUTPUT.name())) { |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 325 | return Instructions.createOutput(getPortNumber(json)); |
Ray Milkey | 2be39ed | 2016-02-22 15:54:19 -0800 | [diff] [blame] | 326 | } else if (type.equals(Instruction.Type.NOACTION.name())) { |
| 327 | return Instructions.createNoAction(); |
Jian Li | 1ef82db | 2016-03-03 14:43:21 -0800 | [diff] [blame] | 328 | } else if (type.equals(Instruction.Type.TABLE.name())) { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 329 | return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID), |
| 330 | InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt()); |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 331 | } else if (type.equals(Instruction.Type.GROUP.name())) { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 332 | GroupId groupId = new DefaultGroupId(nullIsIllegal(json.get(InstructionCodec.GROUP_ID), |
| 333 | InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt()); |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 334 | return Instructions.createGroup(groupId); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 335 | } else if (type.equals(Instruction.Type.METER.name())) { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 336 | MeterId meterId = MeterId.meterId(nullIsIllegal(json.get(InstructionCodec.METER_ID), |
| 337 | InstructionCodec.METER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong()); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 338 | return Instructions.meterTraffic(meterId); |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 339 | } else if (type.equals(Instruction.Type.QUEUE.name())) { |
Jayasree Ghosh | 8aca677 | 2016-10-04 03:32:11 +0530 | [diff] [blame] | 340 | long queueId = nullIsIllegal(json.get(InstructionCodec.QUEUE_ID), |
| 341 | InstructionCodec.QUEUE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong(); |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 342 | return Instructions.setQueue(queueId, getPortNumber(json)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 343 | } else if (type.equals(Instruction.Type.L0MODIFICATION.name())) { |
| 344 | return decodeL0(); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 345 | } else if (type.equals(Instruction.Type.L1MODIFICATION.name())) { |
| 346 | return decodeL1(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 347 | } else if (type.equals(Instruction.Type.L2MODIFICATION.name())) { |
| 348 | return decodeL2(); |
| 349 | } else if (type.equals(Instruction.Type.L3MODIFICATION.name())) { |
| 350 | return decodeL3(); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 351 | } else if (type.equals(Instruction.Type.L4MODIFICATION.name())) { |
| 352 | return decodeL4(); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 353 | } else if (type.equals(Instruction.Type.EXTENSION.name())) { |
| 354 | return decodeExtension(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 355 | } |
| 356 | throw new IllegalArgumentException("Instruction type " |
| 357 | + type + " is not supported"); |
| 358 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 359 | } |