diff --git a/src/main/java/net/floodlightcontroller/util/ActionUtils.java b/src/main/java/net/floodlightcontroller/util/ActionUtils.java index 6d905ab9627e07dbfa4b308546a2821ea609cbca..68832bed235f1efb2d0e4a8a2186e245407f7734 100644 --- a/src/main/java/net/floodlightcontroller/util/ActionUtils.java +++ b/src/main/java/net/floodlightcontroller/util/ActionUtils.java @@ -94,7 +94,6 @@ import org.projectfloodlight.openflow.types.IpEcn; import org.projectfloodlight.openflow.types.IpProtocol; import org.projectfloodlight.openflow.types.MacAddress; import org.projectfloodlight.openflow.types.OFBooleanValue; -import org.projectfloodlight.openflow.types.OFGroup; import org.projectfloodlight.openflow.types.OFMetadata; import org.projectfloodlight.openflow.types.OFPort; import org.projectfloodlight.openflow.types.OFVlanVidMatch; @@ -527,16 +526,12 @@ public class ActionUtils { } switch (actionData[0]) { - case MatchUtils.STR_ARP_OPCODE: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildArpOp().setValue(ArpOpcode.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildArpOp().setValue(ArpOpcode.of(Integer.parseInt(actionData[1]))).build()) - .build(); - } + case MatchUtils.STR_ARP_OPCODE: + a = f.actions().buildSetField() + .setField(f.oxms().buildArpOp() + .setValue(ArpOpcode.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_ARP_SHA: a = f.actions().buildSetField() @@ -574,15 +569,11 @@ public class ActionUtils { .build(); break; case MatchUtils.STR_DL_TYPE: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildEthType().setValue(EthType.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildEthType().setValue(EthType.of(Integer.parseInt(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildEthType() + .setValue(EthType.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_DL_SRC: a = f.actions().buildSetField() @@ -595,81 +586,53 @@ public class ActionUtils { .build(); break; case MatchUtils.STR_DL_VLAN: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildVlanVid().setValue(OFVlanVidMatch.ofVlan(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildVlanVid().setValue(OFVlanVidMatch.ofVlan(Integer.parseInt(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildVlanVid() + .setValue(OFVlanVidMatch.ofVlan(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_DL_VLAN_PCP: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildVlanPcp().setValue(VlanPcp.of(Byte.parseByte(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildVlanPcp().setValue(VlanPcp.of(Byte.parseByte(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildVlanPcp() + .setValue(VlanPcp.of(ParseUtils.parseHexOrDecByte(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_ICMP_CODE: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv4Code().setValue(ICMPv4Code.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv4Code().setValue(ICMPv4Code.of(Short.parseShort(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIcmpv4Code() + .setValue(ICMPv4Code.of(ParseUtils.parseHexOrDecShort(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_ICMP_TYPE: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv4Type().setValue(ICMPv4Type.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv4Type().setValue(ICMPv4Type.of(Short.parseShort(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIcmpv4Type() + .setValue(ICMPv4Type.of(ParseUtils.parseHexOrDecShort(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_ICMPV6_CODE: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv6Code().setValue(U8.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv6Code().setValue(U8.of(Short.parseShort(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIcmpv6Code() + .setValue(U8.of(ParseUtils.parseHexOrDecShort(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_ICMPV6_TYPE: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv6Type().setValue(U8.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIcmpv6Type().setValue(U8.of(Short.parseShort(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIcmpv6Type() + .setValue(U8.of(ParseUtils.parseHexOrDecShort(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_NW_PROTO: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpProto().setValue(IpProtocol.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpProto().setValue(IpProtocol.of(Short.parseShort(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIpProto() + .setValue(IpProtocol.of(ParseUtils.parseHexOrDecShort(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_NW_SRC: a = f.actions().buildSetField() @@ -692,122 +655,110 @@ public class ActionUtils { .build(); break; case MatchUtils.STR_IPV6_FLOW_LABEL: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpv6Flabel().setValue(IPv6FlowLabel.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpv6Flabel().setValue(IPv6FlowLabel.of(Integer.parseInt(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIpv6Flabel() + .setValue(IPv6FlowLabel.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_NW_ECN: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpEcn().setValue(IpEcn.of(Byte.parseByte(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpEcn().setValue(IpEcn.of(Byte.parseByte(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIpEcn() + .setValue(IpEcn.of(ParseUtils.parseHexOrDecByte(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_NW_DSCP: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpDscp().setValue(IpDscp.of(Byte.parseByte(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildIpDscp().setValue(IpDscp.of(Byte.parseByte(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildIpDscp() + .setValue(IpDscp.of(ParseUtils.parseHexOrDecByte(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_SCTP_SRC: a = f.actions().buildSetField() - .setField(f.oxms().buildSctpSrc().setValue(TransportPort.of(Integer.parseInt(actionData[1]))).build()) + .setField(f.oxms().buildSctpSrc() + .setValue(TransportPort.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) .build(); break; case MatchUtils.STR_SCTP_DST: a = f.actions().buildSetField() - .setField(f.oxms().buildSctpDst().setValue(TransportPort.of(Integer.parseInt(actionData[1]))).build()) + .setField(f.oxms().buildSctpDst() + .setValue(TransportPort.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) .build(); break; case MatchUtils.STR_TCP_SRC: a = f.actions().buildSetField() - .setField(f.oxms().buildTcpSrc().setValue(TransportPort.of(Integer.parseInt(actionData[1]))).build()) + .setField(f.oxms().buildTcpSrc() + .setValue(TransportPort.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) .build(); break; case MatchUtils.STR_TCP_DST: a = f.actions().buildSetField() - .setField(f.oxms().buildTcpDst().setValue(TransportPort.of(Integer.parseInt(actionData[1]))).build()) + .setField(f.oxms().buildTcpDst() + .setValue(TransportPort.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) .build(); break; case MatchUtils.STR_UDP_SRC: a = f.actions().buildSetField() - .setField(f.oxms().buildUdpSrc().setValue(TransportPort.of(Integer.parseInt(actionData[1]))).build()) + .setField(f.oxms().buildUdpSrc() + .setValue(TransportPort.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) .build(); break; case MatchUtils.STR_UDP_DST: a = f.actions().buildSetField() - .setField(f.oxms().buildUdpDst().setValue(TransportPort.of(Integer.parseInt(actionData[1]))).build()) + .setField(f.oxms().buildUdpDst() + .setValue(TransportPort.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) .build(); break; case MatchUtils.STR_MPLS_LABEL: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildMplsLabel().setValue(U32.of(Long.parseLong(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildMplsLabel().setValue(U32.of(Long.parseLong(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildMplsLabel() + .setValue(U32.of(ParseUtils.parseHexOrDecLong(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_MPLS_TC: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildMplsTc().setValue(U8.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildMplsTc().setValue(U8.of(Short.parseShort(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildMplsTc() + .setValue(U8.of(ParseUtils.parseHexOrDecShort(actionData[1]))) + .build()) + .build(); break; case MatchUtils.STR_MPLS_BOS: a = f.actions().buildSetField() - .setField(f.oxms().buildMplsBos().setValue(OFBooleanValue.of(Boolean.parseBoolean(actionData[1]))).build()) // interprets anything other than "true" as false + .setField(f.oxms().buildMplsBos() + .setValue(OFBooleanValue.of(Boolean.parseBoolean(actionData[1]))) + .build()) // interprets anything other than "true" as false .build(); break; case MatchUtils.STR_METADATA: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildMetadata().setValue(OFMetadata.of(U64.of(Long.parseLong(actionData[1].replaceFirst("0x", ""), 16)))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildMetadata().setValue(OFMetadata.of(U64.of(Long.parseLong(actionData[1])))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildMetadata() + .setValue(OFMetadata.of(U64.of(ParseUtils.parseHexOrDecLong(actionData[1])))) + .build()) + .build(); break; case MatchUtils.STR_ACTSET_OUTPUT: - a = f.actions().buildSetField() - .setField(f.oxms().buildActsetOutput().setValue(portFromString(actionData[1])).build()) - .build(); - + a = f.actions().buildSetField() + .setField(f.oxms().buildActsetOutput() + .setValue(portFromString(actionData[1])) + .build()) + .build(); + break; case MatchUtils.STR_TCP_FLAGS: - if (actionData[1].startsWith("0x")) { - a = f.actions().buildSetField() - .setField(f.oxms().buildTcpFlags().setValue(U16.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) - .build(); - } else { - a = f.actions().buildSetField() - .setField(f.oxms().buildTcpFlags().setValue(U16.of(Integer.parseInt(actionData[1]))).build()) - .build(); - } + a = f.actions().buildSetField() + .setField(f.oxms().buildTcpFlags() + .setValue(U16.of(ParseUtils.parseHexOrDecInt(actionData[1]))) + .build()) + .build(); break; default: log.error("Unexpected OF1.2+ setfield '{}'", actionData); @@ -815,73 +766,37 @@ public class ActionUtils { } break; case STR_GROUP: - if (pair.startsWith("0x")) { - a = f.actions().buildGroup() - .setGroup(OFGroup.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) - .build(); - } else { - a = f.actions().buildGroup() - .setGroup(OFGroup.of(Integer.parseInt(pair))) - .build(); - } + a = f.actions().buildGroup() + .setGroup(GroupUtils.groupIdFromString(pair)) + .build(); break; case STR_MPLS_LABEL_SET: - if (pair.startsWith("0x")) { - a = f.actions().buildSetMplsLabel() - .setMplsLabel(Long.parseLong(pair.replaceFirst("0x", ""), 16)) - .build(); - } else { - a = f.actions().buildSetMplsLabel() - .setMplsLabel(Long.parseLong(pair)) - .build(); - } + a = f.actions().buildSetMplsLabel() + .setMplsLabel(ParseUtils.parseHexOrDecLong(pair)) + .build(); break; case STR_MPLS_POP: - if (pair.startsWith("0x")) { - a = f.actions().buildPopMpls() - .setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) - .build(); - } else { - a = f.actions().buildPopMpls() - .setEthertype(EthType.of(Integer.parseInt(pair))) - .build(); - } + a = f.actions().buildPopMpls() + .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair))) + .build(); break; case STR_MPLS_PUSH: - if (pair.startsWith("0x")) { - a = f.actions().buildPushMpls() - .setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) - .build(); - } else { - a = f.actions().buildPushMpls() - .setEthertype(EthType.of(Integer.parseInt(pair))) - .build(); - } + a = f.actions().buildPushMpls() + .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair))) + .build(); break; case STR_MPLS_TC_SET: - if (pair.startsWith("0x")) { - a = f.actions().buildSetMplsTc() - .setMplsTc(Short.parseShort(pair.replaceFirst("0x", ""), 16)) - .build(); - } else { - a = f.actions().buildSetMplsTc() - .setMplsTc(Short.parseShort(pair)) - .build(); - } + a = f.actions().buildSetMplsTc() + .setMplsTc(ParseUtils.parseHexOrDecShort(pair)) + .build(); break; case STR_MPLS_TTL_DEC: a = f.actions().decMplsTtl(); break; case STR_MPLS_TTL_SET: - if (pair.startsWith("0x")) { - a = f.actions().buildSetMplsTtl() - .setMplsTtl(Short.parseShort(pair.replaceFirst("0x", ""), 16)) - .build(); - } else { - a = f.actions().buildSetMplsTtl() - .setMplsTtl(Short.parseShort(pair)) - .build(); - } + a = f.actions().buildSetMplsTtl() + .setMplsTtl(ParseUtils.parseHexOrDecShort(pair)) + .build(); break; case STR_NW_TOS_SET: a = decode_set_tos_bits(pair, v); // should only be used by OF1.0 @@ -893,54 +808,30 @@ public class ActionUtils { a = decode_set_dst_ip(pair, v); break; case STR_NW_ECN_SET: // loxi does not support DSCP set for OF1.1 - if (pair.startsWith("0x")) { - a = f.actions().buildSetNwEcn() - .setNwEcn(IpEcn.of(Byte.parseByte(pair.replaceFirst("0x", ""), 16))) - .build(); - } else { - a = f.actions().buildSetNwEcn() - .setNwEcn(IpEcn.of(Byte.parseByte(pair))) - .build(); - } + a = f.actions().buildSetNwEcn() + .setNwEcn(IpEcn.of(ParseUtils.parseHexOrDecByte(pair))) + .build(); break; case STR_NW_TTL_DEC: a = f.actions().decNwTtl(); break; case STR_NW_TTL_SET: - if (pair.startsWith("0x")) { - a = f.actions().buildSetNwTtl() - .setNwTtl(Short.parseShort(pair.replaceFirst("0x", ""), 16)) - .build(); - } else { - a = f.actions().buildSetNwTtl() - .setNwTtl(Short.parseShort(pair)) - .build(); - } + a = f.actions().buildSetNwTtl() + .setNwTtl(ParseUtils.parseHexOrDecShort(pair)) + .build(); break; case STR_PBB_POP: a = f.actions().popPbb(); break; case STR_PBB_PUSH: - if (pair.startsWith("0x")) { - a = f.actions().buildPushPbb() - .setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) - .build(); - } else { - a = f.actions().buildPushPbb() - .setEthertype(EthType.of(Integer.parseInt(pair))) - .build(); - } + a = f.actions().buildPushPbb() + .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair))) + .build(); break; case STR_QUEUE_SET: - if (pair.startsWith("0x")) { - a = f.actions().buildSetQueue() - .setQueueId(Long.parseLong(pair.replaceFirst("0x", ""), 16)) - .build(); - } else { - a = f.actions().buildSetQueue() - .setQueueId(Long.parseLong(pair)) - .build(); - } + a = f.actions().buildSetQueue() + .setQueueId(ParseUtils.parseHexOrDecLong(pair)) + .build(); break; case STR_TP_SRC_SET: a = decode_set_src_port(pair, v); @@ -958,15 +849,9 @@ public class ActionUtils { a = f.actions().popVlan(); break; case STR_VLAN_PUSH: - if (pair.startsWith("0x")) { - a = f.actions().buildPushVlan() - .setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) - .build(); - } else { - a = f.actions().buildPushVlan() - .setEthertype(EthType.of(Integer.parseInt(pair))) - .build(); - } + a = f.actions().buildPushVlan() + .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair))) + .build(); break; case STR_VLAN_STRIP: a = f.actions().stripVlan(); @@ -978,15 +863,9 @@ public class ActionUtils { a = decode_set_vlan_priority(pair, v); break; case STR_METER: - if (pair.startsWith("0x")) { - a = f.actions().buildMeter() - .setMeterId(Long.parseLong(pair.replaceFirst("0x", ""), 16)) - .build(); - } else { - a = f.actions().buildMeter() - .setMeterId(Long.parseLong(pair)) - .build(); - } + a = f.actions().buildMeter() + .setMeterId(ParseUtils.parseHexOrDecLong(pair)) + .build(); break; case STR_FIELD_COPY: a = (OFAction) copyFieldFromJson(pair, f.getVersion()); @@ -1177,7 +1056,7 @@ public class ActionUtils { public static OFPort portFromString(String s) { return MatchUtils.portFromString(s); } - + public static String portToString(OFPort p) { return MatchUtils.portToString(p); } @@ -1234,7 +1113,7 @@ public class ActionUtils { int queueid = 0; if (n.group(2) != null) { try { - queueid = get_int(n.group(2)); + queueid = ParseUtils.parseHexOrDecInt(n.group(2)); } catch (NumberFormatException e) { log.debug("Invalid queue-id in: '{}' (error ignored)", actionToDecode); @@ -1268,7 +1147,7 @@ public class ActionUtils { if (n.matches()) { if (n.group(1) != null) { try { - VlanVid vlanid = VlanVid.ofVlan(get_short(n.group(1))); + VlanVid vlanid = VlanVid.ofVlan(ParseUtils.parseHexOrDecShort(n.group(1))); OFActionSetVlanVid a = OFFactories.getFactory(version).actions().buildSetVlanVid() .setVlanVid(vlanid) .build(); @@ -1303,7 +1182,7 @@ public class ActionUtils { if (n.group(1) != null) { try { OFActionSetVlanPcp a = OFFactories.getFactory(version).actions().buildSetVlanPcp() - .setVlanPcp(VlanPcp.of(get_byte(n.group(1)))) + .setVlanPcp(VlanPcp.of(ParseUtils.parseHexOrDecByte(n.group(1)))) .build(); log.debug("action {}", a); return a; @@ -1382,7 +1261,7 @@ public class ActionUtils { if (n.group(1) != null) { try { OFActionSetNwTos a = OFFactories.getFactory(version).actions().buildSetNwTos() - .setNwTos(get_byte(n.group(1))) + .setNwTos(ParseUtils.parseHexOrDecByte(n.group(1))) .build(); log.debug("action {}", a); return a; @@ -1489,31 +1368,4 @@ public class ActionUtils { return null; } } - - /** - * Parse int as decimal, hex (start with 0x or #) or octal (starts with 0) - * @param str - * @return - */ - private static int get_int(String str) { - return Integer.parseInt(str); - } - - /** - * Parse short as decimal, hex (start with 0x or #) or octal (starts with 0) - * @param str - * @return - */ - private static short get_short(String str) { - return (short) Integer.parseInt(str); - } - - /** - * Parse byte as decimal, hex (start with 0x or #) or octal (starts with 0) - * @param str - * @return - */ - private static byte get_byte(String str) { - return (byte) Integer.parseInt(str); - } } \ No newline at end of file diff --git a/src/main/java/net/floodlightcontroller/util/GroupUtils.java b/src/main/java/net/floodlightcontroller/util/GroupUtils.java index 36bbab38a965f25489e4d4e1eb9194ecd91810a6..ae029756ef0369bf94861b75bf3965aad92316c1 100644 --- a/src/main/java/net/floodlightcontroller/util/GroupUtils.java +++ b/src/main/java/net/floodlightcontroller/util/GroupUtils.java @@ -118,7 +118,7 @@ public class GroupUtils { } else if (s.equals(GROUP_ID_MAX)) { return OFGroup.MAX; } else { - return OFGroup.of(s.startsWith("0x") ? Integer.parseInt(s.replaceFirst("0x", ""), 16) : Integer.parseInt(s)); + return OFGroup.of(ParseUtils.parseHexOrDecInt(s)); } } catch (Exception e) { log.error("Could not parse group ID {}", s); @@ -332,7 +332,7 @@ public class GroupUtils { String value = jp.getText().toLowerCase().trim(); switch (key) { case BUCKET_ID: - bucketId = value.startsWith("0x") ? Integer.parseInt(value.replaceFirst("0x", ""), 16) : Integer.parseInt(value); + bucketId = ParseUtils.parseHexOrDecInt(value); break; case BUCKET_WATCH_GROUP: b.setWatchGroup(groupIdFromString(key)); @@ -341,7 +341,7 @@ public class GroupUtils { b.setWatchPort(MatchUtils.portFromString(key)); break; case BUCKET_WEIGHT: - b.setWeight(value.startsWith("0x") ? Integer.parseInt(value.replaceFirst("0x", ""), 16) : Integer.parseInt(value)); + b.setWeight(ParseUtils.parseHexOrDecInt(value)); break; case BUCKET_ACTIONS: b.setActions(ActionUtils.fromString(value, b.getVersion())); // TODO update to JSON diff --git a/src/main/java/net/floodlightcontroller/util/InstructionUtils.java b/src/main/java/net/floodlightcontroller/util/InstructionUtils.java index af98df032495f692a9318fd4ba335a1a13c4711d..281325867ede8929247775559d78d853fddc8189 100644 --- a/src/main/java/net/floodlightcontroller/util/InstructionUtils.java +++ b/src/main/java/net/floodlightcontroller/util/InstructionUtils.java @@ -150,11 +150,7 @@ public class InstructionUtils { OFInstructionGotoTable.Builder ib = OFFactories.getFactory(fmb.getVersion()).instructions().buildGotoTable(); // Get the table ID - if (inst.startsWith("0x")) { - ib.setTableId(TableId.of(Integer.parseInt(inst.replaceFirst("0x", ""), 16))); - } else { - ib.setTableId(TableId.of(Integer.parseInt(inst))).build(); - } + ib.setTableId(TableId.of(ParseUtils.parseHexOrDecInt(inst))); log.debug("Appending GotoTable instruction: {}", ib.build()); appendInstruction(fmb, ib.build()); @@ -209,19 +205,11 @@ public class InstructionUtils { } // Get the metadata - if (keyValue[0].startsWith("0x")) { - ib.setMetadata(U64.of(Long.valueOf(keyValue[0].replaceFirst("0x", ""), 16))); - } else { - ib.setMetadata(U64.of(Long.valueOf(keyValue[0]))); - } + ib.setMetadata(U64.of(ParseUtils.parseHexOrDecLong(keyValue[0]))); // Get the optional mask if (keyValue.length == 2) { - if (keyValue[1].startsWith("0x")) { - ib.setMetadataMask(U64.of(Long.valueOf(keyValue[1].replaceFirst("0x", ""), 16))); - } else { - ib.setMetadataMask(U64.of(Long.valueOf(keyValue[1]))); - } + ib.setMetadataMask(U64.of(ParseUtils.parseHexOrDecLong(keyValue[1]))); } else { ib.setMetadataMask(U64.NO_MASK); } @@ -370,11 +358,7 @@ public class InstructionUtils { OFInstructionMeter.Builder ib = OFFactories.getFactory(fmb.getVersion()).instructions().buildMeter(); - if (inst.startsWith("0x")) { - ib.setMeterId(Long.valueOf(inst.replaceFirst("0x", ""), 16)); - } else { - ib.setMeterId(Long.valueOf(inst)); - } + ib.setMeterId(ParseUtils.parseHexOrDecLong(inst)); log.debug("Appending (Goto)Meter instruction: {}", ib.build()); appendInstruction(fmb, ib.build()); @@ -507,7 +491,7 @@ public class InstructionUtils { } break; case "value": - value = U64.of(Long.parseLong(jp.getText())); + value = U64.of(ParseUtils.parseHexOrDecLong(jp.getText())); break; default: log.warn("Unexpected OXS threshold key {}", jp.getCurrentName()); diff --git a/src/main/java/net/floodlightcontroller/util/MatchUtils.java b/src/main/java/net/floodlightcontroller/util/MatchUtils.java index b11a9ed061096b97c7dc5dc608651a8145770c24..8bcd39226a1f9e5e3126b5cfeb00253549a95fc5 100644 --- a/src/main/java/net/floodlightcontroller/util/MatchUtils.java +++ b/src/main/java/net/floodlightcontroller/util/MatchUtils.java @@ -635,27 +635,27 @@ public class MatchUtils { break; case STR_DL_TYPE: if (dataMask.length == 1) { - mb.setExact(MatchField.ETH_TYPE, EthType.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.ETH_TYPE, EthType.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.ETH_TYPE, EthType.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - EthType.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.ETH_TYPE, EthType.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + EthType.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } break; case STR_DL_VLAN: if (dataMask.length == 1) { - mb.setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofRawVid(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofRawVid(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { mb.setMasked(MatchField.VLAN_VID, OFVlanVidMatchWithMask.of( - OFVlanVidMatch.ofRawVid(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0])), - OFVlanVidMatch.ofRawVid(dataMask[1].contains("0x") ? Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[1])))); + OFVlanVidMatch.ofRawVid(ParseUtils.parseHexOrDecShort(dataMask[0])), + OFVlanVidMatch.ofRawVid(ParseUtils.parseHexOrDecShort(dataMask[1])))); } break; case STR_DL_VLAN_PCP: if (dataMask.length == 1) { - mb.setExact(MatchField.VLAN_PCP, VlanPcp.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0])))); + mb.setExact(MatchField.VLAN_PCP, VlanPcp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0])))); } else { - mb.setMasked(MatchField.VLAN_PCP, VlanPcp.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0]))), - VlanPcp.of(dataMask[1].contains("0x") ? U8.t(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[1])))); + mb.setMasked(MatchField.VLAN_PCP, VlanPcp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0]))), + VlanPcp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[1])))); } break; case STR_NW_DST: /* Only accept dotted-decimal for IPv4 addresses */ @@ -681,45 +681,45 @@ public class MatchUtils { throw new IllegalArgumentException("OF Version incompatible"); } if (dataMask.length == 1) { - mb.setExact(MatchField.IPV6_FLABEL, IPv6FlowLabel.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.IPV6_FLABEL, IPv6FlowLabel.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.IPV6_FLABEL, IPv6FlowLabel.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - IPv6FlowLabel.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.IPV6_FLABEL, IPv6FlowLabel.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + IPv6FlowLabel.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } break; case STR_NW_PROTO: if (dataMask.length == 1) { - mb.setExact(MatchField.IP_PROTO, IpProtocol.of(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.IP_PROTO, IpProtocol.of(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { - mb.setMasked(MatchField.IP_PROTO, IpProtocol.of(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0])), - IpProtocol.of(dataMask[1].contains("0x") ? Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[1]))); + mb.setMasked(MatchField.IP_PROTO, IpProtocol.of(ParseUtils.parseHexOrDecShort(dataMask[0])), + IpProtocol.of(ParseUtils.parseHexOrDecShort(dataMask[1]))); } break; case STR_NW_TOS: if (dataMask.length == 1) { - mb.setExact(MatchField.IP_ECN, IpEcn.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0])))); - mb.setExact(MatchField.IP_DSCP, IpDscp.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0])))); + mb.setExact(MatchField.IP_ECN, IpEcn.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0])))); + mb.setExact(MatchField.IP_DSCP, IpDscp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0])))); } else { - mb.setMasked(MatchField.IP_ECN, IpEcn.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0]))), - IpEcn.of(dataMask[1].contains("0x") ? U8.t(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[1])))); - mb.setMasked(MatchField.IP_DSCP, IpDscp.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0]))), - IpDscp.of(dataMask[1].contains("0x") ? U8.t(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[1])))); + mb.setMasked(MatchField.IP_ECN, IpEcn.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0]))), + IpEcn.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[1])))); + mb.setMasked(MatchField.IP_DSCP, IpDscp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0]))), + IpDscp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[1])))); } break; case STR_NW_ECN: if (dataMask.length == 1) { - mb.setExact(MatchField.IP_ECN, IpEcn.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0])))); + mb.setExact(MatchField.IP_ECN, IpEcn.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0])))); } else { - mb.setMasked(MatchField.IP_ECN, IpEcn.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0]))), - IpEcn.of(dataMask[1].contains("0x") ? U8.t(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[1])))); + mb.setMasked(MatchField.IP_ECN, IpEcn.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0]))), + IpEcn.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[1])))); } break; case STR_NW_DSCP: if (dataMask.length == 1) { - mb.setExact(MatchField.IP_DSCP, IpDscp.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0])))); + mb.setExact(MatchField.IP_DSCP, IpDscp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0])))); } else { - mb.setMasked(MatchField.IP_DSCP, IpDscp.of(dataMask[0].contains("0x") ? U8.t(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[0]))), - IpDscp.of(dataMask[1].contains("0x") ? U8.t(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.t(Short.parseShort(dataMask[1])))); + mb.setMasked(MatchField.IP_DSCP, IpDscp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[0]))), + IpDscp.of(U8.t(ParseUtils.parseHexOrDecShort(dataMask[1])))); } break; case STR_SCTP_DST: // for transport ports, if we don't know the transport protocol yet, postpone parsing this [key, value] pair until we know. Put it at the back of the queue. @@ -727,10 +727,10 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else { if (dataMask.length == 1) { - mb.setExact(MatchField.SCTP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.SCTP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.SCTP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.SCTP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -739,10 +739,10 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else { if (dataMask.length == 1) { - mb.setExact(MatchField.SCTP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.SCTP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.SCTP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.SCTP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -751,10 +751,10 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else { if (dataMask.length == 1) { - mb.setExact(MatchField.UDP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.UDP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.UDP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.UDP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -763,10 +763,10 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else { if (dataMask.length == 1) { - mb.setExact(MatchField.UDP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.UDP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.UDP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.UDP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -775,10 +775,10 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else { if (dataMask.length == 1) { - mb.setExact(MatchField.TCP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.TCP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.TCP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.TCP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -787,10 +787,10 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else { if (dataMask.length == 1) { - mb.setExact(MatchField.TCP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.TCP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.TCP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.TCP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -799,24 +799,24 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else if (ipProto == IpProtocol.TCP){ if (dataMask.length == 1) { - mb.setExact(MatchField.TCP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.TCP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.TCP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.TCP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } else if (ipProto == IpProtocol.UDP){ if (dataMask.length == 1) { - mb.setExact(MatchField.UDP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.UDP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.UDP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.UDP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } else if (ipProto == IpProtocol.SCTP){ if (dataMask.length == 1) { - mb.setExact(MatchField.SCTP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.SCTP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.SCTP_DST, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.SCTP_DST, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; @@ -825,41 +825,41 @@ public class MatchUtils { llValues.add(key_value); // place it back if we can't proceed yet } else if (ipProto == IpProtocol.TCP){ if (dataMask.length == 1) { - mb.setExact(MatchField.TCP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.TCP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.TCP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.TCP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } else if (ipProto == IpProtocol.UDP){ if (dataMask.length == 1) { - mb.setExact(MatchField.UDP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.UDP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.UDP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.UDP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } else if (ipProto == IpProtocol.SCTP){ if (dataMask.length == 1) { - mb.setExact(MatchField.SCTP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.SCTP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.SCTP_SRC, TransportPort.of(dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0])), - TransportPort.of(dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.SCTP_SRC, TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + TransportPort.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } } break; case STR_ICMP_TYPE: if (dataMask.length == 1) { - mb.setExact(MatchField.ICMPV4_TYPE, ICMPv4Type.of(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.ICMPV4_TYPE, ICMPv4Type.of(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { - mb.setMasked(MatchField.ICMPV4_TYPE, ICMPv4Type.of(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0])), - ICMPv4Type.of(dataMask[1].contains("0x") ? Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[1]))); + mb.setMasked(MatchField.ICMPV4_TYPE, ICMPv4Type.of(ParseUtils.parseHexOrDecShort(dataMask[0])), + ICMPv4Type.of(ParseUtils.parseHexOrDecShort(dataMask[1]))); } break; case STR_ICMP_CODE: if (dataMask.length == 1) { - mb.setExact(MatchField.ICMPV4_CODE, ICMPv4Code.of(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.ICMPV4_CODE, ICMPv4Code.of(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { - mb.setMasked(MatchField.ICMPV4_CODE, ICMPv4Code.of(dataMask[0].contains("0x") ? Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[0])), - ICMPv4Code.of(dataMask[1].contains("0x") ? Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16) : Short.parseShort(dataMask[1]))); + mb.setMasked(MatchField.ICMPV4_CODE, ICMPv4Code.of(ParseUtils.parseHexOrDecShort(dataMask[0])), + ICMPv4Code.of(ParseUtils.parseHexOrDecShort(dataMask[1]))); } break; case STR_ICMPV6_TYPE: @@ -867,10 +867,10 @@ public class MatchUtils { throw new IllegalArgumentException("OF Version incompatible"); } if (dataMask.length == 1) { - mb.setExact(MatchField.ICMPV6_TYPE, dataMask[0].contains("0x") ? U8.of(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.ICMPV6_TYPE, U8.of(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { - mb.setMasked(MatchField.ICMPV6_TYPE, dataMask[0].contains("0x") ? U8.of(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[0])), - dataMask[1].contains("0x") ? U8.of(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[1]))); + mb.setMasked(MatchField.ICMPV6_TYPE, U8.of(ParseUtils.parseHexOrDecShort(dataMask[0])), + U8.of(ParseUtils.parseHexOrDecShort(dataMask[1]))); } break; case STR_ICMPV6_CODE: @@ -878,10 +878,10 @@ public class MatchUtils { throw new IllegalArgumentException("OF Version incompatible"); } if (dataMask.length == 1) { - mb.setExact(MatchField.ICMPV6_CODE, dataMask[0].contains("0x") ? U8.of(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.ICMPV6_CODE, U8.of(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { - mb.setMasked(MatchField.ICMPV6_CODE, dataMask[0].contains("0x") ? U8.of(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[0])), - dataMask[1].contains("0x") ? U8.of(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[1]))); + mb.setMasked(MatchField.ICMPV6_CODE, U8.of(ParseUtils.parseHexOrDecShort(dataMask[0])), + U8.of(ParseUtils.parseHexOrDecShort(dataMask[1]))); } break; case STR_IPV6_ND_SLL: @@ -912,18 +912,18 @@ public class MatchUtils { break; case STR_IPV6_EXTHDR: if (dataMask.length == 1) { - mb.setExact(MatchField.IPV6_EXTHDR, dataMask[0].contains("0x") ? U16.of(Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16)) : U16.of(Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.IPV6_EXTHDR, U16.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.IPV6_EXTHDR, dataMask[0].contains("0x") ? U16.of(Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16)) : U16.of(Integer.parseInt(dataMask[0])), - dataMask[1].contains("0x") ? U16.of(Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16)) : U16.of(Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.IPV6_EXTHDR, U16.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + U16.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } break; case STR_ARP_OPCODE: if (dataMask.length == 1) { - mb.setExact(MatchField.ARP_OP, dataMask[0].contains("0x") ? ArpOpcode.of(Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16)) : ArpOpcode.of(Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.ARP_OP, ArpOpcode.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.ARP_OP, dataMask[0].contains("0x") ? ArpOpcode.of(Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16)) : ArpOpcode.of(Integer.parseInt(dataMask[0])), - dataMask[1].contains("0x") ? ArpOpcode.of(Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16)) : ArpOpcode.of(Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.ARP_OP, ArpOpcode.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + ArpOpcode.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } break; case STR_ARP_SHA: @@ -948,37 +948,37 @@ public class MatchUtils { break; case STR_MPLS_LABEL: if (dataMask.length == 1) { - mb.setExact(MatchField.MPLS_LABEL, dataMask[0].contains("0x") ? U32.of(Long.parseLong(dataMask[0].replaceFirst("0x", ""), 16)) : U32.of(Long.parseLong(dataMask[0]))); + mb.setExact(MatchField.MPLS_LABEL, U32.of(ParseUtils.parseHexOrDecLong(dataMask[0]))); } else { - mb.setMasked(MatchField.MPLS_LABEL, dataMask[0].contains("0x") ? U32.of(Long.parseLong(dataMask[0].replaceFirst("0x", ""), 16)) : U32.of(Long.parseLong(dataMask[0])), - dataMask[1].contains("0x") ? U32.of(Long.parseLong(dataMask[1].replaceFirst("0x", ""), 16)) : U32.of(Long.parseLong(dataMask[1]))); + mb.setMasked(MatchField.MPLS_LABEL, U32.of(ParseUtils.parseHexOrDecLong(dataMask[0])), + U32.of(ParseUtils.parseHexOrDecLong(dataMask[1]))); } break; case STR_MPLS_TC: if (dataMask.length == 1) { - mb.setExact(MatchField.MPLS_TC, dataMask[0].contains("0x") ? U8.of(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[0]))); + mb.setExact(MatchField.MPLS_TC, U8.of(ParseUtils.parseHexOrDecShort(dataMask[0]))); } else { - mb.setMasked(MatchField.MPLS_TC, dataMask[0].contains("0x") ? U8.of(Short.parseShort(dataMask[0].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[0])), - dataMask[1].contains("0x") ? U8.of(Short.parseShort(dataMask[1].replaceFirst("0x", ""), 16)) : U8.of(Short.parseShort(dataMask[1]))); + mb.setMasked(MatchField.MPLS_TC, U8.of(ParseUtils.parseHexOrDecShort(dataMask[0])), + U8.of(ParseUtils.parseHexOrDecShort(dataMask[1]))); } break; case STR_MPLS_BOS: - mb.setExact(MatchField.MPLS_BOS, key_value[1].equalsIgnoreCase("true") ? OFBooleanValue.TRUE : OFBooleanValue.FALSE); + mb.setExact(MatchField.MPLS_BOS, OFBooleanValue.of(Boolean.parseBoolean(key_value[1]))); break; case STR_METADATA: if (dataMask.length == 1) { - mb.setExact(MatchField.METADATA, dataMask[0].contains("0x") ? OFMetadata.ofRaw(Long.parseLong(dataMask[0].replaceFirst("0x", ""), 16)) : OFMetadata.ofRaw(Long.parseLong(dataMask[0]))); + mb.setExact(MatchField.METADATA, OFMetadata.ofRaw(ParseUtils.parseHexOrDecLong(dataMask[0]))); } else { - mb.setMasked(MatchField.METADATA, dataMask[0].contains("0x") ? OFMetadata.ofRaw(Long.parseLong(dataMask[0].replaceFirst("0x", ""), 16)) : OFMetadata.ofRaw(Long.parseLong(dataMask[0])), - dataMask[1].contains("0x") ? OFMetadata.ofRaw(Long.parseLong(dataMask[1].replaceFirst("0x", ""), 16)) : OFMetadata.ofRaw(Long.parseLong(dataMask[1]))); + mb.setMasked(MatchField.METADATA, OFMetadata.ofRaw(ParseUtils.parseHexOrDecLong(dataMask[0])), + OFMetadata.ofRaw(ParseUtils.parseHexOrDecLong(dataMask[1]))); } break; case STR_TUNNEL_ID: if (dataMask.length == 1) { - mb.setExact(MatchField.TUNNEL_ID, dataMask[0].contains("0x") ? U64.of(Long.parseLong(dataMask[0].replaceFirst("0x", ""), 16)) : U64.of(Long.parseLong(dataMask[0]))); + mb.setExact(MatchField.TUNNEL_ID, U64.of(ParseUtils.parseHexOrDecLong(dataMask[0]))); } else { - mb.setMasked(MatchField.TUNNEL_ID, dataMask[0].contains("0x") ? U64.of(Long.parseLong(dataMask[0].replaceFirst("0x", ""), 16)) : U64.of(Long.parseLong(dataMask[0])), - dataMask[1].contains("0x") ? U64.of(Long.parseLong(dataMask[1].replaceFirst("0x", ""), 16)) : U64.of(Long.parseLong(dataMask[1]))); + mb.setMasked(MatchField.TUNNEL_ID, U64.of(ParseUtils.parseHexOrDecLong(dataMask[0])), + U64.of(ParseUtils.parseHexOrDecLong(dataMask[1]))); } break; case STR_TUNNEL_IPV4_SRC: @@ -1009,10 +1009,10 @@ public class MatchUtils { break; case STR_TCP_FLAGS: if (dataMask.length == 1) { - mb.setExact(MatchField.TCP_FLAGS, dataMask[0].contains("0x") ? U16.of(Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16)) : U16.of(Integer.parseInt(dataMask[0]))); + mb.setExact(MatchField.TCP_FLAGS, U16.of(ParseUtils.parseHexOrDecInt(dataMask[0]))); } else { - mb.setMasked(MatchField.TCP_FLAGS, dataMask[0].contains("0x") ? U16.of(Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16)) : U16.of(Integer.parseInt(dataMask[0])), - dataMask[1].contains("0x") ? U16.of(Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16)) : U16.of(Integer.parseInt(dataMask[1]))); + mb.setMasked(MatchField.TCP_FLAGS, U16.of(ParseUtils.parseHexOrDecInt(dataMask[0])), + U16.of(ParseUtils.parseHexOrDecInt(dataMask[1]))); } break; case STR_ACTSET_OUTPUT: @@ -1032,10 +1032,8 @@ public class MatchUtils { if (dataMask.length != 2) { log.error("Ignoring invalid PACKET_TYPE OXM. Must specify namespace and namespace type in the form 'ns/nstype'"); } else { - mb.setExact(MatchField.PACKET_TYPE, - PacketType.of( - dataMask[0].contains("0x") ? Integer.parseInt(dataMask[0].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[0]), - dataMask[1].contains("0x") ? Integer.parseInt(dataMask[1].replaceFirst("0x", ""), 16) : Integer.parseInt(dataMask[1]))); + mb.setExact(MatchField.PACKET_TYPE, PacketType.of(ParseUtils.parseHexOrDecInt(dataMask[0]), + ParseUtils.parseHexOrDecInt(dataMask[1]))); } break; default: @@ -1075,9 +1073,7 @@ public class MatchUtils { } try { - return OFPort.of(U32.of(s.contains("0x") ? - Long.parseLong(s.replaceFirst("0x", ""), 16) : - Long.parseLong(s)).getRaw()); + return OFPort.of(U32.of(ParseUtils.parseHexOrDecLong(s)).getRaw()); } catch (NumberFormatException e) { log.error("Could not parse port '{}'", s); return null; diff --git a/src/main/java/net/floodlightcontroller/util/ParseUtils.java b/src/main/java/net/floodlightcontroller/util/ParseUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..c09e181ddd0a84c29d60b942ac75396405447bf5 --- /dev/null +++ b/src/main/java/net/floodlightcontroller/util/ParseUtils.java @@ -0,0 +1,131 @@ +package net.floodlightcontroller.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Simple class to assist in parsing hex or decimal primitive types + * from String to their respective types. + * + * Use this in place of e.g. Integer.decode() if you wish to avoid + * unnecessarily creating objects when you want just the primitive type. + * + * @author rizard + * + */ +public class ParseUtils { + private static final Logger log = LoggerFactory.getLogger(ParseUtils.class); + private static final byte exceptionReturnValue = 0; + + /** + * Parse an int from a String. + * + * Hex is expected to have a leading "0x". + * + * @throws NumberFormatException + * @param s + * @return + */ + public static int parseHexOrDecInt(String s) { + if (s == null) { + throw new IllegalArgumentException("String cannot be null"); + } + s = s.trim().toLowerCase(); + if (s.startsWith("0x")) { + return Integer.parseInt(s.replaceFirst("0x", ""), 16); + } else { + return Integer.parseInt(s); + } + } + + /** + * Parse a short from a String. + * + * Hex is expected to have a leading "0x". + * + * @throws NumberFormatException + * @param s + * @return + */ + public static short parseHexOrDecShort(String s) { + if (s == null) { + throw new IllegalArgumentException("String cannot be null"); + } + s = s.trim().toLowerCase(); + try { + if (s.startsWith("0x")) { + return Short.parseShort(s.replaceFirst("0x", ""), 16); + } else { + return Short.parseShort(s); + } + } catch (NumberFormatException e) { + log.error("Could not parse short {}. Returning default", s); + return exceptionReturnValue; + } + } + + /** + * Parse a long from a String. + * + * Hex is expected to have a leading "0x". + * + * @throws NumberFormatException + * @param s + * @return + */ + public static long parseHexOrDecLong(String s) { + if (s == null) { + throw new IllegalArgumentException("String cannot be null"); + } + s = s.trim().toLowerCase(); + if (s.startsWith("0x")) { + return Long.parseLong(s.replaceFirst("0x", ""), 16); + } else { + return Long.parseLong(s); + } + } + + /** + * Parse a byte from a String. + * + * Hex is expected to have a leading "0x". + * + * @throws NumberFormatException + * @param s + * @return + */ + public static byte parseHexOrDecByte(String s) { + if (s == null) { + throw new IllegalArgumentException("String cannot be null"); + } + s = s.trim().toLowerCase(); + if (s.startsWith("0x")) { + return Byte.parseByte(s.replaceFirst("0x", ""), 16); + } else { + return Byte.parseByte(s); + } + } + + /** + * Convert any number to a boolean, where any positive number is + * true and zero or negative number is false. The largest supported + * type is a Long. + * + * Hex is expected to have a leading "0x". + * + * @throws NumberFormatException + * @param s + * @return true or false + */ + public static boolean parseHexOrDecBool(String s) { + if (s == null) { + throw new IllegalArgumentException("String cannot be null"); + } + s = s.trim().toLowerCase(); + if (s.startsWith("0x")) { + return Long.parseLong(s.replaceFirst("0x", ""), 16) > 0; + } else { + return Long.parseLong(s) > 0; + } + } +}