Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | # 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 | """ |
| 29 | @brief Utilities involving LOXI naming conventions |
| 30 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 31 | Utility functions for OpenFlow class generation |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 32 | |
| 33 | These may need to be sorted out into language specific functions |
| 34 | """ |
| 35 | |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 36 | import re |
Rich Lane | c340517 | 2013-02-20 18:18:57 -0800 | [diff] [blame] | 37 | import sys |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 38 | |
| 39 | import loxi_globals |
Andreas Wundsam | 37c7bc2 | 2013-09-17 13:48:35 -0700 | [diff] [blame] | 40 | from generic_utils import find, memoize |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 41 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 42 | ## |
| 43 | # Class types: |
| 44 | # |
| 45 | # Virtual |
| 46 | # A virtual class is one which does not have an explicit wire |
| 47 | # representation. For example, an inheritance super class |
| 48 | # or a list type. |
| 49 | # |
| 50 | # List |
| 51 | # A list of objects of some other type |
| 52 | # |
| 53 | # TLV16 |
| 54 | # The wire represenation starts with 16-bit type and length fields |
| 55 | # |
| 56 | # OXM |
| 57 | # An extensible match object |
| 58 | # |
Murat Parlakisik | f95672c | 2016-12-05 00:53:17 -0800 | [diff] [blame^] | 59 | # OXS |
| 60 | # Extensible Stats Object |
| 61 | # |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 62 | # Message |
| 63 | # A top level OpenFlow message |
| 64 | # |
| 65 | # |
| 66 | |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 67 | class NoneClass(object): |
| 68 | def is_instanceof(self, x): |
| 69 | return False |
| 70 | none_item = NoneClass() |
| 71 | |
| 72 | def _unified_by_name(cls): |
| 73 | c = loxi_globals.unified.class_by_name(cls) |
| 74 | return c if c is not None else none_item |
| 75 | |
| 76 | @memoize |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 77 | def class_is_message(cls): |
| 78 | """ |
| 79 | Return True if cls is a message object based on info in unified |
| 80 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 81 | if cls == "of_header": |
| 82 | return False |
| 83 | else: |
| 84 | return _unified_by_name(cls).is_instanceof("of_header") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 85 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 86 | def class_is_oxm(cls): |
| 87 | """ |
| 88 | Return True if cls_name is an OXM object |
| 89 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 90 | return _unified_by_name(cls).is_instanceof("of_oxm") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 91 | |
Murat Parlakisik | f95672c | 2016-12-05 00:53:17 -0800 | [diff] [blame^] | 92 | def class_is_oxs(cls): |
| 93 | """ |
| 94 | Return True if cls_name is an OXS object |
| 95 | """ |
| 96 | return _unified_by_name(cls).is_instanceof("of_oxs") |
| 97 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 98 | def class_is_action(cls): |
| 99 | """ |
| 100 | Return True if cls_name is an action object |
| 101 | |
| 102 | Note that action_id is not an action object, though it has |
| 103 | the same header. It looks like an action header, but the type |
| 104 | is used to identify a kind of action, it does not indicate the |
| 105 | type of the object following. |
| 106 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 107 | return _unified_by_name(cls).is_instanceof("of_action") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 108 | |
| 109 | def class_is_action_id(cls): |
| 110 | """ |
| 111 | Return True if cls_name is an action object |
| 112 | |
| 113 | Note that action_id is not an action object, though it has |
| 114 | the same header. It looks like an action header, but the type |
| 115 | is used to identify a kind of action, it does not indicate the |
| 116 | type of the object following. |
| 117 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 118 | return _unified_by_name(cls).is_instanceof("of_action_id") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 119 | |
| 120 | def class_is_instruction(cls): |
| 121 | """ |
| 122 | Return True if cls_name is an instruction object |
| 123 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 124 | return _unified_by_name(cls).is_instanceof("of_instruction") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 125 | |
| 126 | def class_is_meter_band(cls): |
| 127 | """ |
| 128 | Return True if cls_name is an instruction object |
| 129 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 130 | return _unified_by_name(cls).is_instanceof("of_meter_band") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 131 | |
| 132 | def class_is_hello_elem(cls): |
| 133 | """ |
| 134 | Return True if cls_name is an instruction object |
| 135 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 136 | return _unified_by_name(cls).is_instanceof("of_hello_elem") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 137 | |
| 138 | def class_is_queue_prop(cls): |
| 139 | """ |
| 140 | Return True if cls_name is a queue_prop object |
| 141 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 142 | return _unified_by_name(cls).is_instanceof("of_queue_prop") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 143 | |
| 144 | def class_is_table_feature_prop(cls): |
| 145 | """ |
| 146 | Return True if cls_name is a queue_prop object |
| 147 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 148 | return _unified_by_name(cls).is_instanceof("of_table_feature_prop") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 149 | |
| 150 | def class_is_stats_message(cls): |
| 151 | """ |
| 152 | Return True if cls_name is a message object based on info in unified |
| 153 | """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 154 | u = _unified_by_name(cls) |
| 155 | return u.is_instanceof("of_stats_request") or u.ir_instanceof("of_stats_reply") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 156 | |
Rich Lane | 1373073 | 2013-12-03 13:05:19 -0800 | [diff] [blame] | 157 | def class_is_bsn_tlv(cls): |
| 158 | """ |
| 159 | Return True if cls_name is a bsn_tlv object |
| 160 | """ |
| 161 | return _unified_by_name(cls).is_instanceof("of_bsn_tlv") |
| 162 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 163 | def class_is_list(cls): |
| 164 | """ |
| 165 | Return True if cls_name is a list object |
| 166 | """ |
| 167 | return (cls.find("of_list_") == 0) |
| 168 | |
Andreas Wundsam | d4b2269 | 2014-01-14 14:17:26 -0800 | [diff] [blame] | 169 | def class_is(cls, cand_name): |
| 170 | return _unified_by_name(cls).is_instanceof(cand_name) |
| 171 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 172 | def type_is_of_object(m_type): |
| 173 | """ |
| 174 | Return True if m_type is an OF object type |
| 175 | """ |
| 176 | # Remove _t from the type id and see if key for unified class |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 177 | return _unified_by_name(re.sub(r'_t$', '', m_type)) != none_item |
Rich Lane | c340517 | 2013-02-20 18:18:57 -0800 | [diff] [blame] | 178 | |
Andreas Wundsam | 37c7bc2 | 2013-09-17 13:48:35 -0700 | [diff] [blame] | 179 | @memoize |
| 180 | def lookup_ir_wiretype(oftype, version): |
| 181 | """ if of is a reference to an enum in ir, resolve it to the wiretype |
| 182 | declared in that enum. Else return oftype """ |
Andreas Wundsam | c94e08c | 2013-11-15 13:37:59 -0800 | [diff] [blame] | 183 | enums = loxi_globals.ir[version].enums |
Andreas Wundsam | 37c7bc2 | 2013-09-17 13:48:35 -0700 | [diff] [blame] | 184 | enum = find(lambda e: e.name == oftype, enums) |
| 185 | if enum and 'wire_type' in enum.params: |
| 186 | return enum.params['wire_type'] |
| 187 | else: |
| 188 | return oftype |
Rich Lane | 2cc2b86 | 2014-06-13 14:50:17 -0700 | [diff] [blame] | 189 | |
| 190 | def oftype_is_list(oftype): |
| 191 | return (oftype.find("list(") == 0) |
| 192 | |
| 193 | # Converts "list(of_flow_stats_entry_t)" to "of_flow_stats_entry" |
| 194 | def oftype_list_elem(oftype): |
| 195 | assert oftype.find("list(") == 0 |
| 196 | return oftype[5:-3] |