-
Notifications
You must be signed in to change notification settings - Fork 390
/
vrouter_api.py
192 lines (165 loc) · 6.43 KB
/
vrouter_api.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright (c) 2014 Juniper Networks, Inc
import logging
import uuid
from thrift.protocol import TBinaryProtocol
from thrift.transport import TTransport
from gen_py.instance_service import InstanceService, ttypes
class ContrailVRouterApi(object):
def __init__(self, server_port=9090, doconnect=False, semaphore=None):
"""
local variables:
_client: current transport connection
_ports: dictionary of active ports keyed by vif uuid.
"""
self._server_port = server_port
self._client = None
self._ports = {}
self._connect = doconnect
self._semaphore = semaphore
def _rpc_client_instance(self):
""" Return an RPC client connection """
import thrift.transport.TSocket as TSocket
socket = TSocket.TSocket('localhost', self._server_port)
try:
transport = TTransport.TFramedTransport(socket)
transport.open()
except TTransport.TTransportException:
logging.error('Unable to connect to contrail-vrouter-agent '
'localhost:%d' % self._server_port)
return None
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = InstanceService.Client(protocol)
if client and self._connect:
client.Connect()
return client
def _resynchronize(self):
""" Add all the active ports to the agent """
self._client.AddPort([port for port in self._ports.itervalues()])
def _uuid_from_string(self, idstr):
""" Convert an uuid string into an uuid object """
if not idstr:
return None
return uuid.UUID(idstr)
def _uuid_to_hex(self, id):
""" Convert an uuid into an array of integers """
hexstr = id.hex
return [int(hexstr[i:i + 2], 16) for i in range(32) if i % 2 == 0]
def _uuid_string_to_hex(self, idstr):
return self._uuid_to_hex(self._uuid_from_string(idstr))
def connect(self):
if self._client is None:
self._client = self._rpc_client_instance()
if self._client is None:
return False
if self._connect:
return True
return self._client.Connect()
def add_port(self, vm_uuid_str, vif_uuid_str, interface_name, mac_address,
**kwargs):
"""
Add a port to the agent. The information is stored in the _ports
dictionary since the vrouter agent may not be running at the
moment or the RPC may fail.
"""
try:
if self._semaphore:
self._semaphore.acquire()
vif_uuid = self._uuid_from_string(vif_uuid_str)
# ip_address and network_uuid are optional to this API but must
# be present in the message. For instance, when running in
# CloudStack/XenServer/XAPI these arguments are not known.
if 'ip_address' in kwargs:
ip_address = kwargs['ip_address']
else:
ip_address = '0.0.0.0'
if 'vn_id' in kwargs:
network_uuid = self._uuid_string_to_hex(kwargs['vn_id'])
else:
network_uuid = [0] * 16
# create port with mandatory arguments
data = ttypes.Port(
self._uuid_to_hex(vif_uuid),
self._uuid_string_to_hex(vm_uuid_str),
interface_name,
ip_address,
network_uuid,
mac_address)
if 'display_name' in kwargs:
data.display_name = kwargs['display_name']
if 'hostname' in kwargs:
data.hostname = kwargs['hostname']
if 'vm_project_id' in kwargs:
data.vm_project_id = self._uuid_string_to_hex(
kwargs['vm_project_id'])
if ('port_type' in kwargs and
kwargs['port_type'] in ttypes.PortTypes._NAMES_TO_VALUES.keys()):
data.port_type = \
ttypes.PortTypes._NAMES_TO_VALUES[kwargs['port_type']]
else:
data.port_type = ttypes.PortTypes.NameSpacePort
if 'ip6_address' in kwargs:
data.ip6_address = kwargs['ip6_address']
if 'vlan' in kwargs:
data.vlan_id = kwargs['vlan']
data.validate()
if self._client is None:
self._client = self._rpc_client_instance()
if self._client is None:
self._ports[vif_uuid] = data
return False
self._resynchronize()
self._ports[vif_uuid] = data
try:
result = self._client.AddPort([data])
except:
self._client = None
raise
return result
finally:
if self._semaphore:
self._semaphore.release()
def delete_port(self, vif_uuid_str):
"""
Delete a port form the agent. The port is first removed from the
internal _ports dictionary.
"""
try:
if self._semaphore:
self._semaphore.acquire()
vif_uuid = self._uuid_from_string(vif_uuid_str)
self._ports.pop(vif_uuid, None)
if self._client is None:
self._client = self._rpc_client_instance()
if self._client is None:
return
self._resynchronize()
try:
self._client.DeletePort(self._uuid_to_hex(vif_uuid))
except:
self._client = None
finally:
if self._semaphore:
self._semaphore.release()
def periodic_connection_check(self):
"""
Periodicly check if the connection to the agent is valid.
It is the API client's resposibility to periodically invoke this
method.
"""
try:
if self._semaphore:
self._semaphore.acquire()
if self._client is None:
self._client = self._rpc_client_instance()
if self._client is None:
return
self._resynchronize()
try:
if self._client:
self._client.KeepAliveCheck()
except Exception as ex:
self._client = None
logging.exception(ex)
finally:
if self._semaphore:
self._semaphore.release()