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 | |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ip6Address; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.MplsLabel; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 24 | import org.onlab.packet.TpPort; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 26 | import org.onlab.util.HexString; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 27 | import org.onosproject.net.ChannelSpacing; |
| 28 | import org.onosproject.net.GridType; |
| 29 | import org.onosproject.net.Lambda; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 30 | import org.onosproject.net.OchSignalType; |
| 31 | import org.onosproject.net.OduSignalId; |
| 32 | import org.onosproject.net.OduSignalType; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.flow.criteria.Criteria; |
| 35 | import org.onosproject.net.flow.criteria.Criterion; |
| 36 | |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 37 | import java.util.HashMap; |
| 38 | import java.util.Map; |
| 39 | |
| 40 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 41 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Decode portion of the criterion codec. |
| 44 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 45 | public final class DecodeCriterionCodecHelper { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 46 | |
| 47 | private final ObjectNode json; |
| 48 | |
| 49 | protected static final String MISSING_MEMBER_MESSAGE = |
| 50 | " member is required in Criterion"; |
| 51 | |
| 52 | private interface CriterionDecoder { |
| 53 | Criterion decodeCriterion(ObjectNode json); |
| 54 | } |
| 55 | private final Map<String, CriterionDecoder> decoderMap; |
| 56 | |
| 57 | /** |
| 58 | * Creates a decode criterion codec object. |
| 59 | * Initializes the lookup map for criterion subclass decoders. |
| 60 | * |
| 61 | * @param json JSON object to decode |
| 62 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 63 | public DecodeCriterionCodecHelper(ObjectNode json) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 64 | this.json = json; |
| 65 | decoderMap = new HashMap<>(); |
| 66 | |
| 67 | decoderMap.put(Criterion.Type.IN_PORT.name(), new InPortDecoder()); |
| 68 | decoderMap.put(Criterion.Type.IN_PHY_PORT.name(), new InPhyPortDecoder()); |
| 69 | decoderMap.put(Criterion.Type.METADATA.name(), new MetadataDecoder()); |
| 70 | decoderMap.put(Criterion.Type.ETH_DST.name(), new EthDstDecoder()); |
| 71 | decoderMap.put(Criterion.Type.ETH_SRC.name(), new EthSrcDecoder()); |
| 72 | decoderMap.put(Criterion.Type.ETH_TYPE.name(), new EthTypeDecoder()); |
| 73 | decoderMap.put(Criterion.Type.VLAN_VID.name(), new VlanVidDecoder()); |
| 74 | decoderMap.put(Criterion.Type.VLAN_PCP.name(), new VlanPcpDecoder()); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 75 | decoderMap.put(Criterion.Type.INNER_VLAN_VID.name(), new InnerVlanVidDecoder()); |
| 76 | decoderMap.put(Criterion.Type.INNER_VLAN_PCP.name(), new InnerVlanPcpDecoder()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 77 | decoderMap.put(Criterion.Type.IP_DSCP.name(), new IpDscpDecoder()); |
| 78 | decoderMap.put(Criterion.Type.IP_ECN.name(), new IpEcnDecoder()); |
| 79 | decoderMap.put(Criterion.Type.IP_PROTO.name(), new IpProtoDecoder()); |
| 80 | decoderMap.put(Criterion.Type.IPV4_SRC.name(), new IpV4SrcDecoder()); |
| 81 | decoderMap.put(Criterion.Type.IPV4_DST.name(), new IpV4DstDecoder()); |
| 82 | decoderMap.put(Criterion.Type.TCP_SRC.name(), new TcpSrcDecoder()); |
| 83 | decoderMap.put(Criterion.Type.TCP_DST.name(), new TcpDstDecoder()); |
| 84 | decoderMap.put(Criterion.Type.UDP_SRC.name(), new UdpSrcDecoder()); |
| 85 | decoderMap.put(Criterion.Type.UDP_DST.name(), new UdpDstDecoder()); |
| 86 | decoderMap.put(Criterion.Type.SCTP_SRC.name(), new SctpSrcDecoder()); |
| 87 | decoderMap.put(Criterion.Type.SCTP_DST.name(), new SctpDstDecoder()); |
| 88 | decoderMap.put(Criterion.Type.ICMPV4_TYPE.name(), new IcmpV4TypeDecoder()); |
| 89 | decoderMap.put(Criterion.Type.ICMPV4_CODE.name(), new IcmpV4CodeDecoder()); |
| 90 | decoderMap.put(Criterion.Type.IPV6_SRC.name(), new IpV6SrcDecoder()); |
| 91 | decoderMap.put(Criterion.Type.IPV6_DST.name(), new IpV6DstDecoder()); |
| 92 | decoderMap.put(Criterion.Type.IPV6_FLABEL.name(), new IpV6FLabelDecoder()); |
| 93 | decoderMap.put(Criterion.Type.ICMPV6_TYPE.name(), new IcmpV6TypeDecoder()); |
| 94 | decoderMap.put(Criterion.Type.ICMPV6_CODE.name(), new IcmpV6CodeDecoder()); |
| 95 | decoderMap.put(Criterion.Type.IPV6_ND_TARGET.name(), new V6NDTargetDecoder()); |
| 96 | decoderMap.put(Criterion.Type.IPV6_ND_SLL.name(), new V6NDSllDecoder()); |
| 97 | decoderMap.put(Criterion.Type.IPV6_ND_TLL.name(), new V6NDTllDecoder()); |
| 98 | decoderMap.put(Criterion.Type.MPLS_LABEL.name(), new MplsLabelDecoder()); |
| 99 | decoderMap.put(Criterion.Type.IPV6_EXTHDR.name(), new IpV6ExthdrDecoder()); |
| 100 | decoderMap.put(Criterion.Type.OCH_SIGID.name(), new OchSigIdDecoder()); |
| 101 | decoderMap.put(Criterion.Type.OCH_SIGTYPE.name(), new OchSigTypeDecoder()); |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 102 | decoderMap.put(Criterion.Type.TUNNEL_ID.name(), new TunnelIdDecoder()); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 103 | decoderMap.put(Criterion.Type.ODU_SIGID.name(), new OduSigIdDecoder()); |
| 104 | decoderMap.put(Criterion.Type.ODU_SIGTYPE.name(), new OduSigTypeDecoder()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private class EthTypeDecoder implements CriterionDecoder { |
| 108 | @Override |
| 109 | public Criterion decodeCriterion(ObjectNode json) { |
Ray Milkey | fe447c5 | 2016-02-24 14:17:54 -0800 | [diff] [blame] | 110 | JsonNode ethTypeNode = nullIsIllegal(json.get(CriterionCodec.ETH_TYPE), |
| 111 | CriterionCodec.ETH_TYPE + MISSING_MEMBER_MESSAGE); |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 112 | int ethType; |
Ray Milkey | fe447c5 | 2016-02-24 14:17:54 -0800 | [diff] [blame] | 113 | if (ethTypeNode.isInt()) { |
| 114 | ethType = ethTypeNode.asInt(); |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 115 | } else { |
Ray Milkey | fe447c5 | 2016-02-24 14:17:54 -0800 | [diff] [blame] | 116 | ethType = Integer.decode(ethTypeNode.textValue()); |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 117 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 118 | return Criteria.matchEthType(ethType); |
| 119 | } |
| 120 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 121 | private class EthDstDecoder implements CriterionDecoder { |
| 122 | @Override |
| 123 | public Criterion decodeCriterion(ObjectNode json) { |
| 124 | MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC), |
| 125 | CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText()); |
| 126 | |
| 127 | return Criteria.matchEthDst(mac); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private class EthSrcDecoder implements CriterionDecoder { |
| 132 | @Override |
| 133 | public Criterion decodeCriterion(ObjectNode json) { |
| 134 | MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC), |
| 135 | CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText()); |
| 136 | |
| 137 | return Criteria.matchEthSrc(mac); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private class InPortDecoder implements CriterionDecoder { |
| 142 | @Override |
| 143 | public Criterion decodeCriterion(ObjectNode json) { |
| 144 | PortNumber port = PortNumber.portNumber(nullIsIllegal(json.get(CriterionCodec.PORT), |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 145 | CriterionCodec.PORT + |
| 146 | MISSING_MEMBER_MESSAGE).asLong()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 147 | |
| 148 | return Criteria.matchInPort(port); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private class InPhyPortDecoder implements CriterionDecoder { |
| 153 | @Override |
| 154 | public Criterion decodeCriterion(ObjectNode json) { |
| 155 | PortNumber port = PortNumber.portNumber(nullIsIllegal(json.get(CriterionCodec.PORT), |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 156 | CriterionCodec.PORT + |
| 157 | MISSING_MEMBER_MESSAGE).asLong()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 158 | |
| 159 | return Criteria.matchInPhyPort(port); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private class MetadataDecoder implements CriterionDecoder { |
| 164 | @Override |
| 165 | public Criterion decodeCriterion(ObjectNode json) { |
| 166 | long metadata = nullIsIllegal(json.get(CriterionCodec.METADATA), |
| 167 | CriterionCodec.METADATA + MISSING_MEMBER_MESSAGE).asLong(); |
| 168 | |
| 169 | return Criteria.matchMetadata(metadata); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private class VlanVidDecoder implements CriterionDecoder { |
| 174 | @Override |
| 175 | public Criterion decodeCriterion(ObjectNode json) { |
| 176 | short vlanId = (short) nullIsIllegal(json.get(CriterionCodec.VLAN_ID), |
| 177 | CriterionCodec.VLAN_ID + MISSING_MEMBER_MESSAGE).asInt(); |
| 178 | |
| 179 | return Criteria.matchVlanId(VlanId.vlanId(vlanId)); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private class VlanPcpDecoder implements CriterionDecoder { |
| 184 | @Override |
| 185 | public Criterion decodeCriterion(ObjectNode json) { |
| 186 | byte priority = (byte) nullIsIllegal(json.get(CriterionCodec.PRIORITY), |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 187 | CriterionCodec.PRIORITY + MISSING_MEMBER_MESSAGE).asInt(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 188 | |
| 189 | return Criteria.matchVlanPcp(priority); |
| 190 | } |
| 191 | } |
| 192 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 193 | private class InnerVlanVidDecoder implements CriterionDecoder { |
| 194 | @Override |
| 195 | public Criterion decodeCriterion(ObjectNode json) { |
| 196 | short vlanId = (short) nullIsIllegal(json.get(CriterionCodec.INNER_VLAN_ID), |
| 197 | CriterionCodec.INNER_VLAN_ID + |
| 198 | MISSING_MEMBER_MESSAGE).asInt(); |
| 199 | |
| 200 | return Criteria.matchInnerVlanId(VlanId.vlanId(vlanId)); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private class InnerVlanPcpDecoder implements CriterionDecoder { |
| 205 | @Override |
| 206 | public Criterion decodeCriterion(ObjectNode json) { |
| 207 | byte priority = (byte) nullIsIllegal(json.get(CriterionCodec.INNER_PRIORITY), |
| 208 | CriterionCodec.INNER_PRIORITY + |
| 209 | MISSING_MEMBER_MESSAGE).asInt(); |
| 210 | |
| 211 | return Criteria.matchInnerVlanPcp(priority); |
| 212 | } |
| 213 | } |
| 214 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 215 | private class IpDscpDecoder implements CriterionDecoder { |
| 216 | @Override |
| 217 | public Criterion decodeCriterion(ObjectNode json) { |
| 218 | byte ipDscp = (byte) nullIsIllegal(json.get(CriterionCodec.IP_DSCP), |
| 219 | CriterionCodec.IP_DSCP + MISSING_MEMBER_MESSAGE).asInt(); |
| 220 | return Criteria.matchIPDscp(ipDscp); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private class IpEcnDecoder implements CriterionDecoder { |
| 225 | @Override |
| 226 | public Criterion decodeCriterion(ObjectNode json) { |
| 227 | byte ipEcn = (byte) nullIsIllegal(json.get(CriterionCodec.IP_ECN), |
| 228 | CriterionCodec.IP_ECN + MISSING_MEMBER_MESSAGE).asInt(); |
| 229 | return Criteria.matchIPEcn(ipEcn); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private class IpProtoDecoder implements CriterionDecoder { |
| 234 | @Override |
| 235 | public Criterion decodeCriterion(ObjectNode json) { |
| 236 | short proto = (short) nullIsIllegal(json.get(CriterionCodec.PROTOCOL), |
| 237 | CriterionCodec.PROTOCOL + MISSING_MEMBER_MESSAGE).asInt(); |
| 238 | return Criteria.matchIPProtocol(proto); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private class IpV4SrcDecoder implements CriterionDecoder { |
| 243 | @Override |
| 244 | public Criterion decodeCriterion(ObjectNode json) { |
| 245 | String ip = nullIsIllegal(json.get(CriterionCodec.IP), |
| 246 | CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText(); |
| 247 | return Criteria.matchIPSrc(IpPrefix.valueOf(ip)); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private class IpV4DstDecoder implements CriterionDecoder { |
| 252 | @Override |
| 253 | public Criterion decodeCriterion(ObjectNode json) { |
| 254 | String ip = nullIsIllegal(json.get(CriterionCodec.IP), |
| 255 | CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText(); |
| 256 | return Criteria.matchIPDst(IpPrefix.valueOf(ip)); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private class IpV6SrcDecoder implements CriterionDecoder { |
| 261 | @Override |
| 262 | public Criterion decodeCriterion(ObjectNode json) { |
| 263 | String ip = nullIsIllegal(json.get(CriterionCodec.IP), |
| 264 | CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText(); |
| 265 | return Criteria.matchIPv6Src(IpPrefix.valueOf(ip)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private class IpV6DstDecoder implements CriterionDecoder { |
| 270 | @Override |
| 271 | public Criterion decodeCriterion(ObjectNode json) { |
| 272 | String ip = nullIsIllegal(json.get(CriterionCodec.IP), |
| 273 | CriterionCodec.IP + MISSING_MEMBER_MESSAGE).asText(); |
| 274 | return Criteria.matchIPv6Dst(IpPrefix.valueOf(ip)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | private class TcpSrcDecoder implements CriterionDecoder { |
| 279 | @Override |
| 280 | public Criterion decodeCriterion(ObjectNode json) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 281 | TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT), |
| 282 | CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 283 | return Criteria.matchTcpSrc(tcpPort); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | private class TcpDstDecoder implements CriterionDecoder { |
| 288 | @Override |
| 289 | public Criterion decodeCriterion(ObjectNode json) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 290 | TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.TCP_PORT), |
| 291 | CriterionCodec.TCP_PORT + MISSING_MEMBER_MESSAGE).asInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 292 | return Criteria.matchTcpDst(tcpPort); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | private class UdpSrcDecoder implements CriterionDecoder { |
| 297 | @Override |
| 298 | public Criterion decodeCriterion(ObjectNode json) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 299 | TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT), |
| 300 | CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 301 | return Criteria.matchUdpSrc(udpPort); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private class UdpDstDecoder implements CriterionDecoder { |
| 306 | @Override |
| 307 | public Criterion decodeCriterion(ObjectNode json) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 308 | TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.UDP_PORT), |
| 309 | CriterionCodec.UDP_PORT + MISSING_MEMBER_MESSAGE).asInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 310 | return Criteria.matchUdpDst(udpPort); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | private class SctpSrcDecoder implements CriterionDecoder { |
| 315 | @Override |
| 316 | public Criterion decodeCriterion(ObjectNode json) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 317 | TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT), |
| 318 | CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 319 | return Criteria.matchSctpSrc(sctpPort); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | private class SctpDstDecoder implements CriterionDecoder { |
| 324 | @Override |
| 325 | public Criterion decodeCriterion(ObjectNode json) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 326 | TpPort sctpPort = TpPort.tpPort(nullIsIllegal(json.get(CriterionCodec.SCTP_PORT), |
| 327 | CriterionCodec.SCTP_PORT + MISSING_MEMBER_MESSAGE).asInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 328 | return Criteria.matchSctpDst(sctpPort); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | private class IcmpV4TypeDecoder implements CriterionDecoder { |
| 333 | @Override |
| 334 | public Criterion decodeCriterion(ObjectNode json) { |
| 335 | short type = (short) nullIsIllegal(json.get(CriterionCodec.ICMP_TYPE), |
| 336 | CriterionCodec.ICMP_TYPE + MISSING_MEMBER_MESSAGE).asInt(); |
| 337 | return Criteria.matchIcmpType(type); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | private class IcmpV4CodeDecoder implements CriterionDecoder { |
| 342 | @Override |
| 343 | public Criterion decodeCriterion(ObjectNode json) { |
| 344 | short code = (short) nullIsIllegal(json.get(CriterionCodec.ICMP_CODE), |
| 345 | CriterionCodec.ICMP_CODE + MISSING_MEMBER_MESSAGE).asInt(); |
| 346 | return Criteria.matchIcmpCode(code); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private class IpV6FLabelDecoder implements CriterionDecoder { |
| 351 | @Override |
| 352 | public Criterion decodeCriterion(ObjectNode json) { |
| 353 | int flowLabel = nullIsIllegal(json.get(CriterionCodec.FLOW_LABEL), |
| 354 | CriterionCodec.FLOW_LABEL + MISSING_MEMBER_MESSAGE).asInt(); |
| 355 | return Criteria.matchIPv6FlowLabel(flowLabel); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | private class IcmpV6TypeDecoder implements CriterionDecoder { |
| 360 | @Override |
| 361 | public Criterion decodeCriterion(ObjectNode json) { |
| 362 | short type = (short) nullIsIllegal(json.get(CriterionCodec.ICMPV6_TYPE), |
| 363 | CriterionCodec.ICMPV6_TYPE + MISSING_MEMBER_MESSAGE).asInt(); |
| 364 | return Criteria.matchIcmpv6Type(type); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | private class IcmpV6CodeDecoder implements CriterionDecoder { |
| 369 | @Override |
| 370 | public Criterion decodeCriterion(ObjectNode json) { |
| 371 | short code = (short) nullIsIllegal(json.get(CriterionCodec.ICMPV6_CODE), |
| 372 | CriterionCodec.ICMPV6_CODE + MISSING_MEMBER_MESSAGE).asInt(); |
| 373 | return Criteria.matchIcmpv6Code(code); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | private class V6NDTargetDecoder implements CriterionDecoder { |
| 378 | @Override |
| 379 | public Criterion decodeCriterion(ObjectNode json) { |
| 380 | Ip6Address target = Ip6Address.valueOf(nullIsIllegal(json.get(CriterionCodec.TARGET_ADDRESS), |
| 381 | CriterionCodec.TARGET_ADDRESS + MISSING_MEMBER_MESSAGE).asText()); |
| 382 | return Criteria.matchIPv6NDTargetAddress(target); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | private class V6NDSllDecoder implements CriterionDecoder { |
| 387 | @Override |
| 388 | public Criterion decodeCriterion(ObjectNode json) { |
| 389 | MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC), |
| 390 | CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText()); |
| 391 | return Criteria.matchIPv6NDSourceLinkLayerAddress(mac); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | private class V6NDTllDecoder implements CriterionDecoder { |
| 396 | @Override |
| 397 | public Criterion decodeCriterion(ObjectNode json) { |
| 398 | MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(CriterionCodec.MAC), |
| 399 | CriterionCodec.MAC + MISSING_MEMBER_MESSAGE).asText()); |
| 400 | return Criteria.matchIPv6NDTargetLinkLayerAddress(mac); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | private class MplsLabelDecoder implements CriterionDecoder { |
| 405 | @Override |
| 406 | public Criterion decodeCriterion(ObjectNode json) { |
| 407 | int label = nullIsIllegal(json.get(CriterionCodec.LABEL), |
| 408 | CriterionCodec.LABEL + MISSING_MEMBER_MESSAGE).asInt(); |
| 409 | return Criteria.matchMplsLabel(MplsLabel.mplsLabel(label)); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | private class IpV6ExthdrDecoder implements CriterionDecoder { |
| 414 | @Override |
| 415 | public Criterion decodeCriterion(ObjectNode json) { |
| 416 | int exthdrFlags = nullIsIllegal(json.get(CriterionCodec.EXT_HDR_FLAGS), |
| 417 | CriterionCodec.EXT_HDR_FLAGS + MISSING_MEMBER_MESSAGE).asInt(); |
| 418 | return Criteria.matchIPv6ExthdrFlags(exthdrFlags); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | private class OchSigIdDecoder implements CriterionDecoder { |
| 423 | @Override |
| 424 | public Criterion decodeCriterion(ObjectNode json) { |
Sho SHIMIZU | cc137a9 | 2016-03-11 15:10:54 -0800 | [diff] [blame] | 425 | JsonNode ochSignalId = nullIsIllegal(json.get(CriterionCodec.OCH_SIGNAL_ID), |
| 426 | CriterionCodec.GRID_TYPE + MISSING_MEMBER_MESSAGE); |
| 427 | GridType gridType = |
| 428 | GridType.valueOf( |
| 429 | nullIsIllegal(ochSignalId.get(CriterionCodec.GRID_TYPE), |
| 430 | CriterionCodec.GRID_TYPE + MISSING_MEMBER_MESSAGE).asText()); |
| 431 | ChannelSpacing channelSpacing = |
| 432 | ChannelSpacing.valueOf( |
| 433 | nullIsIllegal(ochSignalId.get(CriterionCodec.CHANNEL_SPACING), |
| 434 | CriterionCodec.CHANNEL_SPACING + MISSING_MEMBER_MESSAGE).asText()); |
| 435 | int spacingMultiplier = nullIsIllegal(ochSignalId.get(CriterionCodec.SPACING_MULIPLIER), |
| 436 | CriterionCodec.SPACING_MULIPLIER + MISSING_MEMBER_MESSAGE).asInt(); |
| 437 | int slotGranularity = nullIsIllegal(ochSignalId.get(CriterionCodec.SLOT_GRANULARITY), |
| 438 | CriterionCodec.SLOT_GRANULARITY + MISSING_MEMBER_MESSAGE).asInt(); |
| 439 | return Criteria.matchLambda( |
| 440 | Lambda.ochSignal(gridType, channelSpacing, |
| 441 | spacingMultiplier, slotGranularity)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | private class OchSigTypeDecoder implements CriterionDecoder { |
| 446 | @Override |
| 447 | public Criterion decodeCriterion(ObjectNode json) { |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 448 | OchSignalType ochSignalType = OchSignalType.valueOf(nullIsIllegal(json.get(CriterionCodec.OCH_SIGNAL_TYPE), |
| 449 | CriterionCodec.OCH_SIGNAL_TYPE + MISSING_MEMBER_MESSAGE).asText()); |
| 450 | return Criteria.matchOchSignalType(ochSignalType); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 454 | private class TunnelIdDecoder implements CriterionDecoder { |
| 455 | @Override |
| 456 | public Criterion decodeCriterion(ObjectNode json) { |
| 457 | long tunnelId = nullIsIllegal(json.get(CriterionCodec.TUNNEL_ID), |
| 458 | CriterionCodec.TUNNEL_ID + MISSING_MEMBER_MESSAGE).asLong(); |
| 459 | return Criteria.matchTunnelId(tunnelId); |
| 460 | } |
| 461 | } |
| 462 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 463 | private class OduSigIdDecoder implements CriterionDecoder { |
| 464 | @Override |
| 465 | public Criterion decodeCriterion(ObjectNode json) { |
| 466 | JsonNode oduSignalId = nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_ID), |
| 467 | CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE); |
| 468 | |
| 469 | int tributaryPortNumber = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_PORT_NUMBER), |
| 470 | CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE).asInt(); |
| 471 | int tributarySlotLen = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_LEN), |
| 472 | CriterionCodec.TRIBUTARY_SLOT_LEN + MISSING_MEMBER_MESSAGE).asInt(); |
| 473 | byte[] tributarySlotBitmap = HexString.fromHexString( |
| 474 | nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP), |
| 475 | CriterionCodec.TRIBUTARY_SLOT_BITMAP + MISSING_MEMBER_MESSAGE).asText()); |
| 476 | |
| 477 | return Criteria.matchOduSignalId( |
| 478 | OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap)); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | private class OduSigTypeDecoder implements CriterionDecoder { |
| 483 | @Override |
| 484 | public Criterion decodeCriterion(ObjectNode json) { |
| 485 | OduSignalType oduSignalType = OduSignalType.valueOf(nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_TYPE), |
| 486 | CriterionCodec.ODU_SIGNAL_TYPE + MISSING_MEMBER_MESSAGE).asText()); |
| 487 | return Criteria.matchOduSignalType(oduSignalType); |
| 488 | } |
| 489 | } |
| 490 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Decodes the JSON into a criterion object. |
| 493 | * |
| 494 | * @return Criterion object |
| 495 | * @throws IllegalArgumentException if the JSON is invalid |
| 496 | */ |
| 497 | public Criterion decode() { |
Ray Milkey | 5207506 | 2016-02-03 17:36:48 -0800 | [diff] [blame] | 498 | String type = |
| 499 | nullIsIllegal(json.get(CriterionCodec.TYPE), "Type not specified") |
| 500 | .asText(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 501 | |
| 502 | CriterionDecoder decoder = decoderMap.get(type); |
| 503 | if (decoder != null) { |
| 504 | return decoder.decodeCriterion(json); |
| 505 | } |
| 506 | |
| 507 | throw new IllegalArgumentException("Type " + type + " is unknown"); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | } |