Refactor criterion codec
The previous implementation was a huge switch statement. These changes attempt to use polymorphism to avoid all of the branching inherent in a switch. Change-Id: If997f028346de02b883356bcde07f62674e319be
Showing
2 changed files
with
320 additions
and
149 deletions
... | @@ -15,6 +15,8 @@ | ... | @@ -15,6 +15,8 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.codec.impl; | 16 | package org.onosproject.codec.impl; |
17 | 17 | ||
18 | +import java.util.EnumMap; | ||
19 | + | ||
18 | import org.onosproject.codec.CodecContext; | 20 | import org.onosproject.codec.CodecContext; |
19 | import org.onosproject.codec.JsonCodec; | 21 | import org.onosproject.codec.JsonCodec; |
20 | import org.onosproject.net.flow.criteria.Criteria; | 22 | import org.onosproject.net.flow.criteria.Criteria; |
... | @@ -31,172 +33,295 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -31,172 +33,295 @@ import static com.google.common.base.Preconditions.checkNotNull; |
31 | */ | 33 | */ |
32 | public final class CriterionCodec extends JsonCodec<Criterion> { | 34 | public final class CriterionCodec extends JsonCodec<Criterion> { |
33 | 35 | ||
34 | - protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); | 36 | + protected static final Logger log = |
37 | + LoggerFactory.getLogger(CriterionCodec.class); | ||
38 | + | ||
39 | + private final EnumMap<Criterion.Type, CriterionTypeFormatter> formatMap; | ||
40 | + | ||
41 | + public CriterionCodec() { | ||
42 | + formatMap = new EnumMap<>(Criterion.Type.class); | ||
43 | + | ||
44 | + formatMap.put(Criterion.Type.IN_PORT, new FormatInPort()); | ||
45 | + formatMap.put(Criterion.Type.IN_PHY_PORT, new FormatInPort()); | ||
46 | + formatMap.put(Criterion.Type.ETH_DST, new FormatEth()); | ||
47 | + formatMap.put(Criterion.Type.ETH_SRC, new FormatEth()); | ||
48 | + formatMap.put(Criterion.Type.IP_PROTO, new FormatIpProto()); | ||
49 | + formatMap.put(Criterion.Type.ETH_TYPE, new FormatEthType()); | ||
50 | + formatMap.put(Criterion.Type.METADATA, new FormatMetadata()); | ||
51 | + formatMap.put(Criterion.Type.VLAN_VID, new FormatVlanVid()); | ||
52 | + formatMap.put(Criterion.Type.VLAN_PCP, new FormatVlanPcp()); | ||
53 | + formatMap.put(Criterion.Type.IP_DSCP, new FormatIpDscp()); | ||
54 | + formatMap.put(Criterion.Type.IP_ECN, new FormatIpEcn()); | ||
55 | + formatMap.put(Criterion.Type.IPV4_SRC, new FormatIp()); | ||
56 | + formatMap.put(Criterion.Type.IPV4_DST, new FormatIp()); | ||
57 | + formatMap.put(Criterion.Type.IPV6_SRC, new FormatIp()); | ||
58 | + formatMap.put(Criterion.Type.IPV6_DST, new FormatIp()); | ||
59 | + formatMap.put(Criterion.Type.TCP_SRC, new FormatTcp()); | ||
60 | + formatMap.put(Criterion.Type.TCP_DST, new FormatTcp()); | ||
61 | + formatMap.put(Criterion.Type.UDP_SRC, new FormatUdp()); | ||
62 | + formatMap.put(Criterion.Type.UDP_DST, new FormatUdp()); | ||
63 | + formatMap.put(Criterion.Type.SCTP_SRC, new FormatSctp()); | ||
64 | + formatMap.put(Criterion.Type.SCTP_DST, new FormatSctp()); | ||
65 | + formatMap.put(Criterion.Type.ICMPV4_TYPE, new FormatIcmpV4Type()); | ||
66 | + formatMap.put(Criterion.Type.ICMPV4_CODE, new FormatIcmpV4Code()); | ||
67 | + formatMap.put(Criterion.Type.IPV6_FLABEL, new FormatIpV6FLabel()); | ||
68 | + formatMap.put(Criterion.Type.ICMPV6_TYPE, new FormatIcmpV6Type()); | ||
69 | + formatMap.put(Criterion.Type.ICMPV6_CODE, new FormatIcmpV6Code()); | ||
70 | + formatMap.put(Criterion.Type.IPV6_ND_TARGET, new FormatV6NDTarget()); | ||
71 | + formatMap.put(Criterion.Type.IPV6_ND_SLL, new FormatV6NDTll()); | ||
72 | + formatMap.put(Criterion.Type.IPV6_ND_TLL, new FormatV6NDTll()); | ||
73 | + formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel()); | ||
74 | + formatMap.put(Criterion.Type.OCH_SIGID, new FormatOchSigId()); | ||
75 | + formatMap.put(Criterion.Type.OCH_SIGTYPE, new FormatOchSigType()); | ||
76 | + formatMap.put(Criterion.Type.MPLS_LABEL, new FormatMplsLabel()); | ||
77 | + | ||
78 | + // Currently unimplemented | ||
79 | + formatMap.put(Criterion.Type.ARP_OP, new FormatUnknown()); | ||
80 | + formatMap.put(Criterion.Type.ARP_SHA, new FormatUnknown()); | ||
81 | + formatMap.put(Criterion.Type.ARP_SPA, new FormatUnknown()); | ||
82 | + formatMap.put(Criterion.Type.ARP_THA, new FormatUnknown()); | ||
83 | + formatMap.put(Criterion.Type.ARP_TPA, new FormatUnknown()); | ||
84 | + formatMap.put(Criterion.Type.MPLS_TC, new FormatUnknown()); | ||
85 | + formatMap.put(Criterion.Type.MPLS_BOS, new FormatUnknown()); | ||
86 | + formatMap.put(Criterion.Type.PBB_ISID, new FormatUnknown()); | ||
87 | + formatMap.put(Criterion.Type.PBB_UCA, new FormatUnknown()); | ||
88 | + formatMap.put(Criterion.Type.TUNNEL_ID, new FormatUnknown()); | ||
89 | + formatMap.put(Criterion.Type.IPV6_EXTHDR, new FormatUnknown()); | ||
90 | + formatMap.put(Criterion.Type.UNASSIGNED_40, new FormatUnknown()); | ||
91 | + formatMap.put(Criterion.Type.TCP_FLAGS, new FormatUnknown()); | ||
92 | + formatMap.put(Criterion.Type.ACTSET_OUTPUT, new FormatUnknown()); | ||
93 | + formatMap.put(Criterion.Type.PACKET_TYPE, new FormatUnknown()); | ||
94 | + } | ||
35 | 95 | ||
36 | - @Override | 96 | + private interface CriterionTypeFormatter { |
37 | - public ObjectNode encode(Criterion criterion, CodecContext context) { | 97 | + ObjectNode formatCriterion(ObjectNode root, Criterion criterion); |
38 | - checkNotNull(criterion, "Criterion cannot be null"); | 98 | + } |
39 | 99 | ||
40 | - final ObjectNode result = context.mapper().createObjectNode() | 100 | + private static class FormatUnknown implements CriterionTypeFormatter { |
41 | - .put("type", criterion.type().toString()); | 101 | + @Override |
102 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
103 | + return root; | ||
104 | + } | ||
105 | + } | ||
42 | 106 | ||
43 | - switch (criterion.type()) { | 107 | + private static class FormatInPort implements CriterionTypeFormatter { |
44 | - | 108 | + @Override |
45 | - case IN_PORT: | 109 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
46 | - case IN_PHY_PORT: | 110 | + final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion; |
47 | - final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion; | 111 | + return root.put("port", portCriterion.port().toLong()); |
48 | - result.put("port", portCriterion.port().toLong()); | 112 | + } |
49 | - break; | 113 | + } |
50 | - | 114 | + |
51 | - case METADATA: | 115 | + private static class FormatEth implements CriterionTypeFormatter { |
52 | - final Criteria.MetadataCriterion metadataCriterion = | 116 | + @Override |
53 | - (Criteria.MetadataCriterion) criterion; | 117 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
54 | - result.put("metadata", metadataCriterion.metadata()); | 118 | + final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion; |
55 | - break; | 119 | + return root.put("mac", ethCriterion.mac().toString()); |
56 | - | 120 | + } |
57 | - case ETH_DST: | 121 | + } |
58 | - case ETH_SRC: | 122 | + |
59 | - final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion; | 123 | + private static class FormatIpProto implements CriterionTypeFormatter { |
60 | - result.put("mac", ethCriterion.mac().toString()); | 124 | + @Override |
61 | - break; | 125 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
62 | - | 126 | + final Criteria.IPProtocolCriterion iPProtocolCriterion = |
63 | - case ETH_TYPE: | 127 | + (Criteria.IPProtocolCriterion) criterion; |
64 | - final Criteria.EthTypeCriterion ethTypeCriterion = | 128 | + return root.put("protocol", iPProtocolCriterion.protocol()); |
65 | - (Criteria.EthTypeCriterion) criterion; | 129 | + } |
66 | - result.put("ethType", ethTypeCriterion.ethType()); | 130 | + } |
67 | - break; | 131 | + |
68 | - | 132 | + private static class FormatEthType implements CriterionTypeFormatter { |
69 | - case VLAN_VID: | 133 | + @Override |
70 | - final Criteria.VlanIdCriterion vlanIdCriterion = | 134 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
71 | - (Criteria.VlanIdCriterion) criterion; | 135 | + final Criteria.EthTypeCriterion ethTypeCriterion = |
72 | - result.put("vlanId", vlanIdCriterion.vlanId().toShort()); | 136 | + (Criteria.EthTypeCriterion) criterion; |
73 | - break; | 137 | + return root.put("ethType", ethTypeCriterion.ethType()); |
74 | - | 138 | + } |
75 | - case VLAN_PCP: | 139 | + } |
140 | + | ||
141 | + private static class FormatMetadata implements CriterionTypeFormatter { | ||
142 | + @Override | ||
143 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
144 | + final Criteria.MetadataCriterion metadataCriterion = | ||
145 | + (Criteria.MetadataCriterion) criterion; | ||
146 | + return root.put("metadata", metadataCriterion.metadata()); | ||
147 | + } | ||
148 | + } | ||
149 | + | ||
150 | + private static class FormatVlanVid implements CriterionTypeFormatter { | ||
151 | + @Override | ||
152 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
153 | + final Criteria.VlanIdCriterion vlanIdCriterion = | ||
154 | + (Criteria.VlanIdCriterion) criterion; | ||
155 | + return root.put("vlanId", vlanIdCriterion.vlanId().toShort()); | ||
156 | + } | ||
157 | + } | ||
158 | + | ||
159 | + private static class FormatVlanPcp implements CriterionTypeFormatter { | ||
160 | + @Override | ||
161 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
76 | final Criteria.VlanPcpCriterion vlanPcpCriterion = | 162 | final Criteria.VlanPcpCriterion vlanPcpCriterion = |
77 | (Criteria.VlanPcpCriterion) criterion; | 163 | (Criteria.VlanPcpCriterion) criterion; |
78 | - result.put("priority", vlanPcpCriterion.priority()); | 164 | + return root.put("priority", vlanPcpCriterion.priority()); |
79 | - break; | 165 | + } |
166 | + } | ||
80 | 167 | ||
81 | - case IP_DSCP: | 168 | + private static class FormatIpDscp implements CriterionTypeFormatter { |
169 | + @Override | ||
170 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
82 | final Criteria.IPDscpCriterion ipDscpCriterion = | 171 | final Criteria.IPDscpCriterion ipDscpCriterion = |
83 | (Criteria.IPDscpCriterion) criterion; | 172 | (Criteria.IPDscpCriterion) criterion; |
84 | - result.put("ipDscp", ipDscpCriterion.ipDscp()); | 173 | + return root.put("ipDscp", ipDscpCriterion.ipDscp()); |
85 | - break; | 174 | + } |
175 | + } | ||
86 | 176 | ||
87 | - case IP_ECN: | 177 | + private static class FormatIpEcn implements CriterionTypeFormatter { |
178 | + @Override | ||
179 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
88 | final Criteria.IPEcnCriterion ipEcnCriterion = | 180 | final Criteria.IPEcnCriterion ipEcnCriterion = |
89 | (Criteria.IPEcnCriterion) criterion; | 181 | (Criteria.IPEcnCriterion) criterion; |
90 | - result.put("ipEcn", ipEcnCriterion.ipEcn()); | 182 | + return root.put("ipEcn", ipEcnCriterion.ipEcn()); |
91 | - break; | 183 | + } |
92 | - | 184 | + } |
93 | - case IP_PROTO: | 185 | + |
94 | - final Criteria.IPProtocolCriterion iPProtocolCriterion = | 186 | + private static class FormatIp implements CriterionTypeFormatter { |
95 | - (Criteria.IPProtocolCriterion) criterion; | 187 | + @Override |
96 | - result.put("protocol", iPProtocolCriterion.protocol()); | 188 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
97 | - break; | ||
98 | - | ||
99 | - case IPV4_SRC: | ||
100 | - case IPV4_DST: | ||
101 | - case IPV6_SRC: | ||
102 | - case IPV6_DST: | ||
103 | final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion; | 189 | final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion; |
104 | - result.put("ip", iPCriterion.ip().toString()); | 190 | + return root.put("ip", iPCriterion.ip().toString()); |
105 | - break; | 191 | + } |
192 | + } | ||
106 | 193 | ||
107 | - case TCP_SRC: | 194 | + private static class FormatTcp implements CriterionTypeFormatter { |
108 | - case TCP_DST: | 195 | + @Override |
196 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
109 | final Criteria.TcpPortCriterion tcpPortCriterion = | 197 | final Criteria.TcpPortCriterion tcpPortCriterion = |
110 | (Criteria.TcpPortCriterion) criterion; | 198 | (Criteria.TcpPortCriterion) criterion; |
111 | - result.put("tcpPort", tcpPortCriterion.tcpPort().byteValue()); | 199 | + return root.put("tcpPort", tcpPortCriterion.tcpPort().byteValue()); |
112 | - break; | 200 | + } |
201 | + } | ||
113 | 202 | ||
114 | - case UDP_SRC: | 203 | + private static class FormatUdp implements CriterionTypeFormatter { |
115 | - case UDP_DST: | 204 | + @Override |
205 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
116 | final Criteria.UdpPortCriterion udpPortCriterion = | 206 | final Criteria.UdpPortCriterion udpPortCriterion = |
117 | (Criteria.UdpPortCriterion) criterion; | 207 | (Criteria.UdpPortCriterion) criterion; |
118 | - result.put("udpPort", udpPortCriterion.udpPort().byteValue()); | 208 | + return root.put("udpPort", udpPortCriterion.udpPort().byteValue()); |
119 | - break; | 209 | + } |
120 | - | 210 | + } |
121 | - case SCTP_SRC: | 211 | + |
122 | - case SCTP_DST: | 212 | + private static class FormatSctp implements CriterionTypeFormatter { |
123 | - final Criteria.SctpPortCriterion sctpPortCriterion = | 213 | + @Override |
124 | - (Criteria.SctpPortCriterion) criterion; | 214 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
125 | - result.put("sctpPort", | 215 | + final Criteria.SctpPortCriterion sctpPortCriterion = |
126 | - sctpPortCriterion.sctpPort().byteValue()); | 216 | + (Criteria.SctpPortCriterion) criterion; |
127 | - break; | 217 | + return root.put("sctpPort", |
128 | - | 218 | + sctpPortCriterion.sctpPort().byteValue()); |
129 | - case ICMPV4_TYPE: | 219 | + } |
130 | - final Criteria.IcmpTypeCriterion icmpTypeCriterion = | 220 | + } |
131 | - (Criteria.IcmpTypeCriterion) criterion; | 221 | + |
132 | - result.put("icmpType", icmpTypeCriterion.icmpType()); | 222 | + private static class FormatIcmpV4Type implements CriterionTypeFormatter { |
133 | - break; | 223 | + @Override |
134 | - | 224 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
135 | - case ICMPV4_CODE: | 225 | + final Criteria.IcmpTypeCriterion icmpTypeCriterion = |
136 | - final Criteria.IcmpCodeCriterion icmpCodeCriterion = | 226 | + (Criteria.IcmpTypeCriterion) criterion; |
137 | - (Criteria.IcmpCodeCriterion) criterion; | 227 | + return root.put("icmpType", icmpTypeCriterion.icmpType()); |
138 | - result.put("icmpCode", icmpCodeCriterion.icmpCode()); | 228 | + } |
139 | - break; | 229 | + } |
140 | - | 230 | + |
141 | - case IPV6_FLABEL: | 231 | + private static class FormatIcmpV4Code implements CriterionTypeFormatter { |
142 | - final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion = | 232 | + @Override |
143 | - (Criteria.IPv6FlowLabelCriterion) criterion; | 233 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
144 | - result.put("flowLabel", | 234 | + final Criteria.IcmpCodeCriterion icmpCodeCriterion = |
145 | - ipv6FlowLabelCriterion.flowLabel()); | 235 | + (Criteria.IcmpCodeCriterion) criterion; |
146 | - break; | 236 | + return root.put("icmpCode", icmpCodeCriterion.icmpCode()); |
147 | - | 237 | + } |
148 | - case ICMPV6_TYPE: | 238 | + } |
149 | - final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion = | 239 | + |
150 | - (Criteria.Icmpv6TypeCriterion) criterion; | 240 | + private static class FormatIpV6FLabel implements CriterionTypeFormatter { |
151 | - result.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type()); | 241 | + @Override |
152 | - break; | 242 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
153 | - | 243 | + final Criteria.IPv6FlowLabelCriterion ipv6FlowLabelCriterion = |
154 | - case ICMPV6_CODE: | 244 | + (Criteria.IPv6FlowLabelCriterion) criterion; |
155 | - final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion = | 245 | + return root.put("flowLabel", ipv6FlowLabelCriterion.flowLabel()); |
156 | - (Criteria.Icmpv6CodeCriterion) criterion; | 246 | + } |
157 | - result.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code()); | 247 | + } |
158 | - break; | 248 | + |
159 | - | 249 | + private static class FormatIcmpV6Type implements CriterionTypeFormatter { |
160 | - case IPV6_ND_TARGET: | 250 | + @Override |
161 | - final Criteria.IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion | 251 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
162 | - = (Criteria.IPv6NDTargetAddressCriterion) criterion; | 252 | + final Criteria.Icmpv6TypeCriterion icmpv6TypeCriterion = |
163 | - result.put("targetAddress", | 253 | + (Criteria.Icmpv6TypeCriterion) criterion; |
164 | - ipv6NDTargetAddressCriterion.targetAddress().toString()); | 254 | + return root.put("icmpv6Type", icmpv6TypeCriterion.icmpv6Type()); |
165 | - break; | 255 | + } |
166 | - | 256 | + } |
167 | - case IPV6_ND_SLL: | 257 | + |
168 | - case IPV6_ND_TLL: | 258 | + private static class FormatIcmpV6Code implements CriterionTypeFormatter { |
169 | - final Criteria.IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion | 259 | + @Override |
170 | - = (Criteria.IPv6NDLinkLayerAddressCriterion) criterion; | 260 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
171 | - result.put("mac", | 261 | + final Criteria.Icmpv6CodeCriterion icmpv6CodeCriterion = |
172 | - ipv6NDLinkLayerAddressCriterion.mac().toString()); | 262 | + (Criteria.Icmpv6CodeCriterion) criterion; |
173 | - break; | 263 | + return root.put("icmpv6Code", icmpv6CodeCriterion.icmpv6Code()); |
174 | - | 264 | + } |
175 | - case MPLS_LABEL: | 265 | + } |
176 | - final Criteria.MplsCriterion mplsCriterion = | 266 | + |
177 | - (Criteria.MplsCriterion) criterion; | 267 | + private static class FormatV6NDTarget implements CriterionTypeFormatter { |
178 | - result.put("label", mplsCriterion.label()); | 268 | + @Override |
179 | - break; | 269 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
180 | - | 270 | + final Criteria.IPv6NDTargetAddressCriterion ipv6NDTargetAddressCriterion |
181 | - case OCH_SIGID: | 271 | + = (Criteria.IPv6NDTargetAddressCriterion) criterion; |
182 | - final Criteria.LambdaCriterion lambdaCriterion = | 272 | + return root.put("targetAddress", ipv6NDTargetAddressCriterion.targetAddress().toString()); |
183 | - (Criteria.LambdaCriterion) criterion; | 273 | + } |
184 | - result.put("lambda", lambdaCriterion.lambda()); | 274 | + } |
185 | - break; | 275 | + |
186 | - | 276 | + private static class FormatV6NDTll implements CriterionTypeFormatter { |
187 | - case OCH_SIGTYPE: | 277 | + @Override |
188 | - final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = | 278 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
189 | - (Criteria.OpticalSignalTypeCriterion) criterion; | 279 | + final Criteria.IPv6NDLinkLayerAddressCriterion ipv6NDLinkLayerAddressCriterion |
190 | - result.put("signalType", opticalSignalTypeCriterion.signalType()); | 280 | + = (Criteria.IPv6NDLinkLayerAddressCriterion) criterion; |
191 | - break; | 281 | + return root.put("mac", ipv6NDLinkLayerAddressCriterion.mac().toString()); |
192 | - | 282 | + } |
193 | - default: | 283 | + } |
194 | - // Don't know how to format this type | 284 | + |
195 | - log.info("Cannot convert criterion of type {} to JSON", | 285 | + private static class FormatMplsLabel implements CriterionTypeFormatter { |
196 | - criterion.type()); | 286 | + @Override |
197 | - break; | 287 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { |
198 | - } | 288 | + final Criteria.MplsCriterion mplsCriterion = |
199 | - | 289 | + (Criteria.MplsCriterion) criterion; |
200 | - return result; | 290 | + return root.put("label", mplsCriterion.label()); |
291 | + } | ||
292 | + } | ||
293 | + | ||
294 | + private static class FormatOchSigId implements CriterionTypeFormatter { | ||
295 | + @Override | ||
296 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
297 | + final Criteria.LambdaCriterion lambdaCriterion = | ||
298 | + (Criteria.LambdaCriterion) criterion; | ||
299 | + return root.put("lambda", lambdaCriterion.lambda()); | ||
300 | + } | ||
301 | + } | ||
302 | + | ||
303 | + private static class FormatOchSigType implements CriterionTypeFormatter { | ||
304 | + @Override | ||
305 | + public ObjectNode formatCriterion(ObjectNode root, Criterion criterion) { | ||
306 | + final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = | ||
307 | + (Criteria.OpticalSignalTypeCriterion) criterion; | ||
308 | + return root.put("signalType", opticalSignalTypeCriterion.signalType()); | ||
309 | + } | ||
310 | + } | ||
311 | + | ||
312 | + @Override | ||
313 | + public ObjectNode encode(Criterion criterion, CodecContext context) { | ||
314 | + checkNotNull(criterion, "Criterion cannot be null"); | ||
315 | + | ||
316 | + final ObjectNode result = context.mapper().createObjectNode() | ||
317 | + .put("type", criterion.type().toString()); | ||
318 | + | ||
319 | + CriterionTypeFormatter formatter = | ||
320 | + checkNotNull( | ||
321 | + formatMap.get(criterion.type()), | ||
322 | + "No formatter found for criterion type " | ||
323 | + + criterion.type().toString()); | ||
324 | + | ||
325 | + return formatter.formatCriterion(result, criterion); | ||
201 | } | 326 | } |
202 | } | 327 | } | ... | ... |
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 | + | ||
18 | +import java.util.EnumMap; | ||
19 | + | ||
20 | +import org.junit.Test; | ||
21 | +import org.onosproject.net.flow.criteria.Criterion; | ||
22 | + | ||
23 | +import static org.onlab.junit.TestUtils.getField; | ||
24 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
25 | +import static org.hamcrest.Matchers.*; | ||
26 | + | ||
27 | +/** | ||
28 | + * Unit tests for criterion codec. | ||
29 | + */ | ||
30 | +public class CriterionCodecTest { | ||
31 | + | ||
32 | + /** | ||
33 | + * Checks that all criterion types are covered by the codec. | ||
34 | + */ | ||
35 | + @Test | ||
36 | + public void checkCriterionTypes() throws Exception { | ||
37 | + CriterionCodec codec = new CriterionCodec(); | ||
38 | + EnumMap<Criterion.Type, Object> formatMap = getField(codec, "formatMap"); | ||
39 | + assertThat(formatMap, notNullValue()); | ||
40 | + | ||
41 | + for (Criterion.Type type : Criterion.Type.values()) { | ||
42 | + assertThat("Entry not found for " + type.toString(), | ||
43 | + formatMap.get(type), notNullValue()); | ||
44 | + } | ||
45 | + } | ||
46 | +} |
-
Please register or login to post a comment