+ # from PLCAPI.GetSlivers.get_slivers()
+ slice_fields = ['slice_id', 'name', 'instantiation', 'expires', 'person_ids', 'slice_tag_ids']
+ slices = self.api.plshell.GetSlices(self.api.plauth, slice_name, slice_fields)
+ # Build up list of users and slice attributes
+ person_ids = set()
+ all_slice_tag_ids = set()
+ for slice in slices:
+ person_ids.update(slice['person_ids'])
+ all_slice_tag_ids.update(slice['slice_tag_ids'])
+ person_ids = list(person_ids)
+ all_slice_tag_ids = list(all_slice_tag_ids)
+ # Get user information
+ all_persons_list = self.api.plshell.GetPersons(self.api.plauth, {'person_id':person_ids,'enabled':True}, ['person_id', 'enabled', 'key_ids'])
+ all_persons = {}
+ for person in all_persons_list:
+ all_persons[person['person_id']] = person
+
+ # Build up list of keys
+ key_ids = set()
+ for person in all_persons.values():
+ key_ids.update(person['key_ids'])
+ key_ids = list(key_ids)
+ # Get user account keys
+ all_keys_list = self.api.plshell.GetKeys(self.api.plauth, key_ids, ['key_id', 'key', 'key_type'])
+ all_keys = {}
+ for key in all_keys_list:
+ all_keys[key['key_id']] = key
+ # Get slice attributes
+ all_slice_tags_list = self.api.plshell.GetSliceTags(self.api.plauth, all_slice_tag_ids)
+ all_slice_tags = {}
+ for slice_tag in all_slice_tags_list:
+ all_slice_tags[slice_tag['slice_tag_id']] = slice_tag
+
+ slivers = []
+ for slice in slices:
+ keys = []
+ for person_id in slice['person_ids']:
+ if person_id in all_persons:
+ person = all_persons[person_id]
+ if not person['enabled']:
+ continue
+ for key_id in person['key_ids']:
+ if key_id in all_keys:
+ key = all_keys[key_id]
+ keys += [{'key_type': key['key_type'],
+ 'key': key['key']}]
+ attributes = []
+ # All (per-node and global) attributes for this slice
+ slice_tags = []
+ for slice_tag_id in slice['slice_tag_ids']:
+ if slice_tag_id in all_slice_tags:
+ slice_tags.append(all_slice_tags[slice_tag_id])
+ # Per-node sliver attributes take precedence over global
+ # slice attributes, so set them first.
+ # Then comes nodegroup slice attributes
+ # Followed by global slice attributes
+ sliver_attributes = []
+
+ if node is not None:
+ for sliver_attribute in filter(lambda a: a['node_id'] == node['node_id'], slice_tags):
+ sliver_attributes.append(sliver_attribute['tagname'])
+ attributes.append({'tagname': sliver_attribute['tagname'],
+ 'value': sliver_attribute['value']})
+
+ # set nodegroup slice attributes
+ for slice_tag in filter(lambda a: a['nodegroup_id'] in node['nodegroup_ids'], slice_tags):
+ # Do not set any nodegroup slice attributes for
+ # which there is at least one sliver attribute
+ # already set.
+ if slice_tag not in slice_tags:
+ attributes.append({'tagname': slice_tag['tagname'],
+ 'value': slice_tag['value']})
+
+ for slice_tag in filter(lambda a: a['node_id'] is None, slice_tags):
+ # Do not set any global slice attributes for
+ # which there is at least one sliver attribute
+ # already set.
+ if slice_tag['tagname'] not in sliver_attributes:
+ attributes.append({'tagname': slice_tag['tagname'],
+ 'value': slice_tag['value']})
+
+ # XXX Sanity check; though technically this should be a system invariant
+ # checked with an assertion
+ if slice['expires'] > MAXINT: slice['expires']= MAXINT
+
+ slivers.append({
+ 'hrn': hrn,
+ 'name': slice['name'],
+ 'slice_id': slice['slice_id'],
+ 'instantiation': slice['instantiation'],
+ 'expires': slice['expires'],
+ 'keys': keys,
+ 'attributes': attributes
+ })
+
+ return slivers
+
+ def get_peer(self, xrn):
+ hrn, type = urn_to_hrn(xrn)
+ # Becaues of myplc federation, we first need to determine if this
+ # slice belongs to out local plc or a myplc peer. We will assume it
+ # is a local site, unless we find out otherwise
+ peer = None
+
+ # get this slice's authority (site)
+ slice_authority = get_authority(hrn)
+
+ # get this site's authority (sfa root authority or sub authority)
+ site_authority = get_authority(slice_authority).lower()
+
+ # check if we are already peered with this site_authority, if so
+ peers = self.api.plshell.GetPeers(self.api.plauth, {}, ['peer_id', 'peername', 'shortname', 'hrn_root'])
+ for peer_record in peers:
+ names = [name.lower() for name in peer_record.values() if isinstance(name, StringTypes)]
+ if site_authority in names:
+ peer = peer_record['shortname']
+
+ return peer
+
+ def get_sfa_peer(self, xrn):
+ hrn, type = urn_to_hrn(xrn)
+
+ # return the authority for this hrn or None if we are the authority
+ sfa_peer = None
+ slice_authority = get_authority(hrn)
+ site_authority = get_authority(slice_authority)
+
+ if site_authority != self.api.hrn:
+ sfa_peer = site_authority
+
+ return sfa_peer
+
+ def verify_site(self, registry, credential, slice_hrn, peer, sfa_peer):
+ authority = get_authority(slice_hrn)
+ authority_urn = hrn_to_urn(authority, 'authority')
+ site_records = registry.resolve(credential, authority_urn)
+
+ site = {}
+ for site_record in site_records:
+ if site_record['type'] == 'authority':
+ site = site_record
+ if not site:
+ raise RecordNotFound(authority)
+ remote_site_id = site.pop('site_id')
+
+ login_base = get_leaf(authority)
+ sites = self.api.plshell.GetSites(self.api.plauth, login_base)
+ if not sites:
+ site_id = self.api.plshell.AddSite(self.api.plauth, site)
+ if peer:
+ self.api.plshell.BindObjectToPeer(self.api.plauth, 'site', site_id, peer, remote_site_id)
+ # mark this site as an sfa peer record
+ if sfa_peer:
+ peer_dict = {'type': 'authority', 'hrn': authority, 'peer_authority': sfa_peer, 'pointer': site_id}
+ registry.register_peer_object(credential, peer_dict)
+ else:
+ site_id = sites[0]['site_id']
+ remote_site_id = sites[0]['peer_site_id']
+ old_site = sites[0]
+ #the site is alredy on the remote agg. Let us update(e.g. max_slices field) it with the latest info.
+ self.sync_site(old_site, site, peer)