blob: 30c5d501fe6b105b4a8b9b57616ba5cfa7ebd123 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001# Copyright 2013, Big Switch Networks, Inc.
2#
3# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4# the following special exception:
5#
6# LOXI Exception
7#
8# As a special exception to the terms of the EPL, you may distribute libraries
9# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10# that copyright and licensing notices generated by LoxiGen are not altered or removed
11# from the LoxiGen Libraries and the notice provided below is (i) included in
12# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13# documentation for the LoxiGen Libraries, if distributed in binary form.
14#
15# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16#
17# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18# a copy of the EPL at:
19#
20# http://www.eclipse.org/legal/epl-v10.html
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25# EPL for the specific language governing permissions and limitations
26# under the EPL.
27
28"""
29Utilities for generating the target Python code
30"""
31
32import os
33import of_g
34import loxi_front_end.type_maps as type_maps
35import loxi_utils.loxi_utils as utils
36
37templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
38
39def render_template(out, name, **context):
40 utils.render_template(out, name, [templates_dir], context)
41
42def render_static(out, name):
43 utils.render_static(out, name, [templates_dir])
44
45def lookup_unified_class(cls, version):
46 unified_class = of_g.unified[cls][version]
47 if "use_version" in unified_class: # deref version ref
48 ref_version = unified_class["use_version"]
49 unified_class = of_g.unified[cls][ref_version]
50 return unified_class
51
52def primary_wire_type(cls, version):
53 if cls in type_maps.stats_reply_list:
54 return type_maps.type_val[("of_stats_reply", version)]
55 elif cls in type_maps.stats_request_list:
56 return type_maps.type_val[("of_stats_request", version)]
57 elif cls in type_maps.flow_mod_list:
58 return type_maps.type_val[("of_flow_mod", version)]
59 elif (cls, version) in type_maps.type_val:
60 return type_maps.type_val[(cls, version)]
61 elif type_maps.message_is_extension(cls, version):
62 return type_maps.type_val[("of_experimenter", version)]
63 elif type_maps.action_is_extension(cls, version):
64 return type_maps.type_val[("of_action_experimenter", version)]
65 elif type_maps.action_id_is_extension(cls, version):
66 return type_maps.type_val[("of_action_id_experimenter", version)]
67 elif type_maps.instruction_is_extension(cls, version):
68 return type_maps.type_val[("of_instruction_experimenter", version)]
69 elif type_maps.queue_prop_is_extension(cls, version):
70 return type_maps.type_val[("of_queue_prop_experimenter", version)]
71 elif type_maps.table_feature_prop_is_extension(cls, version):
72 return type_maps.type_val[("of_table_feature_prop_experimenter", version)]
73 else:
Andreas Wundsamd157d4a2013-05-06 14:13:29 -070074 raise ValueError("No wiretype for %s in version %d" % (cls, version))
Rich Lanea06d0c32013-03-25 08:52:03 -070075
76def constant_for_value(version, group, value):
77 return (["const." + v["ofp_name"] for k, v in of_g.identifiers.items()
78 if k in of_g.identifiers_by_group[group] and
Rich Lanefe735bc2013-04-08 16:58:49 -070079 v["values_by_version"].get(version, None) == value] or [value])[0]