-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvppapi.py
More file actions
83 lines (65 loc) · 2.13 KB
/
Copy pathvppapi.py
File metadata and controls
83 lines (65 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'''
The functions in this file interact with the VPP API to retrieve certain
interface metadata.
'''
from vpp_papi import VPPApiClient
import os
import fnmatch
import logging
class NullHandler(logging.Handler):
def emit(self, record):
pass
logger = logging.getLogger('agentx.vppapi')
logger.addHandler(NullHandler())
class VPPApi():
def __init__(self, address='/run/vpp/api.sock', clientname='vppapi-client'):
self.address = address
self.connected = False
self.clientname = clientname
self.vpp = None
def connect(self):
if self.connected:
return True
vpp_json_dir = '/usr/share/vpp/api/'
# construct a list of all the json api files
jsonfiles = []
for root, dirnames, filenames in os.walk(vpp_json_dir):
for filename in fnmatch.filter(filenames, '*.api.json'):
jsonfiles.append(os.path.join(root, filename))
if not jsonfiles:
logger.error('no json api files found')
return False
self.vpp = VPPApiClient(apifiles=jsonfiles,
server_address=self.address)
try:
logger.info('Connecting to VPP')
self.vpp.connect(self.clientname)
except:
return False
v = self.vpp.api.show_version()
logger.info('VPP version is %s' % v.version)
self.connected = True
return True
def disconnect(self):
if not self.connected:
return True
self.vpp.disconnect()
self.connected = False
return True
def get_ifaces(self):
ret = {}
if not self.connected:
return ret
try:
iface_list = self.vpp.api.sw_interface_dump()
except Exception as e:
logger.error("VPP communication error, disconnecting", e)
self.vpp.disconnect()
self.connected = False
return ret
if not iface_list:
logger.error("Can't get interface list")
return ret
for iface in iface_list:
ret[iface.interface_name] = iface
return ret