blob: f18d07e75e7ed73df9a41c333f59619d8cabac0e [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#
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"""
18Convert loxi objects to openflow_13 messages and back.
19"""
20from copy import copy
21
22from google.protobuf.descriptor import FieldDescriptor
23
24import loxi.of13 as of13
25from protobuf_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP
26from protos import openflow_13_pb2 as pb2
27
28
29type_callable_map = copy(TYPE_CALLABLE_MAP)
30type_callable_map.update({
31 FieldDescriptor.TYPE_STRING: str
32})
33
34def 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
44def to_loxi(grpc_object):
45 cls = grpc_object.__class__
46 converter = to_loxi_converters[cls]
47 return converter(grpc_object)
48
49def to_grpc(loxi_object):
50 cls = loxi_object.__class__
51 converter = to_grpc_converters[cls]
52 return converter(loxi_object)
53
54def ofp_port_to_loxi_port_desc(pb):
55 kw = pb2dict(pb)
56 return of13.common.port_desc(**kw)
57
58def ofp_flow_stats_to_loxi_flow_stats(pb):
59 kw = pb2dict(pb)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070060
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 Haraszti023ea7c2016-10-16 19:30:34 -070077 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 Haraszti023ea7c2016-10-16 19:30:34 -070086 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 Haraszti8a774382016-10-24 18:25:54 -070098
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070099to_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 Haraszti023ea7c2016-10-16 19:30:34 -0700104
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700105def 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
122def loxi_group_mod_to_ofp_group_mod(lo):
123 return pb2.ofp_group_mod(
124 command=lo.command,
125 type=lo.type,
126 group_id=lo.group_id,
127 buckets=[to_grpc(b) for b in lo.buckets])
128
129
130def loxi_match_v3_to_ofp_match(lo):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700131 return pb2.ofp_match(
132 type=pb2.OFPMT_OXM,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700133 oxm_fields=[to_grpc(f) for f in lo.oxm_list])
134
135
136def 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 Haraszti023ea7c2016-10-16 19:30:34 -0700142 )
143
144def 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 Haraszti8a774382016-10-24 18:25:54 -0700149 eth_type=lo.value))
150
151
152def 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
160def 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
168def 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
176def 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
184def 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 Haraszti023ea7c2016-10-16 19:30:34 -0700191
192def 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 Haraszti8a774382016-10-24 18:25:54 -0700196 actions=[to_grpc(a) for a in lo.actions]))
197
198
199def 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 Haraszti023ea7c2016-10-16 19:30:34 -0700204
205def loxi_output_action_to_ofp_action(lo):
206 return pb2.ofp_action(
207 type=pb2.OFPAT_OUTPUT,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700208 output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len))
209
210
211def 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
217def 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
223def loxi_pop_vlan_action_to_ofp_action(lo):
224 return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN)
225
226
227def 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 Haraszti023ea7c2016-10-16 19:30:34 -0700232
233to_grpc_converters = {
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700234
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700235 of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700236 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 Haraszti023ea7c2016-10-16 19:30:34 -0700245 of13.common.match_v3: loxi_match_v3_to_ofp_match,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700246 of13.common.bucket: loxi_bucket_to_ofp_bucket,
247
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700248 of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700249 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 Haraszti023ea7c2016-10-16 19:30:34 -0700255 of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700256 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 Haraszti023ea7c2016-10-16 19:30:34 -0700263}