blob: 1bebc4fa40b4253af75c02ce4b3bb38f5c336c9f [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:
211 return of13.instruction.write_metadata(
212 metadata=inst['write_metadata']['metadata'])
213 elif type == pb2.OFPIT_METER:
214 return of13.instruction.meter(
215 meter_id=inst['meter']['meter_id'])
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800216
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700217 else:
218 raise NotImplementedError('Instruction type %d' % type)
219
220 kw['match'] = make_loxi_match(kw['match'])
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000221 # if the flow action is drop, then the instruction is not found in the dict
222 if 'instructions' in kw:
223 kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']]
Zsolt Haraszti66862032016-11-28 14:28:39 -0800224 del kw['id']
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700225 return of13.flow_stats_entry(**kw)
226
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800227
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700228def ofp_packet_in_to_loxi_packet_in(pb):
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800229 packet_in = of13.message.packet_in(
230 buffer_id=pb.buffer_id,
231 reason=pb.reason,
232 table_id=pb.table_id,
233 cookie=pb.cookie,
234 match=make_loxi_match(pb2dict(pb.match)),
235 data=pb.data
236 )
237 return packet_in
238
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000239
alshabibf4fb2682017-01-12 00:32:56 -0600240def ofp_group_desc_to_loxi_group_desc(pb):
241 return of13.group_desc_stats_entry(
242 group_type=pb.type,
243 group_id=pb.group_id,
244 buckets=[to_loxi(bucket) for bucket in pb.buckets])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700245
alshabibf4fb2682017-01-12 00:32:56 -0600246
247def ofp_group_stats_to_loxi_group_stats(pb):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800248 return of13.group_stats_entry(
alshabibf4fb2682017-01-12 00:32:56 -0600249 group_id=pb.group_id,
250 ref_count=pb.ref_count,
251 packet_count=pb.packet_count,
252 byte_count=pb.byte_count,
253 duration_sec=pb.duration_sec,
254 duration_nsec=pb.duration_nsec,
255 bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats])
Zsolt Haraszti66862032016-11-28 14:28:39 -0800256
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800257
Zsolt Haraszti66862032016-11-28 14:28:39 -0800258def ofp_bucket_counter_to_loxy_bucket_counter(pb):
259 return of13.bucket_counter(
260 packet_count=pb.packet_count,
261 byte_count=pb.byte_count)
262
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700263
alshabibf4fb2682017-01-12 00:32:56 -0600264def ofp_bucket_to_loxi_bucket(pb):
265 return of13.bucket(
266 weight=pb.weight,
267 watch_port=pb.watch_port,
268 watch_group=pb.watch_group,
269 actions=[to_loxi(action) for action in pb.actions]
270 )
271
272
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700273to_loxi_converters = {
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800274 'ofp_port': ofp_port_to_loxi_port_desc,
275 'ofp_port_status': ofp_port_status_to_loxi_port_status,
Gamze Abaka9d343292019-02-20 06:35:18 +0000276 'ofp_flow_removed': ofp_flow_removed_to_loxi_flow_removed,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800277 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats,
278 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in,
alshabibf4fb2682017-01-12 00:32:56 -0600279 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats,
280 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc,
281 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter,
282 'ofp_bucket': ofp_bucket_to_loxi_bucket,
Nicolas Palpacuerfd7b8b12018-06-15 13:58:06 -0400283 'ofp_action': make_loxi_action,
Koray Kokten8592a232018-08-27 07:41:14 +0000284 'ofp_port_stats': ofp_port_stats_to_loxi_port_stats,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000285 'ofp_meter_stats': ofp_meter_stats_to_loxi_meter_stats,
286 'ofp_meter_band_stats': ofp_meter_band_stats_to_loxi_meter_stats
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700287}
288
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700289
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700290def loxi_flow_mod_to_ofp_flow_mod(lo):
291 return pb2.ofp_flow_mod(
292 cookie=lo.cookie,
293 cookie_mask=lo.cookie_mask,
294 table_id=lo.table_id,
295 command=lo._command,
296 idle_timeout=lo.idle_timeout,
297 hard_timeout=lo.hard_timeout,
298 priority=lo.priority,
299 buffer_id=lo.buffer_id,
300 out_port=lo.out_port,
301 out_group=lo.out_group,
302 flags=lo.flags,
303 match=to_grpc(lo.match),
304 instructions=[to_grpc(i) for i in lo.instructions])
305
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000306
Koray Kokten8592a232018-08-27 07:41:14 +0000307def loxi_meter_mod_to_ofp_meter_mod(lo):
308 return pb2.ofp_meter_mod(
309 command=lo.command,
310 flags=lo.flags,
311 meter_id=lo.meter_id,
312 bands=[to_grpc(i) for i in lo.meters])
313
314
315def loxi_meter_band_drop_to_ofp_meter_band_drop(lo):
316 return pb2.ofp_meter_band_header(
317 type=lo.type,
318 rate=lo.rate,
319 burst_size=lo.burst_size)
320
321
322def loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark(lo):
323 return pb2.ofp_meter_band_header(
324 type=lo.type,
325 rate=lo.rate,
326 burst_size=lo.burst_size,
327 dscp_remark=pb2.ofp_meter_band_dscp_remark(prec_level=lo.prec_level))
328
329
330def loxi_meter_band_experimenter_to_ofp_meter_band_experimenter(lo):
331 return pb2.ofp_meter_band_header(
332 type=lo.type,
333 rate=lo.rate,
334 burst_size=lo.burst_size,
335 experimenter=pb2.ofp_meter_band_experimenter(experimenter=lo.experimenter))
336
337
338def loxi_meter_multipart_request_to_ofp_meter_multipart_request(lo):
339 return pb2.ofp_meter_multipart_request(
340 meter_id=lo.meter_id)
341
342
343def loxi_meter_stats_to_ofp_meter_stats(lo):
344 return pb2.ofp_meter_stats(
345 meter_id=lo.meter_id,
346 flow_count=lo.flow_count,
347 packet_in_count =lo.packet_in_count,
348 byte_in_count=lo.byte_in_count,
349 duration_sec=lo.duration_sec,
350 duration_nsec=lo.duration_nsec,
351 band_stats=lo.band_stats)
352
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700353
354def loxi_group_mod_to_ofp_group_mod(lo):
355 return pb2.ofp_group_mod(
356 command=lo.command,
Zsolt Haraszti9125b1a2016-10-24 22:54:33 -0700357 type=lo.group_type,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700358 group_id=lo.group_id,
359 buckets=[to_grpc(b) for b in lo.buckets])
360
361
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700362def loxi_packet_out_to_ofp_packet_out(lo):
363 return pb2.ofp_packet_out(
364 buffer_id=lo.buffer_id,
365 in_port=lo.in_port,
366 actions=[to_grpc(a) for a in lo.actions],
367 data=lo.data)
368
369
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700370def loxi_match_v3_to_ofp_match(lo):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700371 return pb2.ofp_match(
372 type=pb2.OFPMT_OXM,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700373 oxm_fields=[to_grpc(f) for f in lo.oxm_list])
374
375
376def loxi_bucket_to_ofp_bucket(lo):
377 return pb2.ofp_bucket(
378 weight=lo.weight,
379 watch_port=lo.watch_port,
380 watch_group=lo.watch_group,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700381 actions=[to_grpc(a) for a in lo.actions])
382
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700383
384def loxi_oxm_eth_type_to_ofp_oxm(lo):
385 return pb2.ofp_oxm_field(
386 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
387 ofb_field=pb2.ofp_oxm_ofb_field(
388 type=pb2.OFPXMT_OFB_ETH_TYPE,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700389 eth_type=lo.value))
390
391
392def loxi_oxm_in_port_to_ofp_oxm(lo):
393 return pb2.ofp_oxm_field(
394 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
395 ofb_field=pb2.ofp_oxm_ofb_field(
396 type=pb2.OFPXMT_OFB_IN_PORT,
397 port=lo.value))
398
399
400def loxi_oxm_ip_proto_to_ofp_oxm(lo):
401 return pb2.ofp_oxm_field(
402 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
403 ofb_field=pb2.ofp_oxm_ofb_field(
404 type=pb2.OFPXMT_OFB_IP_PROTO,
405 ip_proto=lo.value))
406
407
408def loxi_oxm_vlan_vid_to_ofp_oxm(lo):
409 return pb2.ofp_oxm_field(
410 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
411 ofb_field=pb2.ofp_oxm_ofb_field(
412 type=pb2.OFPXMT_OFB_VLAN_VID,
413 vlan_vid=lo.value))
414
415
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000416def loxi_oxm_vlan_vid_masked_to_ofp_oxm(lo):
417 return pb2.ofp_oxm_field(
418 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
419 ofb_field=pb2.ofp_oxm_ofb_field(
420 type=pb2.OFPXMT_OFB_VLAN_VID,
421 has_mask=True,
422 vlan_vid=lo.value,
423 vlan_vid_mask=lo.value_mask))
424
425
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700426def loxi_oxm_vlan_pcp_to_ofp_oxm(lo):
427 return pb2.ofp_oxm_field(
428 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
429 ofb_field=pb2.ofp_oxm_ofb_field(
430 type=pb2.OFPXMT_OFB_VLAN_PCP,
431 vlan_pcp=lo.value))
432
433
434def loxi_oxm_ipv4_dst_to_ofp_oxm(lo):
435 return pb2.ofp_oxm_field(
436 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
437 ofb_field=pb2.ofp_oxm_ofb_field(
438 type=pb2.OFPXMT_OFB_IPV4_DST,
439 ipv4_dst=lo.value))
440
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700441
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800442def loxi_oxm_udp_dst_to_ofp_oxm(lo):
443 return pb2.ofp_oxm_field(
444 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
445 ofb_field=pb2.ofp_oxm_ofb_field(
446 type=pb2.OFPXMT_OFB_UDP_DST,
447 udp_dst=lo.value))
448
449
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800450def loxi_oxm_udp_src_to_ofp_oxm(lo):
451 return pb2.ofp_oxm_field(
452 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
453 ofb_field=pb2.ofp_oxm_ofb_field(
454 type=pb2.OFPXMT_OFB_UDP_SRC,
455 udp_src=lo.value))
456
457
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800458def loxi_oxm_metadata_to_ofp_oxm(lo):
459 return pb2.ofp_oxm_field(
460 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
461 ofb_field=pb2.ofp_oxm_ofb_field(
462 type=pb2.OFPXMT_OFB_METADATA,
463 table_metadata=lo.value))
464
465
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700466def loxi_apply_actions_to_ofp_instruction(lo):
467 return pb2.ofp_instruction(
468 type=pb2.OFPIT_APPLY_ACTIONS,
469 actions=pb2.ofp_instruction_actions(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700470 actions=[to_grpc(a) for a in lo.actions]))
471
Gamze Abaka61c2e982018-02-14 11:03:36 +0000472def loxi_clear_actions_to_ofp_instruction(lo):
473 return pb2.ofp_instruction(
474 type=pb2.OFPIT_CLEAR_ACTIONS)
475
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700476
477def loxi_goto_table_to_ofp_instruction(lo):
478 return pb2.ofp_instruction(
479 type=pb2.OFPIT_GOTO_TABLE,
480 goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id))
481
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700482
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000483def loxi_write_metadata_to_ofp_instruction(lo):
484 return pb2.ofp_instruction(
485 type=pb2.OFPIT_WRITE_METADATA,
486 write_metadata=pb2.ofp_instruction_write_metadata(
487 metadata=lo.metadata,
488 metadata_mask=lo.metadata_mask))
489
490
491def loxi_meter_to_ofp_instruction(lo):
492 return pb2.ofp_instruction(
493 type=pb2.OFPIT_METER,
494 meter=pb2.ofp_instruction_meter(meter_id=lo.meter_id))
495
496
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700497def loxi_output_action_to_ofp_action(lo):
498 return pb2.ofp_action(
499 type=pb2.OFPAT_OUTPUT,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700500 output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len))
501
502
Koray Koktenefcdf522018-12-06 00:16:56 +0300503def loxi_write_actions_to_ofp_instruction(lo):
504 return pb2.ofp_instruction(
505 type=pb2.OFPIT_WRITE_ACTIONS,
506 actions=pb2.ofp_instruction_actions(
507 actions=[to_grpc(a) for a in lo.actions]))
508
509
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700510def loxi_group_action_to_ofp_action(lo):
511 return pb2.ofp_action(
512 type=pb2.OFPAT_GROUP,
513 group=pb2.ofp_action_group(group_id=lo.group_id))
514
515
516def loxi_set_field_action_to_ofp_action(lo):
517 return pb2.ofp_action(
518 type=pb2.OFPAT_SET_FIELD,
519 set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field)))
520
521
522def loxi_pop_vlan_action_to_ofp_action(lo):
523 return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN)
524
525
526def loxi_push_vlan_action_to_ofp_action(lo):
527 return pb2.ofp_action(
528 type=pb2.OFPAT_PUSH_VLAN,
529 push=pb2.ofp_action_push(ethertype=lo.ethertype))
530
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700531
532to_grpc_converters = {
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700533
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700534 of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700535 of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod,
536 of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod,
537 of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod,
538 of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod,
Koray Kokten8592a232018-08-27 07:41:14 +0000539 of13.message.meter_mod: loxi_meter_mod_to_ofp_meter_mod,
540 of13.message.meter_stats_request: loxi_meter_stats_to_ofp_meter_stats,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700541
542 of13.message.group_add: loxi_group_mod_to_ofp_group_mod,
543 of13.message.group_delete: loxi_group_mod_to_ofp_group_mod,
544 of13.message.group_modify: loxi_group_mod_to_ofp_group_mod,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700545 of13.message.packet_out: loxi_packet_out_to_ofp_packet_out,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700546
Koray Kokten8592a232018-08-27 07:41:14 +0000547 of13.meter_band.drop: loxi_meter_band_drop_to_ofp_meter_band_drop,
548 of13.meter_band.dscp_remark: loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark,
549 of13.meter_band.experimenter: loxi_meter_band_experimenter_to_ofp_meter_band_experimenter,
550
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700551 of13.common.match_v3: loxi_match_v3_to_ofp_match,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700552 of13.common.bucket: loxi_bucket_to_ofp_bucket,
553
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700554 of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700555 of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm,
556 of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm,
557 of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000558 of13.oxm.vlan_vid_masked: loxi_oxm_vlan_vid_masked_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700559 of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm,
560 of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800561 of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm,
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800562 of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm,
563 of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700564
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700565 of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction,
Gamze Abaka61c2e982018-02-14 11:03:36 +0000566 of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction,
Koray Koktenefcdf522018-12-06 00:16:56 +0300567 of13.instruction.write_actions: loxi_write_actions_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700568 of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000569 of13.instruction.write_metadata: loxi_write_metadata_to_ofp_instruction,
570 of13.instruction.meter: loxi_meter_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700571
572 of13.action.output: loxi_output_action_to_ofp_action,
573 of13.action.group: loxi_group_action_to_ofp_action,
574 of13.action.set_field: loxi_set_field_action_to_ofp_action,
575 of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action,
576 of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action,
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700577}