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