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