Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / PLC / Methods / GetPeerData.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4
5 import time
6
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
11
12 from PLC.Peers import Peer, Peers
13
14 from PLC.Sites import Site, Sites
15 from PLC.Keys import Key, Keys
16 from PLC.Nodes import Node, Nodes
17 from PLC.Persons import Person, Persons
18 from PLC.Slices import Slice, Slices
19 from PLC.SliceTags import SliceTags
20
21 class GetPeerData(Method):
22     """
23     Returns lists of local objects that a peer should cache in its
24     database as foreign objects. Also returns the list of foreign
25     nodes in this database, for which the calling peer is
26     authoritative, to assist in synchronization of slivers.
27
28     See the implementation of RefreshPeer for how this data is used.
29     """
30
31     roles = ['admin', 'peer']
32
33     accepts = [Auth()]
34
35     returns = {
36         'Sites': Parameter([dict], "List of local sites"),
37         'Keys': Parameter([dict], "List of local keys"),
38         'Nodes': Parameter([dict], "List of local nodes"),
39         'Persons': Parameter([dict], "List of local users"),
40         'Slices': Parameter([dict], "List of local slices"),
41         'db_time': Parameter(float, "(Debug) Database fetch time"),
42         }
43
44     def call (self, auth):
45         start = time.time()
46
47         # Filter out various secrets
48         node_fields = [ field for field in Node.fields if field \
49                             not in ['boot_nonce', 'key', 'session', 'root_person_ids']]
50         try:
51             node_fields += ['hrn']
52             nodes = Nodes(self.api, {'peer_id': None}, node_fields)
53         except:
54             nodes = Nodes(self.api, {'peer_id': None}, node_fields)
55         # filter out whitelisted nodes
56         nodes = [ n for n in nodes if not n['slice_ids_whitelist']]
57
58
59         person_fields = [ field for field in Person.fields if field \
60                               not in ['password', 'verification_key', 'verification_expires']]
61
62         site_fields = [field for field in Site.fields]
63         slice_fields = [field for field in Slice.fields]
64
65         try:
66             person_fields += ['sfa_created','hrn']
67             site_fields += ['sfa_created','hrn']
68             slice_fields += ['sfa_created','hrn']
69         
70             # XXX Optimize to return only those Persons, Keys, and Slices
71             # necessary for slice creation on the calling peer's nodes.
72
73             # filter out special person
74         
75             persons = Persons(self.api, {'~email':[self.api.config.PLC_API_MAINTENANCE_USER, self.api.config.PLC_ROOT_USER], 'peer_id': None}, person_fields)
76
77             # filter out system slices
78             system_slice_ids = SliceTags(self.api, {'name': 'system', 'value': '1'}).dict('slice_id')
79             slices = Slices(self.api, {'peer_id': None,'~slice_id':system_slice_ids.keys()}, slice_fields)
80
81             sites = Sites(self.api, {'peer_id': None}, site_fields)
82        
83             # filter out objects with  sfa_created=True
84             filtered_sites = [site for site in sites if site.get('sfa_created', None) != 'True']
85             filtered_slices = [slice for slice in slices if slice.get('sfa_created', None) != 'True']
86             filtered_persons = [person for person in persons if person.get('sfa_created', None) != 'True']  
87
88         except:
89             # handle peers with old version of MyPLC that does not support 'sfa_created' and 'hrn' fields for Site/Slice/Person 
90  
91             # XXX Optimize to return only those Persons, Keys, and Slices
92             # necessary for slice creation on the calling peer's nodes.
93
94             # filter out special person
95
96             filtered_persons = Persons(self.api, {'~email':[self.api.config.PLC_API_MAINTENANCE_USER, self.api.config.PLC_ROOT_USER], 'peer_id': None}, person_fields)
97
98             # filter out system slices
99             system_slice_ids = SliceTags(self.api, {'name': 'system', 'value': '1'}).dict('slice_id')
100             filtered_slices = Slices(self.api, {'peer_id': None,
101                                    '~slice_id':system_slice_ids.keys()}, slice_fields)
102
103             filtered_sites = Sites(self.api, {'peer_id': None}, site_fields)
104
105
106         result = {
107             'Sites': filtered_sites,
108             'Keys': Keys(self.api, {'peer_id': None}),
109             'Nodes': nodes,
110             'Persons': filtered_persons,
111             'Slices': filtered_slices,
112             }
113
114
115         if isinstance(self.caller, Peer):
116             result['PeerNodes'] = Nodes(self.api, {'peer_id': self.caller['peer_id']})
117
118         result['db_time'] = time.time() - start
119
120         return result