b8ae20eb6dc3b33b8991794a701aab1aa14f60a6
[plcapi.git] / PLC / Methods / DeleteEmulationLink.py
1 # Delete an Emulation link
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.Auth import Auth
11 from PLC.Method import Method
12 from PLC.NodeTags import *
13 from PLC.Accessors.Accessors_dummynetbox import * # import dummynet accessors
14
15 class DeleteEmulationLink(Method):
16     """
17     Delete a connection between a node and a dummynetbox.
18
19     Returns 1 if successful, faults otherwise.
20
21     """
22     roles = ['admin', 'pi', 'tech']
23
24     accepts = [
25         Auth(),
26         Parameter(int, 'node_id'),
27     ]
28
29     returns = Parameter(int, '1 is successful, 0 if not found, fault otherwise')
30
31     def call(self, auth, node_id):
32
33         assert self.caller is not None
34
35         # check for node existence
36         nodes= Nodes(self.api, [node_id])
37         if not nodes:
38             raise PLCInvalidArgument, "Node %s not found" % node_id
39
40         # check for roles permission to call this method
41         if 'admin' not in self.caller['roles']:
42             if site not in self.caller['site_ids']:
43                 raise PLCPermissionDenied, "Not allowed to delete this link"
44
45         # check for the right subclass
46         subclass = GetNodeSubclass(self.api)
47         node_subclass = subclass.call(auth, node_id)
48         if node_subclass != None:
49                 raise  PLCInvalidArgument, "%s is not a node, subclass is %s" % (node_id, node_subclass)
50
51         # Delete from the nodetags the entry with
52         # the given node_id and tagtype = 'dummynetbox'
53         nodetag = NodeTags(self.api, {'node_id':node_id, 'tagname':'dummynetbox_id'})
54
55         if not nodetag:
56             return 0
57
58         nodetag[0].delete()
59
60         return 1