Commit 260d2dfc authored by David Hancock's avatar David Hancock

Implement RuleTranslationGuide

Now, rule translation is held in memory associated with each
virtual device.  The old way parsed the whole .json file
every time just to filter and return the command templates relevant
to a (table, action) pair.
parent 5dbd2608
......@@ -3,19 +3,6 @@
import argparse
import re
parser = argparse.ArgumentParser(description='HP4 Annotated Commands Converter')
parser.add_argument('--input', help='Annotated hp4 commands file',
type=str, action="store", required=True)
parser.add_argument('--output', help='Where to write hp4-ready commands file',
type=str, action="store", required=True)
parser.add_argument('--progID', help='Program ID',
type=str, action="store", default='1')
parser.add_argument('--phys_ports', help='Physical ports for which program applies',
type=str, nargs='*', action="store", default=['1'])
parser.add_argument('--virt_ports', help='Numbers to assign to virtual ports 0-3',
type=str, nargs='*', action="store", default=['65', '66', '67', '68'])
args = parser.parse_args()
print("FROM HP4L")
print(args)
......@@ -26,10 +13,6 @@ sr = {}
# TODO: read defines.p4 directly
sr['[program ID]'] = args.progID
sr['[VIRT_PORT_0]'] = args.virt_ports[0]
sr['[VIRT_PORT_1]'] = args.virt_ports[1]
sr['[VIRT_PORT_2]'] = args.virt_ports[2]
sr['[VIRT_PORT_3]'] = args.virt_ports[3]
sr['[PROCEED]'] = '0'
sr['[PARSE_SELECT_SEB]'] = '1'
sr['[PARSE_SELECT_20_29]'] = '2'
......@@ -116,3 +99,5 @@ for line in f_ac:
f_c.close()
f_ac.close()
from virtualdevice import VirtualDevice
import hp4compiler
from hp4translator import RuleTranslationGuide
class CompileError(Exception):
pass
......
......@@ -11,3 +11,38 @@ class Translator():
class RuleTranslationGuide():
def __init__(self, rtg_path):
# key method: ~/hp4-src/p4c-hp4/controller.py::DPMUServer::parse_json
self.templates_match = {}
self.templates_prims = {}
with open(rtg_path) as json_data:
d = json.load(json_data)
for hp4_command in d:
attributes = {}
attributes['src_table'] = hp4_command['source_table']
#src_action = hp4_command['source_action']
key = (src_table, src_action)
command_type = hp4_command['command']
attributes['table'] = hp4_command['table']
attributes['action'] = hp4_command['action']
attributes['mparams'] = hp4_command['match_params']
attributes['aparams'] = hp4_command['action_params']
if hp4_command['__class__'] == 'HP4_Match_Command':
#templates_match[key] = HP4_Match_Command(src_table, src_action,
# command, table, action,
# match_params, action_params)
self.templates_match[key] = P4Command(command_type, attributes)
elif hp4_command['__class__'] == 'HP4_Primitive_Command':
attributes['src_aparam_id'] = hp4_command['src_aparam_id']
if templates_prims.has_key(key) == False:
templates_prims[key] = []
self.templates_prims[key].append(P4Command(command_type, attributes)
else:
print("ERROR: Unrecognized class: %s" % hp4_command['__class__'])
self.templates = {}
for key in self.templates_match:
self.templates[key] = {'match': self.templates_match[key], 'primitives': []}
if self.templates_prims.has_key(key):
self.templates[key]['primitives'] = self.templates_prims[key]
#def get_templates(self, table, action):
# return self.templates[(table, action)]
......@@ -2,3 +2,11 @@ class P4Command():
def __init__(self, command_type, attributes):
self.command_type = command_type
self.attributes = attributes # {<attrib_name>: <attribute}
# standard attributes:
# table
# action
# mparams
# aparams
# handle
# src_table (for HP4 match commands)
# src_aparam_id (for HP4 primitive commands)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment