999ddd79ba6e9ded375655e598b827fcc1ffcd36
[sfa.git] / archive / changes / AddNode.py
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
9 import sys
10 sys.path.append('../../../../util')
11 from pl_to_geni import *
12 from util import *
13 from db import *
14
15 can_update = lambda (field, value): field in \
16              ['hostname', 'boot_state', 'model', 'version', 'uuid']
17
18 class AddNode(Method):
19     """
20     Adds a new node. Any values specified in node_fields are used,
21     otherwise defaults are used.
22
23     PIs and techs may only add nodes to their own sites. Admins may
24     add nodes to any site.
25
26     Returns the new node_id (> 0) if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'tech']
30
31     node_fields = dict(filter(can_update, Node.fields.items()))
32
33     accepts = [
34         Auth(),
35         Mixed(Site.fields['site_id'],
36               Site.fields['login_base']),
37         node_fields
38         ]
39
40     returns = Parameter(int, 'New node_id (> 0) if successful')
41
42     def call(self, auth, site_id_or_login_base, node_fields):
43         node_fields = dict(filter(can_update, node_fields.items()))
44
45         # Get site information
46         sites = Sites(self.api, [site_id_or_login_base])
47         if not sites:
48             raise PLCInvalidArgument, "No such site"
49
50         site = sites[0]
51
52         # Authenticated function
53         assert self.caller is not None
54
55         # If we are not an admin, make sure that the caller is a
56         # member of the site.
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"
61             else:
62                 assert self.caller['person_id'] in site['person_ids']
63
64         node = Node(self.api, node_fields)
65         node['site_id'] = site['site_id']
66         node['uuid'] = str(uuid.uuid4().int)###############################soners
67         node.sync()
68
69         self.event_objects = {'Site': [site['site_id']],
70                              'Node': [node['node_id']]} 
71         self.message = "Node %s created" % node['node_id']
72
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)
77         if dbinfo == None:
78                 raise PLCInvalidArgument, "No GENI authority corresponding to the site "+site['name']
79         cnx = dbinfo[0]
80         tablename = dbinfo[1]
81         
82         new_hrn = plnode_to_node(node['hostname'], 0)
83         existing = cnx.query("SELECT * FROM "+tablename+" WHERE hrn = '"+new_hrn+"'; ")
84         if existing != None:
85                 new_hrn = plnode_to_node(node['hostname'], 1)
86                 existing = cnx.query("SELECT * FROM "+tablename+" WHERE hrn = '"+new_hrn+"'; ")
87                 if existing != None:
88                         new_hrn = plnode_to_node(node['hostname'], 2)
89                 
90         geni_record = {'hrn':''}
91         geni_record["hrn"] = new_hrn
92         geni_record["type"] = 'node'
93         geni_record['pointer'] = node['node_id']
94         
95         querystr = generate_querystr('INSERT', tablename, geni_record)
96         cnx.query(querystr)
97         
98         return node['node_id']