svn keywords
[plcapi.git] / PLC / Methods / GetPeerData.py
1 #
2 # Thierry Parmentelat - INRIA
3
4 # $Id$
5 # $URL$
6
7 import time
8
9 from PLC.Faults import *
10 from PLC.Method import Method
11 from PLC.Parameter import Parameter, Mixed
12 from PLC.Auth import Auth
13
14 from PLC.Peers import Peer, Peers
15
16 from PLC.Sites import Site, Sites
17 from PLC.Keys import Key, Keys
18 from PLC.Nodes import Node, Nodes
19 from PLC.Persons import Person, Persons
20 from PLC.Slices import Slice, Slices
21 from PLC.SliceTags import SliceTags
22
23 class GetPeerData(Method):
24     """
25     Returns lists of local objects that a peer should cache in its
26     database as foreign objects. Also returns the list of foreign
27     nodes in this database, for which the calling peer is
28     authoritative, to assist in synchronization of slivers.
29     
30     See the implementation of RefreshPeer for how this data is used.
31     """
32
33     roles = ['admin', 'peer']
34
35     accepts = [Auth()]
36
37     returns = {
38         'Sites': Parameter([dict], "List of local sites"),
39         'Keys': Parameter([dict], "List of local keys"),
40         'Nodes': Parameter([dict], "List of local nodes"),
41         'Persons': Parameter([dict], "List of local users"),
42         'Slices': Parameter([dict], "List of local slices"),
43         'db_time': Parameter(float, "(Debug) Database fetch time"),
44         }
45
46     def call (self, auth):
47         start = time.time()
48
49         # Filter out various secrets
50         node_fields = filter(lambda field: field not in \
51                              ['boot_nonce', 'key', 'session', 'root_person_ids'],
52                              Node.fields)
53         nodes = Nodes(self.api, {'peer_id': None}, node_fields);
54         # filter out whitelisted nodes
55         nodes = [ n for n in nodes if not n['slice_ids_whitelist']] 
56         
57
58         person_fields = filter(lambda field: field not in \
59                                ['password', 'verification_key', 'verification_expires'],
60                                Person.fields)
61
62         # XXX Optimize to return only those Persons, Keys, and Slices
63         # necessary for slice creation on the calling peer's nodes.
64
65         # filter out special person
66         persons = Persons(self.api, {'~email':[self.api.config.PLC_API_MAINTENANCE_USER,
67                                                self.api.config.PLC_ROOT_USER],
68                                      'peer_id': None}, person_fields)
69
70         # filter out system slices
71         system_slice_ids = SliceTags(self.api, {'name': 'system', 'value': '1'}).dict('slice_id')
72         slices = Slices(self.api, {'peer_id': None,
73                                    '~slice_id':system_slice_ids.keys()})
74         
75         result = {
76             'Sites': Sites(self.api, {'peer_id': None}),
77             'Keys': Keys(self.api, {'peer_id': None}),
78             'Nodes': nodes,
79             'Persons': persons,
80             'Slices': slices,
81             }
82
83         if isinstance(self.caller, Peer):
84             result['PeerNodes'] = Nodes(self.api, {'peer_id': self.caller['peer_id']})
85
86         result['db_time'] = time.time() - start
87
88         return result