These files bridge sfatables with sfa. They let sfa load rules and apply them to...
authorSapan Bhatia <sapanb@cs.princeton.edu>
Thu, 10 Sep 2009 18:58:19 +0000 (18:58 +0000)
committerSapan Bhatia <sapanb@cs.princeton.edu>
Thu, 10 Sep 2009 18:58:19 +0000 (18:58 +0000)
sfatables/exec.py [new file with mode: 0644]
sfatables/matches/hrn.xsl
sfatables/xmlrule.py [new file with mode: 0644]

diff --git a/sfatables/exec.py b/sfatables/exec.py
new file mode 100644 (file)
index 0000000..fcff3aa
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+
+import sys
+import os
+import pdb
+import libxml2
+
+from optparse import OptionParser
+from sfatables import commands, matches, targets
+from sfatables.xmlextension import Xmlextension
+from sfatables.globals import *
+from sfatables.commands.List import *
+from sfatables.xmlrule import *
+
+class SFATablesRules:
+    sorted_rule_list = None
+
+    def __init__(self, chain_name):
+        chain_dir_path = "%s/%s"%(sfatables_config,chain_name)
+        rule_list = List().get_rule_list(chain_dir_path)
+        for rule_number in rule_list:
+            self.sorted_rule_list.append(XMLRule(rule_number))
+        return
+
+    def apply(self, rspec):
+        intermediate_rspec = rspec
+        for rule in self.sorted_rule_list:
+            intermediate_rspec  = rule.apply(intermediate_rspec)
+
+        return intermediate_rspec
index 3781b43..29988a5 100644 (file)
@@ -1,22 +1,17 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
 <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-    <xsl:template match="//rspec-match">
-        <xsl:variable name="result">
-            <xsl:for-each select="//rspec-match/context-input//user">
-                <xsl:variable name="context-hrn" select="hrn"/>
-                <xsl:for-each select="//rspec-match/rule-input//user">
+    <xsl:variable name="context-hrn" select="hrn"/>
+    <xsl:template match="user">
                     <xsl:choose>
                     <xsl:when test="starts-with($context-hrn, hrn)">
-                        True
+                        True <!--Match -->
                     </xsl:when>
                     <xsl:otherwise>
-                        False
+                        False <!-- No match -->
                     </xsl:otherwise>
                 </xsl:choose>
-                </xsl:for-each>
-            </xsl:for-each>
-        </xsl:variable>
         <xsl:value-of select="$result"/>
     </xsl:template>
+
 </xsl:stylesheet>
diff --git a/sfatables/xmlrule.py b/sfatables/xmlrule.py
new file mode 100644 (file)
index 0000000..bce804a
--- /dev/null
@@ -0,0 +1,51 @@
+import libxml2
+from sfatables.globals import *
+
+class Xmlrule:
+    rule_number = None
+    chain = None
+    xmldoc = None
+
+    arguments = {'match':None,'target':None}
+    processors = {'match':None,'target':None}
+    context = {'match':None,'target':None}
+
+    def load_xml_extension (self, type, chain, rule_number):
+        filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s.xml"%(rule_number,type)
+
+        self.xmldoc = libxml2.parseFile(filename)
+        p = self.xmldoc.xpathNewContext()
+
+        context = p.xpathEval('//context/@select')
+        self.context[type] = context[0].content
+
+        processor = p.xpathEval('//processor/@filename')
+
+        self.processor[type] = processor[0].content
+        self.arguments[type] = p.xpathEval('//rule')
+
+        p.xpathFreeContext()
+
+        return 
+
+    def wrap_rspec (self, type, rspec):
+        argument = self.arguments[type]
+        p = rspec.xmldoc.xpathNewContext()
+        root_node = p.xpathEval('/RSpec')
+        if (!root_node or !len(root_node)):
+            raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
+
+        root_node.addChild(arguments[type])
+        return rspec
+
+    def __init__(self, chain, rule_number):
+
+        self.load_xml_extension('match', chain, rule_number)
+        self.load_xml_extension('target',chain, rule_number)
+        self.rule_number = rule_number
+        self.chain = chain
+
+        return
+        
+    def free(self):
+        self.xmldoc.freeDoc()