Browse Source

Implement commit command

Serj Kalichev 1 year ago
parent
commit
4ff1cb07a2
4 changed files with 64 additions and 0 deletions
  1. 1 0
      src/plugin.c
  2. 57 0
      src/syms.c
  3. 1 0
      src/syms.h
  4. 5 0
      xml/sysrepo.xml

+ 1 - 0
src/plugin.c

@@ -69,6 +69,7 @@ int kplugin_sysrepo_init(kcontext_t *context)
 	kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new("srp_insert", srp_insert));
+	kplugin_add_syms(plugin, ksym_new("srp_commit", srp_commit));
 
 	return 0;
 }

+ 57 - 0
src/syms.c

@@ -702,3 +702,60 @@ err:
 
 	return ret;
 }
+
+
+int srp_commit(kcontext_t *context)
+{
+	int ret = -1;
+	faux_argv_t *args = NULL;
+	pline_t *pline = NULL;
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	pexpr_t *expr = NULL;
+	size_t err_num = 0;
+	faux_argv_t *cur_path = NULL;
+	kplugin_t *plugin = NULL;
+
+	assert(context);
+
+	if (sr_connect(SR_CONN_DEFAULT, &conn)) {
+		fprintf(stderr, "Can't connect to candidate data store\n");
+		return -1;
+	}
+
+	// Validate candidate config
+	if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
+		fprintf(stderr, "Can't connect to candidate data store\n");
+		goto err;
+	}
+	if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
+		fprintf(stderr, "Invalid candidate configuration\n");
+		goto err;
+	}
+
+	// Copy candidate to running-config
+	if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
+		fprintf(stderr, "Can't connect to running-config data store\n");
+		goto err;
+	}
+	if (sr_copy_config(sess, NULL, SRP_REPO_EDIT, 0) != SR_ERR_OK) {
+		fprintf(stderr, "Can't commit to running-config\n");
+		goto err;
+	}
+
+	// Copy running-config to startup-config
+	if (sr_session_switch_ds(sess, SR_DS_STARTUP)) {
+		fprintf(stderr, "Can't connect to startup-config data store\n");
+		goto err;
+	}
+	if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
+		fprintf(stderr, "Can't store data to startup-config\n");
+		goto err;
+	}
+
+	ret = 0;
+err:
+	sr_disconnect(conn);
+
+	return ret;
+}

+ 1 - 0
src/syms.h

@@ -34,6 +34,7 @@ int srp_edit(kcontext_t *context);
 int srp_top(kcontext_t *context);
 int srp_up(kcontext_t *context);
 int srp_insert(kcontext_t *context);
+int srp_commit(kcontext_t *context);
 
 C_DECL_END
 

+ 5 - 0
xml/sysrepo.xml

@@ -158,6 +158,11 @@
 		<ACTION sym="srp_insert@sysrepo"/>
 	</ENTRY>
 
+	<ENTRY name="commit" help="Commit data to running-config" mode="sequence">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ACTION sym="srp_commit@sysrepo"/>
+	</ENTRY>
+
 </ENTRY>
 
 </KLISH>