-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowParser.py
More file actions
175 lines (147 loc) · 6.15 KB
/
flowParser.py
File metadata and controls
175 lines (147 loc) · 6.15 KB
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
import re
import json
import xml.etree.ElementTree as ET
import xml.dom.minidom
from constants import (
FLOW_INVENTORY_NAMESPACE,
)
def flowParser(flow_ovs, flow_id, **kwargs):
"""
Parse an OVS flow into an ODL-XML format flow, until now
supported flows look like this :
ovs-flow :: <match 1>,<match 2> ... ,actions=<action 1>,<action 2> ...
|--------matches--------||------------actions-------------|
so we can split the command from 'actions=' and get the comma-seperated
list of matches and actions, now supported matches are:
--> table, in_port, eth_type, nw_src, nw_dst, dl_src, dl_dst, mpls_bos,
mpls_label, mpls_tc
list of actions supported :
--> output, goto_table, push_mpls, pop_mpls, set_field
"""
flow_ovs = flow_ovs.replace(" ","")
if ',actions=' in flow_ovs:
ls = flow_ovs.split(',actions=')
actions = ls[1].split(',')
matches = ls[0].split(',')
flow = ET.Element('flow', xmlns=FLOW_INVENTORY_NAMESPACE)
ET.SubElement(flow, "id").text = str(flow_id)
# for element in matches
for i in range(len(matches)):
element = matches[i]
ls = element.split('=')
(key, value) = (ls[0], ls[1])
if key == "table":
ET.SubElement(flow, "table_id").text = value
if key == "priority":
ET.SubElement(flow, "priority").text = value
elif key == "in_port":
if "match" not in locals():
match = ET.SubElement(flow, "match")
ET.SubElement(match, "in-port").text = value
elif key == "nw_src":
if "match" not in locals():
match = ET.SubElement(flow, "match")
ET.SubElement(match, "ipv4-source").text = value
elif key == "nw_dst":
if "match" not in locals():
match = ET.SubElement(flow, "match")
ET.SubElement(match, "ipv4-destination").text = value
elif key == "dl_src":
if "match" not in locals():
match = ET.SubElement(flow, "match")
if "ethernet_match" not in locals():
ethernet_match = ET.SubElement(match, "ethernet-match")
if "ethernet_source" not in locals():
ethernet_source = ET.SubElement(ethernet_match, "ethernet-source")
ET.SubElement(ethernet_source, "address").text = value
elif key == "dl_dst":
if "match" not in locals():
match = ET.SubElement(flow, "match")
if "ethernet_match" not in locals():
ethernet_match = ET.SubElement(match, "ethernet-match")
if "ethernet_destination" not in locals():
ethernet_destination = ET.SubElement(ethernet_match, "ethernet-destination")
ET.SubElement(ethernet_destination, "address").text = value
elif key == "eth_type":
if "match" not in locals():
match = ET.SubElement(flow, "match")
if "ethernet_match" not in locals():
ethernet_match = ET.SubElement(match, "ethernet-match")
if "ethernet_type" not in locals():
ethernet_type = ET.SubElement(ethernet_match, "ethernet-type")
ET.SubElement(ethernet_type, "type").text = value
elif key == "mpls_bos":
if "match" not in locals():
match = ET.SubElement(flow, "match")
if "protocol_match_fields" not in locals():
protocol_match_fields = ET.SubElement(match, "protocol-match-fields")
ET.SubElement(protocol_match_fields, "mpls-bos").text = value
elif key == "mpls_tc":
if "match" not in locals():
match = ET.SubElement(flow, "match")
if "protocol_match_fields" not in locals():
protocol_match_fields = ET.SubElement(match, "protocol-match-fields")
ET.SubElement(protocol_match_fields, "mpls-tc").text = value
elif key == "mpls_label":
if "match" not in locals():
match = ET.SubElement(flow, "match")
if "protocol_match_fields" not in locals():
protocol_match_fields = ET.SubElement(match, "protocol-match-fields")
ET.SubElement(protocol_match_fields, "mpls-label").text = value
# print("*"*10 + "check" + "*"*10)
# print(actions)
# for element in actions:
for i in range(len(actions)):
element = actions[i]
ls = element.split(":")
(key, value) = (ls[0], ls[1])
if "instructions" not in locals():
instructions = ET.SubElement(flow, "instructions")
instruction = ET.SubElement(instructions, "instruction")
ET.SubElement(instruction, "order").text = str(0)
apply_actions = ET.SubElement(instruction, "apply-actions")
action = ET.SubElement(apply_actions, "action")
ET.SubElement(action, "order").text = str(i)
if key == "output":
output_action = ET.SubElement(action, "output-action")
ET.SubElement(output_action, "output-node-connector").text = value
if key == "push_mpls":
push_mpls_action = ET.SubElement(action, "push-mpls-action")
ET.SubElement(push_mpls_action, "ethernet-type").text = str(34887)
if key == "pop_mpls":
pop_mpls_action = ET.SubElement(action, "pop-mpls-action")
ET.SubElement(pop_mpls_action, "ethernet-type").text = str(2048)
if key == "set_field":
set_field = ET.SubElement(action, "set-field")
le = value.split("->")
(field_name, field_value) = (le[1], le[0])
if field_name == "mpls_label":
protocol_match_fields = ET.SubElement(set_field, "protocol-match-fields")
ET.SubElement(protocol_match_fields, "mpls-label").text = field_value
elif field_name == "nw_dst":
ET.SubElement("ipv4-destination").text = field_value
elif field_name == "nw_src":
ET.SubElement("ipv4-source").text = field_value
elif field_name == "dl_dst":
ethernet_match = ET.SubElement(set_field, "ethernet-match")
ethernet_destination = ET.SubElement(ethernet_match, "ethernet-destination")
ET.SubElement(ethernet_destination, "address").text = field_value
elif field_name == "dl_src":
ethernet_match = ET.SubElement(set_field, "ethernet-match")
ethernet_source = ET.SubElement(ethernet_match, "ethernet-source")
ET.SubElement(ethernet_source, "address").text = field_value
for (key, value) in kwargs.items():
fixed_key = str(key).replace("_", "-")
ET.SubElement(flow, fixed_key).text = str(value)
data = ET.tostring(flow).decode("utf-8")
return data
# file = open("test_parser.xml", "w")
# file.write(data)
# file.close()
if __name__ == '__main__':
flow = input()
flow_id = input()
data = flowParser(flow, flow_id, barrier="false", idle_timeout=100)
file = open("test_parser.xml", "w")
file.write(data)
file.close()