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 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 20 | import org.onlab.util.HexString; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.net.OchSignal; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 23 | import org.onosproject.net.OduSignalId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.criteria.Criterion; |
| 25 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 26 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 27 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 28 | import org.onosproject.net.flow.criteria.IPDscpCriterion; |
| 29 | import org.onosproject.net.flow.criteria.IPEcnCriterion; |
| 30 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 31 | import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; |
| 32 | import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; |
| 33 | import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; |
| 34 | import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; |
| 35 | import org.onosproject.net.flow.criteria.IcmpCodeCriterion; |
| 36 | import org.onosproject.net.flow.criteria.IcmpTypeCriterion; |
| 37 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 38 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
| 39 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
Jonathan Hart | cc962d8 | 2016-08-09 16:52:22 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 42 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 43 | import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.OduSignalIdCriterion; |
| 45 | import org.onosproject.net.flow.criteria.OduSignalTypeCriterion; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.criteria.PiCriterion; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 48 | import org.onosproject.net.flow.criteria.SctpPortCriterion; |
| 49 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 52 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 53 | import org.onosproject.net.flow.criteria.VlanPcpCriterion; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 54 | import org.onosproject.net.pi.model.PiMatchType; |
| 55 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 56 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
| 57 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
| 58 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 59 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 60 | |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 61 | import java.util.EnumMap; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 62 | |
| 63 | import static com.google.common.base.Preconditions.checkNotNull; |
| 64 | |
| 65 | /** |
| 66 | * Encode portion of the criterion codec. |
| 67 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 68 | public final class EncodeCriterionCodecHelper { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 69 | |
| 70 | private final Criterion criterion; |
| 71 | private final CodecContext context; |
| 72 | |
| 73 | private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap; |
| 74 | |
| 75 | /** |
| 76 | * Creates an encoder object for a criterion. |
| 77 | * Initializes the formatter lookup map for the criterion subclasses. |
| 78 | * |
| 79 | * @param criterion Criterion to encode |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 80 | * @param context context of the JSON encoding |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 81 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 82 | public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 83 | this.criterion = criterion; |
| 84 | this.context = context; |
| 85 | |
| 86 | formatMap = new EnumMap<>(Criterion.Type.class); |
| 87 | |
| 88 | formatMap.put(Criterion.Type.IN_PORT, new FormatInPort()); |
| 89 | formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort()); |
| 90 | formatMap.put(Criterion.Type.METADATA, new FormatMetadata()); |
| 91 | formatMap.put(Criterion.Type.ETH_DST, new FormatEth()); |
| 92 | formatMap.put(Criterion.Type.ETH_SRC, new FormatEth()); |
| 93 | formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType()); |
| 94 | formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid()); |
| 95 | formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp()); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 96 | formatMap.put(Criterion.Type.INNER_VLAN_VID, new FormatInnerVlanVid()); |
| 97 | formatMap.put(Criterion.Type.INNER_VLAN_PCP, new FormatInnerVlanPcp()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 98 | formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp()); |
| 99 | formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn()); |
| 100 | formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto()); |
| 101 | formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp()); |
| 102 | formatMap.put(Criterion.Type.IPV4_DST, new FormatIp()); |
| 103 | formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp()); |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 104 | formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatTcpMask()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 105 | formatMap.put(Criterion.Type.TCP_DST, new FormatTcp()); |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 106 | formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatTcpMask()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 107 | formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp()); |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 108 | formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUdpMask()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 109 | formatMap.put(Criterion.Type.UDP_DST, new FormatUdp()); |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 110 | formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUdpMask()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 111 | formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp()); |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 112 | formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatSctpMask()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 113 | formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp()); |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 114 | formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatSctpMask()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 115 | formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type()); |
| 116 | formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code()); |
| 117 | formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp()); |
| 118 | formatMap.put(Criterion.Type.IPV6_DST, new FormatIp()); |
| 119 | formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel()); |
| 120 | formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type()); |
| 121 | formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code()); |
| 122 | formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget()); |
| 123 | formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll()); |
| 124 | formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll()); |
| 125 | formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel()); |
Jonathan Hart | cc962d8 | 2016-08-09 16:52:22 -0700 | [diff] [blame] | 126 | formatMap.put(Criterion.Type.MPLS_BOS, new FormatMplsBos()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 127 | formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr()); |
| 128 | formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId()); |
| 129 | formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType()); |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 130 | formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 131 | formatMap.put(Criterion.Type.DUMMY, new FormatDummyType()); |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 132 | formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId()); |
| 133 | formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType()); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 134 | formatMap.put(Criterion.Type.PROTOCOL_INDEPENDENT, new FormatProtocolIndependent()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 135 | // Currently unimplemented |
| 136 | formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown()); |
| 137 | formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown()); |
| 138 | formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown()); |
| 139 | formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown()); |
| 140 | formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown()); |
| 141 | formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 142 | formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 143 | formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown()); |
| 144 | formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown()); |
| 145 | formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown()); |
| 146 | formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown()); |
| 147 | formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown()); |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame] | 148 | formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown()); |
Saurav Das | 9d6c86b | 2016-02-19 09:01:07 -0800 | [diff] [blame] | 149 | formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatUnknown()); |
| 150 | formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown()); |
Andreas Gilbert | 75b882f7 | 2017-02-03 09:58:07 +0100 | [diff] [blame] | 151 | formatMap.put(Criterion.Type.TCP_SRC_MASKED, new FormatUnknown()); |
| 152 | formatMap.put(Criterion.Type.TCP_DST_MASKED, new FormatUnknown()); |
| 153 | formatMap.put(Criterion.Type.UDP_SRC_MASKED, new FormatUnknown()); |
| 154 | formatMap.put(Criterion.Type.UDP_DST_MASKED, new FormatUnknown()); |
| 155 | formatMap.put(Criterion.Type.SCTP_SRC_MASKED, new FormatUnknown()); |
| 156 | formatMap.put(Criterion.Type.SCTP_DST_MASKED, new FormatUnknown()); |
Saurav Das | 9d6c86b | 2016-02-19 09:01:07 -0800 | [diff] [blame] | 157 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private interface CriterionTypeFormatter { |
| 161 | ObjectNode encodeCriterion(ObjectNode root, Criterion criterion); |
| 162 | } |
| 163 | |
| 164 | private static class FormatUnknown implements CriterionTypeFormatter { |
| 165 | @Override |
| 166 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 167 | return root; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private static class FormatInPort implements CriterionTypeFormatter { |
| 172 | @Override |
| 173 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 174 | final PortCriterion portCriterion = (PortCriterion) criterion; |
| 175 | return root.put(CriterionCodec.PORT, portCriterion.port().toLong()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private static class FormatMetadata implements CriterionTypeFormatter { |
| 180 | @Override |
| 181 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 182 | final MetadataCriterion metadataCriterion = |
| 183 | (MetadataCriterion) criterion; |
| 184 | return root.put(CriterionCodec.METADATA, metadataCriterion.metadata()); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private static class FormatEth implements CriterionTypeFormatter { |
| 189 | @Override |
| 190 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 191 | final EthCriterion ethCriterion = (EthCriterion) criterion; |
| 192 | return root.put(CriterionCodec.MAC, ethCriterion.mac().toString()); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private static class FormatEthType implements CriterionTypeFormatter { |
| 197 | @Override |
| 198 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 199 | final EthTypeCriterion ethTypeCriterion = |
| 200 | (EthTypeCriterion) criterion; |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 201 | return root.put(CriterionCodec.ETH_TYPE, "0x" |
| 202 | + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | private static class FormatVlanVid implements CriterionTypeFormatter { |
| 207 | @Override |
| 208 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 209 | final VlanIdCriterion vlanIdCriterion = |
| 210 | (VlanIdCriterion) criterion; |
| 211 | return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort()); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private static class FormatVlanPcp implements CriterionTypeFormatter { |
| 216 | @Override |
| 217 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 218 | final VlanPcpCriterion vlanPcpCriterion = |
| 219 | (VlanPcpCriterion) criterion; |
| 220 | return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority()); |
| 221 | } |
| 222 | } |
| 223 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 224 | private static class FormatInnerVlanVid implements CriterionTypeFormatter { |
| 225 | @Override |
| 226 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 227 | final VlanIdCriterion vlanIdCriterion = |
| 228 | (VlanIdCriterion) criterion; |
| 229 | return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private static class FormatInnerVlanPcp implements CriterionTypeFormatter { |
| 234 | @Override |
| 235 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 236 | final VlanPcpCriterion vlanPcpCriterion = |
| 237 | (VlanPcpCriterion) criterion; |
| 238 | return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority()); |
| 239 | } |
| 240 | } |
| 241 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 242 | private static class FormatIpDscp implements CriterionTypeFormatter { |
| 243 | @Override |
| 244 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 245 | final IPDscpCriterion ipDscpCriterion = |
| 246 | (IPDscpCriterion) criterion; |
| 247 | return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp()); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private static class FormatIpEcn implements CriterionTypeFormatter { |
| 252 | @Override |
| 253 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 254 | final IPEcnCriterion ipEcnCriterion = |
| 255 | (IPEcnCriterion) criterion; |
| 256 | return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn()); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private static class FormatIpProto implements CriterionTypeFormatter { |
| 261 | @Override |
| 262 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 263 | final IPProtocolCriterion iPProtocolCriterion = |
| 264 | (IPProtocolCriterion) criterion; |
| 265 | return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol()); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private static class FormatIp implements CriterionTypeFormatter { |
| 270 | @Override |
| 271 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 272 | final IPCriterion iPCriterion = (IPCriterion) criterion; |
| 273 | return root.put(CriterionCodec.IP, iPCriterion.ip().toString()); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private static class FormatTcp implements CriterionTypeFormatter { |
| 278 | @Override |
| 279 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 280 | final TcpPortCriterion tcpPortCriterion = |
| 281 | (TcpPortCriterion) criterion; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 282 | return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 286 | private static class FormatTcpMask implements CriterionTypeFormatter { |
| 287 | @Override |
| 288 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 289 | final TcpPortCriterion tcpPortCriterion = |
| 290 | (TcpPortCriterion) criterion; |
| 291 | |
| 292 | root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt()); |
| 293 | root.put(CriterionCodec.TCP_MASK, tcpPortCriterion.mask().toInt()); |
| 294 | |
| 295 | return root; |
| 296 | } |
| 297 | } |
| 298 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 299 | private static class FormatUdp implements CriterionTypeFormatter { |
| 300 | @Override |
| 301 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 302 | final UdpPortCriterion udpPortCriterion = |
| 303 | (UdpPortCriterion) criterion; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 304 | return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 308 | private static class FormatUdpMask implements CriterionTypeFormatter { |
| 309 | @Override |
| 310 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 311 | final UdpPortCriterion udpPortCriterion = |
| 312 | (UdpPortCriterion) criterion; |
| 313 | |
| 314 | root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt()); |
| 315 | root.put(CriterionCodec.UDP_MASK, udpPortCriterion.mask().toInt()); |
| 316 | |
| 317 | return root; |
| 318 | } |
| 319 | } |
| 320 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 321 | private static class FormatSctp implements CriterionTypeFormatter { |
| 322 | @Override |
| 323 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 324 | final SctpPortCriterion sctpPortCriterion = |
| 325 | (SctpPortCriterion) criterion; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 326 | return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Andreas Gilbert | 5e95961 | 2017-12-20 10:30:18 +0100 | [diff] [blame] | 330 | private static class FormatSctpMask implements CriterionTypeFormatter { |
| 331 | @Override |
| 332 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 333 | final SctpPortCriterion sctpPortCriterion = |
| 334 | (SctpPortCriterion) criterion; |
| 335 | |
| 336 | root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt()); |
| 337 | root.put(CriterionCodec.SCTP_MASK, sctpPortCriterion.mask().toInt()); |
| 338 | |
| 339 | return root; |
| 340 | } |
| 341 | } |
| 342 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 343 | private static class FormatIcmpV4Type implements CriterionTypeFormatter { |
| 344 | @Override |
| 345 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 346 | final IcmpTypeCriterion icmpTypeCriterion = |
| 347 | (IcmpTypeCriterion) criterion; |
| 348 | return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType()); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | private static class FormatIcmpV4Code implements CriterionTypeFormatter { |
| 353 | @Override |
| 354 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 355 | final IcmpCodeCriterion icmpCodeCriterion = |
| 356 | (IcmpCodeCriterion) criterion; |
| 357 | return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode()); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | private static class FormatIpV6FLabel implements CriterionTypeFormatter { |
| 362 | @Override |
| 363 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 364 | final IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
| 365 | (IPv6FlowLabelCriterion) criterion; |
| 366 | return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | private static class FormatIcmpV6Type implements CriterionTypeFormatter { |
| 371 | @Override |
| 372 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 373 | final Icmpv6TypeCriterion icmpv6TypeCriterion = |
| 374 | (Icmpv6TypeCriterion) criterion; |
| 375 | return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type()); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | private static class FormatIcmpV6Code implements CriterionTypeFormatter { |
| 380 | @Override |
| 381 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 382 | final Icmpv6CodeCriterion icmpv6CodeCriterion = |
| 383 | (Icmpv6CodeCriterion) criterion; |
| 384 | return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code()); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | private static class FormatV6NDTarget implements CriterionTypeFormatter { |
| 389 | @Override |
| 390 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 391 | final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion |
| 392 | = (IPv6NDTargetAddressCriterion) criterion; |
| 393 | return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString()); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | private static class FormatV6NDTll implements CriterionTypeFormatter { |
| 398 | @Override |
| 399 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 400 | final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion |
| 401 | = (IPv6NDLinkLayerAddressCriterion) criterion; |
| 402 | return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString()); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | private static class FormatMplsLabel implements CriterionTypeFormatter { |
| 407 | @Override |
| 408 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 409 | final MplsCriterion mplsCriterion = |
| 410 | (MplsCriterion) criterion; |
| 411 | return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt()); |
| 412 | } |
| 413 | } |
| 414 | |
Jonathan Hart | cc962d8 | 2016-08-09 16:52:22 -0700 | [diff] [blame] | 415 | private static class FormatMplsBos implements CriterionTypeFormatter { |
| 416 | @Override |
| 417 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 418 | final MplsBosCriterion mplsBosCriterion = |
| 419 | (MplsBosCriterion) criterion; |
| 420 | return root.put(CriterionCodec.BOS, mplsBosCriterion.mplsBos()); |
| 421 | } |
| 422 | } |
| 423 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 424 | private static class FormatIpV6Exthdr implements CriterionTypeFormatter { |
| 425 | @Override |
| 426 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 427 | final IPv6ExthdrFlagsCriterion exthdrCriterion = |
| 428 | (IPv6ExthdrFlagsCriterion) criterion; |
| 429 | return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags()); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | private static class FormatOchSigId implements CriterionTypeFormatter { |
| 434 | @Override |
| 435 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 436 | OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda(); |
| 437 | ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID); |
| 438 | |
| 439 | child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name()); |
| 440 | child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name()); |
| 441 | child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier()); |
| 442 | child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity()); |
| 443 | |
| 444 | return root; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | private static class FormatOchSigType implements CriterionTypeFormatter { |
| 449 | @Override |
| 450 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 451 | final OchSignalTypeCriterion ochSignalTypeCriterion = |
| 452 | (OchSignalTypeCriterion) criterion; |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 453 | return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 457 | private static class FormatTunnelId implements CriterionTypeFormatter { |
| 458 | @Override |
| 459 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 460 | final TunnelIdCriterion tunnelIdCriterion = |
| 461 | (TunnelIdCriterion) criterion; |
| 462 | return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId()); |
| 463 | } |
| 464 | } |
| 465 | |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 466 | private static class FormatOduSignalId implements CriterionTypeFormatter { |
| 467 | @Override |
| 468 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 469 | OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId(); |
| 470 | ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID); |
| 471 | |
| 472 | child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber()); |
| 473 | child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength()); |
| 474 | child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap())); |
| 475 | |
| 476 | return root; |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 480 | |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 481 | private static class FormatOduSignalType implements CriterionTypeFormatter { |
| 482 | @Override |
| 483 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 484 | final OduSignalTypeCriterion oduSignalTypeCriterion = |
| 485 | (OduSignalTypeCriterion) criterion; |
| 486 | return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name()); |
| 487 | } |
| 488 | } |
| 489 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 490 | private ObjectNode parsePiMatchExact(PiExactFieldMatch exactFieldMatch) { |
| 491 | |
| 492 | ObjectNode matchExactNode = context.mapper().createObjectNode(); |
| 493 | matchExactNode.put(CriterionCodec.PI_MATCH_FIELD_ID, exactFieldMatch.fieldId().id()); |
| 494 | matchExactNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.EXACT.name().toLowerCase()); |
| 495 | matchExactNode.put(CriterionCodec.PI_MATCH_VALUE, |
| 496 | HexString.toHexString(exactFieldMatch.value().asArray(), |
| 497 | null)); |
| 498 | return matchExactNode; |
| 499 | } |
| 500 | |
| 501 | private ObjectNode parsePiMatchLpm(PiLpmFieldMatch lpmFieldMatch) { |
| 502 | |
| 503 | ObjectNode matchLpmNode = context.mapper().createObjectNode(); |
| 504 | matchLpmNode.put(CriterionCodec.PI_MATCH_FIELD_ID, lpmFieldMatch.fieldId().id()); |
| 505 | matchLpmNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.LPM.name().toLowerCase()); |
| 506 | matchLpmNode.put(CriterionCodec.PI_MATCH_VALUE, |
| 507 | HexString.toHexString(lpmFieldMatch.value().asArray(), |
| 508 | null)); |
| 509 | matchLpmNode.put(CriterionCodec.PI_MATCH_PREFIX, lpmFieldMatch.prefixLength()); |
| 510 | |
| 511 | return matchLpmNode; |
| 512 | } |
| 513 | |
| 514 | private ObjectNode parsePiMatchTernary(PiTernaryFieldMatch ternaryFieldMatch) { |
| 515 | |
| 516 | ObjectNode matchTernaryNode = context.mapper().createObjectNode(); |
| 517 | matchTernaryNode.put(CriterionCodec.PI_MATCH_FIELD_ID, ternaryFieldMatch.fieldId().id()); |
| 518 | matchTernaryNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.TERNARY.name().toLowerCase()); |
| 519 | matchTernaryNode.put(CriterionCodec.PI_MATCH_VALUE, |
| 520 | HexString.toHexString(ternaryFieldMatch.value().asArray(), |
| 521 | null)); |
| 522 | matchTernaryNode.put(CriterionCodec.PI_MATCH_MASK, |
| 523 | HexString.toHexString(ternaryFieldMatch.mask().asArray(), |
| 524 | null)); |
| 525 | |
| 526 | return matchTernaryNode; |
| 527 | } |
| 528 | |
| 529 | private ObjectNode parsePiMatchRange(PiRangeFieldMatch rangeFieldMatch) { |
| 530 | |
| 531 | ObjectNode matchRangeNode = context.mapper().createObjectNode(); |
| 532 | matchRangeNode.put(CriterionCodec.PI_MATCH_FIELD_ID, rangeFieldMatch.fieldId().id()); |
| 533 | matchRangeNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.RANGE.name().toLowerCase()); |
| 534 | matchRangeNode.put(CriterionCodec.PI_MATCH_HIGH_VALUE, |
| 535 | HexString.toHexString(rangeFieldMatch.highValue().asArray(), |
| 536 | null)); |
| 537 | matchRangeNode.put(CriterionCodec.PI_MATCH_LOW_VALUE, |
| 538 | HexString.toHexString(rangeFieldMatch.lowValue().asArray(), |
| 539 | null)); |
| 540 | |
| 541 | return matchRangeNode; |
| 542 | } |
| 543 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 544 | private class FormatProtocolIndependent implements CriterionTypeFormatter { |
| 545 | @Override |
| 546 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 547 | final PiCriterion piCriterion = (PiCriterion) criterion; |
| 548 | ArrayNode matchNodes = context.mapper().createArrayNode(); |
| 549 | for (PiFieldMatch fieldMatch : piCriterion.fieldMatches()) { |
| 550 | switch (fieldMatch.type()) { |
| 551 | case EXACT: |
| 552 | matchNodes.add(parsePiMatchExact((PiExactFieldMatch) fieldMatch)); |
| 553 | break; |
| 554 | case LPM: |
| 555 | matchNodes.add(parsePiMatchLpm((PiLpmFieldMatch) fieldMatch)); |
| 556 | break; |
| 557 | case TERNARY: |
| 558 | matchNodes.add(parsePiMatchTernary((PiTernaryFieldMatch) fieldMatch)); |
| 559 | break; |
| 560 | case RANGE: |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 561 | matchNodes.add(parsePiMatchRange((PiRangeFieldMatch) fieldMatch)); |
| 562 | break; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 563 | default: |
| 564 | throw new IllegalArgumentException("Type " + fieldMatch.type().name() + " is unsupported"); |
| 565 | } |
| 566 | } |
| 567 | return (ObjectNode) root.set(CriterionCodec.PI_MATCHES, matchNodes); |
| 568 | } |
| 569 | } |
| 570 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 571 | private class FormatDummyType implements CriterionTypeFormatter { |
| 572 | |
| 573 | @Override |
| 574 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 575 | checkNotNull(criterion, "Criterion cannot be null"); |
| 576 | |
| 577 | return root.put(CriterionCodec.TYPE, criterion.type().toString()); |
| 578 | |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Encodes a criterion into a JSON node. |
| 584 | * |
| 585 | * @return encoded JSON object for the given criterion |
| 586 | */ |
| 587 | public ObjectNode encode() { |
| 588 | final ObjectNode result = context.mapper().createObjectNode() |
| 589 | .put(CriterionCodec.TYPE, criterion.type().toString()); |
| 590 | |
| 591 | CriterionTypeFormatter formatter = |
| 592 | checkNotNull( |
| 593 | formatMap.get(criterion.type()), |
| 594 | "No formatter found for criterion type " |
| 595 | + criterion.type().toString()); |
| 596 | |
| 597 | return formatter.encodeCriterion(result, criterion); |
| 598 | } |
| 599 | |
| 600 | } |