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; ...@@ -94,7 +94,6 @@ import org.projectfloodlight.openflow.types.IpEcn;
import org.projectfloodlight.openflow.types.IpProtocol; import org.projectfloodlight.openflow.types.IpProtocol;
import org.projectfloodlight.openflow.types.MacAddress; import org.projectfloodlight.openflow.types.MacAddress;
import org.projectfloodlight.openflow.types.OFBooleanValue; import org.projectfloodlight.openflow.types.OFBooleanValue;
import org.projectfloodlight.openflow.types.OFGroup;
import org.projectfloodlight.openflow.types.OFMetadata; import org.projectfloodlight.openflow.types.OFMetadata;
import org.projectfloodlight.openflow.types.OFPort; import org.projectfloodlight.openflow.types.OFPort;
import org.projectfloodlight.openflow.types.OFVlanVidMatch; import org.projectfloodlight.openflow.types.OFVlanVidMatch;
...@@ -527,16 +526,12 @@ public class ActionUtils { ...@@ -527,16 +526,12 @@ public class ActionUtils {
} }
switch (actionData[0]) { switch (actionData[0]) {
case MatchUtils.STR_ARP_OPCODE: case MatchUtils.STR_ARP_OPCODE:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildArpOp()
.setField(f.oxms().buildArpOp().setValue(ArpOpcode.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(ArpOpcode.of(ParseUtils.parseHexOrDecInt(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildArpOp().setValue(ArpOpcode.of(Integer.parseInt(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_ARP_SHA: case MatchUtils.STR_ARP_SHA:
a = f.actions().buildSetField() a = f.actions().buildSetField()
...@@ -574,15 +569,11 @@ public class ActionUtils { ...@@ -574,15 +569,11 @@ public class ActionUtils {
.build(); .build();
break; break;
case MatchUtils.STR_DL_TYPE: case MatchUtils.STR_DL_TYPE:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildEthType()
.setField(f.oxms().buildEthType().setValue(EthType.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(EthType.of(ParseUtils.parseHexOrDecInt(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildEthType().setValue(EthType.of(Integer.parseInt(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_DL_SRC: case MatchUtils.STR_DL_SRC:
a = f.actions().buildSetField() a = f.actions().buildSetField()
...@@ -595,81 +586,53 @@ public class ActionUtils { ...@@ -595,81 +586,53 @@ public class ActionUtils {
.build(); .build();
break; break;
case MatchUtils.STR_DL_VLAN: case MatchUtils.STR_DL_VLAN:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildVlanVid()
.setField(f.oxms().buildVlanVid().setValue(OFVlanVidMatch.ofVlan(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(OFVlanVidMatch.ofVlan(ParseUtils.parseHexOrDecInt(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildVlanVid().setValue(OFVlanVidMatch.ofVlan(Integer.parseInt(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_DL_VLAN_PCP: case MatchUtils.STR_DL_VLAN_PCP:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildVlanPcp()
.setField(f.oxms().buildVlanPcp().setValue(VlanPcp.of(Byte.parseByte(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(VlanPcp.of(ParseUtils.parseHexOrDecByte(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildVlanPcp().setValue(VlanPcp.of(Byte.parseByte(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_ICMP_CODE: case MatchUtils.STR_ICMP_CODE:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIcmpv4Code()
.setField(f.oxms().buildIcmpv4Code().setValue(ICMPv4Code.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(ICMPv4Code.of(ParseUtils.parseHexOrDecShort(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIcmpv4Code().setValue(ICMPv4Code.of(Short.parseShort(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_ICMP_TYPE: case MatchUtils.STR_ICMP_TYPE:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIcmpv4Type()
.setField(f.oxms().buildIcmpv4Type().setValue(ICMPv4Type.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(ICMPv4Type.of(ParseUtils.parseHexOrDecShort(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIcmpv4Type().setValue(ICMPv4Type.of(Short.parseShort(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_ICMPV6_CODE: case MatchUtils.STR_ICMPV6_CODE:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIcmpv6Code()
.setField(f.oxms().buildIcmpv6Code().setValue(U8.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(U8.of(ParseUtils.parseHexOrDecShort(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIcmpv6Code().setValue(U8.of(Short.parseShort(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_ICMPV6_TYPE: case MatchUtils.STR_ICMPV6_TYPE:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIcmpv6Type()
.setField(f.oxms().buildIcmpv6Type().setValue(U8.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(U8.of(ParseUtils.parseHexOrDecShort(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIcmpv6Type().setValue(U8.of(Short.parseShort(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_NW_PROTO: case MatchUtils.STR_NW_PROTO:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIpProto()
.setField(f.oxms().buildIpProto().setValue(IpProtocol.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(IpProtocol.of(ParseUtils.parseHexOrDecShort(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIpProto().setValue(IpProtocol.of(Short.parseShort(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_NW_SRC: case MatchUtils.STR_NW_SRC:
a = f.actions().buildSetField() a = f.actions().buildSetField()
...@@ -692,122 +655,110 @@ public class ActionUtils { ...@@ -692,122 +655,110 @@ public class ActionUtils {
.build(); .build();
break; break;
case MatchUtils.STR_IPV6_FLOW_LABEL: case MatchUtils.STR_IPV6_FLOW_LABEL:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIpv6Flabel()
.setField(f.oxms().buildIpv6Flabel().setValue(IPv6FlowLabel.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(IPv6FlowLabel.of(ParseUtils.parseHexOrDecInt(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIpv6Flabel().setValue(IPv6FlowLabel.of(Integer.parseInt(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_NW_ECN: case MatchUtils.STR_NW_ECN:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIpEcn()
.setField(f.oxms().buildIpEcn().setValue(IpEcn.of(Byte.parseByte(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(IpEcn.of(ParseUtils.parseHexOrDecByte(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIpEcn().setValue(IpEcn.of(Byte.parseByte(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_NW_DSCP: case MatchUtils.STR_NW_DSCP:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildIpDscp()
.setField(f.oxms().buildIpDscp().setValue(IpDscp.of(Byte.parseByte(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(IpDscp.of(ParseUtils.parseHexOrDecByte(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildIpDscp().setValue(IpDscp.of(Byte.parseByte(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_SCTP_SRC: case MatchUtils.STR_SCTP_SRC:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_SCTP_DST: case MatchUtils.STR_SCTP_DST:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_TCP_SRC: case MatchUtils.STR_TCP_SRC:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_TCP_DST: case MatchUtils.STR_TCP_DST:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_UDP_SRC: case MatchUtils.STR_UDP_SRC:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_UDP_DST: case MatchUtils.STR_UDP_DST:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_MPLS_LABEL: case MatchUtils.STR_MPLS_LABEL:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildMplsLabel()
.setField(f.oxms().buildMplsLabel().setValue(U32.of(Long.parseLong(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(U32.of(ParseUtils.parseHexOrDecLong(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildMplsLabel().setValue(U32.of(Long.parseLong(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_MPLS_TC: case MatchUtils.STR_MPLS_TC:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildMplsTc()
.setField(f.oxms().buildMplsTc().setValue(U8.of(Short.parseShort(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(U8.of(ParseUtils.parseHexOrDecShort(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildMplsTc().setValue(U8.of(Short.parseShort(actionData[1]))).build())
.build();
}
break; break;
case MatchUtils.STR_MPLS_BOS: case MatchUtils.STR_MPLS_BOS:
a = f.actions().buildSetField() 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(); .build();
break; break;
case MatchUtils.STR_METADATA: case MatchUtils.STR_METADATA:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildMetadata()
.setField(f.oxms().buildMetadata().setValue(OFMetadata.of(U64.of(Long.parseLong(actionData[1].replaceFirst("0x", ""), 16)))).build()) .setValue(OFMetadata.of(U64.of(ParseUtils.parseHexOrDecLong(actionData[1]))))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildMetadata().setValue(OFMetadata.of(U64.of(Long.parseLong(actionData[1])))).build())
.build();
}
break; break;
case MatchUtils.STR_ACTSET_OUTPUT: case MatchUtils.STR_ACTSET_OUTPUT:
a = f.actions().buildSetField() a = f.actions().buildSetField()
.setField(f.oxms().buildActsetOutput().setValue(portFromString(actionData[1])).build()) .setField(f.oxms().buildActsetOutput()
.build(); .setValue(portFromString(actionData[1]))
.build())
.build();
break; break;
case MatchUtils.STR_TCP_FLAGS: case MatchUtils.STR_TCP_FLAGS:
if (actionData[1].startsWith("0x")) { a = f.actions().buildSetField()
a = f.actions().buildSetField() .setField(f.oxms().buildTcpFlags()
.setField(f.oxms().buildTcpFlags().setValue(U16.of(Integer.parseInt(actionData[1].replaceFirst("0x", ""), 16))).build()) .setValue(U16.of(ParseUtils.parseHexOrDecInt(actionData[1])))
.build(); .build())
} else { .build();
a = f.actions().buildSetField()
.setField(f.oxms().buildTcpFlags().setValue(U16.of(Integer.parseInt(actionData[1]))).build())
.build();
}
break; break;
default: default:
log.error("Unexpected OF1.2+ setfield '{}'", actionData); log.error("Unexpected OF1.2+ setfield '{}'", actionData);
...@@ -815,73 +766,37 @@ public class ActionUtils { ...@@ -815,73 +766,37 @@ public class ActionUtils {
} }
break; break;
case STR_GROUP: case STR_GROUP:
if (pair.startsWith("0x")) { a = f.actions().buildGroup()
a = f.actions().buildGroup() .setGroup(GroupUtils.groupIdFromString(pair))
.setGroup(OFGroup.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) .build();
.build();
} else {
a = f.actions().buildGroup()
.setGroup(OFGroup.of(Integer.parseInt(pair)))
.build();
}
break; break;
case STR_MPLS_LABEL_SET: case STR_MPLS_LABEL_SET:
if (pair.startsWith("0x")) { a = f.actions().buildSetMplsLabel()
a = f.actions().buildSetMplsLabel() .setMplsLabel(ParseUtils.parseHexOrDecLong(pair))
.setMplsLabel(Long.parseLong(pair.replaceFirst("0x", ""), 16)) .build();
.build();
} else {
a = f.actions().buildSetMplsLabel()
.setMplsLabel(Long.parseLong(pair))
.build();
}
break; break;
case STR_MPLS_POP: case STR_MPLS_POP:
if (pair.startsWith("0x")) { a = f.actions().buildPopMpls()
a = f.actions().buildPopMpls() .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair)))
.setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) .build();
.build();
} else {
a = f.actions().buildPopMpls()
.setEthertype(EthType.of(Integer.parseInt(pair)))
.build();
}
break; break;
case STR_MPLS_PUSH: case STR_MPLS_PUSH:
if (pair.startsWith("0x")) { a = f.actions().buildPushMpls()
a = f.actions().buildPushMpls() .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair)))
.setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) .build();
.build();
} else {
a = f.actions().buildPushMpls()
.setEthertype(EthType.of(Integer.parseInt(pair)))
.build();
}
break; break;
case STR_MPLS_TC_SET: case STR_MPLS_TC_SET:
if (pair.startsWith("0x")) { a = f.actions().buildSetMplsTc()
a = f.actions().buildSetMplsTc() .setMplsTc(ParseUtils.parseHexOrDecShort(pair))
.setMplsTc(Short.parseShort(pair.replaceFirst("0x", ""), 16)) .build();
.build();
} else {
a = f.actions().buildSetMplsTc()
.setMplsTc(Short.parseShort(pair))
.build();
}
break; break;
case STR_MPLS_TTL_DEC: case STR_MPLS_TTL_DEC:
a = f.actions().decMplsTtl(); a = f.actions().decMplsTtl();
break; break;
case STR_MPLS_TTL_SET: case STR_MPLS_TTL_SET:
if (pair.startsWith("0x")) { a = f.actions().buildSetMplsTtl()
a = f.actions().buildSetMplsTtl() .setMplsTtl(ParseUtils.parseHexOrDecShort(pair))
.setMplsTtl(Short.parseShort(pair.replaceFirst("0x", ""), 16)) .build();
.build();
} else {
a = f.actions().buildSetMplsTtl()
.setMplsTtl(Short.parseShort(pair))
.build();
}
break; break;
case STR_NW_TOS_SET: case STR_NW_TOS_SET:
a = decode_set_tos_bits(pair, v); // should only be used by OF1.0 a = decode_set_tos_bits(pair, v); // should only be used by OF1.0
...@@ -893,54 +808,30 @@ public class ActionUtils { ...@@ -893,54 +808,30 @@ public class ActionUtils {
a = decode_set_dst_ip(pair, v); a = decode_set_dst_ip(pair, v);
break; break;
case STR_NW_ECN_SET: // loxi does not support DSCP set for OF1.1 case STR_NW_ECN_SET: // loxi does not support DSCP set for OF1.1
if (pair.startsWith("0x")) { a = f.actions().buildSetNwEcn()
a = f.actions().buildSetNwEcn() .setNwEcn(IpEcn.of(ParseUtils.parseHexOrDecByte(pair)))
.setNwEcn(IpEcn.of(Byte.parseByte(pair.replaceFirst("0x", ""), 16))) .build();
.build();
} else {
a = f.actions().buildSetNwEcn()
.setNwEcn(IpEcn.of(Byte.parseByte(pair)))
.build();
}
break; break;
case STR_NW_TTL_DEC: case STR_NW_TTL_DEC:
a = f.actions().decNwTtl(); a = f.actions().decNwTtl();
break; break;
case STR_NW_TTL_SET: case STR_NW_TTL_SET:
if (pair.startsWith("0x")) { a = f.actions().buildSetNwTtl()
a = f.actions().buildSetNwTtl() .setNwTtl(ParseUtils.parseHexOrDecShort(pair))
.setNwTtl(Short.parseShort(pair.replaceFirst("0x", ""), 16)) .build();
.build();
} else {
a = f.actions().buildSetNwTtl()
.setNwTtl(Short.parseShort(pair))
.build();
}
break; break;
case STR_PBB_POP: case STR_PBB_POP:
a = f.actions().popPbb(); a = f.actions().popPbb();
break; break;
case STR_PBB_PUSH: case STR_PBB_PUSH:
if (pair.startsWith("0x")) { a = f.actions().buildPushPbb()
a = f.actions().buildPushPbb() .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair)))
.setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) .build();
.build();
} else {
a = f.actions().buildPushPbb()
.setEthertype(EthType.of(Integer.parseInt(pair)))
.build();
}
break; break;
case STR_QUEUE_SET: case STR_QUEUE_SET:
if (pair.startsWith("0x")) { a = f.actions().buildSetQueue()
a = f.actions().buildSetQueue() .setQueueId(ParseUtils.parseHexOrDecLong(pair))
.setQueueId(Long.parseLong(pair.replaceFirst("0x", ""), 16)) .build();
.build();
} else {
a = f.actions().buildSetQueue()
.setQueueId(Long.parseLong(pair))
.build();
}
break; break;
case STR_TP_SRC_SET: case STR_TP_SRC_SET:
a = decode_set_src_port(pair, v); a = decode_set_src_port(pair, v);
...@@ -958,15 +849,9 @@ public class ActionUtils { ...@@ -958,15 +849,9 @@ public class ActionUtils {
a = f.actions().popVlan(); a = f.actions().popVlan();
break; break;
case STR_VLAN_PUSH: case STR_VLAN_PUSH:
if (pair.startsWith("0x")) { a = f.actions().buildPushVlan()
a = f.actions().buildPushVlan() .setEthertype(EthType.of(ParseUtils.parseHexOrDecInt(pair)))
.setEthertype(EthType.of(Integer.parseInt(pair.replaceFirst("0x", ""), 16))) .build();
.build();
} else {
a = f.actions().buildPushVlan()
.setEthertype(EthType.of(Integer.parseInt(pair)))
.build();
}
break; break;
case STR_VLAN_STRIP: case STR_VLAN_STRIP:
a = f.actions().stripVlan(); a = f.actions().stripVlan();
...@@ -978,15 +863,9 @@ public class ActionUtils { ...@@ -978,15 +863,9 @@ public class ActionUtils {
a = decode_set_vlan_priority(pair, v); a = decode_set_vlan_priority(pair, v);
break; break;
case STR_METER: case STR_METER:
if (pair.startsWith("0x")) { a = f.actions().buildMeter()
a = f.actions().buildMeter() .setMeterId(ParseUtils.parseHexOrDecLong(pair))
.setMeterId(Long.parseLong(pair.replaceFirst("0x", ""), 16)) .build();
.build();
} else {
a = f.actions().buildMeter()
.setMeterId(Long.parseLong(pair))
.build();
}
break; break;
case STR_FIELD_COPY: case STR_FIELD_COPY:
a = (OFAction) copyFieldFromJson(pair, f.getVersion()); a = (OFAction) copyFieldFromJson(pair, f.getVersion());
...@@ -1177,7 +1056,7 @@ public class ActionUtils { ...@@ -1177,7 +1056,7 @@ public class ActionUtils {
public static OFPort portFromString(String s) { public static OFPort portFromString(String s) {
return MatchUtils.portFromString(s); return MatchUtils.portFromString(s);
} }
public static String portToString(OFPort p) { public static String portToString(OFPort p) {
return MatchUtils.portToString(p); return MatchUtils.portToString(p);
} }
...@@ -1234,7 +1113,7 @@ public class ActionUtils { ...@@ -1234,7 +1113,7 @@ public class ActionUtils {
int queueid = 0; int queueid = 0;
if (n.group(2) != null) { if (n.group(2) != null) {
try { try {
queueid = get_int(n.group(2)); queueid = ParseUtils.parseHexOrDecInt(n.group(2));
} }
catch (NumberFormatException e) { catch (NumberFormatException e) {
log.debug("Invalid queue-id in: '{}' (error ignored)", actionToDecode); log.debug("Invalid queue-id in: '{}' (error ignored)", actionToDecode);
...@@ -1268,7 +1147,7 @@ public class ActionUtils { ...@@ -1268,7 +1147,7 @@ public class ActionUtils {
if (n.matches()) { if (n.matches()) {
if (n.group(1) != null) { if (n.group(1) != null) {
try { 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() OFActionSetVlanVid a = OFFactories.getFactory(version).actions().buildSetVlanVid()
.setVlanVid(vlanid) .setVlanVid(vlanid)
.build(); .build();
...@@ -1303,7 +1182,7 @@ public class ActionUtils { ...@@ -1303,7 +1182,7 @@ public class ActionUtils {
if (n.group(1) != null) { if (n.group(1) != null) {
try { try {
OFActionSetVlanPcp a = OFFactories.getFactory(version).actions().buildSetVlanPcp() OFActionSetVlanPcp a = OFFactories.getFactory(version).actions().buildSetVlanPcp()
.setVlanPcp(VlanPcp.of(get_byte(n.group(1)))) .setVlanPcp(VlanPcp.of(ParseUtils.parseHexOrDecByte(n.group(1))))
.build(); .build();
log.debug("action {}", a); log.debug("action {}", a);
return a; return a;
...@@ -1382,7 +1261,7 @@ public class ActionUtils { ...@@ -1382,7 +1261,7 @@ public class ActionUtils {
if (n.group(1) != null) { if (n.group(1) != null) {
try { try {
OFActionSetNwTos a = OFFactories.getFactory(version).actions().buildSetNwTos() OFActionSetNwTos a = OFFactories.getFactory(version).actions().buildSetNwTos()
.setNwTos(get_byte(n.group(1))) .setNwTos(ParseUtils.parseHexOrDecByte(n.group(1)))
.build(); .build();
log.debug("action {}", a); log.debug("action {}", a);
return a; return a;
...@@ -1489,31 +1368,4 @@ public class ActionUtils { ...@@ -1489,31 +1368,4 @@ public class ActionUtils {
return null; 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 { ...@@ -118,7 +118,7 @@ public class GroupUtils {
} else if (s.equals(GROUP_ID_MAX)) { } else if (s.equals(GROUP_ID_MAX)) {
return OFGroup.MAX; return OFGroup.MAX;
} else { } 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) { } catch (Exception e) {
log.error("Could not parse group ID {}", s); log.error("Could not parse group ID {}", s);
...@@ -332,7 +332,7 @@ public class GroupUtils { ...@@ -332,7 +332,7 @@ public class GroupUtils {
String value = jp.getText().toLowerCase().trim(); String value = jp.getText().toLowerCase().trim();
switch (key) { switch (key) {
case BUCKET_ID: case BUCKET_ID:
bucketId = value.startsWith("0x") ? Integer.parseInt(value.replaceFirst("0x", ""), 16) : Integer.parseInt(value); bucketId = ParseUtils.parseHexOrDecInt(value);
break; break;
case BUCKET_WATCH_GROUP: case BUCKET_WATCH_GROUP:
b.setWatchGroup(groupIdFromString(key)); b.setWatchGroup(groupIdFromString(key));
...@@ -341,7 +341,7 @@ public class GroupUtils { ...@@ -341,7 +341,7 @@ public class GroupUtils {
b.setWatchPort(MatchUtils.portFromString(key)); b.setWatchPort(MatchUtils.portFromString(key));
break; break;
case BUCKET_WEIGHT: case BUCKET_WEIGHT:
b.setWeight(value.startsWith("0x") ? Integer.parseInt(value.replaceFirst("0x", ""), 16) : Integer.parseInt(value)); b.setWeight(ParseUtils.parseHexOrDecInt(value));
break; break;
case BUCKET_ACTIONS: case BUCKET_ACTIONS:
b.setActions(ActionUtils.fromString(value, b.getVersion())); // TODO update to JSON b.setActions(ActionUtils.fromString(value, b.getVersion())); // TODO update to JSON
......
...@@ -150,11 +150,7 @@ public class InstructionUtils { ...@@ -150,11 +150,7 @@ public class InstructionUtils {
OFInstructionGotoTable.Builder ib = OFFactories.getFactory(fmb.getVersion()).instructions().buildGotoTable(); OFInstructionGotoTable.Builder ib = OFFactories.getFactory(fmb.getVersion()).instructions().buildGotoTable();
// Get the table ID // Get the table ID
if (inst.startsWith("0x")) { ib.setTableId(TableId.of(ParseUtils.parseHexOrDecInt(inst)));
ib.setTableId(TableId.of(Integer.parseInt(inst.replaceFirst("0x", ""), 16)));
} else {
ib.setTableId(TableId.of(Integer.parseInt(inst))).build();
}
log.debug("Appending GotoTable instruction: {}", ib.build()); log.debug("Appending GotoTable instruction: {}", ib.build());
appendInstruction(fmb, ib.build()); appendInstruction(fmb, ib.build());
...@@ -209,19 +205,11 @@ public class InstructionUtils { ...@@ -209,19 +205,11 @@ public class InstructionUtils {
} }
// Get the metadata // Get the metadata
if (keyValue[0].startsWith("0x")) { ib.setMetadata(U64.of(ParseUtils.parseHexOrDecLong(keyValue[0])));
ib.setMetadata(U64.of(Long.valueOf(keyValue[0].replaceFirst("0x", ""), 16)));
} else {
ib.setMetadata(U64.of(Long.valueOf(keyValue[0])));
}
// Get the optional mask // Get the optional mask
if (keyValue.length == 2) { if (keyValue.length == 2) {
if (keyValue[1].startsWith("0x")) { ib.setMetadataMask(U64.of(ParseUtils.parseHexOrDecLong(keyValue[1])));
ib.setMetadataMask(U64.of(Long.valueOf(keyValue[1].replaceFirst("0x", ""), 16)));
} else {
ib.setMetadataMask(U64.of(Long.valueOf(keyValue[1])));
}
} else { } else {
ib.setMetadataMask(U64.NO_MASK); ib.setMetadataMask(U64.NO_MASK);
} }
...@@ -370,11 +358,7 @@ public class InstructionUtils { ...@@ -370,11 +358,7 @@ public class InstructionUtils {
OFInstructionMeter.Builder ib = OFFactories.getFactory(fmb.getVersion()).instructions().buildMeter(); OFInstructionMeter.Builder ib = OFFactories.getFactory(fmb.getVersion()).instructions().buildMeter();
if (inst.startsWith("0x")) { ib.setMeterId(ParseUtils.parseHexOrDecLong(inst));
ib.setMeterId(Long.valueOf(inst.replaceFirst("0x", ""), 16));
} else {
ib.setMeterId(Long.valueOf(inst));
}
log.debug("Appending (Goto)Meter instruction: {}", ib.build()); log.debug("Appending (Goto)Meter instruction: {}", ib.build());
appendInstruction(fmb, ib.build()); appendInstruction(fmb, ib.build());
...@@ -507,7 +491,7 @@ public class InstructionUtils { ...@@ -507,7 +491,7 @@ public class InstructionUtils {
} }
break; break;
case "value": case "value":
value = U64.of(Long.parseLong(jp.getText())); value = U64.of(ParseUtils.parseHexOrDecLong(jp.getText()));
break; break;
default: default:
log.warn("Unexpected OXS threshold key {}", jp.getCurrentName()); 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