ソースを参照

Lazy connect to sysrepo

Serj Kalichev 9 ヶ月 前
コミット
91eeb4dd21
2 ファイル変更87 行追加28 行削除
  1. 51 27
      src/plugin.c
  2. 36 1
      src/syms.c

+ 51 - 27
src/plugin.c

@@ -229,20 +229,7 @@ void srp_udata_set_path(kcontext_t *context, faux_argv_t *path)
 }
 
 
-sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
-{
-	srp_udata_t *udata = NULL;
-
-	assert(context);
-
-	udata = srp_udata(context);
-	assert(udata);
-
-	return udata->sr_sess;
-}
-
-
-static int kplugin_sysrepo_init_session(kcontext_t *context)
+static bool_t kplugin_sysrepo_connect(kcontext_t *context)
 {
 	srp_udata_t *udata = NULL;
 	const char *user = NULL;
@@ -257,26 +244,60 @@ static int kplugin_sysrepo_init_session(kcontext_t *context)
 
 	// Connect to Sysrepo
 	if (sr_connect(SR_CONN_DEFAULT, &(udata->sr_conn))) {
+		udata->sr_conn = NULL;
 		syslog(LOG_ERR, "Can't connect to Sysrepo");
-		return -1;
+		return BOOL_FALSE;
 	}
 	if (sr_session_start(udata->sr_conn, SRP_REPO_EDIT, &(udata->sr_sess))) {
-		syslog(LOG_ERR, "Can't connect create Sysrepo session");
 		sr_disconnect(udata->sr_conn);
-		return -1;
+		udata->sr_conn = NULL;
+		syslog(LOG_ERR, "Can't connect create Sysrepo session");
+		return BOOL_FALSE;
 	}
 	sr_session_set_orig_name(udata->sr_sess, user);
 	// Init NACM session
 	if (udata->opts.enable_nacm) {
 		if (sr_nacm_init(udata->sr_sess, 0, &(udata->nacm_sub)) != SR_ERR_OK) {
 			sr_disconnect(udata->sr_conn);
-			return -1;
+			udata->sr_conn = NULL;
+			return BOOL_FALSE;
 		}
 		sr_nacm_set_user(udata->sr_sess, user);
 	}
 
 	syslog(LOG_INFO, "Start SysRepo session for \"%s\"", user);
 
+	return BOOL_TRUE;
+}
+
+
+sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
+{
+	srp_udata_t *udata = NULL;
+
+	assert(context);
+
+	udata = srp_udata(context);
+	assert(udata);
+
+	// Sysrepo is already connected
+	if (udata->sr_conn)
+		return udata->sr_sess;
+
+	// Lazy connection to Sysrepo
+	if (!kplugin_sysrepo_connect(context)) {
+		fprintf(stderr, "Error: Can't connect to config storage");
+		return NULL;
+	}
+
+	return udata->sr_sess;
+}
+
+
+static int kplugin_sysrepo_init_session(kcontext_t *context)
+{
+	context = context; // Happy compiler
+
 	return 0;
 }
 
@@ -284,23 +305,26 @@ static int kplugin_sysrepo_init_session(kcontext_t *context)
 static int kplugin_sysrepo_fini_session(kcontext_t *context)
 {
 	srp_udata_t *udata = NULL;
-	const char *user = NULL;
 
 	assert(context);
 
 	udata = srp_udata(context);
 	assert(udata);
 
-	// Remote user name
-	user = ksession_user(kcontext_session(context));
+	// Due to lazy connect to sysrepo the connection can be down
+	if (udata->sr_conn) {
+		const char *user = NULL;
 
-	if (udata->opts.enable_nacm) {
-		sr_unsubscribe(udata->nacm_sub);
-		sr_nacm_destroy();
+		if (udata->opts.enable_nacm) {
+			sr_unsubscribe(udata->nacm_sub);
+			sr_nacm_destroy();
+		}
+		sr_disconnect(udata->sr_conn);
+		// Remote user name
+		user = ksession_user(kcontext_session(context));
+		syslog(LOG_INFO, "Stop SysRepo session for \"%s\"",
+			user ? user : "<unknown>");
 	}
-	sr_disconnect(udata->sr_conn);
-
-	syslog(LOG_INFO, "Stop SysRepo session for \"%s\"", user ? user : "<unknown>");
 
 	return 0;
 }

+ 36 - 1
src/syms.c

@@ -109,6 +109,8 @@ static int srp_compl_or_help(kcontext_t *context, bool_t help,
 	assert(context);
 
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	if (use_cur_path)
 		cur_path = (faux_argv_t *)srp_udata_path(context);
@@ -252,6 +254,8 @@ static int srp_check_type(kcontext_t *context,
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	if (use_cur_path)
 		cur_path = (faux_argv_t *)srp_udata_path(context);
@@ -367,6 +371,8 @@ int srp_PLINE_INSERT_TO(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	value = kcontext_candidate_value(context);
@@ -401,6 +407,8 @@ static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	args = assemble_insert_to(sess, kcontext_parent_pargv(context),
@@ -439,6 +447,8 @@ int srp_set(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
@@ -500,6 +510,8 @@ int srp_del(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
@@ -553,6 +565,8 @@ int srp_edit(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
@@ -617,6 +631,8 @@ int srp_up(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	if (!cur_path)
@@ -676,6 +692,8 @@ int srp_insert(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	pargv = kcontext_pargv(context);
@@ -774,6 +792,8 @@ int srp_verify(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	// Validate candidate config
 	if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
@@ -795,6 +815,8 @@ int srp_commit(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	// Validate candidate config. The copy operation is not enough to fully
 	// verify candidate config. It verifies only the part of it. So verify
@@ -839,6 +861,8 @@ int srp_reset(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	// Copy running-config to candidate config
 	if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
@@ -866,6 +890,8 @@ int srp_show_xml(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
@@ -931,8 +957,10 @@ static int show(kcontext_t *context, sr_datastore_t ds,
 	size_t xpath_depth = 0;
 
 	assert(context);
-
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
+
 	if (ds != SRP_REPO_EDIT)
 		sr_session_switch_ds(sess, ds);
 	if (use_cur_path)
@@ -1021,6 +1049,8 @@ int srp_deactivate(kcontext_t *context)
 	assert(context);
 
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 	args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
@@ -1104,6 +1134,8 @@ int srp_diff(kcontext_t *context)
 
 	assert(context);
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
 
 	cur_path = (faux_argv_t *)srp_udata_path(context);
 
@@ -1202,6 +1234,9 @@ int srp_compl_xpath(kcontext_t *context)
 		return -1;
 
 	sess = srp_udata_sr_sess(context);
+	if (!sess)
+		return -1;
+
 	if (ds != SRP_REPO_EDIT)
 		sr_session_switch_ds(sess, ds);