*/
public static FirewallRule jsonToFirewallRule(String fmJson) throws IOException {
FirewallRule rule = new FirewallRule();
MappingJsonFactory f = new MappingJsonFactory();
JsonParser jp;
try {
jp = f.createJsonParser(fmJson);
} catch (JsonParseException e) {
throw new IOException(e);
}
jp.nextToken();
if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
throw new IOException("Expected START_OBJECT");
}
while (jp.nextToken() != JsonToken.END_OBJECT) {
if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
throw new IOException("Expected FIELD_NAME");
}
String n = jp.getCurrentName();
jp.nextToken();
if (jp.getText().equals(""))
continue;
String tmp;
// This is currently only applicable for remove(). In store(), ruleid takes a random number
if (n == "ruleid") {
rule.ruleid = Integer.parseInt(jp.getText());
}
// This assumes user having dpid info for involved switches
else if (n == "switchid") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("-1") == false) {
// user inputs hex format dpid
rule.dpid = HexString.toLong(tmp);
rule.wildcard_dpid = false;
}
}
else if (n == "src-inport") {
rule.in_port = Short.parseShort(jp.getText());
rule.wildcard_in_port = false;
}
else if (n == "src-mac") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("ANY") == false) {
rule.wildcard_dl_src = false;
rule.dl_src = Ethernet.toLong(Ethernet.toMACAddress(tmp));
}
}
else if (n == "dst-mac") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("ANY") == false) {
rule.wildcard_dl_dst = false;
rule.dl_dst = Ethernet.toLong(Ethernet.toMACAddress(tmp));
}
}
else if (n == "dl-type") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("ARP")) {
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_ARP;
}
if (tmp.equalsIgnoreCase("IPv4")) {
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_IPv4;
}
}
else if (n == "src-ip") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("ANY") == false) {
rule.wildcard_nw_src = false;
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_IPv4;
int[] cidr = IPCIDRToPrefixBits(tmp);
rule.nw_src_prefix = cidr[0];
rule.nw_src_maskbits = cidr[1];
}
}
else if (n == "dst-ip") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("ANY") == false) {
rule.wildcard_nw_dst = false;
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_IPv4;
int[] cidr = IPCIDRToPrefixBits(tmp);
rule.nw_dst_prefix = cidr[0];
rule.nw_dst_maskbits = cidr[1];
}
}
else if (n == "nw-proto") {
tmp = jp.getText();
if (tmp.equalsIgnoreCase("TCP")) {
rule.wildcard_nw_proto = false;
rule.nw_proto = IPv4.PROTOCOL_TCP;
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_IPv4;
} else if (tmp.equalsIgnoreCase("UDP")) {
rule.wildcard_nw_proto = false;
rule.nw_proto = IPv4.PROTOCOL_UDP;
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_IPv4;
} else if (tmp.equalsIgnoreCase("ICMP")) {
rule.wildcard_nw_proto = false;
rule.nw_proto = IPv4.PROTOCOL_ICMP;
rule.wildcard_dl_type = false;
rule.dl_type = Ethernet.TYPE_IPv4;
}
}
else if (n == "tp-src") {
rule.wildcard_tp_src = false;
rule.tp_src = Short.parseShort(jp.getText());
}
else if (n == "tp-dst") {
rule.wildcard_tp_dst = false;
rule.tp_dst = Short.parseShort(jp.getText());
}
else if (n == "priority") {
rule.priority = Integer.parseInt(jp.getText());
}
else if (n == "action") {
if (jp.getText().equalsIgnoreCase("allow") == true) {
rule.action = FirewallRule.FirewallAction.ALLOW;
} else if (jp.getText().equalsIgnoreCase("deny") == true) {
rule.action = FirewallRule.FirewallAction.DENY;
}
}
}