blob: 78b48b5005668d603bf6e0abcd3e677f880fb7bb [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Convert loxi objects to openflow_13 messages and back.
19"""
20from copy import copy
21
22from google.protobuf.descriptor import FieldDescriptor
23
24import loxi.of13 as of13
25from protobuf_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP
26from protos import openflow_13_pb2 as pb2
27
28
29type_callable_map = copy(TYPE_CALLABLE_MAP)
30type_callable_map.update({
31 FieldDescriptor.TYPE_STRING: str
32})
33
Gamze Abaka53cc0a22019-01-31 12:06:11 +000034
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070035def pb2dict(pb):
36 """
37 Convert protobuf to a dict of values good for instantiating
38 loxi objects (or any other objects). We specialize the protobuf_to_dict
39 library call with our modified decoders.
40 :param pb: protobuf as loaded into Python
41 :return: dict of values
42 """
43 return protobuf_to_dict(pb, type_callable_map)
44
Gamze Abaka53cc0a22019-01-31 12:06:11 +000045
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070046def to_loxi(grpc_object):
47 cls = grpc_object.__class__
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080048 converter = to_loxi_converters[cls.__name__]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070049 return converter(grpc_object)
50
Gamze Abaka53cc0a22019-01-31 12:06:11 +000051
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070052def to_grpc(loxi_object):
53 cls = loxi_object.__class__
54 converter = to_grpc_converters[cls]
55 return converter(loxi_object)
56
Gamze Abaka53cc0a22019-01-31 12:06:11 +000057
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070058def ofp_port_to_loxi_port_desc(pb):
59 kw = pb2dict(pb)
60 return of13.common.port_desc(**kw)
61
Gamze Abaka53cc0a22019-01-31 12:06:11 +000062
Zsolt Haraszti217a12e2016-12-19 16:37:55 -080063def ofp_port_status_to_loxi_port_status(pb):
64 return of13.message.port_status(
65 reason=pb.reason,
66 desc=ofp_port_to_loxi_port_desc(pb.desc)
67 )
68
Gamze Abaka9d343292019-02-20 06:35:18 +000069def ofp_flow_removed_to_loxi_flow_removed(pb):
70 return of13.message.flow_removed(
71 cookie=pb.cookie,
72 priority=pb.priority,
73 reason=pb.reason,
74 table_id=pb.table_id,
75 match=make_loxi_match(pb2dict(pb.match))
76 )
Gamze Abaka53cc0a22019-01-31 12:06:11 +000077
Nicolas Palpacuerfd7b8b12018-06-15 13:58:06 -040078def ofp_port_stats_to_loxi_port_stats(pb):
79 kw = pb2dict(pb)
80 return of13.port_stats_entry(**kw)
81
Gamze Abaka53cc0a22019-01-31 12:06:11 +000082
Koray Kokten8592a232018-08-27 07:41:14 +000083def ofp_meter_stats_to_loxi_meter_stats(pb):
Gamze Abaka53cc0a22019-01-31 12:06:11 +000084 return of13.meter_stats(
85 meter_id=pb.meter_id,
86 flow_count=pb.flow_count,
87 packet_in_count=pb.packet_in_count,
88 byte_in_count=pb.byte_in_count,
89 duration_sec=pb.duration_sec,
90 duration_nsec=pb.duration_nsec,
91 band_stats=[to_loxi(band_stat) for band_stat in pb.band_stats])
92
93
94def ofp_meter_band_stats_to_loxi_meter_stats(pb):
95 return of13.meter_band_stats(
96 packet_band_count=pb.packet_band_count,
97 byte_band_count=pb.byte_band_count
98 )
99
Koray Kokten8592a232018-08-27 07:41:14 +0000100
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800101def make_loxi_field(oxm_field):
102 assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC
103 ofb_field = oxm_field['ofb_field']
104 field_type = ofb_field.get('type', 0)
105
106 if field_type == pb2.OFPXMT_OFB_ETH_TYPE:
107 return (
108 of13.oxm.eth_type(value=ofb_field['eth_type']))
109
110 elif field_type == pb2.OFPXMT_OFB_IN_PORT:
111 return (
112 of13.oxm.in_port(value=ofb_field['port']))
113
114 elif field_type == pb2.OFPXMT_OFB_IP_PROTO:
115 return (
116 of13.oxm.ip_proto(value=ofb_field['ip_proto']))
117
118 elif field_type == pb2.OFPXMT_OFB_VLAN_VID:
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000119 if ofb_field.get('has_mask', 0):
120 return of13.oxm.vlan_vid_masked(value=ofb_field['vlan_vid'],
121 value_mask=ofb_field['vlan_vid_mask'])
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800122 return (
123 of13.oxm.vlan_vid(value=ofb_field['vlan_vid']))
124
125 elif field_type == pb2.OFPXMT_OFB_VLAN_PCP:
126 return (
127 of13.oxm.vlan_pcp(value=ofb_field['vlan_pcp']))
128
129 elif field_type == pb2.OFPXMT_OFB_IPV4_SRC:
130 return (
131 of13.oxm.ipv4_src(value=ofb_field['ipv4_src']))
132
133 elif field_type == pb2.OFPXMT_OFB_IPV4_DST:
134 return (
135 of13.oxm.ipv4_dst(value=ofb_field['ipv4_dst']))
136
137 elif field_type == pb2.OFPXMT_OFB_UDP_SRC:
138 return (
139 of13.oxm.udp_src(value=ofb_field['udp_src']))
140
141 elif field_type == pb2.OFPXMT_OFB_UDP_DST:
142 return (
143 of13.oxm.udp_dst(value=ofb_field['udp_dst']))
144
145 elif field_type == pb2.OFPXMT_OFB_METADATA:
146 return (
147 of13.oxm.metadata(value=ofb_field['table_metadata']))
148
149 else:
150 raise NotImplementedError(
151 'OXM match field for type %s' % field_type)
152
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000153
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700154def make_loxi_match(match):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800155 assert match.get('type', pb2.OFPMT_STANDARD) == pb2.OFPMT_OXM
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700156 loxi_match_fields = []
Zsolt Haraszti66862032016-11-28 14:28:39 -0800157 for oxm_field in match.get('oxm_fields', []):
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800158 loxi_match_fields.append(make_loxi_field(oxm_field))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700159 return of13.match_v3(oxm_list=loxi_match_fields)
160
alshabibf4fb2682017-01-12 00:32:56 -0600161
162def make_loxi_action(a):
163 if type(a) is not dict:
164 a = pb2dict(a)
165
166 typ = a.get('type', 0)
167
168 if typ == pb2.OFPAT_OUTPUT:
169 output_kws = a['output']
170 return of13.action.output(**output_kws)
171
172 elif typ == pb2.OFPAT_POP_VLAN:
173 return of13.action.pop_vlan()
174
175 elif typ == pb2.OFPAT_PUSH_VLAN:
176 push_vlan_kws = a['push']
177 return of13.action.push_vlan(**push_vlan_kws)
178
179 elif typ == pb2.OFPAT_SET_FIELD:
180 loxi_field = make_loxi_field(a['set_field']['field'])
181 return of13.action.set_field(loxi_field)
182
183 elif typ == pb2.OFPAT_GROUP:
184 group_kws = a['group']
185 return of13.action.group(**group_kws)
186
187 else:
188 raise NotImplementedError(
189 'Action decoder for action OFPAT_* %d' % typ)
190
191
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700192def ofp_flow_stats_to_loxi_flow_stats(pb):
193 kw = pb2dict(pb)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700194
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700195 def make_loxi_instruction(inst):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700196 type = inst['type']
197 if type == pb2.OFPIT_APPLY_ACTIONS:
198 return of13.instruction.apply_actions(
199 actions=[make_loxi_action(a)
200 for a in inst['actions']['actions']])
Gamze Abaka61c2e982018-02-14 11:03:36 +0000201 elif type == pb2.OFPIT_CLEAR_ACTIONS:
202 return of13.instruction.clear_actions()
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800203 elif type == pb2.OFPIT_GOTO_TABLE:
204 return of13.instruction.goto_table(
205 table_id=inst['goto_table']['table_id'])
Koray Koktenefcdf522018-12-06 00:16:56 +0300206 elif type == pb2.OFPIT_WRITE_ACTIONS:
207 return of13.instruction.write_actions(
208 actions=[make_loxi_action(a)
209 for a in inst['actions']['actions']])
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000210 elif type == pb2.OFPIT_WRITE_METADATA:
Saurav Das45c53142019-06-07 11:36:55 -0700211 if 'metadata' in inst['write_metadata']:
212 return of13.instruction.write_metadata(
213 metadata=inst['write_metadata']['metadata'])
214 else:
215 return of13.instruction.write_metadata(0)
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000216 elif type == pb2.OFPIT_METER:
217 return of13.instruction.meter(
218 meter_id=inst['meter']['meter_id'])
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800219
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700220 else:
221 raise NotImplementedError('Instruction type %d' % type)
222
223 kw['match'] = make_loxi_match(kw['match'])
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000224 # if the flow action is drop, then the instruction is not found in the dict
225 if 'instructions' in kw:
226 kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']]
Zsolt Haraszti66862032016-11-28 14:28:39 -0800227 del kw['id']
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700228 return of13.flow_stats_entry(**kw)
229
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800230
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700231def ofp_packet_in_to_loxi_packet_in(pb):
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800232 packet_in = of13.message.packet_in(
233 buffer_id=pb.buffer_id,
234 reason=pb.reason,
235 table_id=pb.table_id,
236 cookie=pb.cookie,
237 match=make_loxi_match(pb2dict(pb.match)),
238 data=pb.data
239 )
240 return packet_in
241
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000242
alshabibf4fb2682017-01-12 00:32:56 -0600243def ofp_group_desc_to_loxi_group_desc(pb):
244 return of13.group_desc_stats_entry(
245 group_type=pb.type,
246 group_id=pb.group_id,
247 buckets=[to_loxi(bucket) for bucket in pb.buckets])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700248
alshabibf4fb2682017-01-12 00:32:56 -0600249
250def ofp_group_stats_to_loxi_group_stats(pb):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800251 return of13.group_stats_entry(
alshabibf4fb2682017-01-12 00:32:56 -0600252 group_id=pb.group_id,
253 ref_count=pb.ref_count,
254 packet_count=pb.packet_count,
255 byte_count=pb.byte_count,
256 duration_sec=pb.duration_sec,
257 duration_nsec=pb.duration_nsec,
258 bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats])
Zsolt Haraszti66862032016-11-28 14:28:39 -0800259
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800260
Zsolt Haraszti66862032016-11-28 14:28:39 -0800261def ofp_bucket_counter_to_loxy_bucket_counter(pb):
262 return of13.bucket_counter(
263 packet_count=pb.packet_count,
264 byte_count=pb.byte_count)
265
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700266
alshabibf4fb2682017-01-12 00:32:56 -0600267def ofp_bucket_to_loxi_bucket(pb):
268 return of13.bucket(
269 weight=pb.weight,
270 watch_port=pb.watch_port,
271 watch_group=pb.watch_group,
272 actions=[to_loxi(action) for action in pb.actions]
273 )
274
275
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700276to_loxi_converters = {
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800277 'ofp_port': ofp_port_to_loxi_port_desc,
278 'ofp_port_status': ofp_port_status_to_loxi_port_status,
Gamze Abaka9d343292019-02-20 06:35:18 +0000279 'ofp_flow_removed': ofp_flow_removed_to_loxi_flow_removed,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800280 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats,
281 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in,
alshabibf4fb2682017-01-12 00:32:56 -0600282 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats,
283 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc,
284 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter,
285 'ofp_bucket': ofp_bucket_to_loxi_bucket,
Nicolas Palpacuerfd7b8b12018-06-15 13:58:06 -0400286 'ofp_action': make_loxi_action,
Koray Kokten8592a232018-08-27 07:41:14 +0000287 'ofp_port_stats': ofp_port_stats_to_loxi_port_stats,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000288 'ofp_meter_stats': ofp_meter_stats_to_loxi_meter_stats,
289 'ofp_meter_band_stats': ofp_meter_band_stats_to_loxi_meter_stats
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700290}
291
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700292
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700293def loxi_flow_mod_to_ofp_flow_mod(lo):
294 return pb2.ofp_flow_mod(
295 cookie=lo.cookie,
296 cookie_mask=lo.cookie_mask,
297 table_id=lo.table_id,
298 command=lo._command,
299 idle_timeout=lo.idle_timeout,
300 hard_timeout=lo.hard_timeout,
301 priority=lo.priority,
302 buffer_id=lo.buffer_id,
303 out_port=lo.out_port,
304 out_group=lo.out_group,
305 flags=lo.flags,
306 match=to_grpc(lo.match),
307 instructions=[to_grpc(i) for i in lo.instructions])
308
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000309
Koray Kokten8592a232018-08-27 07:41:14 +0000310def loxi_meter_mod_to_ofp_meter_mod(lo):
311 return pb2.ofp_meter_mod(
312 command=lo.command,
313 flags=lo.flags,
314 meter_id=lo.meter_id,
315 bands=[to_grpc(i) for i in lo.meters])
316
317
318def loxi_meter_band_drop_to_ofp_meter_band_drop(lo):
319 return pb2.ofp_meter_band_header(
320 type=lo.type,
321 rate=lo.rate,
322 burst_size=lo.burst_size)
323
324
325def loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark(lo):
326 return pb2.ofp_meter_band_header(
327 type=lo.type,
328 rate=lo.rate,
329 burst_size=lo.burst_size,
330 dscp_remark=pb2.ofp_meter_band_dscp_remark(prec_level=lo.prec_level))
331
332
333def loxi_meter_band_experimenter_to_ofp_meter_band_experimenter(lo):
334 return pb2.ofp_meter_band_header(
335 type=lo.type,
336 rate=lo.rate,
337 burst_size=lo.burst_size,
338 experimenter=pb2.ofp_meter_band_experimenter(experimenter=lo.experimenter))
339
340
341def loxi_meter_multipart_request_to_ofp_meter_multipart_request(lo):
342 return pb2.ofp_meter_multipart_request(
343 meter_id=lo.meter_id)
344
345
346def loxi_meter_stats_to_ofp_meter_stats(lo):
347 return pb2.ofp_meter_stats(
348 meter_id=lo.meter_id,
349 flow_count=lo.flow_count,
350 packet_in_count =lo.packet_in_count,
351 byte_in_count=lo.byte_in_count,
352 duration_sec=lo.duration_sec,
353 duration_nsec=lo.duration_nsec,
354 band_stats=lo.band_stats)
355
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700356
357def loxi_group_mod_to_ofp_group_mod(lo):
358 return pb2.ofp_group_mod(
359 command=lo.command,
Zsolt Haraszti9125b1a2016-10-24 22:54:33 -0700360 type=lo.group_type,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700361 group_id=lo.group_id,
362 buckets=[to_grpc(b) for b in lo.buckets])
363
364
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700365def loxi_packet_out_to_ofp_packet_out(lo):
366 return pb2.ofp_packet_out(
367 buffer_id=lo.buffer_id,
368 in_port=lo.in_port,
369 actions=[to_grpc(a) for a in lo.actions],
370 data=lo.data)
371
372
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700373def loxi_match_v3_to_ofp_match(lo):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700374 return pb2.ofp_match(
375 type=pb2.OFPMT_OXM,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700376 oxm_fields=[to_grpc(f) for f in lo.oxm_list])
377
378
379def loxi_bucket_to_ofp_bucket(lo):
380 return pb2.ofp_bucket(
381 weight=lo.weight,
382 watch_port=lo.watch_port,
383 watch_group=lo.watch_group,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700384 actions=[to_grpc(a) for a in lo.actions])
385
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700386
387def loxi_oxm_eth_type_to_ofp_oxm(lo):
388 return pb2.ofp_oxm_field(
389 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
390 ofb_field=pb2.ofp_oxm_ofb_field(
391 type=pb2.OFPXMT_OFB_ETH_TYPE,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700392 eth_type=lo.value))
393
394
395def loxi_oxm_in_port_to_ofp_oxm(lo):
396 return pb2.ofp_oxm_field(
397 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
398 ofb_field=pb2.ofp_oxm_ofb_field(
399 type=pb2.OFPXMT_OFB_IN_PORT,
400 port=lo.value))
401
402
403def loxi_oxm_ip_proto_to_ofp_oxm(lo):
404 return pb2.ofp_oxm_field(
405 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
406 ofb_field=pb2.ofp_oxm_ofb_field(
407 type=pb2.OFPXMT_OFB_IP_PROTO,
408 ip_proto=lo.value))
409
410
411def loxi_oxm_vlan_vid_to_ofp_oxm(lo):
412 return pb2.ofp_oxm_field(
413 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
414 ofb_field=pb2.ofp_oxm_ofb_field(
415 type=pb2.OFPXMT_OFB_VLAN_VID,
416 vlan_vid=lo.value))
417
418
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000419def loxi_oxm_vlan_vid_masked_to_ofp_oxm(lo):
420 return pb2.ofp_oxm_field(
421 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
422 ofb_field=pb2.ofp_oxm_ofb_field(
423 type=pb2.OFPXMT_OFB_VLAN_VID,
424 has_mask=True,
425 vlan_vid=lo.value,
426 vlan_vid_mask=lo.value_mask))
427
428
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700429def loxi_oxm_vlan_pcp_to_ofp_oxm(lo):
430 return pb2.ofp_oxm_field(
431 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
432 ofb_field=pb2.ofp_oxm_ofb_field(
433 type=pb2.OFPXMT_OFB_VLAN_PCP,
434 vlan_pcp=lo.value))
435
436
437def loxi_oxm_ipv4_dst_to_ofp_oxm(lo):
438 return pb2.ofp_oxm_field(
439 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
440 ofb_field=pb2.ofp_oxm_ofb_field(
441 type=pb2.OFPXMT_OFB_IPV4_DST,
442 ipv4_dst=lo.value))
443
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700444
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800445def loxi_oxm_udp_dst_to_ofp_oxm(lo):
446 return pb2.ofp_oxm_field(
447 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
448 ofb_field=pb2.ofp_oxm_ofb_field(
449 type=pb2.OFPXMT_OFB_UDP_DST,
450 udp_dst=lo.value))
451
452
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800453def loxi_oxm_udp_src_to_ofp_oxm(lo):
454 return pb2.ofp_oxm_field(
455 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
456 ofb_field=pb2.ofp_oxm_ofb_field(
457 type=pb2.OFPXMT_OFB_UDP_SRC,
458 udp_src=lo.value))
459
460
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800461def loxi_oxm_metadata_to_ofp_oxm(lo):
462 return pb2.ofp_oxm_field(
463 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
464 ofb_field=pb2.ofp_oxm_ofb_field(
465 type=pb2.OFPXMT_OFB_METADATA,
466 table_metadata=lo.value))
467
468
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700469def loxi_apply_actions_to_ofp_instruction(lo):
470 return pb2.ofp_instruction(
471 type=pb2.OFPIT_APPLY_ACTIONS,
472 actions=pb2.ofp_instruction_actions(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700473 actions=[to_grpc(a) for a in lo.actions]))
474
Gamze Abaka61c2e982018-02-14 11:03:36 +0000475def loxi_clear_actions_to_ofp_instruction(lo):
476 return pb2.ofp_instruction(
477 type=pb2.OFPIT_CLEAR_ACTIONS)
478
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700479
480def loxi_goto_table_to_ofp_instruction(lo):
481 return pb2.ofp_instruction(
482 type=pb2.OFPIT_GOTO_TABLE,
483 goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id))
484
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700485
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000486def loxi_write_metadata_to_ofp_instruction(lo):
487 return pb2.ofp_instruction(
488 type=pb2.OFPIT_WRITE_METADATA,
489 write_metadata=pb2.ofp_instruction_write_metadata(
490 metadata=lo.metadata,
491 metadata_mask=lo.metadata_mask))
492
493
494def loxi_meter_to_ofp_instruction(lo):
495 return pb2.ofp_instruction(
496 type=pb2.OFPIT_METER,
497 meter=pb2.ofp_instruction_meter(meter_id=lo.meter_id))
498
499
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700500def loxi_output_action_to_ofp_action(lo):
501 return pb2.ofp_action(
502 type=pb2.OFPAT_OUTPUT,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700503 output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len))
504
505
Koray Koktenefcdf522018-12-06 00:16:56 +0300506def loxi_write_actions_to_ofp_instruction(lo):
507 return pb2.ofp_instruction(
508 type=pb2.OFPIT_WRITE_ACTIONS,
509 actions=pb2.ofp_instruction_actions(
510 actions=[to_grpc(a) for a in lo.actions]))
511
512
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700513def loxi_group_action_to_ofp_action(lo):
514 return pb2.ofp_action(
515 type=pb2.OFPAT_GROUP,
516 group=pb2.ofp_action_group(group_id=lo.group_id))
517
518
519def loxi_set_field_action_to_ofp_action(lo):
520 return pb2.ofp_action(
521 type=pb2.OFPAT_SET_FIELD,
522 set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field)))
523
524
525def loxi_pop_vlan_action_to_ofp_action(lo):
526 return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN)
527
528
529def loxi_push_vlan_action_to_ofp_action(lo):
530 return pb2.ofp_action(
531 type=pb2.OFPAT_PUSH_VLAN,
532 push=pb2.ofp_action_push(ethertype=lo.ethertype))
533
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700534
535to_grpc_converters = {
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700536
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700537 of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700538 of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod,
539 of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod,
540 of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod,
541 of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod,
Koray Kokten8592a232018-08-27 07:41:14 +0000542 of13.message.meter_mod: loxi_meter_mod_to_ofp_meter_mod,
543 of13.message.meter_stats_request: loxi_meter_stats_to_ofp_meter_stats,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700544
545 of13.message.group_add: loxi_group_mod_to_ofp_group_mod,
546 of13.message.group_delete: loxi_group_mod_to_ofp_group_mod,
547 of13.message.group_modify: loxi_group_mod_to_ofp_group_mod,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700548 of13.message.packet_out: loxi_packet_out_to_ofp_packet_out,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700549
Koray Kokten8592a232018-08-27 07:41:14 +0000550 of13.meter_band.drop: loxi_meter_band_drop_to_ofp_meter_band_drop,
551 of13.meter_band.dscp_remark: loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark,
552 of13.meter_band.experimenter: loxi_meter_band_experimenter_to_ofp_meter_band_experimenter,
553
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700554 of13.common.match_v3: loxi_match_v3_to_ofp_match,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700555 of13.common.bucket: loxi_bucket_to_ofp_bucket,
556
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700557 of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700558 of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm,
559 of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm,
560 of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000561 of13.oxm.vlan_vid_masked: loxi_oxm_vlan_vid_masked_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700562 of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm,
563 of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800564 of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm,
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800565 of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm,
566 of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700567
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700568 of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction,
Gamze Abaka61c2e982018-02-14 11:03:36 +0000569 of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction,
Koray Koktenefcdf522018-12-06 00:16:56 +0300570 of13.instruction.write_actions: loxi_write_actions_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700571 of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000572 of13.instruction.write_metadata: loxi_write_metadata_to_ofp_instruction,
573 of13.instruction.meter: loxi_meter_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700574
575 of13.action.output: loxi_output_action_to_ofp_action,
576 of13.action.group: loxi_group_action_to_ofp_action,
577 of13.action.set_field: loxi_set_field_action_to_ofp_action,
578 of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action,
579 of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action,
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700580}