Guest User

test.py

a guest
Sep 16th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import os
  4. import fnmatch
  5. import time
  6. import sys
  7.  
  8. from vpp_papi import VPP
  9.  
  10. simple_counter_type = [
  11.     'VNET_INTERFACE_COUNTER_DROP',
  12.     'VNET_INTERFACE_COUNTER_PUNT',
  13.     'VNET_INTERFACE_COUNTER_IP4',
  14.     'VNET_INTERFACE_COUNTER_IP6',
  15.     'VNET_INTERFACE_COUNTER_RX_NO_BUF',
  16.     'VNET_INTERFACE_COUNTER_RX_MISS',
  17.     'VNET_INTERFACE_COUNTER_RX_ERROR',
  18.     'VNET_INTERFACE_COUNTER_TX_ERROR',
  19.     'VNET_INTERFACE_COUNTER_MPLS',
  20.     'VNET_N_SIMPLE_INTERFACE_COUNTER']
  21.  
  22. combined_counter_type = [
  23.     'VNET_INTERFACE_COUNTER_RX',
  24.     'VNET_INTERFACE_COUNTER_RX_UNICAST',
  25.     'VNET_INTERFACE_COUNTER_RX_MULTICAST',
  26.     'VNET_INTERFACE_COUNTER_RX_BROADCAST',
  27.     'VNET_INTERFACE_COUNTER_TX',
  28.     'VNET_INTERFACE_COUNTER_TX_UNICAST',
  29.     'VNET_INTERFACE_COUNTER_TX_MULTICAST',
  30.     'VNET_INTERFACE_COUNTER_TX_BROADCAST',
  31.     'VNET_N_COMBINED_INTERFACE_COUNTER']
  32.  
  33.  
  34. # event handler function to handle reply messages
  35. # it handle two type of message currently
  36. # received value is a struct that could be parsed
  37. def papi_event_handler(msgname, result):
  38.     idx = 0
  39.     if msgname == 'vnet_interface_combined_counters':
  40.         values = []
  41.         # format string will let you to print output by specific format,
  42.         # {0:^11s} means 0 args of format function, it's type is string and maximum length is 11. ^ means align center.
  43.         print('{0:^11s}{1:^40s}{2:^40s}{3:^20s}{4:^20s}'.format('sw_if_index', 'Counter', 'Type', 'Packets', 'Bytes'))
  44.         print('=' * 131)
  45.         for row in result.data:
  46.             print('{0:^11d}{1:^40s}{2:^40s}{3:^20d}{4:^20d}'.format(idx, msgname, combined_counter_type[result.vnet_counter_type], row.packets,
  47.                                                                     row.bytes))
  48.             values.append((row.packets, row.bytes))
  49.             idx += 1
  50.  
  51.     elif msgname == 'vnet_interface_simple_counters':
  52.         values = []
  53.         # format string will let you to print output by specific format,
  54.         # {0:^11s} means 0 args of format function, it's type is string and maximum length is 11. ^ means align center.
  55.         print('{0:^11s}{1:^40s}{2:^40s}{3:^20s}'.format('sw_if_index', 'Counter', 'Type', 'Packets'))
  56.         print('=' * 111)
  57.         for row in result.data:
  58.             print('{0:^11d}{1:^40s}{2:^40s}{3:^20d}'.format(idx, msgname, simple_counter_type[result.vnet_counter_type], row))
  59.  
  60.             values.append(row)
  61.             idx += 1
  62.  
  63.     print()
  64.  
  65. # set the LD_LIBRARY_PATH such that it points to the directory containing libvppapiclient.so
  66. os.environ['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/'
  67.  
  68. # directory containing all the json api files.
  69. # if vpp is installed on the system, these will be in /usr/share/vpp/api/
  70. vpp_json_dir = '/usr/share/vpp/api/'
  71.  
  72. # construct a list of all the json api files
  73. jsonfiles = []
  74. for root, dirnames, filenames in os.walk(vpp_json_dir):
  75.     for filename in fnmatch.filter(filenames, '*.api.json'):
  76.         jsonfiles.append(os.path.join(vpp_json_dir, filename))
  77.  
  78. if not jsonfiles:
  79.     print('Error: no json api files found')
  80.     sys.exit(-1)
  81.  
  82. # use all those files to create vpp.
  83. # Note that there will be no vpp method available before vpp.connect()
  84. vpp = VPP(jsonfiles)
  85. rv = vpp.connect("test_papi")
  86. # print(rv)
  87.  
  88. # print all specification (variables and functions) that exist in vpp.api
  89. # for func in dir(vpp.api):
  90. #     print(func)
  91. # print()
  92.  
  93. interface_details = vpp.api.sw_interface_dump()
  94.  
  95. # set program asynchronous
  96. async = True
  97.  
  98. # register an event handler to receive reply messages of an api
  99. rv = vpp.register_event_callback(papi_event_handler)
  100.  
  101. # enable want_stats api in vpp to receive stats
  102. rv = vpp.api.want_stats(enable_disable=True)
  103. # print(rv)
  104.  
  105. # lifetime of program
  106. time.sleep(10)
  107.  
  108. # enable want_stats api in vpp to receive stats
  109. rv = vpp.api.want_stats(enable_disable=False)
  110. # print(rv)
  111.  
  112. # disconnect from vpp
  113. rv = vpp.disconnect()
  114. # print(rv)
  115.  
  116. sys.exit(rv)
Advertisement
Add Comment
Please, Sign In to add comment