initial checkin of helper sfatables runtime methods. Moved fetch_contect() and __run_...
authorTony Mack <tmack@cs.princeton.edu>
Tue, 3 Aug 2010 20:12:29 +0000 (20:12 +0000)
committerTony Mack <tmack@cs.princeton.edu>
Tue, 3 Aug 2010 20:12:29 +0000 (20:12 +0000)
sfa/util/sfatablesRuntime.py [new file with mode: 0644]

diff --git a/sfa/util/sfatablesRuntime.py b/sfa/util/sfatablesRuntime.py
new file mode 100644 (file)
index 0000000..14ec6f7
--- /dev/null
@@ -0,0 +1,38 @@
+from sfatables.runtime import SFATablesRules
+
+def fetch_context(slice_hrn, user_hrn, contexts):
+    """
+    Returns the request context required by sfatables. At some point, this
+    mechanism should be changed to refer to "contexts", which is the
+    information that sfatables is requesting. But for now, we just return
+    the basic information needed in a dict.
+    """
+    slice_hrn = urn_to_hrn(slice_xrn)[0]
+    user_hrn = urn_to_hrn(user_xrn)[0]
+    base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
+    return base_context
+
+def run_sfatables(chain, hrn, origin_hrn, rspec, context_callback = None ):
+    """
+    Run the rspec through sfatables
+    @param chain Name of rule chain
+    @param hrn Object's hrn
+    @param origin_hrn Original caller's hrn
+    @param rspec Incoming rspec
+    @param context_callback Callback used to generate the request context  
+
+    @return rspec
+    """
+    if not context_callback:
+        context_callback = fetch_context
+
+    chain = chain.upper()
+    rules = SFATablesRules(chain)
+    if rules.sorted_rule_list:
+        contexts = rules.contexts
+        request_context = context_callback(hrn, origin_hrn, contexts)
+        rules.set_context(request_context)
+        newrspec = rules.apply(rspec)
+    else:
+        newrspec = rspec
+    return newrspec