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