1 # Copyright (c) 2009, 2010, 2011 Nicira, Inc.
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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from six.moves import range
21 from ovs.db import error
24 uuidRE = re.compile("^xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx$"
25 .replace('x', '[0-9a-fA-F]'))
29 return uuid.UUID(int=0)
32 def is_valid_string(s):
33 return uuidRE.match(s) is not None
37 if not is_valid_string(s):
38 raise error.Error("%s is not a valid UUID" % s)
42 def from_json(json, symtab=None):
44 s = ovs.db.parser.unwrap_json(json, "uuid", six.string_types, "string")
45 if not uuidRE.match(s):
46 raise error.Error("\"%s\" is not a valid UUID" % s, json)
48 except error.Error as e:
52 name = ovs.db.parser.unwrap_json(json, "named-uuid",
53 six.string_types, "string")
57 if name not in symtab:
58 symtab[name] = uuid.uuid4()
63 return ["uuid", str(uuid_)]
66 def to_c_assignment(uuid_, var):
67 """Returns an array of strings, each of which contain a C statement. The
68 statements assign 'uuid_' to a "struct uuid" as defined in Open vSwitch
71 hex_string = uuid_.hex
72 return ["%s.parts[%d] = 0x%s;" % (var, x, hex_string[x * 8:(x + 1) * 8])