Skip to content
Snippets Groups Projects
Commit c723c4f4 authored by Ryan Izard's avatar Ryan Izard
Browse files

Introduce ParseUtils as a cleaner way to parse hex or decimal strings to their...

Introduce ParseUtils as a cleaner way to parse hex or decimal strings to their primitive types. This is done instead of e.g. Integer.decode() in order to eliminiate the need to instantiate a new object wrapped around the primitive type if we're just going to use the primivitive type in the first place. Cleaned up all *Utils classes to use the new ParseUtils fuctions, which reduced a lot of duplicate code.
parent b368e88a
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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
......
......@@ -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());
......
This diff is collapsed.
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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment