OSDN Git Service

add CSVParser, TSVParser, BaseParser.
[ftg/ftg.git] / ftg / parser / tsv.py
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2 # -*- coding: utf-8 -*-
3 #
4
5 import sys
6 import os
7 import re
8 from ftg.utils.common import CommonUtils
9 from ftg.flow_table import FlowTable
10 from ftg.flow_priority import FlowPriority
11 from ftg.flow_entry import FlowEntry
12 from ftg.parser.base import BaseParser
13
14
15 class TSVParser(BaseParser):
16
17     def parse(self):
18         self.util.debug_out(self.dump_file)
19         for dump_line in open(self.dump_file, 'r'):
20             dump_line = dump_line.rstrip()
21             instructions = []
22             if self.util.d_push(re.match(r'(\d+)-(\d+)\t(.*)\t(.*)', dump_line)) is not None:
23                 match = self.util.d_pop()
24                 table_no = match.group(1)
25                 priority_no = match.group(2)
26                 match_exp = match.group(3)
27                 instruction = match.group(4)
28                 instructions = self._parse_actions_with_bracket(
29                     instruction, instructions)
30                 self.util.debug_out(
31                     "[1] " + table_no + "-" + priority_no + " m:" + match_exp + " a:" + instruction)
32                 self.regist_flow(
33                     table_no, priority_no, match_exp, instructions)
34             elif self.util.d_push(re.match(r'(\d+)\t(.*)\t(.*)', dump_line)) is not None:
35                 match = self.util.d_pop()
36                 table_no = match.group(1)
37                 priority_no = "32768"
38                 match_exp = match.group(2)
39                 instruction = match.group(3)
40                 instructions = self._parse_actions_with_bracket(
41                     instruction, instructions)
42                 self.util.debug_out(
43                     "[3] " + table_no + "-" + priority_no + " m:" + match_exp + " a:" + instruction)
44                 self.regist_flow(
45                     table_no, priority_no, match_exp, instructions)
46             elif dump_line.strip() == "":
47                 continue
48             elif self.util.d_push(re.match(r'Table-Priority\tMatch\tInstruction', dump_line)) is not None:
49                 continue
50             else:
51                 if not self.util.d_push(re.match(r'.*NXST_FLOW reply.*', dump_line)):
52                     self.util.error_out("PARSE ERROR: " + dump_line)
53