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