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