1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Nodes import Node, Nodes
5 from PLC.NodeGroups import NodeGroup, NodeGroups
6 from PLC.Sites import Site, Sites
7 from PLC.Auth import Auth
8 import uuid ##################################soners
10 sys.path.append('../../../../util')
11 from pl_to_geni import *
15 can_update = lambda (field, value): field in \
16 ['hostname', 'boot_state', 'model', 'version', 'uuid']
18 class AddNode(Method):
20 Adds a new node. Any values specified in node_fields are used,
21 otherwise defaults are used.
23 PIs and techs may only add nodes to their own sites. Admins may
24 add nodes to any site.
26 Returns the new node_id (> 0) if successful, faults otherwise.
29 roles = ['admin', 'pi', 'tech']
31 node_fields = dict(filter(can_update, Node.fields.items()))
35 Mixed(Site.fields['site_id'],
36 Site.fields['login_base']),
40 returns = Parameter(int, 'New node_id (> 0) if successful')
42 def call(self, auth, site_id_or_login_base, node_fields):
43 node_fields = dict(filter(can_update, node_fields.items()))
45 # Get site information
46 sites = Sites(self.api, [site_id_or_login_base])
48 raise PLCInvalidArgument, "No such site"
52 # Authenticated function
53 assert self.caller is not None
55 # If we are not an admin, make sure that the caller is a
57 if 'admin' not in self.caller['roles']:
58 if site['site_id'] not in self.caller['site_ids']:
59 assert self.caller['person_id'] not in site['person_ids']
60 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
62 assert self.caller['person_id'] in site['person_ids']
64 node = Node(self.api, node_fields)
65 node['site_id'] = site['site_id']
66 node['uuid'] = str(uuid.uuid4().int)###############################soners
69 self.event_objects = {'Site': [site['site_id']],
70 'Node': [node['node_id']]}
71 self.message = "Node %s created" % node['node_id']
73 #insert the record into GENI tables ###############################soner
74 (global_sr_tree, global_cr_tree) = get_tree_globals()
75 (site_id, site_hrn) = site_to_auth(site['site_id'])
76 dbinfo = determine_dbinfo(site_hrn, global_cr_tree)
78 raise PLCInvalidArgument, "No GENI authority corresponding to the site "+site['name']
82 new_hrn = plnode_to_node(node['hostname'], 0)
83 existing = cnx.query("SELECT * FROM "+tablename+" WHERE hrn = '"+new_hrn+"'; ")
85 new_hrn = plnode_to_node(node['hostname'], 1)
86 existing = cnx.query("SELECT * FROM "+tablename+" WHERE hrn = '"+new_hrn+"'; ")
88 new_hrn = plnode_to_node(node['hostname'], 2)
90 geni_record = {'hrn':''}
91 geni_record["hrn"] = new_hrn
92 geni_record["type"] = 'node'
93 geni_record['pointer'] = node['node_id']
95 querystr = generate_querystr('INSERT', tablename, geni_record)
98 return node['node_id']