8 from optparse import OptionParser
9 from sfatables import commands
10 from sfatables.globals import *
11 from sfatables.commands.List import *
12 from sfatables.xmlrule import *
15 def __init__(self, chain_name):
16 self.active_context = {}
17 self.contexts = None # placeholder for rspec_manger
18 self.sorted_rule_list = []
19 chain_dir_path = os.path.join(sfatables_config,chain_name)
20 rule_list = List().get_rule_list(chain_dir_path)
21 for rule_number in rule_list:
22 self.sorted_rule_list = self.sorted_rule_list + [XMLRule(chain_name, rule_number)]
26 def set_context(self, request_context):
27 self.active_context = request_context
30 def add_request_context_to_rspec(self,doc):
31 p = doc.xpathNewContext()
32 context = p.xpathEval("//rspec")
34 raise Exception('Request is not an rspec')
36 # Add the request context
37 ruleNode = libxml2.newNode('rule-context')
38 ac = self.active_context
40 argumentNode = libxml2.newNode('argument')
41 nameNode = libxml2.newNode('name')
42 nameNode.addContent(k)
43 valueNode = libxml2.newNode('value')
44 valueNode.addContent(ac[k])
45 argumentNode.addChild(nameNode)
46 argumentNode.addChild(valueNode)
47 ruleNode.addChild(argumentNode)
48 context[0].addChild(ruleNode)
55 def apply(self, rspec):
56 doc = libxml2.parseDoc(rspec)
57 doc = self.add_request_context_to_rspec(doc)
59 intermediate_rspec = doc
61 for rule in self.sorted_rule_list:
62 intermediate_rspec = rule.apply_interpreted(intermediate_rspec)
66 final_rspec = XMLRule().wrap_up(intermediate_rspec)
70 incoming = SFATablesRules('INCOMING')
72 outgoing = SFATablesRules('OUTGOING')
73 print "%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list)
74 print incoming.sorted_rule_list[0].processors
76 print "%d rules loaded for OUTGOING chain"%len(outgoing.sorted_rule_list)
77 print outgoing.sorted_rule_list[0].processors
79 rspec = open(sys.argv[1]).read()
80 newrspec = incoming.apply(rspec)
84 if __name__=="__main__":