1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
22 #----------------------------------------------------------------------
26 from sfa.util.faults import SfaAPIError
28 # for convenience and smoother translation - we should get rid of these functions eventually
29 def get_leaf(hrn): return Xrn(hrn).get_leaf()
30 def get_authority(hrn): return Xrn(hrn).get_authority_hrn()
31 def urn_to_hrn(urn): xrn=Xrn(urn); return (xrn.hrn, xrn.type)
32 def hrn_to_urn(hrn,type): return Xrn(hrn, type=type).urn
33 def hrn_authfor_hrn(parenthrn, hrn): return Xrn.hrn_is_auth_for_hrn(parenthrn, hrn)
35 def urn_to_sliver_id(urn, slice_id, node_id, index=0):
36 return ":".join(map(str, [urn, slice_id, node_id, index]))
40 ########## basic tools on HRNs
41 # split a HRN-like string into pieces
42 # this is like split('.') except for escaped (backslashed) dots
43 # e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
46 return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
48 # e.g. hrn_leaf ('a\.b.c.d') -> 'd'
50 def hrn_leaf(hrn): return Xrn.hrn_split(hrn)[-1]
52 # e.g. hrn_auth_list ('a\.b.c.d') -> ['a\.b', 'c']
54 def hrn_auth_list(hrn): return Xrn.hrn_split(hrn)[0:-1]
56 # e.g. hrn_auth ('a\.b.c.d') -> 'a\.b.c'
58 def hrn_auth(hrn): return '.'.join(Xrn.hrn_auth_list(hrn))
60 # e.g. escape ('a.b') -> 'a\.b'
62 def escape(token): return re.sub(r'([^\\])\.', r'\1\.', token)
64 # e.g. unescape ('a\.b') -> 'a.b'
66 def unescape(token): return token.replace('\\.','.')
68 # Return the HRN authority chain from top to bottom.
69 # e.g. hrn_auth_chain('a\.b.c.d') -> ['a\.b', 'a\.b.c']
71 def hrn_auth_chain(hrn):
72 parts = Xrn.hrn_auth_list(hrn)
74 for i in range(len(parts)):
75 chain.append('.'.join(parts[:i+1]))
76 # Include the HRN itself?
80 # Is the given HRN a true authority over the namespace of the other
82 # A better alternative than childHRN.startswith(parentHRN)
83 # e.g. hrn_is_auth_for_hrn('a\.b', 'a\.b.c.d') -> True,
84 # but hrn_is_auth_for_hrn('a', 'a\.b.c.d') -> False
85 # Also hrn_is_uauth_for_hrn('a\.b.c.d', 'a\.b.c.d') -> True
87 def hrn_is_auth_for_hrn(parenthrn, hrn):
90 for auth in Xrn.hrn_auth_chain(hrn):
95 URN_PREFIX = "urn:publicid:IDN"
97 ########## basic tools on URNs
100 if urn.startswith(Xrn.URN_PREFIX): return urn
101 else: return Xrn.URN_PREFIX+urn
103 def urn_meaningful (urn):
104 if urn.startswith(Xrn.URN_PREFIX): return urn[len(Xrn.URN_PREFIX):]
108 return Xrn.urn_meaningful(urn).split('+')
111 # the local fields that are kept consistent
116 # provide either urn, or (hrn + type)
117 def __init__ (self, xrn, type=None):
119 # user has specified xrn : guess if urn or hrn
120 if xrn.startswith(Xrn.URN_PREFIX):
129 # happens all the time ..
131 # debug_logger.debug("type-less Xrn's are not safe")
133 def get_urn(self): return self.urn
134 def get_hrn(self): return self.hrn
135 def get_type(self): return self.type
136 def get_hrn_type(self): return (self.hrn, self.type)
138 def _normalize(self):
139 if self.hrn is None: raise SfaAPIError, "Xrn._normalize"
140 if not hasattr(self,'leaf'):
141 self.leaf=Xrn.hrn_split(self.hrn)[-1]
142 # self.authority keeps a list
143 if not hasattr(self,'authority'):
144 self.authority=Xrn.hrn_auth_list(self.hrn)
150 def get_authority_hrn(self):
152 return '.'.join( self.authority )
154 def get_authority_urn(self):
156 return ':'.join( [Xrn.unescape(x) for x in self.authority] )
158 def urn_to_hrn(self):
160 compute tuple (hrn, type) from urn
163 # if not self.urn or not self.urn.startswith(Xrn.URN_PREFIX):
164 if not self.urn.startswith(Xrn.URN_PREFIX):
165 raise SfaAPIError, "Xrn.urn_to_hrn"
167 parts = Xrn.urn_split(self.urn)
169 # Remove the authority name (e.g. '.sa')
170 if type == 'authority':
172 # Drop the sa. This is a bad hack, but its either this
173 # or completely change how record types are generated/stored
175 type = type + "+" + name
178 name = parts.pop(len(parts)-1)
179 # convert parts (list) into hrn (str) by doing the following
180 # 1. remove blank parts
181 # 2. escape dots inside parts
182 # 3. replace ':' with '.' inside parts
183 # 3. join parts using '.'
184 hrn = '.'.join([Xrn.escape(part).replace(':','.') for part in parts if part])
185 # dont replace ':' in the name section
187 hrn += '.%s' % Xrn.escape(name)
192 def hrn_to_urn(self):
194 compute urn from (hrn, type)
197 # if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
198 if self.hrn.startswith(Xrn.URN_PREFIX):
199 raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
201 if self.type and self.type.startswith('authority'):
202 self.authority = Xrn.hrn_split(self.hrn)
203 type_parts = self.type.split("+")
204 self.type = type_parts[0]
206 if len(type_parts) > 1:
209 self.authority = Xrn.hrn_auth_list(self.hrn)
210 name = Xrn.hrn_leaf(self.hrn)
212 authority_string = self.get_authority_urn()
214 if self.type == None:
215 urn = "+".join(['',authority_string,Xrn.unescape(name)])
217 urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])
219 self.urn = Xrn.URN_PREFIX + urn
221 def dump_string(self):
222 result="-------------------- XRN\n"
223 result += "URN=%s\n"%self.urn
224 result += "HRN=%s\n"%self.hrn
225 result += "TYPE=%s\n"%self.type
226 result += "LEAF=%s\n"%self.get_leaf()
227 result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
228 result += "AUTH(urn format)=%s\n"%self.get_authority_urn()