summaryrefslogtreecommitdiffstats
path: root/lib/northbound_db.c
blob: cefcfbcf1f99d68ba90d3b8b998705b8083dab37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2018  NetDEF, Inc.
 *                     Renato Westphal
 */

#include <zebra.h>

#include "libfrr.h"
#include "log.h"
#include "lib_errors.h"
#include "command.h"
#include "db.h"
#include "northbound.h"
#include "northbound_db.h"

int nb_db_init(void)
{
#ifdef HAVE_CONFIG_ROLLBACKS
	/*
	 * NOTE: the delete_tail SQL trigger is used to implement a ring buffer
	 * where only the last N transactions are recorded in the configuration
	 * log.
	 */
	if (db_execute(
		    "BEGIN TRANSACTION;\n"
		    "  CREATE TABLE IF NOT EXISTS transactions(\n"
		    "    client         CHAR(32)             NOT NULL,\n"
		    "    date           DATETIME             DEFAULT CURRENT_TIMESTAMP,\n"
		    "    comment        CHAR(80)             ,\n"
		    "    configuration  TEXT                 NOT NULL\n"
		    "  );\n"
		    "  CREATE TRIGGER IF NOT EXISTS delete_tail\n"
		    "    AFTER INSERT ON transactions\n"
		    "    FOR EACH ROW\n"
		    "    BEGIN\n"
		    "    DELETE\n"
		    "    FROM\n"
		    "      transactions\n"
		    "    WHERE\n"
		    "      rowid%%%u=NEW.rowid%%%u AND rowid!=NEW.rowid;\n"
		    "    END;\n"
		    "COMMIT;",
		    NB_DLFT_MAX_CONFIG_ROLLBACKS, NB_DLFT_MAX_CONFIG_ROLLBACKS)
	    != 0)
		return NB_ERR;
#endif /* HAVE_CONFIG_ROLLBACKS */

	return NB_OK;
}

int nb_db_transaction_save(const struct nb_transaction *transaction,
			   uint32_t *transaction_id)
{
#ifdef HAVE_CONFIG_ROLLBACKS
	struct sqlite3_stmt *ss;
	const char *client_name;
	char *config_str = NULL;
	int ret = NB_ERR;

	/*
	 * Use a transaction to ensure consistency between the INSERT and SELECT
	 * queries.
	 */
	if (db_execute("BEGIN TRANSACTION;") != 0)
		return NB_ERR;

	ss = db_prepare(
		"INSERT INTO transactions\n"
		"  (client, comment, configuration)\n"
		"VALUES\n"
		"  (?, ?, ?);");
	if (!ss)
		goto exit;

	client_name = nb_client_name(transaction->context->client);
	/*
	 * Always record configurations in the XML format, save the default
	 * values too, as this covers the case where defaults may change.
	 */
	if (lyd_print_mem(&config_str, transaction->config->dnode, LYD_XML,
			  LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL)
	    != 0)
		goto exit;

	if (db_bindf(ss, "%s%s%s", client_name, strlen(client_name),
		     transaction->comment, strlen(transaction->comment),
		     config_str ? config_str : "",
		     config_str ? strlen(config_str) : 0)
	    != 0)
		goto exit;

	if (db_run(ss) != SQLITE_OK)
		goto exit;

	db_finalize(&ss);

	/*
	 * transaction_id is an optional output parameter that provides the ID
	 * of the recorded transaction.
	 */
	if (transaction_id) {
		ss = db_prepare("SELECT last_insert_rowid();");
		if (!ss)
			goto exit;

		if (db_run(ss) != SQLITE_ROW)
			goto exit;

		if (db_loadf(ss, "%i", transaction_id) != 0)
			goto exit;

		db_finalize(&ss);
	}

	if (db_execute("COMMIT;") != 0)
		goto exit;

	ret = NB_OK;

exit:
	if (config_str)
		free(config_str);
	if (ss)
		db_finalize(&ss);
	if (ret != NB_OK)
		(void)db_execute("ROLLBACK TRANSACTION;");

	return ret;
#else  /* HAVE_CONFIG_ROLLBACKS */
	return NB_OK;
#endif /* HAVE_CONFIG_ROLLBACKS */
}

struct nb_config *nb_db_transaction_load(uint32_t transaction_id)
{
	struct nb_config *config = NULL;
#ifdef HAVE_CONFIG_ROLLBACKS
	struct lyd_node *dnode;
	const char *config_str;
	struct sqlite3_stmt *ss;
	LY_ERR err;

	ss = db_prepare(
		"SELECT\n"
		"  configuration\n"
		"FROM\n"
		"  transactions\n"
		"WHERE\n"
		"  rowid=?;");
	if (!ss)
		return NULL;

	if (db_bindf(ss, "%d", transaction_id) != 0)
		goto exit;

	if (db_run(ss) != SQLITE_ROW)
		goto exit;

	if (db_loadf(ss, "%s", &config_str) != 0)
		goto exit;

	err = lyd_parse_data_mem(ly_native_ctx, config_str, LYD_XML,
				 LYD_PARSE_STRICT | LYD_PARSE_NO_STATE,
				 LYD_VALIDATE_NO_STATE, &dnode);
	if (err || !dnode)
		flog_warn(EC_LIB_LIBYANG, "%s: lyd_parse_data_mem() failed",
			  __func__);
	else
		config = nb_config_new(dnode);

exit:
	db_finalize(&ss);
#endif /* HAVE_CONFIG_ROLLBACKS */

	return config;
}

int nb_db_clear_transactions(unsigned int n_oldest)
{
#ifdef HAVE_CONFIG_ROLLBACKS
	/* Delete oldest N entries. */
	if (db_execute("DELETE\n"
		       "FROM\n"
		       "  transactions\n"
		       "WHERE\n"
		       "  ROWID IN (\n"
		       "    SELECT\n"
		       "      ROWID\n"
		       "    FROM\n"
		       "      transactions\n"
		       "    ORDER BY ROWID ASC LIMIT %u\n"
		       "  );",
		       n_oldest)
	    != 0)
		return NB_ERR;
#endif /* HAVE_CONFIG_ROLLBACKS */

	return NB_OK;
}

int nb_db_set_max_transactions(unsigned int max)
{
#ifdef HAVE_CONFIG_ROLLBACKS
	/*
	 * Delete old entries if necessary and update the SQL trigger that
	 * auto-deletes old entries.
	 */
	if (db_execute("BEGIN TRANSACTION;\n"
		       "  DELETE\n"
		       "  FROM\n"
		       "    transactions\n"
		       "  WHERE\n"
		       "    ROWID IN (\n"
		       "      SELECT\n"
		       "        ROWID\n"
		       "      FROM\n"
		       "        transactions\n"
		       "      ORDER BY ROWID DESC LIMIT -1 OFFSET %u\n"
		       "    );\n"
		       "  DROP TRIGGER delete_tail;\n"
		       "  CREATE TRIGGER delete_tail\n"
		       "  AFTER INSERT ON transactions\n"
		       "    FOR EACH ROW\n"
		       "    BEGIN\n"
		       "    DELETE\n"
		       "    FROM\n"
		       "      transactions\n"
		       "    WHERE\n"
		       "      rowid%%%u=NEW.rowid%%%u AND rowid!=NEW.rowid;\n"
		       "    END;\n"
		       "COMMIT;",
		       max, max, max)
	    != 0)
		return NB_ERR;
#endif /* HAVE_CONFIG_ROLLBACKS */

	return NB_OK;
}

int nb_db_transactions_iterate(void (*func)(void *arg, int transaction_id,
					    const char *client_name,
					    const char *date,
					    const char *comment),
			       void *arg)
{
#ifdef HAVE_CONFIG_ROLLBACKS
	struct sqlite3_stmt *ss;

	/* Send SQL query and parse the result. */
	ss = db_prepare(
		"SELECT\n"
		"  rowid, client, date, comment\n"
		"FROM\n"
		"  transactions\n"
		"ORDER BY\n"
		"  rowid DESC;");
	if (!ss)
		return NB_ERR;

	while (db_run(ss) == SQLITE_ROW) {
		int transaction_id;
		const char *client_name;
		const char *date;
		const char *comment;
		int ret;

		ret = db_loadf(ss, "%i%s%s%s", &transaction_id, &client_name,
			       &date, &comment);
		if (ret != 0)
			continue;

		(*func)(arg, transaction_id, client_name, date, comment);
	}

	db_finalize(&ss);
#endif /* HAVE_CONFIG_ROLLBACKS */

	return NB_OK;
}