svn keywords
[plcapi.git] / PLC / Methods / UpdateEmulationLink.py
1 # Connect a Node with a Dummynet box, using Accessors
2 #
3 # Marta Carbone - unipi
4 # $Id$
5 # $URL$
6
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Nodes import Node, Nodes
11 from PLC.NodeGroups import NodeGroup, NodeGroups
12 from PLC.Sites import Site, Sites
13 from PLC.Auth import Auth
14 from PLC.Accessors.Accessors_dummynetbox import *                       # import dummynet accessors
15
16 class UpdateEmulationLink(Method):
17     """
18     Connect a Node with a Dummynet box.
19     Takes as input two node_id, the first should be a regular node,
20     the second a dummynet box.
21
22     This operation is restricted to PIs and techs owner of the site
23     on which the Dummynet box and the Node are located.
24     Admins may create emulation links for any site, but the Dummynet
25     and the Node must belong to the same site.
26
27     XXX Dummynet accessors should not be called directly, since they can
28     be used to create connection whitout checks.
29
30     Returns 1 if successful, faults otherwise.
31     """
32
33     roles = ['admin', 'pi', 'tech']
34
35     accepts = [
36         Auth(),
37         Parameter(int, 'node_id'),       # a node
38         Mixed(Parameter(int, 'node_id'), # a dummynet box, or None to delete the entry
39               None),
40     ]
41
42     returns = Parameter(int, '1 is successful, fault otherwise')
43
44     # Before to create the link we do the following checks:
45     #  - node existence,
46     #  - dummnet box existence,
47     #  - right roles (admin, pi, tech),
48     #  - node and dummynet box site should match.
49
50     def call(self, auth, node_id, dummynet_id):
51
52         assert self.caller is not None
53
54         # check for node existence
55         # Retrieve nodes from database
56         # We do not fetch both node and dummynet
57         # since we need to preserve the order of returned objects
58         nodes= Nodes(self.api, {'node_id':node_id, 'node_type':'regular'})
59         if not nodes:
60             raise PLCInvalidArgument, "Node %s not found" % node_id
61         node = nodes[0]
62
63         # check for dummynet box existence
64         nodes = Nodes(self.api, {'node_id':dummynet_id, 'node_type':'dummynet'})
65         if (dummynet_id != None) and not nodes:
66             raise PLCInvalidArgument, "Dummynet box %s not found" % dummynet_id
67
68         # check for site matching when create a link
69         if (dummynet_id != None):
70             dummynet = nodes[0]
71
72             # check if the node and the dummynet_id
73             # belong to the same site
74             if (node['site_id'] != dummynet['site_id']):
75                 raise PLCInvalidArgument, \
76                       "The Dummynet box must belog to the same site of the Node"
77
78         # check for roles permission to call this method
79         if 'admin' not in self.caller['roles']:
80             if site not in self.caller['site_ids']:
81                 raise PLCPermissionDenied, "Not allowed to manage on this link"
82
83         # Add the dummynetbox
84         emulation_link = SetNodeDummynetBox(self.api)
85         emulation_link.call(auth, node_id, dummynet_id)
86
87         return 1
88