http_static: add .json content 56/22656/2
authorDave Barach <dave@barachs.net>
Thu, 10 Oct 2019 17:29:35 +0000 (13:29 -0400)
committerFlorin Coras <florin.coras@gmail.com>
Thu, 10 Oct 2019 20:53:43 +0000 (20:53 +0000)
Type: feature

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: Ibf8583175c181dc12a589b3e6c11a10ee77fc160

extras/http/startup.cfg
src/plugins/builtinurl/builtins.c

index a7b9dc3..b2a50d3 100644 (file)
@@ -1,5 +1,7 @@
 unix {
      interactive
+     poll-sleep-usec 500
+     startup-config /scratch/vpp-http/extras/http/setup.http
 }
 
 tls {
index 7358222..471fe58 100644 (file)
@@ -81,8 +81,6 @@ handle_get_interface_stats (u8 * request, http_session_t * hs)
   p = hash_get (vnm->interface_main.hw_interface_by_name, request);
   if (!p)
     {
-      clib_warning ("Couldn't find interface '%v'", request);
-
       s = format (s, "{\"interface_stats\": {");
       s = format (s, "   \"name\": \"%s\",", request);
       s = format (s, "   \"stats\": \"%s\"", "ERRORUnknownInterface");
@@ -95,6 +93,16 @@ handle_get_interface_stats (u8 * request, http_session_t * hs)
 
   stats = format_vnet_sw_interface_cntrs (stats, &vnm->interface_main, si,
                                          1 /* want json */ );
+
+  /* No active counters */
+  if (stats == 0)
+    {
+      s = format (s, "{\"interface_stats\": {");
+      s = format (s, "   \"name\": \"%s\"", request);
+      s = format (s, "}}\r\n");
+      goto out;
+    }
+
   /* Build answer */
   s = format (s, "{\"interface_stats\": {");
   s = format (s, "\"name\": \"%s\",\n", request);
@@ -110,12 +118,55 @@ out:
   return 0;
 }
 
+int
+handle_get_interface_list (u8 * request, http_session_t * hs)
+{
+  u8 *s = 0;
+  int i;
+  vnet_main_t *vnm = vnet_get_main ();
+  vnet_interface_main_t *im = &vnm->interface_main;
+  vnet_hw_interface_t *hi;
+  u32 *hw_if_indices = 0;
+  int need_comma = 0;
+
+  /* Construct vector of active hw_if_indexes ... */
+  /* *INDENT-OFF* */
+  pool_foreach (hi, im->hw_interfaces,
+  ({
+    /* No point in mentioning "local0"... */
+    if (hi - im->hw_interfaces)
+      vec_add1 (hw_if_indices, hi - im->hw_interfaces);
+  }));
+  /* *INDENT-ON* */
+
+  /* Build answer */
+  s = format (s, "{\"interface_list\": [\n");
+  for (i = 0; i < vec_len (hw_if_indices); i++)
+    {
+      if (need_comma)
+       s = format (s, ",\n");
+      hi = pool_elt_at_index (im->hw_interfaces, hw_if_indices[i]);
+      s = format (s, "\"%v\"", hi->name);
+      need_comma = 1;
+    }
+  s = format (s, "]}\n");
+  vec_free (hw_if_indices);
+
+  hs->data = s;
+  hs->data_offset = 0;
+  hs->cache_pool_index = ~0;
+  hs->free_data = 1;
+  return 0;
+}
+
 void
 builtinurl_handler_init (builtinurl_main_t * bm)
 {
 
   bm->register_handler (handle_get_version, "version.json",
                        HTTP_BUILTIN_METHOD_GET);
+  bm->register_handler (handle_get_interface_list, "interface_list.json",
+                       HTTP_BUILTIN_METHOD_GET);
   bm->register_handler (handle_get_interface_stats,
                        "interface_stats.json", HTTP_BUILTIN_METHOD_POST);
 }