forked from mehdiBezahaf/intent-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
176 lines (117 loc) · 3.81 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""This is the NILE parser"""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing_extensions import TypeGuard
from typing import List, Literal, Optional, Tuple, NamedTuple
from lark import Lark, Transformer
from lark.visitors import v_args
parser = Lark(
r"""
start: statement
DATE: /\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}(Z|\+\d{2}\:\d{2})/
IDENT: /[a-zA-Z][a-zA-Z0-9_\-]*/
STRING: /[^']+/
_string: "'" STRING "'"
_qident: "'" IDENT "'"
statement: "define" "intent" IDENT ":" _command+
_command: action
| target
| endpoints
| path
| period
| middlebox
action: "do" "action" "(" _qident ")"
target: "for" "target" "(" _qident ")"
endpoints: "from" _endpoint "to" _endpoint
_endpoint: "endpoint" "(" _qident ")"
path: "following" "path" "(" _qident ("," _qident)* ")"
period: "start" _time "to" _time
_time: "hour" "(" "'" DATE "'" ")"
middlebox: "add" _mboxname ("," _mboxname)* ("with" middlebox_qos)* middlebox_block*
_mboxname: "middlebox" "(" _qident ")"
middlebox_qos: /latency|throughput|jitter|loss/ "(" (_string | "'" "none" "'") ["," _string] ")"
middlebox_block: "block" "traffic" "(" _qident ")"
%import common.WS
%ignore WS
""",
parser="lalr",
)
class NileAction(NamedTuple):
name: str
class NileTarget(NamedTuple):
name: str
class NileEndpoints(NamedTuple):
src: str
dst: str
NileQOSType = Literal["latency", "throughput", "jitter", "loss"]
class NileQOS(NamedTuple):
type: NileQOSType
constraint: Optional[Tuple[str, str]]
class NileBlock(NamedTuple):
name: str
class NileMiddlebox(NamedTuple):
name: str
qos: List[NileQOS]
blocks: List[NileBlock]
class NilePath(NamedTuple):
path: List[str]
class NilePeriod(NamedTuple):
start: str # TODO: parse
end: str
# TODO: should we really just throw everything into a list
class NileIntent(NamedTuple):
name: str
actions: List[NileAction]
targets: List[NileTarget]
endpoints: List[NileEndpoints]
paths: List[NilePath]
periods: List[NilePeriod]
middleboxes: List[NileMiddlebox]
def ensure_middlebox_type(s: str) -> TypeGuard[NileQOSType]:
return s in ("latency", "throughput", "jitter", "loss")
class NileTransformer(Transformer):
def start(self, args):
return args[0]
def statement(self, args):
name = args[0]
stmt = NileIntent(name, [], [], [], [], [], [])
attrs = {
NileAction: stmt.actions,
NileTarget: stmt.targets,
NileEndpoints: stmt.endpoints,
NilePath: stmt.paths,
NilePeriod: stmt.periods,
NileMiddlebox: stmt.middleboxes,
}
for command in args[1:]:
attrs[type(command)].append(command)
return stmt
DATE = str
IDENT = str
STRING = str
action = v_args(inline=True)(NileAction)
target = v_args(inline=True)(NileTarget)
endpoints = v_args(inline=True)(NileEndpoints)
path = NilePath
period = v_args(inline=True)(NilePeriod)
def middlebox_qos(self, args):
type_, *params = args
type_ = str(type_)
assert ensure_middlebox_type(type_)
constraint = (params[0], params[1]) if params else None
return NileQOS(type_, constraint)
middlebox_block = v_args(inline=True)(NileBlock)
def middlebox(self, args):
name, *rest = args
mbox = NileMiddlebox(name, [], [])
attrs = {
NileQOS: mbox.qos,
NileBlock: mbox.blocks,
}
for thing in rest:
attrs[type(thing)].append(thing)
return mbox
def parse(input: str) -> NileIntent:
r = parser.parse(input)
return NileTransformer().transform(r)