add filtering for node status, add custom filtering class
[sface.git] / sface / screens / mainscreen.py
index 0cdb3da..2963754 100644 (file)
@@ -1,10 +1,13 @@
 
+import datetime
 import os
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 
-from sfa.util.rspecHelper import RSpec
+#from sfa.util.rspecHelper import RSpec
+from sfa.rspecs.rspec_parser import parse_rspec
 from sface.config import config
+from sface.sfirenew import SfiRenewer
 from sface.sfiprocess import SfiProcess
 from sface.screens.sfascreen import SfaScreen
 
@@ -23,6 +26,11 @@ tag_status = { "in": "Already Set",
 default_tags = "Default tags"
 settable_tags = ['delegations', 'initscript']
 
+NAME_COLUMN = 0
+NODE_STATUS_COLUMN = 1
+MEMBERSHIP_STATUS_COLUMN = 2
+KIND_COLUMN = 3
+
 def itemType(index):
     if index.parent().parent().isValid():
         return "tag"
@@ -46,9 +54,9 @@ class NodeView(QTreeView):
     def mouseDoubleClickEvent(self, event):
         index = self.currentIndex()
         model = index.model()
-        status_index = model.index(index.row(), 1, index.parent())
+        status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
         status_data = status_index.data().toString()
-        node_index = model.index(index.row(), 0, index.parent())
+        node_index = model.index(index.row(), NAME_COLUMN, index.parent())
         node_data = node_index.data().toString()
 
         if itemType(node_index) == "tag":
@@ -138,11 +146,11 @@ class NodeNameDelegate(QStyledItemDelegate):
 
     def paint(self, painter, option, index):
         model = index.model()
-        status_index = model.index(index.row(), 1, index.parent())
+        status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
         status_data = status_index.data().toString()
 
         fm = QFontMetrics(option.font)
-        rect = option.rect
+        rect = QRect(option.rect)
 
         data = index.data().toString()
         rect.setHeight(rect.height() - 2)
@@ -158,48 +166,124 @@ class NodeNameDelegate(QStyledItemDelegate):
         painter.save()
         painter.setRenderHint(QPainter.Antialiasing)
 
+        if option.state & QStyle.State_Selected:
+            painter.fillRect(option.rect, option.palette.color(QPalette.Active, QPalette.Highlight))
+
         if itemType(index) == "node":
             if status_data == node_status['in']: # already in the slice
-                painter.fillPath(path, QColor("cyan"))
+                painter.fillPath(path, QColor.fromRgb(0, 250, 250))
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
             elif status_data == node_status['add']: # newly added to the slice
                 painter.fillPath(path, QColor.fromRgb(0, 250, 0))
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
             elif status_data == node_status['remove']: # removed from the slice
                 painter.fillPath(path, QColor.fromRgb(250, 0, 0))
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
             else:
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
         else:
             if status_data == tag_status['in']: # already in the slice
-                painter.fillPath(path, QColor("cyan"))
+                painter.fillPath(path, QColor.fromRgb(0, 250, 250))
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
             elif status_data == tag_status['add']: # newly added to the slice
                 painter.fillPath(path, QColor.fromRgb(0, 250, 0))
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
             elif status_data == tag_status['remove']: # removed from the slice
                 painter.fillPath(path, QColor.fromRgb(250, 0, 0))
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
             else:
                 painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(option.rect, 0, QString(data))
+                painter.drawText(rect, 0, QString(data))
 
         painter.restore()
 
+class NodeStatusDelegate(QStyledItemDelegate):
+    def __init__(self, parent):
+        QStyledItemDelegate.__init__(self, parent)
+
+    def paint(self, painter, option, index):
+        model = index.model()
+        nodestatus_index = model.index(index.row(), NODE_STATUS_COLUMN, index.parent())
+        nodestatus_data = nodestatus_index.data().toString()
+
+        fm = QFontMetrics(option.font)
+        rect = QRect(option.rect)
+
+        data = index.data().toString()
+        rect.setHeight(rect.height() - 2)
+        rect.setWidth(fm.width(QString(data)) + 6)
+        rect.setX(rect.x() + 5)
+        rect.setY(rect.y() - 1)
+
+        x, y, h, w = rect.x(), rect.y(), rect.height(), rect.width()
+
+        path = QPainterPath()
+        path.addRoundedRect(x - 1, y + 1, w, h, 4, 4)
+
+        painter.save()
+        painter.setRenderHint(QPainter.Antialiasing)
+
+        if option.state & QStyle.State_Selected:
+            painter.fillRect(option.rect, option.palette.color(QPalette.Active, QPalette.Highlight))
+
+        if (nodestatus_data == ""):
+                painter.setPen(QColor.fromRgb(0, 0, 0))
+                painter.drawText(rect, 0, QString(data))
+        elif (nodestatus_data == "boot"):
+                painter.fillPath(path, QColor.fromRgb(0, 250, 0))
+                painter.setPen(QColor.fromRgb(0, 0, 0))
+                painter.drawText(rect, 0, QString(data))
+        else:
+                painter.fillPath(path, QColor.fromRgb(250, 0, 0))
+                painter.setPen(QColor.fromRgb(0, 0, 0))
+                painter.drawText(rect, 0, QString(data))
+
+        painter.restore()
+
+class NodeFilterProxyModel(QSortFilterProxyModel):
+    def __init__(self, parent=None):
+        QSortFilterProxyModel.__init__(self, parent)
+        self.hostname_filter_regex = None
+        self.nodestatus_filter = None
+
+    def setHostNameFilter(self, hostname):
+        self.hostname_filter_regex = QRegExp(hostname)
+        self.invalidateFilter()
+
+    def setNodeStatusFilter(self, status):
+        if (status == "all"):
+            self.nodestatus_filter = None
+        else:
+            self.nodestatus_filter = status
+        self.invalidateFilter()
+
+    def filterAcceptsRow(self, sourceRow, source_parent):
+        kind_data = self.sourceModel().index(sourceRow, KIND_COLUMN, source_parent).data().toString()
+        if (kind_data == "node"):
+            if self.hostname_filter_regex:
+                name_data = self.sourceModel().index(sourceRow, NAME_COLUMN, source_parent).data().toString()
+                if (self.hostname_filter_regex.indexIn(name_data) < 0):
+                    return False
+            if self.nodestatus_filter:
+                nodestatus_data = self.sourceModel().index(sourceRow, NODE_STATUS_COLUMN, source_parent).data().toString()
+                if (nodestatus_data != self.nodestatus_filter):
+                    return False
+        return True
+
 class SliceWidget(QWidget):
     def __init__(self, parent):
         QWidget.__init__(self, parent)
@@ -210,6 +294,9 @@ class SliceWidget(QWidget):
         self.slicename = QLabel("", self)
         self.updateSliceName()
         self.slicename.setScaledContents(False)
+        filterlabel = QLabel ("Filter: ", self)
+        filterbox = QComboBox(self)
+        filterbox.addItems(["all", "boot", "disabled", "reinstall", "safeboot"])
         searchlabel = QLabel ("Search: ", self)
         searchlabel.setScaledContents(False)
         searchbox = QLineEdit(self)
@@ -218,22 +305,28 @@ class SliceWidget(QWidget):
         toplayout = QHBoxLayout()
         toplayout.addWidget(self.slicename, 0, Qt.AlignLeft)
         toplayout.addStretch()
+        toplayout.addWidget(filterlabel, 0, Qt.AlignRight)
+        toplayout.addWidget(filterbox, 0, Qt.AlignRight)
         toplayout.addWidget(searchlabel, 0, Qt.AlignRight)
         toplayout.addWidget(searchbox, 0, Qt.AlignRight)
 
         self.nodeView = NodeView(self)
-        self.nodeModel = QStandardItemModel(0, 2, self)
-        self.filterModel = QSortFilterProxyModel(self) # enable filtering
+        self.nodeModel = QStandardItemModel(0, 4, self)
+        self.filterModel = NodeFilterProxyModel(self)
 
         self.nodeNameDelegate = NodeNameDelegate(self)
+        self.nodeStatusDelegate = NodeStatusDelegate(self)
 
-        refresh = QPushButton("Update Slice Data", self)
+        refresh = QPushButton("Refresh Slice Data", self)
         refresh.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
+        renew = QPushButton("Renew Slice", self)
+        renew.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
         submit = QPushButton("Submit", self)
         submit.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
 
         bottomlayout = QHBoxLayout()
         bottomlayout.addWidget(refresh, 0, Qt.AlignLeft)
+        bottomlayout.addWidget(renew, 0, Qt.AlignLeft)
         bottomlayout.addStretch()
         bottomlayout.addWidget(submit, 0, Qt.AlignRight)
 
@@ -245,8 +338,10 @@ class SliceWidget(QWidget):
         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
 
         self.connect(refresh, SIGNAL('clicked()'), self.refresh)
+        self.connect(renew, SIGNAL('clicked()'), self.renew)
         self.connect(submit, SIGNAL('clicked()'), self.submit)
-        self.connect(searchbox, SIGNAL('textChanged(QString)'), self.filter)
+        self.connect(searchbox, SIGNAL('textChanged(QString)'), self.search)
+        self.connect(filterbox, SIGNAL('currentIndexChanged(QString)'), self.filter)
         self.connect(self.nodeView, SIGNAL('hostnameClicked(QString)'),
                      self.nodeSelectionChanged)
 
@@ -257,7 +352,7 @@ class SliceWidget(QWidget):
         QTimer.singleShot(1000, self.refresh)
 
     def refreshFinished(self):
-        self.setStatus("<font color='green'>Slice data updated.</font>", timeout=5000)
+        self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
         self.updateView()
         self.parent().signalAll("rspecUpdated")
 
@@ -265,7 +360,7 @@ class SliceWidget(QWidget):
         rspec_file = config.getSliceRSpecFile()
         if os.path.exists(rspec_file):
             xml = open(rspec_file).read()
-            return RSpec(xml)
+            return parse_rspec(xml)
         return None
 
     def setStatus(self, msg, timeout=None):
@@ -277,14 +372,11 @@ class SliceWidget(QWidget):
             return True
         return False
 
+    def search(self, search_string):
+        self.filterModel.setHostNameFilter(str(search_string))
+
     def filter(self, filter_string):
-        # for hierarchical models QSortFilterProxyModel applies the
-        # sort recursively. if the parent doesn't match the criteria
-        # we won't be able to match the children. so we need to match
-        # parent (by matching the network_names)
-        networks = ["^%s$" % n for n in self.network_names]
-        filters = networks + [str(filter_string)]
-        self.filterModel.setFilterRegExp(QRegExp('|'.join(filters)))
+        self.filterModel.setNodeStatusFilter(str(filter_string))
 
     def itemStatus(self, item):
         statusItem = item.parent().child(item.row(), 1)
@@ -306,11 +398,11 @@ class SliceWidget(QWidget):
             status = self.itemStatus(item)
             if status == node_status['add']:
                 print "Add hostname: %s" % hostname
-                rspec.add_sliver(hostname, testbed)
+                rspec.add_slivers(str(hostname), testbed)
                 change = True
             elif status == node_status['remove']:
                 print "Remove hostname: %s" % hostname
-                rspec.remove_sliver(hostname, testbed)
+                rspec.remove_slivers(str(hostname), testbed)
                 change = True
         elif depth == 3: # Tag
             tag, value = self.itemText(item).split(": ")
@@ -357,7 +449,22 @@ class SliceWidget(QWidget):
 
         self.process.applyRSpec(rspec)
         self.setStatus("Sending slice data (RSpec). This will take some time...")
-        
+
+    def renew(self):
+        dlg = RenewWindow(parent=self)
+        if (dlg.exec_() == QDialog.Accepted):
+            self.setStatus("Renewing Slice.")
+
+            self.renewProcess = SfiRenewer(config.getSlice(), dlg.get_new_expiration(), self)
+            self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
+
+    def renewFinished(self):
+        if self.renewProcess.statusMsg:
+            self.setStatus("Renew " + self.renewProcess.status + ": " + self.renewProcess.statusMsg)
+        else:
+            self.setStatus("Renew " + self.renewProcess.status)
+        self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
+        self.renewProcess = None
 
     def refresh(self):
         if not config.getSlice():
@@ -372,7 +479,7 @@ class SliceWidget(QWidget):
         self.connect(self.process, SIGNAL('finished()'), self.refreshFinished)
 
         self.process.getRSpecFromSM()
-        self.setStatus("Updating slice data. This will take some time...")
+        self.setStatus("Refreshing slice data. This will take some time...")
 
     def updateView(self):
         global already_in_nodes
@@ -385,15 +492,20 @@ class SliceWidget(QWidget):
             return None
 
         rootItem = self.nodeModel.invisibleRootItem()
-        networks = rspec.get_network_list()
+        #networks = sorted(rspec.get_network_list())
+        networks = rspec.get_networks()
         for network in networks:
             self.network_names.append(network)
-            networkItem = QStandardItem(QString(network))
-            rootItem.appendRow([networkItem, QStandardItem(QString(""))])
 
-            all_nodes = rspec.get_node_list(network)
-            sliver_nodes = rspec.get_sliver_list(network)
-            available_nodes = filter(lambda x:x not in sliver_nodes, all_nodes)
+            #all_nodes = rspec.get_node_list(network)
+            #sliver_nodes = rspec.get_sliver_list(network)
+            all_nodes = rspec.get_nodes(network)
+            sliver_nodes = rspec.get_nodes_with_slivers(network)
+            available_nodes = [ node for node in all_nodes if node not in sliver_nodes ]
+
+            networkItem = QStandardItem(QString(network))
+            msg = "%s Nodes\t%s Selected" % (len(all_nodes), len(sliver_nodes))
+            rootItem.appendRow([networkItem, QStandardItem(QString("")), QStandardItem(QString(msg)), QStandardItem(QString("network"))])
 
             already_in_nodes += sliver_nodes
 
@@ -406,34 +518,39 @@ class SliceWidget(QWidget):
                     tagstring = QString("%s: %s" % (name, value))
                     tagItem = QStandardItem(tagstring)
                     status = QStandardItem(QString(tag_status['in']))
-                    nodeItem.appendRow([tagItem, status])
+                    nodeStatus = QStandardItem(QString(""))
+                    nodeItem.appendRow([tagItem, nodeStatus, status, QStandardItem(QString("attribute"))])
 
             for node in sliver_nodes:
                 nodeItem = QStandardItem(QString(node))
                 statusItem = QStandardItem(QString(node_status['in']))
-                networkItem.appendRow([nodeItem, statusItem])
+                nodeStatus = QStandardItem(QString(rspec.get_node_element(node, network).attrib.get("boot_state","")))
+                networkItem.appendRow([nodeItem, nodeStatus, statusItem, QStandardItem(QString("node"))])
 
                 attrs = rspec.get_sliver_attributes(node, network)
                 for (name, value) in attrs:
                     tagstring = QString("%s: %s" % (name, value))
                     tagItem = QStandardItem(tagstring)
                     statusItem = QStandardItem(QString(tag_status['in']))
-                    nodeItem.appendRow([tagItem, statusItem])
+                    nodeStatus = QStandardItem(QString(""))
+                    nodeItem.appendRow([tagItem, nodeStatus, statusItem, QStandardItem(QString("attribute"))])
 
             for node in available_nodes:
                 nodeItem = QStandardItem(QString(node))
                 statusItem = QStandardItem(QString(node_status['out']))
-                networkItem.appendRow([nodeItem, statusItem])
+                nodeStatus = QStandardItem(QString(rspec.get_node_element(node, network).attrib.get("boot_state","")))
+                networkItem.appendRow([nodeItem, nodeStatus, statusItem, QStandardItem(QString("node"))])
 
         self.filterModel.setSourceModel(self.nodeModel)
-        self.filterModel.setFilterKeyColumn(-1)
         self.filterModel.setDynamicSortFilter(True)
 
-        headers = QStringList() << "Hostname or Tag" << "Status"
+        headers = QStringList() << "Hostname or Tag" << "Node Status" << "Membership Status" << "Kind"
         self.nodeModel.setHorizontalHeaderLabels(headers)
 
         self.nodeView.setItemDelegateForColumn(0, self.nodeNameDelegate)
+        self.nodeView.setItemDelegateForColumn(1, self.nodeStatusDelegate)
         self.nodeView.setModel(self.filterModel)
+        self.nodeView.hideColumn(KIND_COLUMN)
         self.nodeView.expandAll()
         self.nodeView.resizeColumnToContents(0)
         self.nodeView.collapseAll()
@@ -444,16 +561,54 @@ class SliceWidget(QWidget):
     def nodeSelectionChanged(self, hostname):
         self.parent().nodeSelectionChanged(hostname)
 
+class RenewWindow(QDialog):
+    def __init__(self, parent=None):
+        super(RenewWindow, self).__init__(parent)
+        self.setWindowTitle("Renew Slivers")
+
+        self.duration = QComboBox()
+
+        self.expirations = []
+
+        durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") )
+
+        now = datetime.datetime.utcnow()
+        for (weeks, desc) in durations:
+            exp = now + datetime.timedelta(days = weeks * 7)
+            desc = desc + " " + exp.strftime("%Y-%m-%d %H:%M:%S")
+            self.expirations.append(exp)
+            self.duration.addItem(desc)
+
+        self.duration.setCurrentIndex(0)
+
+        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
+        buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
+
+        layout = QVBoxLayout()
+        layout.addWidget(self.duration)
+        layout.addWidget(buttonBox)
+        self.setLayout(layout)
+
+        self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
+        self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
+
+    def accept(self):
+        QDialog.accept(self)
+
+    def get_new_expiration(self):
+        index = self.duration.currentIndex()
+        return self.expirations[index]
+
 class MainScreen(SfaScreen):
     def __init__(self, parent):
         SfaScreen.__init__(self, parent)
 
         slice = SliceWidget(self)
-        self.init(slice, "Main Window", "OneLab Federation GUI")
+        self.init(slice, "Nodes", "OneLab SFA crawler")
 
     def rspecUpdated(self):
         self.mainwin.rspecWindow.updateView()
-        
+
     def configurationChanged(self):
         self.widget.updateSliceName()
         self.widget.updateView()