merge from geni-api branch
[sfa.git] / sfa / util / namespace.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.util.faults import *
5 URN_PREFIX = "urn:publicid:IDN"
6
7 def get_leaf(hrn):
8     parts = hrn.split(".")
9     return ".".join(parts[-1:])
10
11 def get_authority(xrn):
12     hrn, type = urn_to_hrn(xrn)
13     if type and type == 'authority':
14         return hrn
15     
16     parts = hrn.split(".")
17     return ".".join(parts[:-1])
18
19 def hrn_to_pl_slicename(hrn):
20     parts = hrn.split(".")
21     return parts[-2] + "_" + parts[-1]
22
23 # assuming hrn is the hrn of an authority, return the plc authority name
24 def hrn_to_pl_authname(hrn):
25     parts = hrn.split(".")
26     return parts[-1]
27
28 # assuming hrn is the hrn of an authority, return the plc login_base
29 def hrn_to_pl_login_base(hrn):
30     return hrn_to_pl_authname(hrn)
31
32 def hostname_to_hrn(auth_hrn, login_base, hostname):
33     """
34     Convert hrn to plantelab name.
35     """
36     sfa_hostname = ".".join([auth_hrn, login_base, hostname.split(".")[0]])
37     return sfa_hostname
38
39 def slicename_to_hrn(auth_hrn, slicename):
40     """
41     Convert hrn to planetlab name.
42     """
43     parts = slicename.split("_")
44     slice_hrn = ".".join([auth_hrn, parts[0]]) + "." + "_".join(parts[1:])
45
46     return slice_hrn
47
48 def email_to_hrn(auth_hrn, email):
49     parts = email.split("@")
50     username = parts[0]
51     username = username.replace(".", "_").replace("+", "_") 
52     person_hrn = ".".join([auth_hrn, username])
53     
54     return person_hrn 
55
56 def urn_to_hrn(urn):
57     """
58     convert a urn to hrn
59     return a tuple (hrn, type)
60     """
61
62     # if this is already a hrn dont do anything
63     if not urn or not urn.startswith(URN_PREFIX):
64         return urn, None
65
66     name = urn[len(URN_PREFIX):]
67     hrn_parts = name.split("+")
68     
69     # type is always the second to last element in the list
70     type = hrn_parts.pop(-2)
71
72     # convert hrn_parts (list) into hrn (str) by doing the following    
73     # remove blank elements
74     # replace ':' with '.'
75     # join list elements using '.'
76     hrn = '.'.join([part.replace(':', '.') for part in hrn_parts if part]) 
77     
78     # Remove the authority name (e.g. '.sa')
79     if type == 'authority':
80         hrn = hrn[:hrn.rindex('.')]        
81    
82     return str(hrn), str(type) 
83     
84     
85 def hrn_to_urn(hrn, type=None):
86     """
87     convert an hrn and type to a urn string
88     """
89     # if  this is already a urn dont do anything 
90     if not hrn or hrn.startswith(URN_PREFIX):
91         return hrn
92
93     authority = get_authority(hrn)
94     name = get_leaf(hrn)
95      
96     if type == 'authority':
97         authority = hrn
98         name = 'sa'   
99     
100     if authority.startswith("plc"):
101         if type == None:
102             urn = "+".join(['',authority.replace('.',':'),name])
103         else:
104             urn = "+".join(['',authority.replace('.',':'),type,name])
105
106     else:
107         urn = "+".join(['',authority,type,name])
108         
109     return URN_PREFIX + urn