Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 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 | """ |
| 18 | Convert loxi objects to openflow_13 messages and back. |
| 19 | """ |
| 20 | from copy import copy |
| 21 | |
| 22 | from google.protobuf.descriptor import FieldDescriptor |
| 23 | |
| 24 | import loxi.of13 as of13 |
| 25 | from protobuf_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP |
| 26 | from protos import openflow_13_pb2 as pb2 |
| 27 | |
| 28 | |
| 29 | type_callable_map = copy(TYPE_CALLABLE_MAP) |
| 30 | type_callable_map.update({ |
| 31 | FieldDescriptor.TYPE_STRING: str |
| 32 | }) |
| 33 | |
| 34 | def pb2dict(pb): |
| 35 | """ |
| 36 | Convert protobuf to a dict of values good for instantiating |
| 37 | loxi objects (or any other objects). We specialize the protobuf_to_dict |
| 38 | library call with our modified decoders. |
| 39 | :param pb: protobuf as loaded into Python |
| 40 | :return: dict of values |
| 41 | """ |
| 42 | return protobuf_to_dict(pb, type_callable_map) |
| 43 | |
| 44 | def to_loxi(grpc_object): |
| 45 | cls = grpc_object.__class__ |
| 46 | converter = to_loxi_converters[cls] |
| 47 | return converter(grpc_object) |
| 48 | |
| 49 | def to_grpc(loxi_object): |
| 50 | cls = loxi_object.__class__ |
| 51 | converter = to_grpc_converters[cls] |
| 52 | return converter(loxi_object) |
| 53 | |
| 54 | def ofp_port_to_loxi_port_desc(pb): |
| 55 | kw = pb2dict(pb) |
| 56 | return of13.common.port_desc(**kw) |
| 57 | |
| 58 | def ofp_flow_stats_to_loxi_flow_stats(pb): |
| 59 | kw = pb2dict(pb) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 60 | |
| 61 | def make_loxi_match(match): |
| 62 | assert match['type'] == pb2.OFPMT_OXM |
| 63 | loxi_match_fields = [] |
| 64 | for oxm_field in match['oxm_fields']: |
| 65 | assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC |
| 66 | ofb_field = oxm_field['ofb_field'] |
| 67 | field_type = ofb_field.get('type', 0) |
| 68 | if field_type == pb2.OFPXMT_OFB_ETH_TYPE: |
| 69 | loxi_match_fields.append( |
| 70 | of13.oxm.eth_type(value=ofb_field['eth_type'])) |
| 71 | else: |
| 72 | raise NotImplementedError( |
| 73 | 'OXM match field for type %s' % field_type) |
| 74 | return of13.match_v3(oxm_list=loxi_match_fields) |
| 75 | |
| 76 | def make_loxi_action(a): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 77 | type = a.get('type', 0) |
| 78 | if type == pb2.OFPAT_OUTPUT: |
| 79 | output = a['output'] |
| 80 | return of13.action.output(**output) |
| 81 | else: |
| 82 | raise NotImplementedError( |
| 83 | 'Action decoder for action OFPAT_* %d' % type) |
| 84 | |
| 85 | def make_loxi_instruction(inst): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 86 | type = inst['type'] |
| 87 | if type == pb2.OFPIT_APPLY_ACTIONS: |
| 88 | return of13.instruction.apply_actions( |
| 89 | actions=[make_loxi_action(a) |
| 90 | for a in inst['actions']['actions']]) |
| 91 | else: |
| 92 | raise NotImplementedError('Instruction type %d' % type) |
| 93 | |
| 94 | kw['match'] = make_loxi_match(kw['match']) |
| 95 | kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']] |
| 96 | return of13.flow_stats_entry(**kw) |
| 97 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 98 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 99 | to_loxi_converters = { |
| 100 | pb2.ofp_port: ofp_port_to_loxi_port_desc, |
| 101 | pb2.ofp_flow_stats: ofp_flow_stats_to_loxi_flow_stats |
| 102 | } |
| 103 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 104 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 105 | def loxi_flow_mod_to_ofp_flow_mod(lo): |
| 106 | return pb2.ofp_flow_mod( |
| 107 | cookie=lo.cookie, |
| 108 | cookie_mask=lo.cookie_mask, |
| 109 | table_id=lo.table_id, |
| 110 | command=lo._command, |
| 111 | idle_timeout=lo.idle_timeout, |
| 112 | hard_timeout=lo.hard_timeout, |
| 113 | priority=lo.priority, |
| 114 | buffer_id=lo.buffer_id, |
| 115 | out_port=lo.out_port, |
| 116 | out_group=lo.out_group, |
| 117 | flags=lo.flags, |
| 118 | match=to_grpc(lo.match), |
| 119 | instructions=[to_grpc(i) for i in lo.instructions]) |
| 120 | |
| 121 | |
| 122 | def loxi_group_mod_to_ofp_group_mod(lo): |
| 123 | return pb2.ofp_group_mod( |
| 124 | command=lo.command, |
Zsolt Haraszti | 9125b1a | 2016-10-24 22:54:33 -0700 | [diff] [blame] | 125 | type=lo.group_type, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 126 | group_id=lo.group_id, |
| 127 | buckets=[to_grpc(b) for b in lo.buckets]) |
| 128 | |
| 129 | |
| 130 | def loxi_match_v3_to_ofp_match(lo): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 131 | return pb2.ofp_match( |
| 132 | type=pb2.OFPMT_OXM, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 133 | oxm_fields=[to_grpc(f) for f in lo.oxm_list]) |
| 134 | |
| 135 | |
| 136 | def loxi_bucket_to_ofp_bucket(lo): |
| 137 | return pb2.ofp_bucket( |
| 138 | weight=lo.weight, |
| 139 | watch_port=lo.watch_port, |
| 140 | watch_group=lo.watch_group, |
| 141 | actions=[to_grpc(a) for a in lo.actions] |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 142 | ) |
| 143 | |
| 144 | def loxi_oxm_eth_type_to_ofp_oxm(lo): |
| 145 | return pb2.ofp_oxm_field( |
| 146 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 147 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 148 | type=pb2.OFPXMT_OFB_ETH_TYPE, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 149 | eth_type=lo.value)) |
| 150 | |
| 151 | |
| 152 | def loxi_oxm_in_port_to_ofp_oxm(lo): |
| 153 | return pb2.ofp_oxm_field( |
| 154 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 155 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 156 | type=pb2.OFPXMT_OFB_IN_PORT, |
| 157 | port=lo.value)) |
| 158 | |
| 159 | |
| 160 | def loxi_oxm_ip_proto_to_ofp_oxm(lo): |
| 161 | return pb2.ofp_oxm_field( |
| 162 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 163 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 164 | type=pb2.OFPXMT_OFB_IP_PROTO, |
| 165 | ip_proto=lo.value)) |
| 166 | |
| 167 | |
| 168 | def loxi_oxm_vlan_vid_to_ofp_oxm(lo): |
| 169 | return pb2.ofp_oxm_field( |
| 170 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 171 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 172 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 173 | vlan_vid=lo.value)) |
| 174 | |
| 175 | |
| 176 | def loxi_oxm_vlan_pcp_to_ofp_oxm(lo): |
| 177 | return pb2.ofp_oxm_field( |
| 178 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 179 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 180 | type=pb2.OFPXMT_OFB_VLAN_PCP, |
| 181 | vlan_pcp=lo.value)) |
| 182 | |
| 183 | |
| 184 | def loxi_oxm_ipv4_dst_to_ofp_oxm(lo): |
| 185 | return pb2.ofp_oxm_field( |
| 186 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 187 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 188 | type=pb2.OFPXMT_OFB_IPV4_DST, |
| 189 | ipv4_dst=lo.value)) |
| 190 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 191 | |
| 192 | def loxi_apply_actions_to_ofp_instruction(lo): |
| 193 | return pb2.ofp_instruction( |
| 194 | type=pb2.OFPIT_APPLY_ACTIONS, |
| 195 | actions=pb2.ofp_instruction_actions( |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 196 | actions=[to_grpc(a) for a in lo.actions])) |
| 197 | |
| 198 | |
| 199 | def loxi_goto_table_to_ofp_instruction(lo): |
| 200 | return pb2.ofp_instruction( |
| 201 | type=pb2.OFPIT_GOTO_TABLE, |
| 202 | goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id)) |
| 203 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 204 | |
| 205 | def loxi_output_action_to_ofp_action(lo): |
| 206 | return pb2.ofp_action( |
| 207 | type=pb2.OFPAT_OUTPUT, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 208 | output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len)) |
| 209 | |
| 210 | |
| 211 | def loxi_group_action_to_ofp_action(lo): |
| 212 | return pb2.ofp_action( |
| 213 | type=pb2.OFPAT_GROUP, |
| 214 | group=pb2.ofp_action_group(group_id=lo.group_id)) |
| 215 | |
| 216 | |
| 217 | def loxi_set_field_action_to_ofp_action(lo): |
| 218 | return pb2.ofp_action( |
| 219 | type=pb2.OFPAT_SET_FIELD, |
| 220 | set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field))) |
| 221 | |
| 222 | |
| 223 | def loxi_pop_vlan_action_to_ofp_action(lo): |
| 224 | return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN) |
| 225 | |
| 226 | |
| 227 | def loxi_push_vlan_action_to_ofp_action(lo): |
| 228 | return pb2.ofp_action( |
| 229 | type=pb2.OFPAT_PUSH_VLAN, |
| 230 | push=pb2.ofp_action_push(ethertype=lo.ethertype)) |
| 231 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 232 | |
| 233 | to_grpc_converters = { |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 234 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 235 | of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 236 | of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod, |
| 237 | of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 238 | of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod, |
| 239 | of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 240 | |
| 241 | of13.message.group_add: loxi_group_mod_to_ofp_group_mod, |
| 242 | of13.message.group_delete: loxi_group_mod_to_ofp_group_mod, |
| 243 | of13.message.group_modify: loxi_group_mod_to_ofp_group_mod, |
| 244 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 245 | of13.common.match_v3: loxi_match_v3_to_ofp_match, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 246 | of13.common.bucket: loxi_bucket_to_ofp_bucket, |
| 247 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 248 | of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 249 | of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm, |
| 250 | of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm, |
| 251 | of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm, |
| 252 | of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm, |
| 253 | of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm, |
| 254 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 255 | of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 256 | of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction, |
| 257 | |
| 258 | of13.action.output: loxi_output_action_to_ofp_action, |
| 259 | of13.action.group: loxi_group_action_to_ofp_action, |
| 260 | of13.action.set_field: loxi_set_field_action_to_ofp_action, |
| 261 | of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action, |
| 262 | of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action, |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 263 | } |