api papi: add alias for timestamp(datetime)/timedelta 01/20801/5
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Tue, 23 Jul 2019 13:53:06 +0000 (09:53 -0400)
committerAndrew Yourtchenko <ayourtch@gmail.com>
Wed, 31 Jul 2019 13:20:52 +0000 (13:20 +0000)
Now that we have support for f64:
- create explicit types for timestamp(datetime)/timedelta
- update log_details to use timestamp and remove redundant string representation.
  If you need the string representation, in python do str(timestamp).
  If you prefer the raw f64 value, the client can pass in the
  _no_type_conversion option.

Type: feature

Change-Id: I547b5fa7122d2afa12628b7db0192c23babbbae8
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
src/vpp-api/python/vpp_papi/vpp_format.py
src/vpp/api/api.c
src/vpp/api/vpe.api
src/vpp/api/vpe_types.api
test/test_vpe_api.py

index b58dad3..7d2dcba 100644 (file)
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
+import datetime
 from socket import inet_pton, AF_INET6, AF_INET
 import socket
 import ipaddress
@@ -117,6 +117,11 @@ conversion_table = {
         'MACAddress': lambda o: o.packed,
         'str': lambda s: macaddress.mac_pton(s)
     },
+    'vl_api_timestamp_t':
+    {
+        'datetime.datetime': lambda o:
+        (o - datetime.datetime(1970, 1, 1)).total_seconds()
+    }
 }
 
 
@@ -142,4 +147,6 @@ conversion_unpacker_table = {
     'vl_api_address_t': lambda o: unformat_api_address_t(o),
     'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
     'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),
+    'vl_api_timestamp_t': lambda o: datetime.datetime.fromtimestamp(o),
+    'vl_api_timedelta_t': lambda o: datetime.timedelta(seconds=o),
 }
index 9ae027d..801cf18 100644 (file)
@@ -86,7 +86,7 @@ _(SHOW_THREADS, show_threads)                                                             \
 _(GET_NODE_GRAPH, get_node_graph)                                       \
 _(GET_NEXT_INDEX, get_next_index)                                       \
 _(LOG_DUMP, log_dump)                                                   \
-_(SHOW_VPE_SYSTEM_TIME_TICKS, show_vpe_system_time_ticks)                              \
+_(SHOW_VPE_SYSTEM_TIME, show_vpe_system_time)                          \
 _(GET_F64_ENDIAN_VALUE, get_f64_endian_value)                                                  \
 _(GET_F64_INCREMENT_BY_ONE, get_f64_increment_by_one)                                  \
 
@@ -480,26 +480,23 @@ vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
 
 static void
 show_log_details (vl_api_registration_t * reg, u32 context,
-                 f64 timestamp_ticks, u8 * timestamp,
+                 f64 timestamp,
                  vl_api_log_level_t * level, u8 * msg_class, u8 * message)
 {
   u32 msg_size;
 
   vl_api_log_details_t *rmp;
-  msg_size =
-    sizeof (*rmp) + vec_len (timestamp) + vec_len (msg_class) +
-    vec_len (message);
+  msg_size = sizeof (*rmp) + vec_len (msg_class) + vec_len (message);
 
   rmp = vl_msg_api_alloc (msg_size);
   clib_memset (rmp, 0, msg_size);
   rmp->_vl_msg_id = ntohs (VL_API_LOG_DETAILS);
 
   rmp->context = context;
-  rmp->timestamp_ticks = clib_host_to_net_f64 (timestamp_ticks);
+  rmp->timestamp = clib_host_to_net_f64 (timestamp);
   rmp->level = htonl (*level);
-  char *p = (char *) &rmp->timestamp;
+  char *p = (char *) &rmp->msg_class;
 
-  p += vl_api_vec_to_api_string (timestamp, (vl_api_string_t *) p);
   p += vl_api_vec_to_api_string (msg_class, (vl_api_string_t *) p);
   p += vl_api_vec_to_api_string (message, (vl_api_string_t *) p);
 
@@ -532,8 +529,6 @@ vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
       e = vec_elt_at_index (lm->entries, i);
       if (start_time <= e->timestamp + time_offset)
        show_log_details (reg, mp->context, e->timestamp + time_offset,
-                         format (0, "%U", format_time_float, 0,
-                                 e->timestamp + time_offset),
                          (vl_api_log_level_t *) & e->level,
                          format (0, "%U", format_vlib_log_class, e->class),
                          e->string);
@@ -543,15 +538,14 @@ vl_api_log_dump_t_handler (vl_api_log_dump_t * mp)
 }
 
 static void
-  vl_api_show_vpe_system_time_ticks_t_handler
-  (vl_api_show_vpe_system_time_ticks_t * mp)
+vl_api_show_vpe_system_time_t_handler (vl_api_show_vpe_system_time_t * mp)
 {
   int rv = 0;
-  vl_api_show_vpe_system_time_ticks_reply_t *rmp;
+  vl_api_show_vpe_system_time_reply_t *rmp;
   /* *INDENT-OFF* */
-  REPLY_MACRO2(VL_API_SHOW_VPE_SYSTEM_TIME_TICKS_REPLY,
+  REPLY_MACRO2(VL_API_SHOW_VPE_SYSTEM_TIME_REPLY,
   ({
-    rmp->vpe_system_time_ticks = clib_host_to_net_f64 (unix_time_now ());
+    rmp->vpe_system_time = clib_host_to_net_f64 (unix_time_now ());
   }));
   /* *INDENT-ON* */
 }
index 7c466b9..9531ea5 100644 (file)
@@ -19,7 +19,9 @@
     called through a shared memory interface. 
 */
 
-option version = "1.4.0";
+option version = "1.5.0";
+
+import "vpp/api/vpe_types.api";
 
 /* 
  * Note: API placement cleanup in progress
@@ -283,29 +285,16 @@ define get_next_index_reply
   u32 next_index;
 };
 
-enum log_level {
-  VPE_API_LOG_LEVEL_EMERG = 0,    /* emerg */
-  VPE_API_LOG_LEVEL_ALERT = 1,    /* alert */
-  VPE_API_LOG_LEVEL_CRIT = 2,     /* crit */
-  VPE_API_LOG_LEVEL_ERR = 3,      /* err  */
-  VPE_API_LOG_LEVEL_WARNING = 4,  /* warn */
-  VPE_API_LOG_LEVEL_NOTICE = 5,   /* notice */
-  VPE_API_LOG_LEVEL_INFO = 6,     /* info */
-  VPE_API_LOG_LEVEL_DEBUG = 7,    /* debug */
-  VPE_API_LOG_LEVEL_DISABLED = 8, /* disabled */
-};
-
 define log_dump {
   u32 client_index;
   u32 context;
-  f64 start_timestamp;
+  vl_api_timestamp_t start_timestamp;
 };
 
 define log_details {
   u32 context;
-  f64 timestamp_ticks;
+  vl_api_timestamp_t timestamp;
   vl_api_log_level_t level;
-  string timestamp [limit=24];
   string msg_class [limit=32];
   string message [limit=256];
 };
@@ -314,22 +303,22 @@ define log_details {
     @param client_index - opaque cookie to identify the sender
     @param context - sender context, to match reply w/ request
 */
-define show_vpe_system_time_ticks
+define show_vpe_system_time
 {
   u32 client_index;
   u32 context;
 };
 
-/** \brief Reply for show vpe system time ticks.
+/** \brief Reply for show vpe system time.
     @param context - sender context which was passed in the request
     @param retval - return value
-    @param vpe_system_time_ticks - the time in ticks of the host system.
+    @param vpe_system_time - the time in seconds since epoch of the host system.
 */
-define show_vpe_system_time_ticks_reply
+define show_vpe_system_time_reply
 {
   u32 context;
   i32 retval;
-  f64 vpe_system_time_ticks;
+  vl_api_timestamp_t vpe_system_time;
 };
 
 /** \brief f64 types are not standardized across the wire. Sense wire format in each direction by sending the f64 value 1.0.
index f1d6e54..7bd4b29 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2015-2016 Cisco and/or its affiliates.
+ * Copyright (c) 2019 Vinci Consulting Corp. All Rights Reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 typeonly define version
 {
@@ -11,3 +26,17 @@ typeonly define version
   u8 build_metadata[17];        /* 16 + "\0" */
 };
 
+ typedef f64 timestamp;
+ typedef f64 timedelta;
+
+ enum log_level {
+  VPE_API_LOG_LEVEL_EMERG = 0,    /* emerg */
+  VPE_API_LOG_LEVEL_ALERT = 1,    /* alert */
+  VPE_API_LOG_LEVEL_CRIT = 2,     /* crit */
+  VPE_API_LOG_LEVEL_ERR = 3,      /* err  */
+  VPE_API_LOG_LEVEL_WARNING = 4,  /* warn */
+  VPE_API_LOG_LEVEL_NOTICE = 5,   /* notice */
+  VPE_API_LOG_LEVEL_INFO = 6,     /* info */
+  VPE_API_LOG_LEVEL_DEBUG = 7,    /* debug */
+  VPE_API_LOG_LEVEL_DISABLED = 8, /* disabled */
+};
index 81c9d9a..54f7e41 100644 (file)
 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
+import datetime
 import time
 import unittest
 from framework import VppTestCase
 
+enable_print = False
+
 
 class TestVpeApi(VppTestCase):
     """TestVpeApi"""
@@ -22,25 +25,31 @@ class TestVpeApi(VppTestCase):
     def test_log_dump_default(self):
         rv = self.vapi.cli('test log notice fib entry this is a test')
         rv = self.vapi.log_dump()
-        # print('\n'.join([str(v) for v in rv]))
+        if enable_print:
+            print('\n'.join([str(v) for v in rv]))
         self.assertTrue(rv)
 
     def test_log_dump_timestamp_0(self):
         rv = self.vapi.cli('test log notice fib entry this is a test')
         rv = self.vapi.log_dump(start_timestamp=0.0)
-        # print('\n'.join([str(v) for v in rv]))
+        if enable_print:
+            print('\n'.join([str(v) for v in rv]))
         self.assertTrue(rv)
 
-    @unittest.skip('Requires https://gerrit.fd.io/r/#/c/19581/ '
-                   'to use timestamp_ticks.')
     def test_log_dump_timestamp_future(self):
         rv = self.vapi.cli('test log debug fib entry test')
         rv = self.vapi.log_dump(start_timestamp=time.time() + 60.0)
-        # print('\n'.join([str(v) for v in rv]))
+        if enable_print:
+            print('\n'.join([str(v) for v in rv]))
         self.assertFalse(rv)
 
-    @unittest.skip('Requires https://gerrit.fd.io/r/#/c/19581/ '
-                   'to use timestamp_ticks.')
-    def test_show_vpe_system_time_ticks(self):
-        rv = self.vapi.show_vpe_system_time_ticks()
-        self.assertTrue(rv.vpe_system_time_ticks > 1.0)
+    def test_show_vpe_system_time(self):
+        local_start_time = datetime.datetime.now()
+        rv = self.vapi.show_vpe_system_time()
+        self.assertTrue(rv.vpe_system_time > local_start_time -
+                        datetime.timedelta(hours=1.0),
+                        'system times differ by more than an hour.')
+        if enable_print:
+            print('\n'.join([str(v) for v in rv]))
+            print('%r %s' % (rv.vpe_system_time,
+                             rv.vpe_system_time))