# Copyright (c) 2008 Board of Trustees, Princeton University
#
# Permission is hereby granted, free of charge, to any person obtaining
# Copyright (c) 2008 Board of Trustees, Princeton University
#
# Permission is hereby granted, free of charge, to any person obtaining
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
-# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
+# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
-# for convenience and smoother translation - we should get rid of these functions eventually
-def get_leaf(hrn): return Xrn(hrn).get_leaf()
-def get_authority(hrn): return Xrn(hrn).get_authority_hrn()
-def urn_to_hrn(urn): xrn=Xrn(urn); return (xrn.hrn, xrn.type)
-def hrn_to_urn(hrn,type): return Xrn(hrn, type=type).urn
-def hrn_authfor_hrn(parenthrn, hrn): return Xrn.hrn_is_auth_for_hrn(parenthrn, hrn)
+# for convenience and smoother translation - we should get rid of these
+# functions eventually
+
+
+def get_leaf(hrn):
+ return Xrn(hrn).get_leaf()
+
+
+def get_authority(hrn):
+ return Xrn(hrn).get_authority_hrn()
+
+
+def urn_to_hrn(urn):
+ xrn = Xrn(urn)
+ return (xrn.hrn, xrn.type)
+
+
+def hrn_to_urn(hrn, type): return Xrn(hrn, type=type).urn
+
+
+def hrn_authfor_hrn(parenthrn, hrn):
+ return Xrn.hrn_is_auth_for_hrn(parenthrn, hrn)
-def urn_to_sliver_id(urn, slice_id, node_id, index=0):
- return Xrn(urn).get_sliver_id(slice_id, node_id, index)
# split a HRN-like string into pieces
# this is like split('.') except for escaped (backslashed) dots
# e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
@staticmethod
def hrn_split(hrn):
# split a HRN-like string into pieces
# this is like split('.') except for escaped (backslashed) dots
# e.g. hrn_split ('a\.b.c.d') -> [ 'a\.b','c','d']
@staticmethod
def hrn_split(hrn):
- return [ x.replace('--sep--','\\.') for x in hrn.replace('\\.','--sep--').split('.') ]
+ return [x.replace('--sep--', '\\.')
+ for x in hrn.replace('\\.', '--sep--').split('.')]
# Return the HRN authority chain from top to bottom.
# e.g. hrn_auth_chain('a\.b.c.d') -> ['a\.b', 'a\.b.c']
# Return the HRN authority chain from top to bottom.
# e.g. hrn_auth_chain('a\.b.c.d') -> ['a\.b', 'a\.b.c']
# A better alternative than childHRN.startswith(parentHRN)
# e.g. hrn_is_auth_for_hrn('a\.b', 'a\.b.c.d') -> True,
# but hrn_is_auth_for_hrn('a', 'a\.b.c.d') -> False
# A better alternative than childHRN.startswith(parentHRN)
# e.g. hrn_is_auth_for_hrn('a\.b', 'a\.b.c.d') -> True,
# but hrn_is_auth_for_hrn('a', 'a\.b.c.d') -> False
-# happens all the time ..
-# if not type:
-# debug_logger.debug("type-less Xrn's are not safe")
-
- def __repr__ (self):
- result="<XRN u=%s h=%s"%(self.urn,self.hrn)
- if hasattr(self,'leaf'): result += " leaf=%s"%self.leaf
- if hasattr(self,'authority'): result += " auth=%s"%self.authority
+ self._normalize()
+
+ def __repr__(self):
+ result = "<XRN u=%s h=%s" % (self.urn, self.hrn)
+ if hasattr(self, 'leaf'):
+ result += " leaf=%s" % self.leaf
+ if hasattr(self, 'authority'):
+ result += " auth=%s" % self.authority
- if self.hrn is None: raise SfaAPIError, "Xrn._normalize"
- if not hasattr(self,'leaf'):
- self.leaf=Xrn.hrn_split(self.hrn)[-1]
+ if self.hrn is None:
+ raise SfaAPIError("Xrn._normalize")
+ if not hasattr(self, 'leaf'):
+ self.leaf = Xrn.hrn_split(self.hrn)[-1]
- if not hasattr(self,'authority'):
- self.authority=Xrn.hrn_auth_list(self.hrn)
+ if not hasattr(self, 'authority'):
+ self.authority = Xrn.hrn_auth_list(self.hrn)
- return ':'.join( [Xrn.unescape(x) for x in self.authority] )
-
- def get_sliver_id(self, slice_id, node_id, index=0):
+ return ':'.join([Xrn.unescape(x) for x in self.authority])
+
+ def set_authority(self, authority):
+ """
+ update the authority section of an existing urn
+ """
+ authority_hrn = self.get_authority_hrn()
+ if not authority_hrn.startswith(authority):
+ hrn = ".".join([authority, authority_hrn, self.get_leaf()])
+ else:
+ hrn = ".".join([authority_hrn, self.get_leaf()])
+
+ self.hrn = hrn
+ self.hrn_to_urn()
- return ":".join(map(str, [self.get_urn(), slice_id, node_id, index]))
-
+
+ # sliver_id_parts is list that contains the sliver's
+ # slice id and node id
+ def get_sliver_id_parts(self):
+ sliver_id_parts = []
+ if self.type == 'sliver' or '-' in self.leaf:
+ sliver_id_parts = self.leaf.split('-')
+ return sliver_id_parts
+
- if not self.urn.startswith(Xrn.URN_PREFIX):
- raise SfaAPIError, "Xrn.urn_to_hrn"
+ if not Xrn.is_urn(self.urn):
+ raise SfaAPIError("Xrn.urn_to_hrn")
# Remove the authority name (e.g. '.sa')
if type == 'authority':
name = parts.pop()
# Drop the sa. This is a bad hack, but its either this
# Remove the authority name (e.g. '.sa')
if type == 'authority':
name = parts.pop()
# Drop the sa. This is a bad hack, but its either this
# convert parts (list) into hrn (str) by doing the following
# 1. remove blank parts
# 2. escape dots inside parts
# 3. replace ':' with '.' inside parts
# 3. join parts using '.'
# convert parts (list) into hrn (str) by doing the following
# 1. remove blank parts
# 2. escape dots inside parts
# 3. replace ':' with '.' inside parts
# 3. join parts using '.'
def hrn_to_urn(self):
"""
compute urn from (hrn, type)
"""
# if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
def hrn_to_urn(self):
"""
compute urn from (hrn, type)
"""
# if not self.hrn or self.hrn.startswith(Xrn.URN_PREFIX):
- if self.hrn.startswith(Xrn.URN_PREFIX):
- raise SfaAPIError, "Xrn.hrn_to_urn, hrn=%s"%self.hrn
+ if Xrn.is_urn(self.hrn):
+ raise SfaAPIError("Xrn.hrn_to_urn, hrn=%s" % self.hrn)
- authority_string = self.get_authority_urn()
-
- if self.type == None:
- urn = "+".join(['',authority_string,Xrn.unescape(name)])
+ if self.type is None:
+ urn = "+".join(['', authority_string, Xrn.unescape(name)])
- urn = "+".join(['',authority_string,self.type,Xrn.unescape(name)])
-
+ urn = "+".join(['', authority_string,
+ self.type, Xrn.unescape(name)])
+
+ if hasattr(self, 'id') and self.id:
+ urn = "%s:%s" % (urn, self.id)
+
- result="-------------------- XRN\n"
- result += "URN=%s\n"%self.urn
- result += "HRN=%s\n"%self.hrn
- result += "TYPE=%s\n"%self.type
- result += "LEAF=%s\n"%self.get_leaf()
- result += "AUTH(hrn format)=%s\n"%self.get_authority_hrn()
- result += "AUTH(urn format)=%s\n"%self.get_authority_urn()
+ result = "-------------------- XRN\n"
+ result += "URN=%s\n" % self.urn
+ result += "HRN=%s\n" % self.hrn
+ result += "TYPE=%s\n" % self.type
+ result += "LEAF=%s\n" % self.get_leaf()
+ result += "AUTH(hrn format)=%s\n" % self.get_authority_hrn()
+ result += "AUTH(urn format)=%s\n" % self.get_authority_urn()