1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 def generate(modname):
23 mod = sys.modules[modname]
28 add_attr('ofp_msg_type_to_str',
29 functools.partial(_msg_type_to_str, mod))
30 add_attr('ofp_error_type_to_str',
31 functools.partial(_error_type_to_str, mod))
32 add_attr('ofp_error_code_to_str',
33 functools.partial(_error_code_to_str, mod))
34 add_attr('ofp_error_to_jsondict',
35 functools.partial(_error_to_jsondict, mod))
38 def _get_value_name(mod, value, pattern):
39 for k, v in mod.__dict__.items():
40 if k.startswith(pattern):
46 def _msg_type_to_str(mod, type_):
48 This method is registered as ofp_msg_type_to_str(type_) method
49 into ryu.ofproto.ofproto_v1_* modules.
50 And this method returns the message type as a string value for given
51 'type' defined in ofp_type enum.
55 >>> ofproto.ofp_msg_type_to_str(14)
58 return '%s(%d)' % (_get_value_name(mod, type_, 'OFPT_'), type_)
61 def _error_type_to_str(mod, type_):
63 This method is registered as ofp_error_type_to_str(type_) method
64 into ryu.ofproto.ofproto_v1_* modules.
65 And this method returns the error type as a string value for given
66 'type' defined in ofp_error_msg structure.
70 >>> ofproto.ofp_error_type_to_str(4)
73 return '%s(%d)' % (_get_value_name(mod, type_, 'OFPET_'), type_)
76 def _get_error_names(mod, type_, code):
77 t_name = _get_value_name(mod, type_, 'OFPET_')
78 if t_name == 'Unknown':
79 return 'Unknown', 'Unknown'
80 # Construct error code name pattern
81 # e.g.) "OFPET_BAD_MATCH" -> "OFPBMC_"
82 if t_name == 'OFPET_FLOW_MONITOR_FAILED':
86 for m in re.findall("_(.)", t_name):
89 c_name = _get_value_name(mod, code, c_name_p)
93 def _error_code_to_str(mod, type_, code):
95 This method is registered as ofp_error_code_to_str(type_, code) method
96 into ryu.ofproto.ofproto_v1_* modules.
97 And this method returns the error code as a string value for given
98 'type' and 'code' defined in ofp_error_msg structure.
102 >>> ofproto.ofp_error_code_to_str(4, 9)
103 'OFPBMC_BAD_PREREQ(9)'
105 (_, c_name) = _get_error_names(mod, type_, code)
106 return '%s(%d)' % (c_name, code)
109 def _error_to_jsondict(mod, type_, code):
111 This method is registered as ofp_error_to_jsondict(type_, code) method
112 into ryu.ofproto.ofproto_v1_* modules.
113 And this method returns ofp_error_msg as a json format for given
114 'type' and 'code' defined in ofp_error_msg structure.
118 >>> ofproto.ofp_error_to_jsondict(4, 9)
119 {'code': 'OFPBMC_BAD_PREREQ(9)', 'type': 'OFPET_BAD_MATCH(4)'}
121 (t_name, c_name) = _get_error_names(mod, type_, code)
122 return {'type': '%s(%d)' % (t_name, type_),
123 'code': '%s(%d)' % (c_name, code)}