move the json stuff into utils - I guess we will need functionality like

this not just for openflow at some point of time.
This commit is contained in:
Johanna Amann 2015-04-07 10:43:36 -07:00
parent fe5408e676
commit 4195a0066a
2 changed files with 18 additions and 27 deletions

View file

@ -1,7 +1,7 @@
@load base/frameworks/openflow
@load base/frameworks/openflow/utils/json
@load base/utils/exec
@load base/utils/active-http
@load base/utils/exec
@load base/utils/json
module OpenflowRyu;
@ -129,7 +129,7 @@ function flow_mod(state: Openflow::ControllerState, flow_mod: Openflow::ofp_flow
local request: ActiveHTTP::Request = ActiveHTTP::Request(
$url=cat("http://", cat(state$host), ":", cat(state$host_port), RYU_FLOWENTRY_PATH, command_type),
$method="POST",
$client_data=OpenflowJSON::convert(_flow_mod)
$client_data=to_json(_flow_mod)
);
# Execute call to Ryu's ReST API
when(local result = ActiveHTTP::request(request))

View file

@ -1,117 +0,0 @@
@load base/utils/strings
module OpenflowJSON;
export {
## A function to convert arbitrary Bro data into a JSON string.
##
## v: The value to convert to JSON. Typically a record.
##
## only_loggable: If the v value is a record this will only cause
## fields with the &log attribute to be included in the JSON.
##
## returns: a JSON formatted string.
global convert: function(v: any, only_loggable: bool &default=F, field_escape_pattern: pattern &default=/^_/): string;
global jsonToRecord: function(input: string): any;
}
function jsonToRecord(input: string): any
{
local lhs: table[count] of string;
lhs = split1(input, / /);
for (i in lhs)
print lhs[i];
return lhs;
}
function convert(v: any, only_loggable: bool &default=F, field_escape_pattern: pattern &default=/^_/): string
{
local tn = type_name(v);
switch ( tn )
{
case "type":
return "";
case "string":
return cat("\"", gsub(gsub(clean(v), /\\/, "\\\\"), /\"/, "\\\""), "\"");
case "port":
return cat(port_to_count(to_port(cat(v))));
case "addr":
return cat("\"", v, "\"");
case "int":
fallthrough;
case "count":
fallthrough;
case "time":
fallthrough;
case "double":
fallthrough;
case "bool":
fallthrough;
case "enum":
return cat(v);
default:
break;
}
if ( /^record/ in tn )
{
local rec_parts: string_vec = vector();
local ft = record_fields(v);
for ( field in ft )
{
local field_desc = ft[field];
# replace the escape pattern in the field.
if( field_escape_pattern in field )
field = cat(sub(field, field_escape_pattern, ""));
if ( field_desc?$value && (!only_loggable || field_desc$log) )
{
local onepart = cat("\"", field, "\": ", OpenflowJSON::convert(field_desc$value, only_loggable));
rec_parts[|rec_parts|] = onepart;
}
}
return cat("{", join_string_vec(rec_parts, ", "), "}");
}
# None of the following are supported.
else if ( /^set/ in tn )
{
local set_parts: string_vec = vector();
local sa: set[bool] = v;
for ( sv in sa )
{
set_parts[|set_parts|] = OpenflowJSON::convert(sv, only_loggable);
}
return cat("[", join_string_vec(set_parts, ", "), "]");
}
else if ( /^table/ in tn )
{
local tab_parts: vector of string = vector();
local ta: table[bool] of any = v;
for ( ti in ta )
{
local ts = OpenflowJSON::convert(ti);
local if_quotes = (ts[0] == "\"") ? "" : "\"";
tab_parts[|tab_parts|] = cat(if_quotes, ts, if_quotes, ": ", OpenflowJSON::convert(ta[ti], only_loggable));
}
return cat("{", join_string_vec(tab_parts, ", "), "}");
}
else if ( /^vector/ in tn )
{
local vec_parts: string_vec = vector();
local va: vector of any = v;
for ( vi in va )
{
vec_parts[|vec_parts|] = OpenflowJSON::convert(va[vi], only_loggable);
}
return cat("[", join_string_vec(vec_parts, ", "), "]");
}
return "\"\"";
}