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.node.ObjectNode; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 19 | import org.onlab.util.HexString; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.net.OchSignal; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 22 | import org.onosproject.net.OduSignalId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.criteria.Criterion; |
| 24 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 25 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 26 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 27 | import org.onosproject.net.flow.criteria.IPDscpCriterion; |
| 28 | import org.onosproject.net.flow.criteria.IPEcnCriterion; |
| 29 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 30 | import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; |
| 31 | import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; |
| 32 | import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; |
| 33 | import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; |
| 34 | import org.onosproject.net.flow.criteria.IcmpCodeCriterion; |
| 35 | import org.onosproject.net.flow.criteria.IcmpTypeCriterion; |
| 36 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 37 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
| 38 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
| 39 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 40 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 41 | import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 42 | import org.onosproject.net.flow.criteria.OduSignalIdCriterion; |
| 43 | import org.onosproject.net.flow.criteria.OduSignalTypeCriterion; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 45 | import org.onosproject.net.flow.criteria.SctpPortCriterion; |
| 46 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 49 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 50 | import org.onosproject.net.flow.criteria.VlanPcpCriterion; |
| 51 | |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 52 | import java.util.EnumMap; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 53 | |
| 54 | import static com.google.common.base.Preconditions.checkNotNull; |
| 55 | |
| 56 | /** |
| 57 | * Encode portion of the criterion codec. |
| 58 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 59 | public final class EncodeCriterionCodecHelper { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 60 | |
| 61 | private final Criterion criterion; |
| 62 | private final CodecContext context; |
| 63 | |
| 64 | private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap; |
| 65 | |
| 66 | /** |
| 67 | * Creates an encoder object for a criterion. |
| 68 | * Initializes the formatter lookup map for the criterion subclasses. |
| 69 | * |
| 70 | * @param criterion Criterion to encode |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 71 | * @param context context of the JSON encoding |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 72 | */ |
Ray Milkey | 6d7968e | 2015-07-06 14:30:02 -0700 | [diff] [blame] | 73 | public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 74 | this.criterion = criterion; |
| 75 | this.context = context; |
| 76 | |
| 77 | formatMap = new EnumMap<>(Criterion.Type.class); |
| 78 | |
| 79 | formatMap.put(Criterion.Type.IN_PORT, new FormatInPort()); |
| 80 | formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort()); |
| 81 | formatMap.put(Criterion.Type.METADATA, new FormatMetadata()); |
| 82 | formatMap.put(Criterion.Type.ETH_DST, new FormatEth()); |
| 83 | formatMap.put(Criterion.Type.ETH_SRC, new FormatEth()); |
| 84 | formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType()); |
| 85 | formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid()); |
| 86 | formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp()); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 87 | formatMap.put(Criterion.Type.INNER_VLAN_VID, new FormatInnerVlanVid()); |
| 88 | formatMap.put(Criterion.Type.INNER_VLAN_PCP, new FormatInnerVlanPcp()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 89 | formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp()); |
| 90 | formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn()); |
| 91 | formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto()); |
| 92 | formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp()); |
| 93 | formatMap.put(Criterion.Type.IPV4_DST, new FormatIp()); |
| 94 | formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp()); |
| 95 | formatMap.put(Criterion.Type.TCP_DST, new FormatTcp()); |
| 96 | formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp()); |
| 97 | formatMap.put(Criterion.Type.UDP_DST, new FormatUdp()); |
| 98 | formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp()); |
| 99 | formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp()); |
| 100 | formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type()); |
| 101 | formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code()); |
| 102 | formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp()); |
| 103 | formatMap.put(Criterion.Type.IPV6_DST, new FormatIp()); |
| 104 | formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel()); |
| 105 | formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type()); |
| 106 | formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code()); |
| 107 | formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget()); |
| 108 | formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll()); |
| 109 | formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll()); |
| 110 | formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel()); |
| 111 | formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatIpV6Exthdr()); |
| 112 | formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId()); |
| 113 | formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType()); |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 114 | formatMap.put(Criterion.Type.TUNNEL_ID, new FormatTunnelId()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 115 | formatMap.put(Criterion.Type.DUMMY, new FormatDummyType()); |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 116 | formatMap.put(Criterion.Type.ODU_SIGID, new FormatOduSignalId()); |
| 117 | formatMap.put(Criterion.Type.ODU_SIGTYPE, new FormatOduSignalType()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 118 | // Currently unimplemented |
| 119 | formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown()); |
| 120 | formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown()); |
| 121 | formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown()); |
| 122 | formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown()); |
| 123 | formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown()); |
| 124 | formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown()); |
| 125 | formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown()); |
| 126 | formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 127 | formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown()); |
| 128 | formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown()); |
| 129 | formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown()); |
| 130 | formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown()); |
| 131 | formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown()); |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame] | 132 | formatMap.put(Criterion.Type.EXTENSION, new FormatUnknown()); |
Saurav Das | 9d6c86b | 2016-02-19 09:01:07 -0800 | [diff] [blame] | 133 | formatMap.put(Criterion.Type.ETH_DST_MASKED, new FormatUnknown()); |
| 134 | formatMap.put(Criterion.Type.ETH_SRC_MASKED, new FormatUnknown()); |
| 135 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private interface CriterionTypeFormatter { |
| 139 | ObjectNode encodeCriterion(ObjectNode root, Criterion criterion); |
| 140 | } |
| 141 | |
| 142 | private static class FormatUnknown implements CriterionTypeFormatter { |
| 143 | @Override |
| 144 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 145 | return root; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private static class FormatInPort implements CriterionTypeFormatter { |
| 150 | @Override |
| 151 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 152 | final PortCriterion portCriterion = (PortCriterion) criterion; |
| 153 | return root.put(CriterionCodec.PORT, portCriterion.port().toLong()); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private static class FormatMetadata implements CriterionTypeFormatter { |
| 158 | @Override |
| 159 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 160 | final MetadataCriterion metadataCriterion = |
| 161 | (MetadataCriterion) criterion; |
| 162 | return root.put(CriterionCodec.METADATA, metadataCriterion.metadata()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private static class FormatEth implements CriterionTypeFormatter { |
| 167 | @Override |
| 168 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 169 | final EthCriterion ethCriterion = (EthCriterion) criterion; |
| 170 | return root.put(CriterionCodec.MAC, ethCriterion.mac().toString()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private static class FormatEthType implements CriterionTypeFormatter { |
| 175 | @Override |
| 176 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 177 | final EthTypeCriterion ethTypeCriterion = |
| 178 | (EthTypeCriterion) criterion; |
andrea | d35f89c | 2015-11-23 10:02:07 -0800 | [diff] [blame] | 179 | return root.put(CriterionCodec.ETH_TYPE, "0x" |
| 180 | + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | private static class FormatVlanVid implements CriterionTypeFormatter { |
| 185 | @Override |
| 186 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 187 | final VlanIdCriterion vlanIdCriterion = |
| 188 | (VlanIdCriterion) criterion; |
| 189 | return root.put(CriterionCodec.VLAN_ID, vlanIdCriterion.vlanId().toShort()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private static class FormatVlanPcp implements CriterionTypeFormatter { |
| 194 | @Override |
| 195 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 196 | final VlanPcpCriterion vlanPcpCriterion = |
| 197 | (VlanPcpCriterion) criterion; |
| 198 | return root.put(CriterionCodec.PRIORITY, vlanPcpCriterion.priority()); |
| 199 | } |
| 200 | } |
| 201 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 202 | private static class FormatInnerVlanVid implements CriterionTypeFormatter { |
| 203 | @Override |
| 204 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 205 | final VlanIdCriterion vlanIdCriterion = |
| 206 | (VlanIdCriterion) criterion; |
| 207 | return root.put(CriterionCodec.INNER_VLAN_ID, vlanIdCriterion.vlanId().toShort()); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private static class FormatInnerVlanPcp implements CriterionTypeFormatter { |
| 212 | @Override |
| 213 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 214 | final VlanPcpCriterion vlanPcpCriterion = |
| 215 | (VlanPcpCriterion) criterion; |
| 216 | return root.put(CriterionCodec.INNER_PRIORITY, vlanPcpCriterion.priority()); |
| 217 | } |
| 218 | } |
| 219 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 220 | private static class FormatIpDscp implements CriterionTypeFormatter { |
| 221 | @Override |
| 222 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 223 | final IPDscpCriterion ipDscpCriterion = |
| 224 | (IPDscpCriterion) criterion; |
| 225 | return root.put(CriterionCodec.IP_DSCP, ipDscpCriterion.ipDscp()); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private static class FormatIpEcn implements CriterionTypeFormatter { |
| 230 | @Override |
| 231 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 232 | final IPEcnCriterion ipEcnCriterion = |
| 233 | (IPEcnCriterion) criterion; |
| 234 | return root.put(CriterionCodec.IP_ECN, ipEcnCriterion.ipEcn()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private static class FormatIpProto implements CriterionTypeFormatter { |
| 239 | @Override |
| 240 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 241 | final IPProtocolCriterion iPProtocolCriterion = |
| 242 | (IPProtocolCriterion) criterion; |
| 243 | return root.put(CriterionCodec.PROTOCOL, iPProtocolCriterion.protocol()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private static class FormatIp implements CriterionTypeFormatter { |
| 248 | @Override |
| 249 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 250 | final IPCriterion iPCriterion = (IPCriterion) criterion; |
| 251 | return root.put(CriterionCodec.IP, iPCriterion.ip().toString()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private static class FormatTcp implements CriterionTypeFormatter { |
| 256 | @Override |
| 257 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 258 | final TcpPortCriterion tcpPortCriterion = |
| 259 | (TcpPortCriterion) criterion; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 260 | return root.put(CriterionCodec.TCP_PORT, tcpPortCriterion.tcpPort().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | private static class FormatUdp implements CriterionTypeFormatter { |
| 265 | @Override |
| 266 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 267 | final UdpPortCriterion udpPortCriterion = |
| 268 | (UdpPortCriterion) criterion; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 269 | return root.put(CriterionCodec.UDP_PORT, udpPortCriterion.udpPort().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | private static class FormatSctp implements CriterionTypeFormatter { |
| 274 | @Override |
| 275 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 276 | final SctpPortCriterion sctpPortCriterion = |
| 277 | (SctpPortCriterion) criterion; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 278 | return root.put(CriterionCodec.SCTP_PORT, sctpPortCriterion.sctpPort().toInt()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | private static class FormatIcmpV4Type implements CriterionTypeFormatter { |
| 283 | @Override |
| 284 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 285 | final IcmpTypeCriterion icmpTypeCriterion = |
| 286 | (IcmpTypeCriterion) criterion; |
| 287 | return root.put(CriterionCodec.ICMP_TYPE, icmpTypeCriterion.icmpType()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | private static class FormatIcmpV4Code implements CriterionTypeFormatter { |
| 292 | @Override |
| 293 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 294 | final IcmpCodeCriterion icmpCodeCriterion = |
| 295 | (IcmpCodeCriterion) criterion; |
| 296 | return root.put(CriterionCodec.ICMP_CODE, icmpCodeCriterion.icmpCode()); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | private static class FormatIpV6FLabel implements CriterionTypeFormatter { |
| 301 | @Override |
| 302 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 303 | final IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
| 304 | (IPv6FlowLabelCriterion) criterion; |
| 305 | return root.put(CriterionCodec.FLOW_LABEL, ipv6FlowLabelCriterion.flowLabel()); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | private static class FormatIcmpV6Type implements CriterionTypeFormatter { |
| 310 | @Override |
| 311 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 312 | final Icmpv6TypeCriterion icmpv6TypeCriterion = |
| 313 | (Icmpv6TypeCriterion) criterion; |
| 314 | return root.put(CriterionCodec.ICMPV6_TYPE, icmpv6TypeCriterion.icmpv6Type()); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | private static class FormatIcmpV6Code implements CriterionTypeFormatter { |
| 319 | @Override |
| 320 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 321 | final Icmpv6CodeCriterion icmpv6CodeCriterion = |
| 322 | (Icmpv6CodeCriterion) criterion; |
| 323 | return root.put(CriterionCodec.ICMPV6_CODE, icmpv6CodeCriterion.icmpv6Code()); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | private static class FormatV6NDTarget implements CriterionTypeFormatter { |
| 328 | @Override |
| 329 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 330 | final IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion |
| 331 | = (IPv6NDTargetAddressCriterion) criterion; |
| 332 | return root.put(CriterionCodec.TARGET_ADDRESS, ipv6NDTargetAddressCriterion.targetAddress().toString()); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private static class FormatV6NDTll implements CriterionTypeFormatter { |
| 337 | @Override |
| 338 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 339 | final IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion |
| 340 | = (IPv6NDLinkLayerAddressCriterion) criterion; |
| 341 | return root.put(CriterionCodec.MAC, ipv6NDLinkLayerAddressCriterion.mac().toString()); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private static class FormatMplsLabel implements CriterionTypeFormatter { |
| 346 | @Override |
| 347 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 348 | final MplsCriterion mplsCriterion = |
| 349 | (MplsCriterion) criterion; |
| 350 | return root.put(CriterionCodec.LABEL, mplsCriterion.label().toInt()); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | private static class FormatIpV6Exthdr implements CriterionTypeFormatter { |
| 355 | @Override |
| 356 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 357 | final IPv6ExthdrFlagsCriterion exthdrCriterion = |
| 358 | (IPv6ExthdrFlagsCriterion) criterion; |
| 359 | return root.put(CriterionCodec.EXT_HDR_FLAGS, exthdrCriterion.exthdrFlags()); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | private static class FormatOchSigId implements CriterionTypeFormatter { |
| 364 | @Override |
| 365 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 366 | OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda(); |
| 367 | ObjectNode child = root.putObject(CriterionCodec.OCH_SIGNAL_ID); |
| 368 | |
| 369 | child.put(CriterionCodec.GRID_TYPE, ochSignal.gridType().name()); |
| 370 | child.put(CriterionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name()); |
| 371 | child.put(CriterionCodec.SPACING_MULIPLIER, ochSignal.spacingMultiplier()); |
| 372 | child.put(CriterionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity()); |
| 373 | |
| 374 | return root; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private static class FormatOchSigType implements CriterionTypeFormatter { |
| 379 | @Override |
| 380 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 381 | final OchSignalTypeCriterion ochSignalTypeCriterion = |
| 382 | (OchSignalTypeCriterion) criterion; |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 383 | return root.put(CriterionCodec.OCH_SIGNAL_TYPE, ochSignalTypeCriterion.signalType().name()); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 387 | private static class FormatTunnelId implements CriterionTypeFormatter { |
| 388 | @Override |
| 389 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 390 | final TunnelIdCriterion tunnelIdCriterion = |
| 391 | (TunnelIdCriterion) criterion; |
| 392 | return root.put(CriterionCodec.TUNNEL_ID, tunnelIdCriterion.tunnelId()); |
| 393 | } |
| 394 | } |
| 395 | |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 396 | private static class FormatOduSignalId implements CriterionTypeFormatter { |
| 397 | @Override |
| 398 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 399 | OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId(); |
| 400 | ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID); |
| 401 | |
| 402 | child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber()); |
| 403 | child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength()); |
| 404 | child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap())); |
| 405 | |
| 406 | return root; |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 410 | |
Yafit Hadar | 52d8155 | 2015-10-07 12:26:52 +0300 | [diff] [blame] | 411 | private static class FormatOduSignalType implements CriterionTypeFormatter { |
| 412 | @Override |
| 413 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 414 | final OduSignalTypeCriterion oduSignalTypeCriterion = |
| 415 | (OduSignalTypeCriterion) criterion; |
| 416 | return root.put(CriterionCodec.ODU_SIGNAL_TYPE, oduSignalTypeCriterion.signalType().name()); |
| 417 | } |
| 418 | } |
| 419 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 420 | private class FormatDummyType implements CriterionTypeFormatter { |
| 421 | |
| 422 | @Override |
| 423 | public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { |
| 424 | checkNotNull(criterion, "Criterion cannot be null"); |
| 425 | |
| 426 | return root.put(CriterionCodec.TYPE, criterion.type().toString()); |
| 427 | |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Encodes a criterion into a JSON node. |
| 433 | * |
| 434 | * @return encoded JSON object for the given criterion |
| 435 | */ |
| 436 | public ObjectNode encode() { |
| 437 | final ObjectNode result = context.mapper().createObjectNode() |
| 438 | .put(CriterionCodec.TYPE, criterion.type().toString()); |
| 439 | |
| 440 | CriterionTypeFormatter formatter = |
| 441 | checkNotNull( |
| 442 | formatMap.get(criterion.type()), |
| 443 | "No formatter found for criterion type " |
| 444 | + criterion.type().toString()); |
| 445 | |
| 446 | return formatter.encodeCriterion(result, criterion); |
| 447 | } |
| 448 | |
| 449 | } |