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

View file

@ -1,6 +1,8 @@
@load base/utils/strings ##! Functions to assist with generating JSON data from Bro data scructures.
module OpenflowJSON; # We might want to implement this in core somtime, this looks... hacky at best.
@load base/utils/strings
export { export {
## A function to convert arbitrary Bro data into a JSON string. ## A function to convert arbitrary Bro data into a JSON string.
@ -11,20 +13,9 @@ export {
## fields with the &log attribute to be included in the JSON. ## fields with the &log attribute to be included in the JSON.
## ##
## returns: a JSON formatted string. ## returns: a JSON formatted string.
global convert: function(v: any, only_loggable: bool &default=F, field_escape_pattern: pattern &default=/^_/): string; global to_json: 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 function convert(v: any, only_loggable: bool &default=F, field_escape_pattern: pattern &default=/^_/): string
{ {
local tn = type_name(v); local tn = type_name(v);
@ -72,7 +63,7 @@ function convert(v: any, only_loggable: bool &default=F, field_escape_pattern: p
field = cat(sub(field, field_escape_pattern, "")); field = cat(sub(field, field_escape_pattern, ""));
if ( field_desc?$value && (!only_loggable || field_desc$log) ) if ( field_desc?$value && (!only_loggable || field_desc$log) )
{ {
local onepart = cat("\"", field, "\": ", OpenflowJSON::convert(field_desc$value, only_loggable)); local onepart = cat("\"", field, "\": ", to_json(field_desc$value, only_loggable));
rec_parts[|rec_parts|] = onepart; rec_parts[|rec_parts|] = onepart;
} }
} }
@ -86,7 +77,7 @@ function convert(v: any, only_loggable: bool &default=F, field_escape_pattern: p
local sa: set[bool] = v; local sa: set[bool] = v;
for ( sv in sa ) for ( sv in sa )
{ {
set_parts[|set_parts|] = OpenflowJSON::convert(sv, only_loggable); set_parts[|set_parts|] = to_json(sv, only_loggable);
} }
return cat("[", join_string_vec(set_parts, ", "), "]"); return cat("[", join_string_vec(set_parts, ", "), "]");
} }
@ -96,9 +87,9 @@ function convert(v: any, only_loggable: bool &default=F, field_escape_pattern: p
local ta: table[bool] of any = v; local ta: table[bool] of any = v;
for ( ti in ta ) for ( ti in ta )
{ {
local ts = OpenflowJSON::convert(ti); local ts = to_json(ti);
local if_quotes = (ts[0] == "\"") ? "" : "\""; local if_quotes = (ts[0] == "\"") ? "" : "\"";
tab_parts[|tab_parts|] = cat(if_quotes, ts, if_quotes, ": ", OpenflowJSON::convert(ta[ti], only_loggable)); tab_parts[|tab_parts|] = cat(if_quotes, ts, if_quotes, ": ", to_json(ta[ti], only_loggable));
} }
return cat("{", join_string_vec(tab_parts, ", "), "}"); return cat("{", join_string_vec(tab_parts, ", "), "}");
} }
@ -108,7 +99,7 @@ function convert(v: any, only_loggable: bool &default=F, field_escape_pattern: p
local va: vector of any = v; local va: vector of any = v;
for ( vi in va ) for ( vi in va )
{ {
vec_parts[|vec_parts|] = OpenflowJSON::convert(va[vi], only_loggable); vec_parts[|vec_parts|] = to_json(va[vi], only_loggable);
} }
return cat("[", join_string_vec(vec_parts, ", "), "]"); return cat("[", join_string_vec(vec_parts, ", "), "]");
} }