X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfatables%2Fruntime.py;h=1aac7731ef2ebe632bd196ccdf12a2390e33fb05;hb=642a3aa93b3974d2e55ba32dbe26957d0d37e20f;hp=0dab15d3869423ef16103f09d28879a459451db3;hpb=f7866302ad749a87e91516e1e37dbaec6f83c61c;p=sfa.git diff --git a/sfatables/runtime.py b/sfatables/runtime.py index 0dab15d3..1aac7731 100644 --- a/sfatables/runtime.py +++ b/sfatables/runtime.py @@ -6,69 +6,98 @@ import pdb import libxml2 from optparse import OptionParser -from sfatables import commands, matches, targets -from sfatables.xmlextension import Xmlextension +from sfatables import commands from sfatables.globals import * from sfatables.commands.List import * 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 = "%s/%s"%(sfatables_config,chain_name) + 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 create_xml_node(self, name, context_dict): + node = libxml2.newNode(name) + for k in context_dict.keys(): + if (type(context_dict[k])==dict): + childNode = self.create_xml_node(k, context_dict[k]) + node.addChild(childNode) + else: + node.addContent(context_dict[k]) + return node + + 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 + requestNode = self.create_xml_node('request-context',self.active_context) + context[0].addChild(requestNode) + p.xpathFreeContext() + return doc + + def add_rule_context_to_rspec(self,arguments, 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): + break - return intermediate_rspec + final_rspec = XMLRule().wrap_up(intermediate_rspec) + return final_rspec def main(): incoming = SFATablesRules('INCOMING') + incoming.set_context({'sfa':{'user':{'hrn':'plc.princeton.sapanb'}}}) + outgoing = SFATablesRules('OUTGOING') + print "%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list) + print incoming.sorted_rule_list[0].processors - rspec = """ - - - plc.princeton.sapan - - - - - hrn - plc - - - whitelist - plc.princeton - - - blacklist - plc.tp - - - - - - - - - - - - - """ - - - print "%d rules loaded for INCOMING chain\n"%len(incoming.sorted_rule_list) - print "%d rules loaded for OUTGOING chain\n"%len(outgoing.sorted_rule_list) + 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) print newrspec return