Continuation - adding request and rule contexts to rspecs currently being processed.
[sfa.git] / sfatables / runtime.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import pdb
6 import libxml2
7
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 *
13
14 class SFATablesRules:
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)]
23         return
24
25
26     def set_context(self, request_context):
27         self.active_context = request_context
28         return
29
30     def create_xml_node(self, name, context_dict):
31         node = libxml2.newNode(name)
32         for k in context_dict.keys():
33             if (type(context_dict[k])==dict):
34                 childNode = self.create_xml_node(k, context_dict[k])
35                 node.addChild(childNode)
36             else:
37                 node.addContent(context_dict[k])
38         return node
39                 
40     def add_request_context_to_rspec(self, doc):
41         p = doc.xpathNewContext()
42         context = p.xpathEval("//rspec")
43         if (not context):
44             raise Exception('Request is not an rspec')
45         else:
46             # Add the request context
47             requestNode = self.create_xml_node('request-context',self.active_context)
48             context[0].addChild(requestNode)
49         p.xpathFreeContext()
50         return doc
51
52     def add_rule_context_to_rspec(self,arguments, doc):
53         p = doc.xpathNewContext()
54         context = p.xpathEval("//rspec")
55         if (not context):
56             raise Exception('Request is not an rspec')
57         else:
58             # Add the request context
59             ruleNode = libxml2.newNode('rule-context')
60             ac = self.active_context
61             for k in ac:
62                 argumentNode = libxml2.newNode('argument')
63                 nameNode = libxml2.newNode('name')
64                 nameNode.addContent(k)
65                 valueNode = libxml2.newNode('value')
66                 valueNode.addContent(ac[k])
67                 argumentNode.addChild(nameNode)
68                 argumentNode.addChild(valueNode)
69                 ruleNode.addChild(argumentNode)
70                 context[0].addChild(ruleNode)
71         p.xpathFreeContext()
72
73         return doc
74
75     def apply(self, rspec):
76         doc = libxml2.parseDoc(rspec)
77         doc = self.add_request_context_to_rspec(doc)
78
79         intermediate_rspec = doc
80
81         for rule in self.sorted_rule_list:
82             intermediate_rspec  = rule.apply_interpreted(intermediate_rspec)
83             if (rule.terminal):
84                 break
85
86         final_rspec = XMLRule().wrap_up(intermediate_rspec) 
87         return final_rspec
88
89 def main():
90     incoming = SFATablesRules('INCOMING')
91     incoming.set_context({'sfa':{'user':{'hrn':'plc.princeton.sapanb'}}})
92
93     outgoing = SFATablesRules('OUTGOING')
94     print "%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list)
95     print incoming.sorted_rule_list[0].processors
96
97     print "%d rules loaded for OUTGOING chain"%len(outgoing.sorted_rule_list)
98     print outgoing.sorted_rule_list[0].processors
99
100     rspec = open(sys.argv[1]).read()
101     newrspec = incoming.apply(rspec)
102     print newrspec
103     return
104
105 if __name__=="__main__":
106     main()