backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / ofproto / ofproto_utils.py
1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import re
17
18
19 def generate(modname):
20     import sys
21     import functools
22
23     mod = sys.modules[modname]
24
25     def add_attr(k, v):
26         setattr(mod, k, v)
27
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))
36
37
38 def _get_value_name(mod, value, pattern):
39     for k, v in mod.__dict__.items():
40         if k.startswith(pattern):
41             if v == value:
42                 return k
43     return 'Unknown'
44
45
46 def _msg_type_to_str(mod, type_):
47     """
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.
52
53     Example::
54
55         >>> ofproto.ofp_msg_type_to_str(14)
56         'OFPT_FLOW_MOD(14)'
57     """
58     return '%s(%d)' % (_get_value_name(mod, type_, 'OFPT_'), type_)
59
60
61 def _error_type_to_str(mod, type_):
62     """
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.
67
68     Example::
69
70         >>> ofproto.ofp_error_type_to_str(4)
71         'OFPET_BAD_MATCH(4)'
72     """
73     return '%s(%d)' % (_get_value_name(mod, type_, 'OFPET_'), type_)
74
75
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':
83         c_name_p = 'OFPMOFC_'
84     else:
85         c_name_p = 'OFP'
86         for m in re.findall("_(.)", t_name):
87             c_name_p += m.upper()
88         c_name_p += 'C_'
89     c_name = _get_value_name(mod, code, c_name_p)
90     return t_name, c_name
91
92
93 def _error_code_to_str(mod, type_, code):
94     """
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.
99
100     Example::
101
102         >>> ofproto.ofp_error_code_to_str(4, 9)
103         'OFPBMC_BAD_PREREQ(9)'
104     """
105     (_, c_name) = _get_error_names(mod, type_, code)
106     return '%s(%d)' % (c_name, code)
107
108
109 def _error_to_jsondict(mod, type_, code):
110     """
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.
115
116     Example::
117
118         >>> ofproto.ofp_error_to_jsondict(4, 9)
119         {'code': 'OFPBMC_BAD_PREREQ(9)', 'type': 'OFPET_BAD_MATCH(4)'}
120     """
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)}