X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfatables%2Fruntime.py;h=bdace139db1c843a8a4d07a0dc2ec97d4139a171;hb=ac59ce7442c58f503368d55a0eefd3ea92fbcfd8;hp=235c67f2590e61e962c0d12e232a442e0418fba1;hpb=11857b3cae673afaf5c9cab28ea77814a5527642;p=sfa.git diff --git a/sfatables/runtime.py b/sfatables/runtime.py index 235c67f2..bdace139 100644 --- a/sfatables/runtime.py +++ b/sfatables/runtime.py @@ -13,20 +13,51 @@ from sfatables.xmlrule import * class SFATablesRules: def __init__(self, chain_name): + self.active_context = {} self.contexts = None # placeholder for rspec_manger self.sorted_rule_list = [] chain_dir_path = os.path.join(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(chain_name, rule_number)) + self.sorted_rule_list = self.sorted_rule_list + [XMLRule(chain_name, rule_number)] return def set_context(self, request_context): + self.active_context = request_context return + def add_request_context_to_rspec(self,doc): + p = doc.xpathNewContext() + context = p.xpathEval("//rspec") + if (not context): + raise Exception('Request is not an rspec') + else: + # Add the request context + ruleNode = libxml2.newNode('rule-context') + ac = self.active_context + for k in ac: + argumentNode = libxml2.newNode('argument') + nameNode = libxml2.newNode('name') + nameNode.addContent(k) + valueNode = libxml2.newNode('value') + valueNode.addContent(ac[k]) + argumentNode.addChild(nameNode) + argumentNode.addChild(valueNode) + ruleNode.addChild(argumentNode) + context[0].addChild(ruleNode) + p.xpathFreeContext() + + return doc + + + def apply(self, rspec): - intermediate_rspec = rspec + doc = libxml2.parseDoc(rspec) + doc = self.add_request_context_to_rspec(doc) + + intermediate_rspec = doc + for rule in self.sorted_rule_list: intermediate_rspec = rule.apply_interpreted(intermediate_rspec) if (rule.terminal): @@ -37,10 +68,13 @@ class SFATablesRules: def main(): incoming = SFATablesRules('INCOMING') - outgoing = SFATablesRules('OUTGOING') + outgoing = SFATablesRules('OUTGOING') print "%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list) + print incoming.sorted_rule_list[0].processors + print "%d rules loaded for OUTGOING chain"%len(outgoing.sorted_rule_list) + print outgoing.sorted_rule_list[0].processors rspec = open(sys.argv[1]).read() newrspec = incoming.apply(rspec)