Skip to content

Commit 4ecf9bd

Browse files
committed
Refactored ascii_of_flowTable to string_of_flowTable. Added optional label argument.
1 parent c8d5739 commit 4ecf9bd

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

lib/SDN_Types.ml

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,13 @@ let string_of_action = make_string_of format_action
402402
let string_of_seq = make_string_of format_seq
403403
let string_of_par = make_string_of format_par
404404
let string_of_flow = make_string_of format_flow
405-
let string_of_flowTable = make_string_of format_flowTable
406405

407406
let string_of_vlan (x : int) : string =
408407
Format.sprintf "Vlan = %d" x
409-
408+
410409
let string_of_vlanpcp (x : dlVlanPcp) : string =
411410
Format.sprintf "VlanPcp = %d" x
412-
411+
413412
let string_of_ethType (x : dlTyp) : string =
414413
let extra = if x = 0x800 then " (ip)"
415414
else if x = 0x806 then " (arp)"
@@ -446,7 +445,7 @@ let string_of_tcpDstPort (x : tpPort) : string =
446445
Format.sprintf "TCPDstPort = %d" x
447446

448447
let string_of_inPort (x : portId) : string =
449-
Format.sprintf "In Port = %lu" x
448+
Format.sprintf "InPort = %lu" x
450449

451450
let check (string_of : 'a -> string)
452451
(x : 'a option)
@@ -473,52 +472,66 @@ let pattern_list (p : Pattern.t) : string list =
473472
* contains the strings of the pattern and the second list contains
474473
* the strings of the actions associated with the pattern. *)
475474
let to_entry (f : flow) : (string list) * (string list) =
476-
let open Core.Std.List in
475+
let open Core.Std in
476+
let open List in
477477
let pattern_list = pattern_list f.pattern in
478478
let action_list = map (concat (concat f.action)) string_of_action in
479479
(pattern_list, action_list)
480480

481481
(* Pads a string with spaces so that it is atleast `len` characters. *)
482482
let pad (len : int) (e : string) : string =
483+
let open Core.Std in
483484
let padding_size = max 0 (len - (String.length e)) in
484485
let padding = String.make padding_size ' ' in
485-
Core.Std.String.concat [e; padding]
486+
String.concat [e; padding]
487+
488+
(* Helper function *)
489+
let unwrap x =
490+
match x with
491+
| None -> 0
492+
| Some x -> x
486493

487494
(* Given a list of entries to be displayed in the table, calculate a pair
488495
* containing the max characters in a pattern string and action string *)
489-
let table_size (sw_id : switchId) (entries : ((string list) * (string list)) list) : int * int =
490-
let open Core.Std.List in
496+
let table_size (label : string) (entries : ((string list) * (string list)) list) : int * int =
497+
let open Core.Std in
498+
let open List in
491499
let patterns = map entries fst |> concat in
492500
let actions = map entries snd |> concat in
493-
let max_p = max_elt (map patterns String.length) (-) |> Core.Std.uw in
494-
let max_a = max_elt (map actions String.length) (-) |> Core.Std.uw in
495-
(max max_p ((Int64.to_string sw_id |> String.length) + 3 + (String.length "Pattern")), max max_a (String.length "Action"))
501+
let max_p = max_elt (map patterns String.length) (-) |> unwrap in
502+
let max_a = max_elt (map actions String.length) (-) |> unwrap in
503+
(max max_p ((String.length label) + 3 + (String.length "Pattern")), max max_a (String.length "Action"))
496504

497505
(* Create the top edge of the table *)
498506
let top max_p max_a : string =
507+
let open Core.Std in
499508
let open Char in
500509
let fill = String.make (max_p + max_a + 5) '-' in
501510
Format.sprintf "+%s+\n" fill
502511

503512
(* Create the bottom edge of the table *)
504513
let bottom max_p max_a : string=
514+
let open Core.Std in
505515
let fill = String.make (max_p + max_a + 5) '-' in
506516
Format.sprintf "+%s+\n" fill
507517

508518
(* Create a divider between entries *)
509519
let div max_p max_a : string =
520+
let open Core.Std in
510521
let fill = String.make (max_p + max_a + 5) '-' in
511522
Format.sprintf "|%s|\n" fill
512523

513524
(* Create the columns of the table *)
514-
let title sw_id max_p max_a : string =
515-
let pattern = pad max_p (Format.sprintf "%Ld | Pattern" sw_id) in
525+
let title label max_p max_a : string =
526+
let open Core.Std in
527+
let pattern = pad max_p (Format.sprintf "%s | Pattern" label) in
516528
let action = pad max_a "Action" in
517529
Format.sprintf "| %s | %s |\n" pattern action
518530

519531
(* Create a row in the table *)
520532
let string_of_entry (max_p : int) (max_a : int) (e : (string list) * (string list)) : string =
521-
let open Core.Std.List in
533+
let open Core.Std in
534+
let open List in
522535
let padded_patterns = map (fst e) (pad max_p) in
523536
let padded_actions = map (snd e) (pad max_a) in
524537
let blank_action = String.make max_a ' ' in
@@ -539,14 +552,15 @@ let string_of_entry (max_p : int) (max_a : int) (e : (string list) * (string lis
539552
helper ps rest acc'
540553
in
541554
helper padded_patterns padded_actions [(div max_p max_a)]
542-
|> rev |> Core.Std.String.concat
555+
|> rev |> String.concat
543556

544-
(* Given a switch id and a flowTable, returns an ascii flowtable *)
545-
let ascii_of_flowTable (sw_id : switchId) (tbl : flowTable) : string =
546-
let entries = Core.Std.List.map tbl to_entry in
547-
let (max_p, max_a) = table_size sw_id entries in
557+
(* Given a label and a flowTable, returns an ascii flowtable *)
558+
let string_of_flowTable ?(label="") (tbl : flowTable) : string =
559+
let open Core.Std in
560+
let entries = List.map tbl to_entry in
561+
let (max_p, max_a) = table_size label entries in
548562
let t = (top max_p max_a) in
549-
let l = (title sw_id max_p max_a) in
550-
let entry_strings = Core.Std.List.map entries (string_of_entry max_p max_a) in
563+
let l = (title label max_p max_a) in
564+
let entry_strings = List.map entries (string_of_entry max_p max_a) in
551565
let b = bottom max_p max_a in
552-
Core.Std.String.concat (t :: l :: (Core.Std.List.append entry_strings [b]))
566+
String.concat (t :: l :: (List.append entry_strings [b]))

lib/SDN_Types.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,4 @@ val string_of_action : action -> string
217217
val string_of_seq : seq -> string
218218
val string_of_par : par -> string
219219
val string_of_flow : flow -> string
220-
val string_of_flowTable : flowTable -> string
221-
val ascii_of_flowTable : switchId -> flowTable -> string
220+
val string_of_flowTable : ?label:string -> flowTable -> string

0 commit comments

Comments
 (0)