1 # Copyright (c) 2009, 2010, 2011 Nicira Networks
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.
18 from ovs.db import error
21 class UUID(uuid.UUID):
23 uuidRE = re.compile("^(%s{8})-(%s{4})-(%s{4})-(%s{4})-(%s{4})(%s{8})$"
26 def __init__(self, s):
27 uuid.UUID.__init__(self, hex=s)
31 return UUID('00000000-0000-0000-0000-000000000000')
37 def is_valid_string(s):
38 return UUID.uuidRE.match(s) != None
42 if not UUID.is_valid_string(s):
43 raise error.Error("%s is not a valid UUID" % s)
47 def from_json(json, symtab=None):
49 s = ovs.db.parser.unwrap_json(json, "uuid", unicode)
50 if not UUID.uuidRE.match(s):
51 raise error.Error("\"%s\" is not a valid UUID" % s, json)
53 except error.Error, e:
57 name = ovs.db.parser.unwrap_json(json, "named-uuid", unicode)
61 if name not in symtab:
62 symtab[name] = uuid4()
66 return ["uuid", str(self)]
68 def cInitUUID(self, var):
69 m = re.match(str(self))
70 return ["%s.parts[0] = 0x%s;" % (var, m.group(1)),
71 "%s.parts[1] = 0x%s%s;" % (var, m.group(2), m.group(3)),
72 "%s.parts[2] = 0x%s%s;" % (var, m.group(4), m.group(5)),
73 "%s.parts[3] = 0x%s;" % (var, m.group(6))]