diff mbox

[ovs-dev] ovs-vsctl: Support identifying Flow_Sample_Collector_Set records by id.

Message ID 1465597143-28897-1-git-send-email-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff June 10, 2016, 10:19 p.m. UTC
This allows commands like
    ovs-vsctl list Flow_Sample_Collector_Set 123
if there's a record with id 123.  It's not perfect, since there can be
more than one record with the same id, but it's helpful.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/db-ctl-base.c     | 35 ++++++++++++++++++++++++-----------
 utilities/ovs-vsctl.c |  4 +++-
 2 files changed, 27 insertions(+), 12 deletions(-)

Comments

Andy Zhou June 11, 2016, 4 a.m. UTC | #1
On Fri, Jun 10, 2016 at 3:19 PM, Ben Pfaff <blp@ovn.org> wrote:

> This allows commands like
>     ovs-vsctl list Flow_Sample_Collector_Set 123
> if there's a record with id 123.  It's not perfect, since there can be
> more than one record with the same id, but it's helpful.
>

> Signed-off-by: Ben Pfaff <blp@ovn.org>
>

Tested, it works.
 Acked-by: Andy Zhou <azhou@ovn.org>


> ---
>  lib/db-ctl-base.c     | 35 ++++++++++++++++++++++++-----------
>  utilities/ovs-vsctl.c |  4 +++-
>  2 files changed, 27 insertions(+), 12 deletions(-)
>
> diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
> index ffef041..9f50c6c 100644
> --- a/lib/db-ctl-base.c
> +++ b/lib/db-ctl-base.c
> @@ -1,5 +1,5 @@
>  /*
> - * Copyright (c) 2015 Nicira, Inc.
> + * Copyright (c) 2015, 2016 Nicira, Inc.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");
>   * you may not use this file except in compliance with the License.
> @@ -254,18 +254,30 @@ get_row_by_id(struct ctl_context *ctx, const struct
> ctl_table_class *table,
>              return NULL;
>          }
>      } else {
> -        const struct ovsdb_idl_row *row;
> -
>          referrer = NULL;
> -        for (row = ovsdb_idl_first_row(ctx->idl, id->table);
> -             row != NULL;
> -             row = ovsdb_idl_next_row(row))
> -            {
> -                const struct ovsdb_datum *name;
>
> -                name = ovsdb_idl_get(row, id->name_column,
> -                                     OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
> -                if (name->n == 1 && !strcmp(name->keys[0].string,
> record_id)) {
> +        ovs_assert(id->name_column->type.value.type == OVSDB_TYPE_VOID);
> +
> +        enum ovsdb_atomic_type key = id->name_column->type.key.type;
> +        if (key == OVSDB_TYPE_INTEGER) {
> +            if (!record_id[0] || record_id[strspn(record_id,
> "0123456789")]) {
> +                return NULL;
> +            }
> +        } else {
> +            ovs_assert(key == OVSDB_TYPE_STRING);
> +        }
> +
> +        for (const struct ovsdb_idl_row *row =
> ovsdb_idl_first_row(ctx->idl,
> +
>  id->table);
> +             row != NULL;
> +             row = ovsdb_idl_next_row(row)) {
> +            const struct ovsdb_datum *name = ovsdb_idl_get(
> +                row, id->name_column, key, OVSDB_TYPE_VOID);
> +            if (name->n == 1) {
> +                const union ovsdb_atom *atom = &name->keys[0];
> +                if (key == OVSDB_TYPE_STRING
> +                    ? !strcmp(atom->string, record_id)
> +                    : atom->integer == strtoll(record_id, NULL, 10)) {
>                      if (referrer) {
>                          ctl_fatal("multiple rows in %s match \"%s\"",
>                                    table->class->name, record_id);
> @@ -273,6 +285,7 @@ get_row_by_id(struct ctl_context *ctx, const struct
> ctl_table_class *table,
>                      referrer = row;
>                  }
>              }
> +        }
>      }
>      if (!referrer) {
>          return NULL;
> diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
> index c9c0f6d..4d7f9f9 100644
> --- a/utilities/ovs-vsctl.c
> +++ b/utilities/ovs-vsctl.c
> @@ -2348,7 +2348,9 @@ static const struct ctl_table_class tables[] = {
>        {NULL, NULL, NULL}}},
>
>      {&ovsrec_table_flow_sample_collector_set,
> -     {{NULL, NULL, NULL},
> +     {{&ovsrec_table_flow_sample_collector_set,
> +       &ovsrec_flow_sample_collector_set_col_id,
> +       NULL},
>        {NULL, NULL, NULL}}},
>
>      {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
> --
> 2.1.3
>
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev
>
Ben Pfaff June 13, 2016, 9:47 p.m. UTC | #2
On Fri, Jun 10, 2016 at 09:00:21PM -0700, Andy Zhou wrote:
> On Fri, Jun 10, 2016 at 3:19 PM, Ben Pfaff <blp@ovn.org> wrote:
> 
> > This allows commands like
> >     ovs-vsctl list Flow_Sample_Collector_Set 123
> > if there's a record with id 123.  It's not perfect, since there can be
> > more than one record with the same id, but it's helpful.
> >
> 
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> >
> 
> Tested, it works.
>  Acked-by: Andy Zhou <azhou@ovn.org>

Thanks!
diff mbox

Patch

diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
index ffef041..9f50c6c 100644
--- a/lib/db-ctl-base.c
+++ b/lib/db-ctl-base.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2015 Nicira, Inc.
+ * Copyright (c) 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -254,18 +254,30 @@  get_row_by_id(struct ctl_context *ctx, const struct ctl_table_class *table,
             return NULL;
         }
     } else {
-        const struct ovsdb_idl_row *row;
-
         referrer = NULL;
-        for (row = ovsdb_idl_first_row(ctx->idl, id->table);
-             row != NULL;
-             row = ovsdb_idl_next_row(row))
-            {
-                const struct ovsdb_datum *name;
 
-                name = ovsdb_idl_get(row, id->name_column,
-                                     OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
-                if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
+        ovs_assert(id->name_column->type.value.type == OVSDB_TYPE_VOID);
+
+        enum ovsdb_atomic_type key = id->name_column->type.key.type;
+        if (key == OVSDB_TYPE_INTEGER) {
+            if (!record_id[0] || record_id[strspn(record_id, "0123456789")]) {
+                return NULL;
+            }
+        } else {
+            ovs_assert(key == OVSDB_TYPE_STRING);
+        }
+
+        for (const struct ovsdb_idl_row *row = ovsdb_idl_first_row(ctx->idl,
+                                                                   id->table);
+             row != NULL;
+             row = ovsdb_idl_next_row(row)) {
+            const struct ovsdb_datum *name = ovsdb_idl_get(
+                row, id->name_column, key, OVSDB_TYPE_VOID);
+            if (name->n == 1) {
+                const union ovsdb_atom *atom = &name->keys[0];
+                if (key == OVSDB_TYPE_STRING
+                    ? !strcmp(atom->string, record_id)
+                    : atom->integer == strtoll(record_id, NULL, 10)) {
                     if (referrer) {
                         ctl_fatal("multiple rows in %s match \"%s\"",
                                   table->class->name, record_id);
@@ -273,6 +285,7 @@  get_row_by_id(struct ctl_context *ctx, const struct ctl_table_class *table,
                     referrer = row;
                 }
             }
+        }
     }
     if (!referrer) {
         return NULL;
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index c9c0f6d..4d7f9f9 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -2348,7 +2348,9 @@  static const struct ctl_table_class tables[] = {
       {NULL, NULL, NULL}}},
 
     {&ovsrec_table_flow_sample_collector_set,
-     {{NULL, NULL, NULL},
+     {{&ovsrec_table_flow_sample_collector_set,
+       &ovsrec_flow_sample_collector_set_col_id,
+       NULL},
       {NULL, NULL, NULL}}},
 
     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}