changes to work with latest rspec versions, try create at aggregates for protogeni...
[sface.git] / sface / screens / mainscreen.py
index c2a2118..985e315 100644 (file)
@@ -1,11 +1,11 @@
 
 import datetime
 import os
+import urlparse
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 
 #from sfa.util.rspecHelper import RSpec
-from sfa.rspecs.rspec_parser import parse_rspec
 from sface.config import config
 from sface.sfirenew import RenewWindow
 from sface.sfiprocess import SfiProcess
@@ -24,6 +24,10 @@ tag_status = { "in": "Already Set",
                 "add": "To be Added",
                 "remove": "To be Removed"}
 
+color_status = { "in": QColor.fromRgb(0, 250, 250),
+                 "add": QColor.fromRgb(0, 250, 0),
+                 "remove": QColor.fromRgb(250, 0, 0) }
+
 default_tags = "Default tags"
 settable_tags = ['delegations', 'initscript']
 
@@ -189,44 +193,20 @@ class NodeNameDelegate(QStyledItemDelegate):
             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.fromRgb(0, 250, 250))
-                painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(rect, 0, QString(data))
+            for x in node_status.keys():
+                if (node_status[x] == status_data) and (x in color_status):
+                    painter.fillPath(path, color_status[x])
 
-            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(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(rect, 0, QString(data))
-
-            else:
-                painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(rect, 0, QString(data))
+            painter.setPen(QColor.fromRgb(0, 0, 0))
+            painter.drawText(rect, 0, QString(data))
 
         else:
-            if status_data == tag_status['in']: # already in the slice
-                painter.fillPath(path, QColor.fromRgb(0, 250, 250))
-                painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(rect, 0, QString(data))
+            for x in tag_status.keys():
+                if (tag_status[x] == status_data) and (x in color_status):
+                    painter.fillPath(path, color_status[x])
 
-            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(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(rect, 0, QString(data))
-
-            else:
-                painter.setPen(QColor.fromRgb(0, 0, 0))
-                painter.drawText(rect, 0, QString(data))
+            painter.setPen(QColor.fromRgb(0, 0, 0))
+            painter.drawText(rect, 0, QString(data))
 
         painter.restore()
 
@@ -358,7 +338,7 @@ class SliceWidget(QWidget):
 
         self.connect(refresh, SIGNAL('clicked()'), self.refresh)
         self.connect(renew, SIGNAL('clicked()'), self.renew)
-        self.connect(submit, SIGNAL('clicked()'), self.submit)
+        self.connect(submit, SIGNAL('clicked()'), self.submit_pg_compat)
         self.connect(searchbox, SIGNAL('textChanged(QString)'), self.search)
         self.connect(filterbox, SIGNAL('currentIndexChanged(QString)'), self.filter)
         self.connect(self.nodeView, SIGNAL('hostnameClicked(QString)'),
@@ -375,21 +355,29 @@ class SliceWidget(QWidget):
         else:
             self.setStatus("<font color='red'>Slice submit failed: %s</font>" % (faultString))
 
-        # no need to do that anymore
-        # QTimer.singleShot(1000, self.refresh)
         self.updateView()
         self.parent().signalAll("rspecUpdated")
 
     def refreshResourcesFinished(self):
         self.disconnect(self.process, SIGNAL('finished()'), self.refreshResourcesFinished)
 
-        self.setStatus("Refreshing slice RSpec.")
-        self.connect(self.process, SIGNAL('finished()'), self.refreshRSpecFinished)
-        self.process.retrieveRspec()
+        faultString = self.process.getFaultString()
+        if not faultString:
+            self.setStatus("Refreshing slice RSpec.")
+            self.connect(self.process, SIGNAL('finished()'), self.refreshRSpecFinished)
+            self.process.retrieveRspec()
+        else:
+            self.setStatus("<font color='red'>Resources refresh failed: %s</font>" % (faultString))
 
     def refreshRSpecFinished(self):
         self.disconnect(self.process, SIGNAL('finished()'), self.refreshRSpecFinished)
-        self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
+
+        faultString = self.process.getFaultString()
+        if not faultString:
+            self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
+        else:
+            self.setStatus("<font color='red'>Slice refresh failed: %s</font>" % (faultString))
+
         self.updateView()
         self.parent().signalAll("rspecUpdated")
 
@@ -410,10 +398,10 @@ class SliceWidget(QWidget):
 
     def itemStatus(self, item):
         statusItem = item.parent().child(item.row(), MEMBERSHIP_STATUS_COLUMN)
-        return statusItem.data(Qt.DisplayRole).toString()
+        return str(statusItem.data(Qt.DisplayRole).toString())
 
     def itemText(self, item):
-        return item.data(Qt.DisplayRole).toString()
+        return str(item.data(Qt.DisplayRole).toString())
 
     # Recursively walk the tree, making changes to the RSpec
     def process_subtree(self, rspec, resources, item, depth = 0):
@@ -435,11 +423,11 @@ class SliceWidget(QWidget):
                     print "Error: Failed to find %s in resources rspec" % hostname
                 else:
                     rspec.merge_node(resource_node, testbed)
-                    rspec.add_slivers(str(hostname), testbed)
+                    rspec.add_slivers([{"hostname": str(hostname)}], testbed)
                     change = True
             elif status == node_status['remove']:
                 print "Remove hostname: %s" % hostname
-                rspec.remove_slivers(str(hostname), testbed)
+                rspec.remove_slivers([{"hostname": str(hostname)}], testbed)
                 change = True
         elif depth == 3: # Tag
             tag, value = self.itemText(item).split(": ")
@@ -450,14 +438,14 @@ class SliceWidget(QWidget):
             testbed = self.itemText(item.parent().parent())
             if status == tag_status['add']:
                 print "Add tag to (%s, %s): %s/%s " % (testbed, node, tag, value)
-                if node.startsWith(default_tags):
+                if node.startswith(default_tags):
                     rspec.add_default_sliver_attribute(tag, value, testbed)
                 else:
                     rspec.add_sliver_attribute(node, tag, value, testbed)
                 change = True
             elif status == tag_status['remove']:
                 print "Remove tag from (%s, %s): %s/%s " % (testbed, node, tag, value)
-                if node.startsWith(default_tags):
+                if node.startswith(default_tags):
                     rspec.remove_default_sliver_attribute(tag, value, testbed)
                 else:
                     rspec.remove_sliver_attribute(node, tag, value, testbed)
@@ -482,11 +470,91 @@ class SliceWidget(QWidget):
             self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
             return
 
-        self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
+        # Several aggregates have issues with the <statistics> section in the
+        # rspec, so make sure it's not there.
+        stats_elems = rspec.xml.xpath("//statistics")
+        if len(stats_elems)>0:
+            stats_elem = stats_elems[0]
+            parent = stats_elem.xpath("..")[0]
+            parent.remove(stats_elem)
 
+        self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
         self.process.applyRSpec(rspec)
         self.setStatus("Sending slice data (RSpec). This will take some time...")
 
+    # ProtoGENI-compatible submit. Contact each aggregate individually rather
+    # than using the slice manager.
+    # This code will be removed when ProtoGENI slicemanager is patched.
+    def submit_pg_compat(self):
+        if self.checkRunningProcess():
+            return
+
+        rspec = SfiData().getSliceRSpec()
+        resources = SfiData().getResourcesRSpec()
+        change = self.process_subtree(rspec, resources, self.nodeModel.invisibleRootItem())
+
+        if not change:
+            self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
+            return
+
+        # Several aggregates have issues with the <statistics> section in the
+        # rspec, so make sure it's not there.
+        stats_elems = rspec.xml.xpath("//statistics")
+        if len(stats_elems)>0:
+            stats_elem = stats_elems[0]
+            parent = stats_elem.xpath("..")[0]
+            parent.remove(stats_elem)
+
+        self.submit_aggSuccessCount = 0
+        self.submit_aggFailCount = 0
+        self.submit_rspec = rspec
+        self.connect(self.process, SIGNAL('finished()'), self.getVersionFinished)
+        self.process.getSliceMgrVersion()
+        self.setStatus("Getting aggregate directory...")
+
+    def getVersionFinished(self):
+        self.disconnect(self.process, SIGNAL('finished()'), self.getVersionFinished)
+
+        faultString = self.process.getFaultString()
+        if not faultString:
+            peers = SfiData().getSliceMgrVersion()["peers"]
+            self.submit_aggs = [(key, peers[key]) for key in peers.keys()]
+            str = "<font color='green'>Successfully retrieved agg list.</font> "
+            self.submitNextAgg(str)
+        else:
+            self.setStatus("<font color='red'>getSliceMgrVersion failed: %s</font>" % (faultString))
+
+    def submitNextAgg(self, statusStr=""):
+        if (self.submit_aggs == []):
+            self.setStatus(statusStr + "<font color='green'>Finished submitting. %d/%d aggs succeeded.</font>" %
+                           (self.submit_aggSuccessCount,self.submit_aggSuccessCount+self.submit_aggFailCount))
+            QTimer.singleShot(2500, self.refresh)
+            return
+
+        self.submit_agg = self.submit_aggs.pop()
+
+        urlParts = urlparse.urlsplit(self.submit_agg[1])
+        amPort = urlParts.port
+        amAddr = urlParts.hostname+urlParts.path
+
+        self.setStatus(statusStr + "Submitting to %s..." % (self.submit_agg[0]))
+
+        self.connect(self.process, SIGNAL('finished()'), self.submitNextAggFinished)
+        self.process.applyRSpec(self.submit_rspec, aggAddr = amAddr, aggPort = amPort, saveObtained=False)
+
+    def submitNextAggFinished(self):
+        self.disconnect(self.process, SIGNAL('finished()'), self.submitNextAggFinished)
+
+        faultString = self.process.getFaultString()
+        if not faultString:
+            self.submit_aggSuccessCount+=1
+            str = "<font color='green'>Succeeded on %s.</font> " % (self.submit_agg[0])
+        else:
+            self.submit_aggFailCount+=1
+            str = "<font color='red'>Failed on %s.</font> " % (self.submit_agg[0])  # , faultString)
+
+        self.submitNextAgg(str)
+
     def renew(self):
         dlg = RenewWindow(parent=self)
         dlg.exec_()