python/ovs/db/idl.py: Transaction._substitute doesn't handle list/tuple
[sliver-openvswitch.git] / tests / test-ovsdb.py
1 # Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import getopt
16 import re
17 import os
18 import signal
19 import sys
20 import uuid
21
22 from ovs.db import error
23 import ovs.db.idl
24 import ovs.db.schema
25 from ovs.db import data
26 from ovs.db import types
27 import ovs.ovsuuid
28 import ovs.poller
29 import ovs.util
30
31
32 def unbox_json(json):
33     if type(json) == list and len(json) == 1:
34         return json[0]
35     else:
36         return json
37
38
39 def do_default_atoms():
40     for type_ in types.ATOMIC_TYPES:
41         if type_ == types.VoidType:
42             continue
43
44         sys.stdout.write("%s: " % type_.to_string())
45
46         atom = data.Atom.default(type_)
47         if atom != data.Atom.default(type_):
48             sys.stdout.write("wrong\n")
49             sys.exit(1)
50
51         sys.stdout.write("OK\n")
52
53
54 def do_default_data():
55     any_errors = False
56     for n_min in 0, 1:
57         for key in types.ATOMIC_TYPES:
58             if key == types.VoidType:
59                 continue
60             for value in types.ATOMIC_TYPES:
61                 if value == types.VoidType:
62                     valueBase = None
63                 else:
64                     valueBase = types.BaseType(value)
65                 type_ = types.Type(types.BaseType(key), valueBase, n_min, 1)
66                 assert type_.is_valid()
67
68                 sys.stdout.write("key %s, value %s, n_min %d: "
69                                  % (key.to_string(), value.to_string(), n_min))
70
71                 datum = data.Datum.default(type_)
72                 if datum != data.Datum.default(type_):
73                     sys.stdout.write("wrong\n")
74                     any_errors = True
75                 else:
76                     sys.stdout.write("OK\n")
77     if any_errors:
78         sys.exit(1)
79
80
81 def do_parse_atomic_type(type_string):
82     type_json = unbox_json(ovs.json.from_string(type_string))
83     atomic_type = types.AtomicType.from_json(type_json)
84     print ovs.json.to_string(atomic_type.to_json(), sort_keys=True)
85
86
87 def do_parse_base_type(type_string):
88     type_json = unbox_json(ovs.json.from_string(type_string))
89     base_type = types.BaseType.from_json(type_json)
90     print ovs.json.to_string(base_type.to_json(), sort_keys=True)
91
92
93 def do_parse_type(type_string):
94     type_json = unbox_json(ovs.json.from_string(type_string))
95     type_ = types.Type.from_json(type_json)
96     print ovs.json.to_string(type_.to_json(), sort_keys=True)
97
98
99 def do_parse_atoms(type_string, *atom_strings):
100     type_json = unbox_json(ovs.json.from_string(type_string))
101     base = types.BaseType.from_json(type_json)
102     for atom_string in atom_strings:
103         atom_json = unbox_json(ovs.json.from_string(atom_string))
104         try:
105             atom = data.Atom.from_json(base, atom_json)
106             print ovs.json.to_string(atom.to_json())
107         except error.Error, e:
108             print unicode(e)
109
110
111 def do_parse_data(type_string, *data_strings):
112     type_json = unbox_json(ovs.json.from_string(type_string))
113     type_ = types.Type.from_json(type_json)
114     for datum_string in data_strings:
115         datum_json = unbox_json(ovs.json.from_string(datum_string))
116         datum = data.Datum.from_json(type_, datum_json)
117         print ovs.json.to_string(datum.to_json())
118
119
120 def do_sort_atoms(type_string, atom_strings):
121     type_json = unbox_json(ovs.json.from_string(type_string))
122     base = types.BaseType.from_json(type_json)
123     atoms = [data.Atom.from_json(base, atom_json)
124              for atom_json in unbox_json(ovs.json.from_string(atom_strings))]
125     print ovs.json.to_string([data.Atom.to_json(atom)
126                               for atom in sorted(atoms)])
127
128
129 def do_parse_column(name, column_string):
130     column_json = unbox_json(ovs.json.from_string(column_string))
131     column = ovs.db.schema.ColumnSchema.from_json(column_json, name)
132     print ovs.json.to_string(column.to_json(), sort_keys=True)
133
134
135 def do_parse_table(name, table_string, default_is_root_string='false'):
136     default_is_root = default_is_root_string == 'true'
137     table_json = unbox_json(ovs.json.from_string(table_string))
138     table = ovs.db.schema.TableSchema.from_json(table_json, name)
139     print ovs.json.to_string(table.to_json(default_is_root), sort_keys=True)
140
141
142 def do_parse_schema(schema_string):
143     schema_json = unbox_json(ovs.json.from_string(schema_string))
144     schema = ovs.db.schema.DbSchema.from_json(schema_json)
145     print ovs.json.to_string(schema.to_json(), sort_keys=True)
146
147
148 def print_idl(idl, step):
149     simple = idl.tables["simple"].rows
150     l1 = idl.tables["link1"].rows
151     l2 = idl.tables["link2"].rows
152
153     n = 0
154     for row in simple.itervalues():
155         s = ("%03d: i=%s r=%s b=%s s=%s u=%s "
156              "ia=%s ra=%s ba=%s sa=%s ua=%s uuid=%s"
157              % (step, row.i, row.r, row.b, row.s, row.u,
158                 row.ia, row.ra, row.ba, row.sa, row.ua, row.uuid))
159         s = re.sub('""|,|u?\'', "", s)
160         s = re.sub('UUID\(([^)]+)\)', r'\1', s)
161         s = re.sub('False', 'false', s)
162         s = re.sub('True', 'true', s)
163         s = re.sub(r'(ba)=([^[][^ ]*) ', r'\1=[\2] ', s)
164         print(s)
165         n += 1
166
167     for row in l1.itervalues():
168         s = ["%03d: i=%s k=" % (step, row.i)]
169         if row.k:
170             s.append(str(row.k.i))
171         s.append(" ka=[")
172         s.append(' '.join(sorted(str(ka.i) for ka in row.ka)))
173         s.append("] l2=")
174         if row.l2:
175             s.append(str(row.l2[0].i))
176         s.append(" uuid=%s" % row.uuid)
177         print(''.join(s))
178         n += 1
179
180     for row in l2.itervalues():
181         s = ["%03d: i=%s l1=" % (step, row.i)]
182         if row.l1:
183             s.append(str(row.l1[0].i))
184         s.append(" uuid=%s" % row.uuid)
185         print(''.join(s))
186         n += 1
187
188     if not n:
189         print("%03d: empty" % step)
190     sys.stdout.flush()
191
192
193 def substitute_uuids(json, symtab):
194     if type(json) in [str, unicode]:
195         symbol = symtab.get(json)
196         if symbol:
197             return str(symbol)
198     elif type(json) == list:
199         return [substitute_uuids(element, symtab) for element in json]
200     elif type(json) == dict:
201         d = {}
202         for key, value in json.iteritems():
203             d[key] = substitute_uuids(value, symtab)
204         return d
205     return json
206
207
208 def parse_uuids(json, symtab):
209     if type(json) in [str, unicode] and ovs.ovsuuid.is_valid_string(json):
210         name = "#%d#" % len(symtab)
211         sys.stderr.write("%s = %s\n" % (name, json))
212         symtab[name] = json
213     elif type(json) == list:
214         for element in json:
215             parse_uuids(element, symtab)
216     elif type(json) == dict:
217         for value in json.itervalues():
218             parse_uuids(value, symtab)
219
220
221 def idltest_find_simple(idl, i):
222     for row in idl.tables["simple"].rows.itervalues():
223         if row.i == i:
224             return row
225     return None
226
227
228 def idl_set(idl, commands, step):
229     txn = ovs.db.idl.Transaction(idl)
230     increment = False
231     for command in commands.split(','):
232         words = command.split()
233         name = words[0]
234         args = words[1:]
235
236         if name == "set":
237             if len(args) != 3:
238                 sys.stderr.write('"set" command requires 3 arguments\n')
239                 sys.exit(1)
240
241             s = idltest_find_simple(idl, int(args[0]))
242             if not s:
243                 sys.stderr.write('"set" command asks for nonexistent i=%d\n'
244                                  % int(args[0]))
245                 sys.exit(1)
246
247             if args[1] == "b":
248                 s.b = args[2] == "1"
249             elif args[1] == "s":
250                 s.s = args[2]
251             elif args[1] == "u":
252                 s.u = uuid.UUID(args[2])
253             elif args[1] == "r":
254                 s.r = float(args[2])
255             else:
256                 sys.stderr.write('"set" comamnd asks for unknown column %s\n'
257                                  % args[2])
258                 sys.stderr.exit(1)
259         elif name == "insert":
260             if len(args) != 1:
261                 sys.stderr.write('"set" command requires 1 argument\n')
262                 sys.exit(1)
263
264             s = txn.insert(idl.tables["simple"])
265             s.i = int(args[0])
266         elif name == "delete":
267             if len(args) != 1:
268                 sys.stderr.write('"delete" command requires 1 argument\n')
269                 sys.exit(1)
270
271             s = idltest_find_simple(idl, int(args[0]))
272             if not s:
273                 sys.stderr.write('"delete" command asks for nonexistent i=%d\n'
274                                  % int(args[0]))
275                 sys.exit(1)
276             s.delete()
277         elif name == "verify":
278             if len(args) != 2:
279                 sys.stderr.write('"verify" command requires 2 arguments\n')
280                 sys.exit(1)
281
282             s = idltest_find_simple(idl, int(args[0]))
283             if not s:
284                 sys.stderr.write('"verify" command asks for nonexistent i=%d\n'
285                                  % int(args[0]))
286                 sys.exit(1)
287
288             if args[1] in ("i", "b", "s", "u", "r"):
289                 s.verify(args[1])
290             else:
291                 sys.stderr.write('"verify" command asks for unknown column '
292                                  '"%s"\n' % args[1])
293                 sys.exit(1)
294         elif name == "increment":
295             if len(args) != 1:
296                 sys.stderr.write('"increment" command requires 1 argument\n')
297                 sys.exit(1)
298
299             s = idltest_find_simple(idl, int(args[0]))
300             if not s:
301                 sys.stderr.write('"set" command asks for nonexistent i=%d\n'
302                                  % int(args[0]))
303                 sys.exit(1)
304
305             s.increment("i")
306             increment = True
307         elif name == "abort":
308             txn.abort()
309             break
310         elif name == "destroy":
311             print "%03d: destroy" % step
312             sys.stdout.flush()
313             txn.abort()
314             return
315         elif name == "linktest":
316             l1_0 = txn.insert(idl.tables["link1"])
317             l1_0.i = 1
318             l1_0.k = [l1_0]
319             l1_0.ka = [l1_0]
320             l1_1 = txn.insert(idl.tables["link1"])
321             l1_1.i = 2
322             l1_1.k = [l1_0]
323             l1_1.ka = [l1_0, l1_1]
324         else:
325             sys.stderr.write("unknown command %s\n" % name)
326             sys.exit(1)
327
328     status = txn.commit_block()
329     sys.stdout.write("%03d: commit, status=%s"
330                      % (step, ovs.db.idl.Transaction.status_to_string(status)))
331     if increment and status == ovs.db.idl.Transaction.SUCCESS:
332         sys.stdout.write(", increment=%d" % txn.get_increment_new_value())
333     sys.stdout.write("\n")
334     sys.stdout.flush()
335
336
337 def do_idl(schema_file, remote, *commands):
338     schema_helper = ovs.db.idl.SchemaHelper(schema_file)
339     schema_helper.register_all()
340     idl = ovs.db.idl.Idl(remote, schema_helper)
341
342     if commands:
343         error, stream = ovs.stream.Stream.open_block(
344             ovs.stream.Stream.open(remote))
345         if error:
346             sys.stderr.write("failed to connect to \"%s\"" % remote)
347             sys.exit(1)
348         rpc = ovs.jsonrpc.Connection(stream)
349     else:
350         rpc = None
351
352     symtab = {}
353     seqno = 0
354     step = 0
355     for command in commands:
356         if command.startswith("+"):
357             # The previous transaction didn't change anything.
358             command = command[1:]
359         else:
360             # Wait for update.
361             while idl.change_seqno == seqno and not idl.run():
362                 rpc.run()
363
364                 poller = ovs.poller.Poller()
365                 idl.wait(poller)
366                 rpc.wait(poller)
367                 poller.block()
368
369             print_idl(idl, step)
370             step += 1
371
372         seqno = idl.change_seqno
373
374         if command == "reconnect":
375             print("%03d: reconnect" % step)
376             sys.stdout.flush()
377             step += 1
378             idl.force_reconnect()
379         elif not command.startswith("["):
380             idl_set(idl, command, step)
381             step += 1
382         else:
383             json = ovs.json.from_string(command)
384             if type(json) in [str, unicode]:
385                 sys.stderr.write("\"%s\": %s\n" % (command, json))
386                 sys.exit(1)
387             json = substitute_uuids(json, symtab)
388             request = ovs.jsonrpc.Message.create_request("transact", json)
389             error, reply = rpc.transact_block(request)
390             if error:
391                 sys.stderr.write("jsonrpc transaction failed: %s"
392                                  % os.strerror(error))
393                 sys.exit(1)
394             elif reply.error is not None:
395                 sys.stderr.write("jsonrpc transaction failed: %s"
396                                  % reply.error)
397                 sys.exit(1)
398
399             sys.stdout.write("%03d: " % step)
400             sys.stdout.flush()
401             step += 1
402             if reply.result is not None:
403                 parse_uuids(reply.result, symtab)
404             reply.id = None
405             sys.stdout.write("%s\n" % ovs.json.to_string(reply.to_json()))
406             sys.stdout.flush()
407
408     if rpc:
409         rpc.close()
410     while idl.change_seqno == seqno and not idl.run():
411         poller = ovs.poller.Poller()
412         idl.wait(poller)
413         poller.block()
414     print_idl(idl, step)
415     step += 1
416     idl.close()
417     print("%03d: done" % step)
418
419
420 def usage():
421     print """\
422 %(program_name)s: test utility for Open vSwitch database Python bindings
423 usage: %(program_name)s [OPTIONS] COMMAND ARG...
424
425 The following commands are supported:
426 default-atoms
427   test ovsdb_atom_default()
428 default-data
429   test ovsdb_datum_default()
430 parse-atomic-type TYPE
431   parse TYPE as OVSDB atomic type, and re-serialize
432 parse-base-type TYPE
433   parse TYPE as OVSDB base type, and re-serialize
434 parse-type JSON
435   parse JSON as OVSDB type, and re-serialize
436 parse-atoms TYPE ATOM...
437   parse JSON ATOMs as atoms of TYPE, and re-serialize
438 parse-atom-strings TYPE ATOM...
439   parse string ATOMs as atoms of given TYPE, and re-serialize
440 sort-atoms TYPE ATOM...
441   print JSON ATOMs in sorted order
442 parse-data TYPE DATUM...
443   parse JSON DATUMs as data of given TYPE, and re-serialize
444 parse-column NAME OBJECT
445   parse column NAME with info OBJECT, and re-serialize
446 parse-table NAME OBJECT [DEFAULT-IS-ROOT]
447   parse table NAME with info OBJECT
448 parse-schema JSON
449   parse JSON as an OVSDB schema, and re-serialize
450 idl SCHEMA SERVER [TRANSACTION...]
451   connect to SERVER (which has the specified SCHEMA) and dump the
452   contents of the database as seen initially by the IDL implementation
453   and after executing each TRANSACTION.  (Each TRANSACTION must modify
454   the database or this command will hang.)
455
456 The following options are also available:
457   -t, --timeout=SECS          give up after SECS seconds
458   -h, --help                  display this help message\
459 """ % {'program_name': ovs.util.PROGRAM_NAME}
460     sys.exit(0)
461
462
463 def main(argv):
464     try:
465         options, args = getopt.gnu_getopt(argv[1:], 't:h',
466                                           ['timeout',
467                                            'help'])
468     except getopt.GetoptError, geo:
469         sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
470         sys.exit(1)
471
472     for key, value in options:
473         if key in ['-h', '--help']:
474             usage()
475         elif key in ['-t', '--timeout']:
476             try:
477                 timeout = int(value)
478                 if timeout < 1:
479                     raise TypeError
480             except TypeError:
481                 raise error.Error("value %s on -t or --timeout is not at "
482                                   "least 1" % value)
483             signal.alarm(timeout)
484         else:
485             sys.exit(0)
486
487     if not args:
488         sys.stderr.write("%s: missing command argument "
489                          "(use --help for help)\n" % ovs.util.PROGRAM_NAME)
490         sys.exit(1)
491
492     commands = {"default-atoms": (do_default_atoms, 0),
493                 "default-data": (do_default_data, 0),
494                 "parse-atomic-type": (do_parse_atomic_type, 1),
495                 "parse-base-type": (do_parse_base_type, 1),
496                 "parse-type": (do_parse_type, 1),
497                 "parse-atoms": (do_parse_atoms, (2,)),
498                 "parse-data": (do_parse_data, (2,)),
499                 "sort-atoms": (do_sort_atoms, 2),
500                 "parse-column": (do_parse_column, 2),
501                 "parse-table": (do_parse_table, (2, 3)),
502                 "parse-schema": (do_parse_schema, 1),
503                 "idl": (do_idl, (2,))}
504
505     command_name = args[0]
506     args = args[1:]
507     if not command_name in commands:
508         sys.stderr.write("%s: unknown command \"%s\" "
509                          "(use --help for help)\n" % (ovs.util.PROGRAM_NAME,
510                                                       command_name))
511         sys.exit(1)
512
513     func, n_args = commands[command_name]
514     if type(n_args) == tuple:
515         if len(args) < n_args[0]:
516             sys.stderr.write("%s: \"%s\" requires at least %d arguments but "
517                              "only %d provided\n"
518                              % (ovs.util.PROGRAM_NAME, command_name,
519                                 n_args, len(args)))
520             sys.exit(1)
521     elif type(n_args) == int:
522         if len(args) != n_args:
523             sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
524                              "provided\n"
525                              % (ovs.util.PROGRAM_NAME, command_name,
526                                 n_args, len(args)))
527             sys.exit(1)
528     else:
529         assert False
530
531     func(*args)
532
533
534 if __name__ == '__main__':
535     try:
536         main(sys.argv)
537     except error.Error, e:
538         sys.stderr.write("%s\n" % e)
539         sys.exit(1)