diff mbox

[ovs-dev,19/27] ovn-sbctl: Get rid of redundant code by using function from db-ctl-base.

Message ID 20170430232231.15151-20-blp@ovn.org
State Superseded
Headers show

Commit Message

Ben Pfaff April 30, 2017, 11:22 p.m. UTC
This renames get_row() to ctl_get_row() and makes it public.  It's
unfortunate that it adds a cast, but getting rid of redundant code seems
worth it to me.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/db-ctl-base.c         | 24 ++++++++++++------------
 lib/db-ctl-base.h         |  7 ++++++-
 ovn/utilities/ovn-sbctl.c | 31 +++----------------------------
 3 files changed, 21 insertions(+), 41 deletions(-)
diff mbox

Patch

diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
index f844a38f65a0..3177d39472e9 100644
--- a/lib/db-ctl-base.c
+++ b/lib/db-ctl-base.c
@@ -351,10 +351,10 @@  get_row_by_id(struct ctl_context *ctx,
     return final;
 }
 
-static const struct ovsdb_idl_row *
-get_row(struct ctl_context *ctx,
-        const struct ovsdb_idl_table_class *table, const char *record_id,
-        bool must_exist)
+const struct ovsdb_idl_row *
+ctl_get_row(struct ctl_context *ctx,
+            const struct ovsdb_idl_table_class *table, const char *record_id,
+            bool must_exist)
 {
     const struct ovsdb_idl_row *row = NULL;
     struct uuid uuid;
@@ -837,7 +837,7 @@  cmd_get(struct ctl_context *ctx)
     }
 
     table = get_table(table_name);
-    row = get_row(ctx, table, record_id, must_exist);
+    row = ctl_get_row(ctx, table, record_id, must_exist);
     if (!row) {
         return;
     }
@@ -1065,7 +1065,7 @@  cmd_list(struct ctl_context *ctx)
     out = ctx->table = list_make_table(columns, n_columns);
     if (ctx->argc > 2) {
         for (i = 2; i < ctx->argc; i++) {
-            list_record(get_row(ctx, table, ctx->argv[i], must_exist),
+            list_record(ctl_get_row(ctx, table, ctx->argv[i], must_exist),
                         columns, n_columns, out);
         }
     } else {
@@ -1231,7 +1231,7 @@  cmd_set(struct ctl_context *ctx)
     int i;
 
     table = get_table(table_name);
-    row = get_row(ctx, table, record_id, must_exist);
+    row = ctl_get_row(ctx, table, record_id, must_exist);
     if (!row) {
         return;
     }
@@ -1271,7 +1271,7 @@  cmd_add(struct ctl_context *ctx)
 
     table = get_table(table_name);
     die_if_error(get_column(table, column_name, &column));
-    row = get_row(ctx, table, record_id, must_exist);
+    row = ctl_get_row(ctx, table, record_id, must_exist);
     if (!row) {
         return;
     }
@@ -1332,7 +1332,7 @@  cmd_remove(struct ctl_context *ctx)
 
     table = get_table(table_name);
     die_if_error(get_column(table, column_name, &column));
-    row = get_row(ctx, table, record_id, must_exist);
+    row = ctl_get_row(ctx, table, record_id, must_exist);
     if (!row) {
         return;
     }
@@ -1403,7 +1403,7 @@  cmd_clear(struct ctl_context *ctx)
     int i;
 
     table = get_table(table_name);
-    row = get_row(ctx, table, record_id, must_exist);
+    row = ctl_get_row(ctx, table, record_id, must_exist);
     if (!row) {
         return;
     }
@@ -1541,7 +1541,7 @@  cmd_destroy(struct ctl_context *ctx)
         for (i = 2; i < ctx->argc; i++) {
             const struct ovsdb_idl_row *row;
 
-            row = get_row(ctx, table, ctx->argv[i], must_exist);
+            row = ctl_get_row(ctx, table, ctx->argv[i], must_exist);
             if (row) {
                 ovsdb_idl_txn_delete(row);
             }
@@ -1575,7 +1575,7 @@  cmd_wait_until(struct ctl_context *ctx)
 
     table = get_table(table_name);
 
-    row = get_row(ctx, table, record_id, false);
+    row = ctl_get_row(ctx, table, record_id, false);
     if (!row) {
         ctx->try_again = true;
         return;
diff --git a/lib/db-ctl-base.h b/lib/db-ctl-base.h
index 29fea2bd94d9..2838921044bd 100644
--- a/lib/db-ctl-base.h
+++ b/lib/db-ctl-base.h
@@ -257,9 +257,14 @@  struct ctl_row_id {
 };
 
 struct ctl_table_class {
-    struct ctl_row_id row_ids[2];
+    struct ctl_row_id row_ids[3];
 };
 
+const struct ovsdb_idl_row *ctl_get_row(struct ctl_context *,
+                                        const struct ovsdb_idl_table_class *,
+                                        const char *record_id,
+                                        bool must_exist);
+
 void ctl_set_column(const char *table_name,
                     const struct ovsdb_idl_row *, const char *arg,
                     struct ovsdb_symbol_table *);
diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
index c77121e39273..69f461176572 100644
--- a/ovn/utilities/ovn-sbctl.c
+++ b/ovn/utilities/ovn-sbctl.c
@@ -736,39 +736,14 @@  is_partial_uuid_match(const struct uuid *uuid, const char *match)
     return !strncmp(s1, s2, strlen(s2));
 }
 
-static const struct sbrec_datapath_binding *
-lookup_datapath(struct ovsdb_idl *idl, const char *s)
-{
-    struct uuid uuid;
-    if (uuid_from_string(&uuid, s)) {
-        const struct sbrec_datapath_binding *datapath;
-        datapath = sbrec_datapath_binding_get_for_uuid(idl, &uuid);
-        if (datapath) {
-            return datapath;
-        }
-    }
-
-    const struct sbrec_datapath_binding *found = NULL;
-    const struct sbrec_datapath_binding *datapath;
-    SBREC_DATAPATH_BINDING_FOR_EACH (datapath, idl) {
-        const char *name = smap_get(&datapath->external_ids, "name");
-        if (name && !strcmp(name, s)) {
-            if (!found) {
-                found = datapath;
-            } else {
-                ctl_fatal("%s: multiple datapaths with this name", s);
-            }
-        }
-    }
-    return found;
-}
-
 static void
 cmd_lflow_list(struct ctl_context *ctx)
 {
     const struct sbrec_datapath_binding *datapath = NULL;
     if (ctx->argc > 1) {
-        datapath = lookup_datapath(ctx->idl, ctx->argv[1]);
+        datapath = (const struct sbrec_datapath_binding *)
+            ctl_get_row(ctx, &sbrec_table_datapath_binding,
+                        ctx->argv[1], false);
         if (datapath) {
             ctx->argc--;
             ctx->argv++;