-
Notifications
You must be signed in to change notification settings - Fork 4
/
ovsdb.py
131 lines (111 loc) · 4 KB
/
ovsdb.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
import sys
import Queue
import socket
import json
from select import select
OVSDB_IP = '127.0.0.1'
OVSDB_PORT = 6632
DEFAULT_DB = 'Open_vSwitch'
BUFFER_SIZE = 4096
# TODO: Could start by getting the DB name and using that for ongoing requests
# TODO: How to keep an eye out for montor, update, echo messages?
def gather_reply(socket):
print "Waiting for reply"
result = ""
# we got the whole thing if we received all the fields
while "error" not in result or "id" not in result or "result" not in result:
reply = socket.recv(BUFFER_SIZE)
result += reply
return json.loads(result)
def listen_for_messages(sock, message_queues):
# To send something, add a message to queue and append sock to outputs
inputs = [sock, sys.stdin]
outputs = []
while sock:
readable, writable, exceptional = select(inputs, outputs, [])
for s in readable:
if s is sock:
data = sock.recv(4096)
# should test if its echo, if so, reply
# message_type = get_msg_type(data)
# if message_type is "echo":
# send_echo(message_
message_queues[sock].put(data)
outputs.append(sock)
print "recv:" + data
elif s is sys.stdin:
print sys.stdin.readline()
sock.close()
return
else:
print "error"
for w in writable:
if w is sock:
sock.send(message_queues[sock].get_nowait())
outputs.remove(sock)
else:
print "error"
def list_dbs():
list_dbs_query = {"method":"list_dbs", "params":[], "id": 0}
return json.dumps(list_dbs_query)
def get_schema(socket, db = DEFAULT_DB, current_id = 0):
list_schema = {"method": "get_schema", "params":[db_name], "id": current_id}
socket.send(json.dumps(list_schema))
result = gather_reply(socket)
return result
def get_schema_version(socket, db = DEFAULT_DB):
db_schema = get_schema(socket, db)
return db_schema['version']
def list_tables(server, db):
# keys that are under 'tables'
db_schema = get_schema(socket, db)
# return db_schema['tables'].keys
return json.loads()
def list_columns(server, db):
return
def transact(server, transactions):
# Variants of this will add stuff
return
def monitor(columns, monitor_id = None, db = DEFAULT_DB):
msg = {"method":"monitor", "params":[db, monitor_id, columns], "id":0}
return json.dumps(msg)
def monitor_cancel():
return
def locking():
return
def echo():
echo_msg = {"method":"echo","id":"echo","params":[]}
return json.dumps(echo_msg)
def dump(server, db):
return
def list_bridges(db = DEFAULT_DB):
# What if we replaced with a more specific query
# columns = {"Bridge":{"name"}}
columns = {"Port":{"columns":["fake_bridge","interfaces","name","tag"]},"Controller":{"columns":[]},"Interface":{"columns":["name"]},"Open_vSwitch":{"columns":["bridges","cur_cfg"]},"Bridge":{"columns":["controller","fail_mode","name","ports"]}}
# TODO: cancel the monitor after we're done?
return monitor(columns, db)
if __name__ == '__main__':
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((OVSDB_IP, OVSDB_PORT))
current_id = 0
s.send(list_dbs())
db_list = gather_reply(s)
db_name = db_list['result'][0]
print "list bridges:"
s.send(list_bridges())
bridge_list = gather_reply(s)
print bridge_list
bridges = bridge_list['result']['Bridge']
print "\nbridges\n"
print bridges.values()
for bridge in bridges.values():
print "---"
print bridge['new']['name']
#db_schema = get_schema(s, db_name)
#print db_schema
#columns = {"Bridge":{"columns":["name"]}}
#print monitor(s, columns, 1)
# TODO: Put this in a thread and use Queues to send/recv data from the thread
message_queues = {}
message_queues[s] = Queue.Queue()
listen_for_messages(s, message_queues)