OSDN Git Service

378ece11e3240e907bd1b84730ca064390d32fc6
[ftg/ftg.git] / ftg / parser / base.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 import re
6 from ftg.utils.common import CommonUtils
7 from ftg.flow_table import FlowTable
8 from ftg.flow_priority import FlowPriority
9 from ftg.flow_entry import FlowEntry
10
11
12 class BaseParser():
13     DEFAULT_PRIORITY = "32768"
14
15     def __init__(self, util, dump_file):
16         self.util = util
17         self.dump_file = dump_file
18         self.tables = {}
19
20     def regist_flow(self, table_no, priority_no, match_exp, instructions, counter=""):
21         exist_table = True
22         exist_priority = True
23
24         if not table_no in self.tables:
25             exist_table = False
26             exist_priority = False
27         elif not priority_no in self.tables[table_no].get_priorities():
28             exist_priority = False
29
30         if exist_table and exist_priority:
31             entry = FlowEntry(match_exp, instructions)
32             self.tables[table_no].get_priority(
33                 priority_no).add_flow_entries(entry)
34         elif exist_table and exist_priority == False:
35             priority = FlowPriority(priority_no)
36             entry = FlowEntry(match_exp, instructions)
37             priority.add_flow_entries(entry)
38             self.tables[table_no].add_priorities({str(priority_no): priority})
39         else:
40             entry = FlowEntry(match_exp, instructions)
41             priority = FlowPriority(priority_no)
42             table = FlowTable(table_no)
43             priority.add_flow_entries(entry)
44             table.add_priorities({str(priority_no): priority})
45             self.tables.update({str(table_no): table})
46
47     def get_tables(self):
48         return self.tables
49
50     def _parse_actions_with_bracket(self, instruction, actions):
51         if self.util.d_push(re.match(r'(.*),(.*?\(.*?\))(.*)', instruction)) is not None:
52             match = self.util.d_pop()
53             if match.group(1).find("(") != -1:
54                 self._parse_actions_with_bracket(match.group(1), actions)
55             else:
56                 actions.extend(self._split_action(match.group(1)))
57
58             actions.append(match.group(2))
59             if match.group(3).strip() != "":
60                 actions.extend(self._split_action(match.group(3)))
61         elif self.util.d_push(re.match(r'(?<!,)(.*?\(.*?\))(.*)', instruction)) is not None:
62             match = self.util.d_pop()
63             if match.group(1).strip() != "":
64                 actions.append(match.group(1))
65
66             if match.group(2).strip() != "":
67                 if match.group(2).find("(") != -1:
68                     self._parse_actions_with_bracket(match.group(2), actions)
69                 else:
70                     actions.extend(self._split_action(match.group(2)))
71         else:
72             actions.extend(self._split_action(instruction))
73
74         return actions
75
76     def _split_action(self, action):
77         action = re.sub(r'^,', '', action)
78         actions = action.split(',')
79         return actions