4 from sfa.util.faults import *
6 URN_PREFIX = "urn:publicid:IDN"
10 return ".".join(parts[-1:])
12 def get_authority(xrn):
13 hrn, type = urn_to_hrn(xrn)
14 if type and type == 'authority':
17 parts = hrn.split(".")
18 return ".".join(parts[:-1])
20 def hrn_to_pl_slicename(hrn):
21 parts = hrn.split(".")
22 return parts[-2] + "_" + parts[-1]
24 # assuming hrn is the hrn of an authority, return the plc authority name
25 def hrn_to_pl_authname(hrn):
26 parts = hrn.split(".")
29 # assuming hrn is the hrn of an authority, return the plc login_base
30 def hrn_to_pl_login_base(hrn):
31 return hrn_to_pl_authname(hrn)
33 def hostname_to_hrn(auth_hrn, login_base, hostname):
35 Convert hrn to plantelab name.
37 sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
40 def slicename_to_hrn(auth_hrn, slicename):
42 Convert hrn to planetlab name.
44 parts = slicename.split("_")
45 slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
49 def email_to_hrn(auth_hrn, email):
50 parts = email.split("@")
52 username = username.replace(".", "_").replace("+", "_")
53 person_hrn = ".".join([auth_hrn, username])
60 return a tuple (hrn, type)
63 # if this is already a hrn dont do anything
64 if not urn or not urn.startswith(URN_PREFIX):
67 name = urn[len(URN_PREFIX):]
68 hrn_parts = name.split("+")
70 # type is always the second to last element in the list
71 type = hrn_parts.pop(-2)
73 # convert hrn_parts (list) into hrn (str) by doing the following
74 # remove blank elements
75 # replace ':' with '.'
76 # join list elements using '.'
77 hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part])
79 return str(hrn), str(type)
82 def hrn_to_urn(hrn, type=None):
84 convert an hrn and type to a urn string
86 # if this is already a urn dont do anything
87 if not hrn or hrn.startswith(URN_PREFIX):
90 authority = get_authority(hrn)
93 if authority.startswith("plc"):
95 urn = "+".join(['',authority.replace('.',':'),name])
97 urn = "+".join(['',authority.replace('.',':'),type,name])
100 urn = "+".join(['',authority,type,name])
102 return URN_PREFIX + urn