we'll need to import omf_slicemgr
[plcapi.git] / PLC / Nodes.py
index 01328e3..46fb99a 100644 (file)
@@ -5,6 +5,7 @@
 # Copyright (C) 2006 The Trustees of Princeton University
 #
 # $Id$
 # Copyright (C) 2006 The Trustees of Princeton University
 #
 # $Id$
+# $URL$
 #
 
 from types import StringTypes
 #
 
 from types import StringTypes
@@ -15,8 +16,9 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Filter import Filter
 from PLC.Debug import profile
 from PLC.Table import Row, Table
 from PLC.Filter import Filter
 from PLC.Debug import profile
 from PLC.Table import Row, Table
-from PLC.Interfaces import Interface, Interfaces
+from PLC.NodeTypes import NodeTypes
 from PLC.BootStates import BootStates
 from PLC.BootStates import BootStates
+from PLC.Interfaces import Interface, Interfaces
 
 def valid_hostname(hostname):
     # 1. Each part begins and ends with a letter or number.
 
 def valid_hostname(hostname):
     # 1. Each part begins and ends with a letter or number.
@@ -38,13 +40,16 @@ class Node(Row):
 
     table_name = 'nodes'
     primary_key = 'node_id'
 
     table_name = 'nodes'
     primary_key = 'node_id'
-    # Thierry -- we use delete on interfaces so the related InterfaceSettings get deleted too
-    join_tables = ['nodegroup_node', 'conf_file_node', 'pcu_node', 'slice_node', 'slice_attribute', 'node_session', 'peer_node','node_slice_whitelist']
+    join_tables = [ 'slice_node', 'peer_node', 'slice_tag', 
+                    'node_session', 'node_slice_whitelist', 
+                    'node_tag', 'conf_file_node', 'pcu_node', ]
     fields = {
         'node_id': Parameter(int, "Node identifier"),
     fields = {
         'node_id': Parameter(int, "Node identifier"),
+        'node_type': Parameter(str,"Node type",max=20),
         'hostname': Parameter(str, "Fully qualified hostname", max = 255),
         'site_id': Parameter(int, "Site at which this node is located"),
         'boot_state': Parameter(str, "Boot state", max = 20),
         'hostname': Parameter(str, "Fully qualified hostname", max = 255),
         'site_id': Parameter(int, "Site at which this node is located"),
         'boot_state': Parameter(str, "Boot state", max = 20),
+        'run_level': Parameter(str, "Run level", max = 20),
         'model': Parameter(str, "Make and model of the actual machine", max = 255, nullok = True),
         'boot_nonce': Parameter(str, "(Admin only) Random value generated by the node at last boot", max = 128),
         'version': Parameter(str, "Apparent Boot CD version", max = 64),
         'model': Parameter(str, "Make and model of the actual machine", max = 255, nullok = True),
         'boot_nonce': Parameter(str, "(Admin only) Random value generated by the node at last boot", max = 128),
         'version': Parameter(str, "Apparent Boot CD version", max = 64),
@@ -52,10 +57,10 @@ class Node(Row):
         'date_created': Parameter(int, "Date and time when node entry was created", ro = True),
         'last_updated': Parameter(int, "Date and time when node entry was created", ro = True),
        'last_contact': Parameter(int, "Date and time when node last contacted plc", ro = True), 
         'date_created': Parameter(int, "Date and time when node entry was created", ro = True),
         'last_updated': Parameter(int, "Date and time when node entry was created", ro = True),
        'last_contact': Parameter(int, "Date and time when node last contacted plc", ro = True), 
+        'verified': Parameter(bool, "Whether the node configuration is verified correct", ro=False),
         'key': Parameter(str, "(Admin only) Node key", max = 256),
         'session': Parameter(str, "(Admin only) Node session value", max = 256, ro = True),
         'interface_ids': Parameter([int], "List of network interfaces that this node has"),
         'key': Parameter(str, "(Admin only) Node key", max = 256),
         'session': Parameter(str, "(Admin only) Node session value", max = 256, ro = True),
         'interface_ids': Parameter([int], "List of network interfaces that this node has"),
-        'nodegroup_ids': Parameter([int], "List of node groups that this node is in"),
         'conf_file_ids': Parameter([int], "List of configuration files specific to this node"),
         # 'root_person_ids': Parameter([int], "(Admin only) List of people who have root access to this node"),
         'slice_ids': Parameter([int], "List of slices on this node"),
         'conf_file_ids': Parameter([int], "List of configuration files specific to this node"),
         # 'root_person_ids': Parameter([int], "(Admin only) List of people who have root access to this node"),
         'slice_ids': Parameter([int], "List of slices on this node"),
@@ -64,29 +69,23 @@ class Node(Row):
         'ports': Parameter([int], "List of PCU ports that this node is connected to"),
         'peer_id': Parameter(int, "Peer to which this node belongs", nullok = True),
         'peer_node_id': Parameter(int, "Foreign node identifier at peer", nullok = True),
         'ports': Parameter([int], "List of PCU ports that this node is connected to"),
         'peer_id': Parameter(int, "Peer to which this node belongs", nullok = True),
         'peer_node_id': Parameter(int, "Foreign node identifier at peer", nullok = True),
-        'tag_ids' : Parameter ([int], "List of tags attached to this node"),
+        'node_tag_ids' : Parameter ([int], "List of tags attached to this node"),
+        'nodegroup_ids': Parameter([int], "List of node groups that this node is in"),
         }
     related_fields = {
        'interfaces': [Mixed(Parameter(int, "Interface identifier"),
         }
     related_fields = {
        'interfaces': [Mixed(Parameter(int, "Interface identifier"),
-                                      Filter(Interface.fields))],
-       'nodegroups': [Mixed(Parameter(int, "NodeGroup identifier"),
-                             Parameter(str, "NodeGroup name"))],
+                             Filter(Interface.fields))],
        'conf_files': [Parameter(int, "ConfFile identifier")],
        'slices': [Mixed(Parameter(int, "Slice identifier"),
                          Parameter(str, "Slice name"))],
        'slices_whitelist': [Mixed(Parameter(int, "Slice identifier"),
                                    Parameter(str, "Slice name"))]
        }
        'conf_files': [Parameter(int, "ConfFile identifier")],
        'slices': [Mixed(Parameter(int, "Slice identifier"),
                          Parameter(str, "Slice name"))],
        'slices_whitelist': [Mixed(Parameter(int, "Slice identifier"),
                                    Parameter(str, "Slice name"))]
        }
-    # for Cache
-    class_key = 'hostname'
-    foreign_fields = ['boot_state','model','version']
-    # forget about these ones, they are read-only anyway
-    # handling them causes Cache to re-sync all over again 
-    # 'date_created','last_updated'
-    foreign_xrefs = [
-       # in this case, we dont need the 'table' but Cache will look it up, so...
-        {'field' : 'site_id' , 'class' : 'Site' , 'table' : 'unused-on-direct-refs' } ,
-       ]
+
+    view_tags_name = "view_node_tags"
+    # tags are used by the Add/Get/Update methods to expose tags
+    # this is initialized here and updated by the accessors factory
+    tags = { }
 
     def validate_hostname(self, hostname):
         if not valid_hostname(hostname):
 
     def validate_hostname(self, hostname):
         if not valid_hostname(hostname):
@@ -99,11 +98,16 @@ class Node(Row):
 
         return hostname
 
 
         return hostname
 
+    def validate_node_type(self, node_type):
+        node_types = [row['node_type'] for row in NodeTypes(self.api)]
+        if node_type not in node_types:
+            raise PLCInvalidArgument, "Invalid node type %r"%node_type
+        return node_type
+
     def validate_boot_state(self, boot_state):
         boot_states = [row['boot_state'] for row in BootStates(self.api)]
         if boot_state not in boot_states:
     def validate_boot_state(self, boot_state):
         boot_states = [row['boot_state'] for row in BootStates(self.api)]
         if boot_state not in boot_states:
-            raise PLCInvalidArgument, "Invalid boot state"
-
+            raise PLCInvalidArgument, "Invalid boot state %r"%boot_state
         return boot_state
 
     validate_date_created = Row.validate_timestamp
         return boot_state
 
     validate_date_created = Row.validate_timestamp
@@ -137,12 +141,12 @@ class Node(Row):
 
     def associate_interfaces(self, auth, field, value):
        """
 
     def associate_interfaces(self, auth, field, value):
        """
-       Delete interfaces not found in value list (using DeleteNodeNetwor)k     
+       Delete interfaces not found in value list (using DeleteInterface)       
        Add interfaces found in value list (using AddInterface)
        Updates interfaces found w/ interface_id in value list (using UpdateInterface) 
        """
 
        Add interfaces found in value list (using AddInterface)
        Updates interfaces found w/ interface_id in value list (using UpdateInterface) 
        """
 
-       assert 'interfacep_ids' in self
+       assert 'interface_ids' in self
         assert 'node_id' in self
         assert isinstance(value, list)
 
         assert 'node_id' in self
         assert isinstance(value, list)
 
@@ -156,38 +160,6 @@ class Node(Row):
             for stale_interface in stale_interfaces:
                 DeleteInterface.__call__(DeleteInterface(self.api), auth, stale_interface['interface_id'])
 
             for stale_interface in stale_interfaces:
                 DeleteInterface.__call__(DeleteInterface(self.api), auth, stale_interface['interface_id'])
 
-    def associate_nodegroups(self, auth, field, value):
-       """
-       Add node to nodegroups found in value list (AddNodeToNodegroup)
-       Delete node from nodegroup not found in value list (DeleteNodeFromNodegroup)
-       """
-       
-       from PLC.NodeGroups import NodeGroups
-       
-       assert 'nodegroup_ids' in self
-       assert 'node_id' in self
-       assert isinstance(value, list)
-
-       (nodegroup_ids, nodegroup_names) = self.separate_types(value)[0:2]
-       
-       if nodegroup_names:
-           nodegroups = NodeGroups(self.api, nodegroup_names, ['nodegroup_id']).dict('nodegroup_id')
-           nodegroup_ids += nodegroups.keys()
-
-       if self['nodegroup_ids'] != nodegroup_ids:
-           from PLC.Methods.AddNodeToNodeGroup import AddNodeToNodeGroup
-           from PLC.Methods.DeleteNodeFromNodeGroup import DeleteNodeFromNodeGroup
-       
-           new_nodegroups = set(nodegroup_ids).difference(self['nodegroup_ids'])
-           stale_nodegroups = set(self['nodegroup_ids']).difference(nodegroup_ids)
-       
-           for new_nodegroup in new_nodegroups:
-               AddNodeToNodeGroup.__call__(AddNodeToNodeGroup(self.api), auth, self['node_id'], new_nodegroup)
-           for stale_nodegroup in stale_nodegroups:
-               DeleteNodeFromNodeGroup.__call__(DeleteNodeFromNodeGroup(self.api), auth, self['node_id'], stale_nodegroup)
-         
-
     def associate_conf_files(self, auth, field, value):
        """
        Add conf_files found in value list (AddConfFileToNode)
     def associate_conf_files(self, auth, field, value):
        """
        Add conf_files found in value list (AddConfFileToNode)
@@ -211,7 +183,6 @@ class Node(Row):
            for stale_conf_file in stale_conf_files:
                DeleteConfFileFromNode.__call__(DeleteConfFileFromNode(self.api), auth, stale_conf_file, self['node_id'])
 
            for stale_conf_file in stale_conf_files:
                DeleteConfFileFromNode.__call__(DeleteConfFileFromNode(self.api), auth, stale_conf_file, self['node_id'])
 
     def associate_slices(self, auth, field, value):
        """
        Add slices found in value list to (AddSliceToNode)
     def associate_slices(self, auth, field, value):
        """
        Add slices found in value list to (AddSliceToNode)
@@ -277,11 +248,15 @@ class Node(Row):
         """
 
         assert 'node_id' in self
         """
 
         assert 'node_id' in self
-       assert 'interface_ids' in self
 
 
-       # we need to clean up InterfaceSettings, so handling interfaces as part of join_tables does not work
-       for interface in Interfaces(self.api,self['interface_ids']):
-           interface.delete()
+       # we need to clean up InterfaceTags, so handling interfaces as part of join_tables does not work
+        # federated nodes don't have interfaces though so for smooth transition from 4.2 to 4.3
+        if 'peer_id' in self and self['peer_id']:
+            pass
+        else:
+            assert 'interface_ids' in self
+            for interface in Interfaces(self.api,self['interface_ids']):
+                interface.delete()
 
         # Clean up miscellaneous join tables
         for table in self.join_tables:
 
         # Clean up miscellaneous join tables
         for table in self.join_tables:
@@ -302,8 +277,15 @@ class Nodes(Table):
     def __init__(self, api, node_filter = None, columns = None):
         Table.__init__(self, api, Node, columns)
 
     def __init__(self, api, node_filter = None, columns = None):
         Table.__init__(self, api, Node, columns)
 
-        sql = "SELECT %s FROM view_nodes WHERE deleted IS False" % \
-              ", ".join(self.columns)
+        # the view that we're selecting upon: start with view_nodes
+        view = "view_nodes"
+        # as many left joins as requested tags
+        for tagname in self.tag_columns:
+            view= "%s left join %s using (%s)"%(view,Node.tagvalue_view_name(tagname),
+                                                Node.primary_key)
+            
+        sql = "SELECT %s FROM %s WHERE deleted IS False" % \
+              (", ".join(self.columns.keys()+self.tag_columns.keys()),view)
 
         if node_filter is not None:
             if isinstance(node_filter, (list, tuple, set)):
 
         if node_filter is not None:
             if isinstance(node_filter, (list, tuple, set)):
@@ -313,7 +295,8 @@ class Nodes(Table):
                 node_filter = Filter(Node.fields, {'node_id': ints, 'hostname': strs})
                 sql += " AND (%s) %s" % node_filter.sql(api, "OR")
             elif isinstance(node_filter, dict):
                 node_filter = Filter(Node.fields, {'node_id': ints, 'hostname': strs})
                 sql += " AND (%s) %s" % node_filter.sql(api, "OR")
             elif isinstance(node_filter, dict):
-                node_filter = Filter(Node.fields, node_filter)
+                allowed_fields=dict(Node.fields.items()+Node.tags.items())
+                node_filter = Filter(allowed_fields, node_filter)
                 sql += " AND (%s) %s" % node_filter.sql(api, "AND")
             elif isinstance (node_filter, StringTypes):
                 node_filter = Filter(Node.fields, {'hostname':[node_filter]})
                 sql += " AND (%s) %s" % node_filter.sql(api, "AND")
             elif isinstance (node_filter, StringTypes):
                 node_filter = Filter(Node.fields, {'hostname':[node_filter]})