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