summaryrefslogtreecommitdiff
path: root/src/commit.ml
diff options
context:
space:
mode:
Diffstat (limited to 'src/commit.ml')
-rw-r--r--src/commit.ml80
1 files changed, 72 insertions, 8 deletions
diff --git a/src/commit.ml b/src/commit.ml
index a0d9d35..acfecfe 100644
--- a/src/commit.ml
+++ b/src/commit.ml
@@ -3,6 +3,8 @@ module CT = Vyos1x.Config_tree
module CD = Vyos1x.Config_diff
module RT = Vyos1x.Reference_tree
+exception Commit_error of string
+
type tree_source = DELETE | ADD
let tree_source_to_yojson = function
@@ -32,29 +34,31 @@ let default_node_data = {
arg_value = None;
path = [];
source = ADD;
- reply = Some { success = false; out = ""; };
+ reply = None;
}
type commit_data = {
session_id: string;
- named_active : string option;
- named_proposed : string option;
dry_run: bool;
atomic: bool;
background: bool;
init: status option;
node_list: node_data list;
+ config_diff: CT.t;
+ config_result: CT.t;
+ result : status;
} [@@deriving to_yojson]
let default_commit_data = {
session_id = "";
- named_active = None;
- named_proposed = None;
dry_run = false;
atomic = false;
background = false;
- init = Some { success = false; out = ""; };
+ init = None;
node_list = [];
+ config_diff = CT.default;
+ config_result = CT.default;
+ result = { success = true; out = ""; };
}
let lex_order c1 c2 =
@@ -154,8 +158,7 @@ let legacy_order del_t a b =
in
CS.fold shift a (a, b)
-let calculate_priority_lists rt at wt =
- let diff = CD.diff_tree [] at wt in
+let calculate_priority_lists rt diff =
let del_tree = CD.get_tagged_delete_tree diff in
let add_tree = CT.get_subtree diff ["add"] in
let cs_del' = get_commit_set rt del_tree DELETE in
@@ -174,3 +177,64 @@ let commit_store c_data =
| false -> acc ^ "\n" ^ r.out
in List.fold_left func "" c_data.node_list
in print_endline out
+
+(* The base config_result is the intersection of running and proposed
+ configs:
+ on success, added paths are added; deleted paths are ignored
+ on failure, deleted paths are added back in, added paths ignored
+ *)
+let config_result_update c_data n_data =
+ match n_data.reply with
+ | None -> c_data (* already exluded in calling function *)
+ | Some r ->
+ match r.success, n_data.source with
+ | true, ADD ->
+ let add = CT.get_subtree c_data.config_diff ["add"] in
+ let add_tree = CD.clone add (CT.default) n_data.path in
+ let config = CD.tree_union add_tree c_data.config_result in
+ let result =
+ { success = c_data.result.success && true;
+ out = c_data.result.out ^ r.out; }
+ in
+ { c_data with config_result = config; result = result; }
+ | false, DELETE ->
+ let del = CT.get_subtree c_data.config_diff ["del"] in
+ let add_tree = CD.clone del (CT.default) n_data.path in
+ let config = CD.tree_union add_tree c_data.config_result in
+ let result =
+ { success = c_data.result.success && false;
+ out = c_data.result.out ^ r.out; }
+ in
+ { c_data with config_result = config; result = result; }
+ | true, DELETE ->
+ let result =
+ { success = c_data.result.success && true;
+ out = c_data.result.out ^ r.out; }
+ in
+ { c_data with result = result; }
+ | false, ADD ->
+ let result =
+ { success = c_data.result.success && false;
+ out = c_data.result.out ^ r.out; }
+ in
+ { c_data with result = result; }
+
+
+let commit_update c_data =
+ match c_data.init with
+ | None -> raise (Commit_error "commitd failure: no init status provided")
+ | Some _ ->
+ let func acc_data nd =
+ match nd.reply with
+ | None -> raise (Commit_error "commitd failure: no reply status provided")
+ | Some _ -> config_result_update acc_data nd
+ in List.fold_left func c_data c_data.node_list
+
+let make_commit_data rt at wt id =
+ let diff = CD.diff_tree [] at wt in
+ let del_list, add_list = calculate_priority_lists rt diff in
+ { default_commit_data with
+ session_id = id;
+ config_diff = diff;
+ config_result = CT.get_subtree diff ["inter"];
+ node_list = del_list @ add_list; }