eaa6bc6ca703f081371c0a9d8fae3e8b18accf32
[cascardo/ovs.git] / lib / db-ctl-base.h
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef DB_CTL_BASE_H
18 #define DB_CTL_BASE_H 1
19
20 #include "compiler.h"
21 #include "dynamic-string.h"
22 #include "shash.h"
23
24 struct ctl_context;
25 struct option;
26 struct ovsdb_idl;
27 struct ovsdb_idl_row;
28 struct ovsdb_idl_txn;
29 struct ovsdb_symbol_table;
30 struct table;
31
32 /* This library module contains the common parts for ovsdb manipulation
33  * (structs, commands and functions).  To utilize this module, user must
34  * define the following:
35  *
36  * - the 'the_idl' and 'the_idl_txn'.
37  *
38  * - the command syntaxes for each command.  (See 'struct ctl_command_syntax'
39  *   for more info)  and regiters them using ctl_register_commands().
40  *
41  * - the *ctl command context by inheriting the 'struct ctl_context' for
42  *   additional commands implemented by user.  (See 'struct ctl_context' for
43  *   more info)
44  *
45  * - the 'tables[]' for each table in the schema.
46  *
47 */
48
49 /* ctl_fatal() also logs the error, so it is preferred in this file. */
50 #define ovs_fatal please_use_ctl_fatal_instead_of_ovs_fatal
51
52 /* idl and idl transaction to be used for the *ctl command execution.
53  * User must provide them.  */
54 extern struct ovsdb_idl *the_idl;
55 extern struct ovsdb_idl_txn *the_idl_txn;
56
57 void ctl_init(void);
58 char *ctl_default_db(void);
59 OVS_NO_RETURN void ctl_exit(int status);
60 OVS_NO_RETURN void ctl_fatal(const char *, ...) OVS_PRINTF_FORMAT(1, 2);
61
62 /* *clt command syntax structure, to be defined by each command implementation.
63  *
64  * Execution Path
65  * ==============
66  *
67  * Three stylized functions accompany each of these data structures:
68  *
69  *               "pre-run"         "run"        "post-run"
70  *            ---------------  ------------  -----------------
71  * *ctl       ->prerequisites     ->run        ->postprocess
72  *
73  * Any *clt command implementation should go through the following execution
74  * path:
75  *
76  *   1. parses user command-line input and finds the corresponding syntax
77  *      structures.
78  *
79  *   2. calls prerequisites() for getting the columns or tables used by each
80  *      command.
81  *
82  *   3. calls run() to execute each command and to generate output.
83  *
84  *   4. calls postprocess() after output has been committed.  (Only needed
85  *      by 'create' command sofar)
86  *
87  * Execution Context
88  * =================
89  *
90  * Each of the stylized functions requires the 'struct ctl_context' as input
91  * to provide context e.g. command-line arguments, table to be modified.  User
92  * may define more specific context (by inheriting 'struct ctl_context') and
93  * write stylized functions that use it.  In that case, CONTAINER_OF() can
94  * be used to cast the generic context to the specific one.
95  *
96  * */
97 struct ctl_command_syntax {
98     const char *name;           /* e.g. "add-br" */
99     int min_args;               /* Min number of arguments following name. */
100     int max_args;               /* Max number of arguments following name. */
101
102     /* Names that roughly describe the arguments that the command
103      * uses.  These should be similar to the names displayed in the
104      * man page or in the help output. */
105     const char *arguments;
106
107     /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
108      * each column or table in ctx->idl that it uses. */
109     void (*prerequisites)(struct ctl_context *ctx);
110
111     /* Does the actual work of the command and puts the command's output, if
112      * any, in ctx->output or ctx->table.
113      *
114      * Alternatively, if some prerequisite of the command is not met and the
115      * caller should wait for something to change and then retry, it may set
116      * ctx->try_again to true.  (Only the "wait-until" command currently does
117      * this.) */
118     void (*run)(struct ctl_context *ctx);
119
120     /* If nonnull, called after the transaction has been successfully
121      * committed.  ctx->output is the output from the "run" function, which
122      * this function may modify and otherwise postprocess as needed.  (Only the
123      * "create" command currently does any postprocessing.) */
124     void (*postprocess)(struct ctl_context *ctx);
125
126     /* A comma-separated list of supported options, e.g. "--a,--b", or the
127      * empty string if the command does not support any options. */
128     const char *options;
129
130     enum { RO, RW } mode;       /* Does this command modify the database? */
131 };
132
133 /* A command extracted from command-line input plus the structs for
134  * output generation. */
135 struct ctl_command {
136     /* Data that remains constant after initialization. */
137     const struct ctl_command_syntax *syntax;
138     int argc;
139     char **argv;
140     struct shash options;
141
142     /* Data modified by commands. */
143     struct ds output;
144     struct table *table;
145 };
146
147 bool ctl_might_write_to_db(char **argv);
148 const char *ctl_get_db_cmd_usage(void);
149 void ctl_print_commands(void);
150 void ctl_print_options(const struct option *);
151 void ctl_add_cmd_options(struct option **, size_t *n_options_p,
152                          size_t *allocated_options_p, int opt_val);
153 void ctl_register_commands(const struct ctl_command_syntax *);
154 const struct shash *ctl_get_all_commands(void);
155 struct ctl_command *ctl_parse_commands(int argc, char *argv[],
156                                        struct shash *local_options,
157                                        size_t *n_commandsp);
158 \f
159 /* The base context struct for conducting the common database
160  * operations (commands listed in 'db_ctl_commands').  User should
161  * define the per-schema context by inheriting this struct as base.
162  *
163  * Database Caches
164  * ===============
165  *
166  * User may implement caches for contents of the database to facilitate
167  * specific commands.  In that case, the common commands defined in
168  * 'db_ctl_commands' that may invalidate the cache must call the
169  * invalidate_cache().
170  *
171  **/
172 struct ctl_context {
173     /* Read-only. */
174     int argc;
175     char **argv;
176     struct shash options;
177
178     /* Modifiable state. */
179     struct ds output;
180     struct table *table;
181     struct ovsdb_idl *idl;
182     struct ovsdb_idl_txn *txn;
183     struct ovsdb_symbol_table *symtab;
184
185     /* For implementation with a cache of the contents of the database,
186      * this function will be called when the database is changed and the
187      * change makes the cache no longer valid. */
188     void (*invalidate_cache)(struct ctl_context *);
189
190     /* A command may set this member to true if some prerequisite is not met
191      * and the caller should wait for something to change and then retry. */
192     bool try_again;
193 };
194
195 void ctl_context_init_command(struct ctl_context *, struct ctl_command *);
196 void ctl_context_init(struct ctl_context *, struct ctl_command *,
197                       struct ovsdb_idl *, struct ovsdb_idl_txn *,
198                       struct ovsdb_symbol_table *,
199                       void (*invalidate_cache)(struct ctl_context *));
200 void ctl_context_done_command(struct ctl_context *, struct ctl_command *);
201 void ctl_context_done(struct ctl_context *, struct ctl_command *);
202
203 struct ctl_row_id {
204     const struct ovsdb_idl_table_class *table;
205     const struct ovsdb_idl_column *name_column;
206     const struct ovsdb_idl_column *uuid_column;
207 };
208
209 struct ctl_table_class {
210     struct ovsdb_idl_table_class *class;
211     struct ctl_row_id row_ids[2];
212 };
213
214 /* Represents all tables in the schema.  User must define 'tables'
215  * in implementation.  And the definition must end with an all-NULL
216  * entry. */
217 extern const struct ctl_table_class tables[];
218
219 const struct ctl_table_class * get_table(const char *table_name);
220 void set_column(const struct ctl_table_class *,
221                 const struct ovsdb_idl_row *, const char *arg,
222                 struct ovsdb_symbol_table *);
223
224 #endif /* db-ctl-base.h */