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