blob: 7156d603fd6e7d73b41a3e9547d552891bf46184 [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 Abaka53cc0a22019-01-31 12:06:11 +000069
Nicolas Palpacuerfd7b8b12018-06-15 13:58:06 -040070def ofp_port_stats_to_loxi_port_stats(pb):
71 kw = pb2dict(pb)
72 return of13.port_stats_entry(**kw)
73
Gamze Abaka53cc0a22019-01-31 12:06:11 +000074
Koray Kokten8592a232018-08-27 07:41:14 +000075def ofp_meter_stats_to_loxi_meter_stats(pb):
Gamze Abaka53cc0a22019-01-31 12:06:11 +000076 return of13.meter_stats(
77 meter_id=pb.meter_id,
78 flow_count=pb.flow_count,
79 packet_in_count=pb.packet_in_count,
80 byte_in_count=pb.byte_in_count,
81 duration_sec=pb.duration_sec,
82 duration_nsec=pb.duration_nsec,
83 band_stats=[to_loxi(band_stat) for band_stat in pb.band_stats])
84
85
86def ofp_meter_band_stats_to_loxi_meter_stats(pb):
87 return of13.meter_band_stats(
88 packet_band_count=pb.packet_band_count,
89 byte_band_count=pb.byte_band_count
90 )
91
Koray Kokten8592a232018-08-27 07:41:14 +000092
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080093def make_loxi_field(oxm_field):
94 assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC
95 ofb_field = oxm_field['ofb_field']
96 field_type = ofb_field.get('type', 0)
97
98 if field_type == pb2.OFPXMT_OFB_ETH_TYPE:
99 return (
100 of13.oxm.eth_type(value=ofb_field['eth_type']))
101
102 elif field_type == pb2.OFPXMT_OFB_IN_PORT:
103 return (
104 of13.oxm.in_port(value=ofb_field['port']))
105
106 elif field_type == pb2.OFPXMT_OFB_IP_PROTO:
107 return (
108 of13.oxm.ip_proto(value=ofb_field['ip_proto']))
109
110 elif field_type == pb2.OFPXMT_OFB_VLAN_VID:
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000111 if ofb_field.get('has_mask', 0):
112 return of13.oxm.vlan_vid_masked(value=ofb_field['vlan_vid'],
113 value_mask=ofb_field['vlan_vid_mask'])
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800114 return (
115 of13.oxm.vlan_vid(value=ofb_field['vlan_vid']))
116
117 elif field_type == pb2.OFPXMT_OFB_VLAN_PCP:
118 return (
119 of13.oxm.vlan_pcp(value=ofb_field['vlan_pcp']))
120
121 elif field_type == pb2.OFPXMT_OFB_IPV4_SRC:
122 return (
123 of13.oxm.ipv4_src(value=ofb_field['ipv4_src']))
124
125 elif field_type == pb2.OFPXMT_OFB_IPV4_DST:
126 return (
127 of13.oxm.ipv4_dst(value=ofb_field['ipv4_dst']))
128
129 elif field_type == pb2.OFPXMT_OFB_UDP_SRC:
130 return (
131 of13.oxm.udp_src(value=ofb_field['udp_src']))
132
133 elif field_type == pb2.OFPXMT_OFB_UDP_DST:
134 return (
135 of13.oxm.udp_dst(value=ofb_field['udp_dst']))
136
137 elif field_type == pb2.OFPXMT_OFB_METADATA:
138 return (
139 of13.oxm.metadata(value=ofb_field['table_metadata']))
140
141 else:
142 raise NotImplementedError(
143 'OXM match field for type %s' % field_type)
144
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000145
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700146def make_loxi_match(match):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800147 assert match.get('type', pb2.OFPMT_STANDARD) == pb2.OFPMT_OXM
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700148 loxi_match_fields = []
Zsolt Haraszti66862032016-11-28 14:28:39 -0800149 for oxm_field in match.get('oxm_fields', []):
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800150 loxi_match_fields.append(make_loxi_field(oxm_field))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700151 return of13.match_v3(oxm_list=loxi_match_fields)
152
alshabibf4fb2682017-01-12 00:32:56 -0600153
154def make_loxi_action(a):
155 if type(a) is not dict:
156 a = pb2dict(a)
157
158 typ = a.get('type', 0)
159
160 if typ == pb2.OFPAT_OUTPUT:
161 output_kws = a['output']
162 return of13.action.output(**output_kws)
163
164 elif typ == pb2.OFPAT_POP_VLAN:
165 return of13.action.pop_vlan()
166
167 elif typ == pb2.OFPAT_PUSH_VLAN:
168 push_vlan_kws = a['push']
169 return of13.action.push_vlan(**push_vlan_kws)
170
171 elif typ == pb2.OFPAT_SET_FIELD:
172 loxi_field = make_loxi_field(a['set_field']['field'])
173 return of13.action.set_field(loxi_field)
174
175 elif typ == pb2.OFPAT_GROUP:
176 group_kws = a['group']
177 return of13.action.group(**group_kws)
178
179 else:
180 raise NotImplementedError(
181 'Action decoder for action OFPAT_* %d' % typ)
182
183
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700184def ofp_flow_stats_to_loxi_flow_stats(pb):
185 kw = pb2dict(pb)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700186
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700187 def make_loxi_instruction(inst):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700188 type = inst['type']
189 if type == pb2.OFPIT_APPLY_ACTIONS:
190 return of13.instruction.apply_actions(
191 actions=[make_loxi_action(a)
192 for a in inst['actions']['actions']])
Gamze Abaka61c2e982018-02-14 11:03:36 +0000193 elif type == pb2.OFPIT_CLEAR_ACTIONS:
194 return of13.instruction.clear_actions()
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800195 elif type == pb2.OFPIT_GOTO_TABLE:
196 return of13.instruction.goto_table(
197 table_id=inst['goto_table']['table_id'])
Koray Koktenefcdf522018-12-06 00:16:56 +0300198 elif type == pb2.OFPIT_WRITE_ACTIONS:
199 return of13.instruction.write_actions(
200 actions=[make_loxi_action(a)
201 for a in inst['actions']['actions']])
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000202 elif type == pb2.OFPIT_WRITE_METADATA:
203 return of13.instruction.write_metadata(
204 metadata=inst['write_metadata']['metadata'])
205 elif type == pb2.OFPIT_METER:
206 return of13.instruction.meter(
207 meter_id=inst['meter']['meter_id'])
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800208
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700209 else:
210 raise NotImplementedError('Instruction type %d' % type)
211
212 kw['match'] = make_loxi_match(kw['match'])
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000213 # if the flow action is drop, then the instruction is not found in the dict
214 if 'instructions' in kw:
215 kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']]
Zsolt Haraszti66862032016-11-28 14:28:39 -0800216 del kw['id']
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700217 return of13.flow_stats_entry(**kw)
218
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800219
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700220def ofp_packet_in_to_loxi_packet_in(pb):
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800221 packet_in = of13.message.packet_in(
222 buffer_id=pb.buffer_id,
223 reason=pb.reason,
224 table_id=pb.table_id,
225 cookie=pb.cookie,
226 match=make_loxi_match(pb2dict(pb.match)),
227 data=pb.data
228 )
229 return packet_in
230
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000231
alshabibf4fb2682017-01-12 00:32:56 -0600232def ofp_group_desc_to_loxi_group_desc(pb):
233 return of13.group_desc_stats_entry(
234 group_type=pb.type,
235 group_id=pb.group_id,
236 buckets=[to_loxi(bucket) for bucket in pb.buckets])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700237
alshabibf4fb2682017-01-12 00:32:56 -0600238
239def ofp_group_stats_to_loxi_group_stats(pb):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800240 return of13.group_stats_entry(
alshabibf4fb2682017-01-12 00:32:56 -0600241 group_id=pb.group_id,
242 ref_count=pb.ref_count,
243 packet_count=pb.packet_count,
244 byte_count=pb.byte_count,
245 duration_sec=pb.duration_sec,
246 duration_nsec=pb.duration_nsec,
247 bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats])
Zsolt Haraszti66862032016-11-28 14:28:39 -0800248
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800249
Zsolt Haraszti66862032016-11-28 14:28:39 -0800250def ofp_bucket_counter_to_loxy_bucket_counter(pb):
251 return of13.bucket_counter(
252 packet_count=pb.packet_count,
253 byte_count=pb.byte_count)
254
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700255
alshabibf4fb2682017-01-12 00:32:56 -0600256def ofp_bucket_to_loxi_bucket(pb):
257 return of13.bucket(
258 weight=pb.weight,
259 watch_port=pb.watch_port,
260 watch_group=pb.watch_group,
261 actions=[to_loxi(action) for action in pb.actions]
262 )
263
264
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700265to_loxi_converters = {
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800266 'ofp_port': ofp_port_to_loxi_port_desc,
267 'ofp_port_status': ofp_port_status_to_loxi_port_status,
268 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats,
269 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in,
alshabibf4fb2682017-01-12 00:32:56 -0600270 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats,
271 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc,
272 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter,
273 'ofp_bucket': ofp_bucket_to_loxi_bucket,
Nicolas Palpacuerfd7b8b12018-06-15 13:58:06 -0400274 'ofp_action': make_loxi_action,
Koray Kokten8592a232018-08-27 07:41:14 +0000275 'ofp_port_stats': ofp_port_stats_to_loxi_port_stats,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000276 'ofp_meter_stats': ofp_meter_stats_to_loxi_meter_stats,
277 'ofp_meter_band_stats': ofp_meter_band_stats_to_loxi_meter_stats
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700278}
279
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700280
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700281def loxi_flow_mod_to_ofp_flow_mod(lo):
282 return pb2.ofp_flow_mod(
283 cookie=lo.cookie,
284 cookie_mask=lo.cookie_mask,
285 table_id=lo.table_id,
286 command=lo._command,
287 idle_timeout=lo.idle_timeout,
288 hard_timeout=lo.hard_timeout,
289 priority=lo.priority,
290 buffer_id=lo.buffer_id,
291 out_port=lo.out_port,
292 out_group=lo.out_group,
293 flags=lo.flags,
294 match=to_grpc(lo.match),
295 instructions=[to_grpc(i) for i in lo.instructions])
296
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000297
Koray Kokten8592a232018-08-27 07:41:14 +0000298def loxi_meter_mod_to_ofp_meter_mod(lo):
299 return pb2.ofp_meter_mod(
300 command=lo.command,
301 flags=lo.flags,
302 meter_id=lo.meter_id,
303 bands=[to_grpc(i) for i in lo.meters])
304
305
306def loxi_meter_band_drop_to_ofp_meter_band_drop(lo):
307 return pb2.ofp_meter_band_header(
308 type=lo.type,
309 rate=lo.rate,
310 burst_size=lo.burst_size)
311
312
313def loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark(lo):
314 return pb2.ofp_meter_band_header(
315 type=lo.type,
316 rate=lo.rate,
317 burst_size=lo.burst_size,
318 dscp_remark=pb2.ofp_meter_band_dscp_remark(prec_level=lo.prec_level))
319
320
321def loxi_meter_band_experimenter_to_ofp_meter_band_experimenter(lo):
322 return pb2.ofp_meter_band_header(
323 type=lo.type,
324 rate=lo.rate,
325 burst_size=lo.burst_size,
326 experimenter=pb2.ofp_meter_band_experimenter(experimenter=lo.experimenter))
327
328
329def loxi_meter_multipart_request_to_ofp_meter_multipart_request(lo):
330 return pb2.ofp_meter_multipart_request(
331 meter_id=lo.meter_id)
332
333
334def loxi_meter_stats_to_ofp_meter_stats(lo):
335 return pb2.ofp_meter_stats(
336 meter_id=lo.meter_id,
337 flow_count=lo.flow_count,
338 packet_in_count =lo.packet_in_count,
339 byte_in_count=lo.byte_in_count,
340 duration_sec=lo.duration_sec,
341 duration_nsec=lo.duration_nsec,
342 band_stats=lo.band_stats)
343
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700344
345def loxi_group_mod_to_ofp_group_mod(lo):
346 return pb2.ofp_group_mod(
347 command=lo.command,
Zsolt Haraszti9125b1a2016-10-24 22:54:33 -0700348 type=lo.group_type,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700349 group_id=lo.group_id,
350 buckets=[to_grpc(b) for b in lo.buckets])
351
352
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700353def loxi_packet_out_to_ofp_packet_out(lo):
354 return pb2.ofp_packet_out(
355 buffer_id=lo.buffer_id,
356 in_port=lo.in_port,
357 actions=[to_grpc(a) for a in lo.actions],
358 data=lo.data)
359
360
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700361def loxi_match_v3_to_ofp_match(lo):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700362 return pb2.ofp_match(
363 type=pb2.OFPMT_OXM,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700364 oxm_fields=[to_grpc(f) for f in lo.oxm_list])
365
366
367def loxi_bucket_to_ofp_bucket(lo):
368 return pb2.ofp_bucket(
369 weight=lo.weight,
370 watch_port=lo.watch_port,
371 watch_group=lo.watch_group,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700372 actions=[to_grpc(a) for a in lo.actions])
373
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700374
375def loxi_oxm_eth_type_to_ofp_oxm(lo):
376 return pb2.ofp_oxm_field(
377 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
378 ofb_field=pb2.ofp_oxm_ofb_field(
379 type=pb2.OFPXMT_OFB_ETH_TYPE,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700380 eth_type=lo.value))
381
382
383def loxi_oxm_in_port_to_ofp_oxm(lo):
384 return pb2.ofp_oxm_field(
385 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
386 ofb_field=pb2.ofp_oxm_ofb_field(
387 type=pb2.OFPXMT_OFB_IN_PORT,
388 port=lo.value))
389
390
391def loxi_oxm_ip_proto_to_ofp_oxm(lo):
392 return pb2.ofp_oxm_field(
393 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
394 ofb_field=pb2.ofp_oxm_ofb_field(
395 type=pb2.OFPXMT_OFB_IP_PROTO,
396 ip_proto=lo.value))
397
398
399def loxi_oxm_vlan_vid_to_ofp_oxm(lo):
400 return pb2.ofp_oxm_field(
401 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
402 ofb_field=pb2.ofp_oxm_ofb_field(
403 type=pb2.OFPXMT_OFB_VLAN_VID,
404 vlan_vid=lo.value))
405
406
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000407def loxi_oxm_vlan_vid_masked_to_ofp_oxm(lo):
408 return pb2.ofp_oxm_field(
409 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
410 ofb_field=pb2.ofp_oxm_ofb_field(
411 type=pb2.OFPXMT_OFB_VLAN_VID,
412 has_mask=True,
413 vlan_vid=lo.value,
414 vlan_vid_mask=lo.value_mask))
415
416
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700417def loxi_oxm_vlan_pcp_to_ofp_oxm(lo):
418 return pb2.ofp_oxm_field(
419 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
420 ofb_field=pb2.ofp_oxm_ofb_field(
421 type=pb2.OFPXMT_OFB_VLAN_PCP,
422 vlan_pcp=lo.value))
423
424
425def loxi_oxm_ipv4_dst_to_ofp_oxm(lo):
426 return pb2.ofp_oxm_field(
427 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
428 ofb_field=pb2.ofp_oxm_ofb_field(
429 type=pb2.OFPXMT_OFB_IPV4_DST,
430 ipv4_dst=lo.value))
431
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700432
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800433def loxi_oxm_udp_dst_to_ofp_oxm(lo):
434 return pb2.ofp_oxm_field(
435 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
436 ofb_field=pb2.ofp_oxm_ofb_field(
437 type=pb2.OFPXMT_OFB_UDP_DST,
438 udp_dst=lo.value))
439
440
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800441def loxi_oxm_udp_src_to_ofp_oxm(lo):
442 return pb2.ofp_oxm_field(
443 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
444 ofb_field=pb2.ofp_oxm_ofb_field(
445 type=pb2.OFPXMT_OFB_UDP_SRC,
446 udp_src=lo.value))
447
448
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800449def loxi_oxm_metadata_to_ofp_oxm(lo):
450 return pb2.ofp_oxm_field(
451 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
452 ofb_field=pb2.ofp_oxm_ofb_field(
453 type=pb2.OFPXMT_OFB_METADATA,
454 table_metadata=lo.value))
455
456
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700457def loxi_apply_actions_to_ofp_instruction(lo):
458 return pb2.ofp_instruction(
459 type=pb2.OFPIT_APPLY_ACTIONS,
460 actions=pb2.ofp_instruction_actions(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700461 actions=[to_grpc(a) for a in lo.actions]))
462
Gamze Abaka61c2e982018-02-14 11:03:36 +0000463def loxi_clear_actions_to_ofp_instruction(lo):
464 return pb2.ofp_instruction(
465 type=pb2.OFPIT_CLEAR_ACTIONS)
466
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700467
468def loxi_goto_table_to_ofp_instruction(lo):
469 return pb2.ofp_instruction(
470 type=pb2.OFPIT_GOTO_TABLE,
471 goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id))
472
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700473
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000474def loxi_write_metadata_to_ofp_instruction(lo):
475 return pb2.ofp_instruction(
476 type=pb2.OFPIT_WRITE_METADATA,
477 write_metadata=pb2.ofp_instruction_write_metadata(
478 metadata=lo.metadata,
479 metadata_mask=lo.metadata_mask))
480
481
482def loxi_meter_to_ofp_instruction(lo):
483 return pb2.ofp_instruction(
484 type=pb2.OFPIT_METER,
485 meter=pb2.ofp_instruction_meter(meter_id=lo.meter_id))
486
487
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700488def loxi_output_action_to_ofp_action(lo):
489 return pb2.ofp_action(
490 type=pb2.OFPAT_OUTPUT,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700491 output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len))
492
493
Koray Koktenefcdf522018-12-06 00:16:56 +0300494def loxi_write_actions_to_ofp_instruction(lo):
495 return pb2.ofp_instruction(
496 type=pb2.OFPIT_WRITE_ACTIONS,
497 actions=pb2.ofp_instruction_actions(
498 actions=[to_grpc(a) for a in lo.actions]))
499
500
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700501def loxi_group_action_to_ofp_action(lo):
502 return pb2.ofp_action(
503 type=pb2.OFPAT_GROUP,
504 group=pb2.ofp_action_group(group_id=lo.group_id))
505
506
507def loxi_set_field_action_to_ofp_action(lo):
508 return pb2.ofp_action(
509 type=pb2.OFPAT_SET_FIELD,
510 set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field)))
511
512
513def loxi_pop_vlan_action_to_ofp_action(lo):
514 return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN)
515
516
517def loxi_push_vlan_action_to_ofp_action(lo):
518 return pb2.ofp_action(
519 type=pb2.OFPAT_PUSH_VLAN,
520 push=pb2.ofp_action_push(ethertype=lo.ethertype))
521
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700522
523to_grpc_converters = {
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700524
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700525 of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700526 of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod,
527 of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod,
528 of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod,
529 of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod,
Koray Kokten8592a232018-08-27 07:41:14 +0000530 of13.message.meter_mod: loxi_meter_mod_to_ofp_meter_mod,
531 of13.message.meter_stats_request: loxi_meter_stats_to_ofp_meter_stats,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700532
533 of13.message.group_add: loxi_group_mod_to_ofp_group_mod,
534 of13.message.group_delete: loxi_group_mod_to_ofp_group_mod,
535 of13.message.group_modify: loxi_group_mod_to_ofp_group_mod,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700536 of13.message.packet_out: loxi_packet_out_to_ofp_packet_out,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700537
Koray Kokten8592a232018-08-27 07:41:14 +0000538 of13.meter_band.drop: loxi_meter_band_drop_to_ofp_meter_band_drop,
539 of13.meter_band.dscp_remark: loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark,
540 of13.meter_band.experimenter: loxi_meter_band_experimenter_to_ofp_meter_band_experimenter,
541
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700542 of13.common.match_v3: loxi_match_v3_to_ofp_match,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700543 of13.common.bucket: loxi_bucket_to_ofp_bucket,
544
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700545 of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700546 of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm,
547 of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm,
548 of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000549 of13.oxm.vlan_vid_masked: loxi_oxm_vlan_vid_masked_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700550 of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm,
551 of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800552 of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm,
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800553 of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm,
554 of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700555
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700556 of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction,
Gamze Abaka61c2e982018-02-14 11:03:36 +0000557 of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction,
Koray Koktenefcdf522018-12-06 00:16:56 +0300558 of13.instruction.write_actions: loxi_write_actions_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700559 of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction,
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000560 of13.instruction.write_metadata: loxi_write_metadata_to_ofp_instruction,
561 of13.instruction.meter: loxi_meter_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700562
563 of13.action.output: loxi_output_action_to_ofp_action,
564 of13.action.group: loxi_group_action_to_ofp_action,
565 of13.action.set_field: loxi_set_field_action_to_ofp_action,
566 of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action,
567 of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action,
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700568}