MatchNwProto.from_of_tlv()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 2
dl 0
loc 5
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 10
c 0
b 0
f 0
1
"""OpenFlow 1.3 OXM match fields.
2
3
Flow's match is very different from OF 1.0. Instead of always having all
4
fields, there's a variable list of match fields and each one is an Openflow
5
eXtended Match Type-Length-Value (OXM TLV) element.
6
7
This module provides high-level Python classes for OXM TLV fields in order to
8
make the OF 1.3 match fields easy to use and to be coded.
9
"""
10
11 1
from pyof.foundation.basic_types import HWAddress, IPAddress
12 1
from pyof.v0x04.common.flow_match import OxmOfbMatchField, OxmTLV, VlanId
13
14
# pylint: disable=unused-import
15 1
from napps.kytos.of_core.v0x04.match_fields_base import (MatchField,
16
                                                         MatchFieldFactory)
17 1
from napps.kytos.of_core.v0x04.match_fields_ipv6 import (MatchEXTHDR,
18
                                                         MatchICMPV6Code,
19
                                                         MatchICMPV6Type,
20
                                                         MatchIPv6Dst,
21
                                                         MatchIPv6FLabel,
22
                                                         MatchIPv6Src,
23
                                                         MatchNDSLL,
24
                                                         MatchNDTarget,
25
                                                         MatchNDTLL)
26
# pylint: enable=unused-import
27 1
from napps.kytos.of_core.v0x04.utils import bytes_to_mask, mask_to_bytes
28
29
30 1
class MatchDLVLAN(MatchField):
31
    """Match for datalink VLAN ID."""
32
33 1
    name = 'dl_vlan'
34 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_VLAN_VID
35
36 1
    def as_of_tlv(self):
37
        """Return a pyof OXM TLV instance."""
38
        try:
39
            value = int(self.value)
40
            mask = None
41
            oxm_hasmask = False
42
        except ValueError:
43
            value, mask = map(int, self.value.split('/'))
44
            oxm_hasmask = True
45
        value = value | VlanId.OFPVID_PRESENT
46
        value_bytes = value.to_bytes(2, 'big')
47
        if mask:
48
            mask = mask | VlanId.OFPVID_PRESENT
49
            value_bytes += mask.to_bytes(2, 'big')
50
        return OxmTLV(oxm_field=self.oxm_field,
51
                      oxm_hasmask=oxm_hasmask,
52
                      oxm_value=value_bytes)
53
54 1
    @classmethod
55
    def from_of_tlv(cls, tlv):
56
        """Return an instance from a pyof OXM TLV."""
57
        vlan_id = int.from_bytes(tlv.oxm_value[:2], 'big') & 4095
58
        value = vlan_id
59
        if tlv.oxm_hasmask:
60
            vlan_mask = int.from_bytes(tlv.oxm_value[2:], 'big') & 4095
61
            value = f'{vlan_id}/{vlan_mask}'
62
        return cls(value)
63
64
65 1
class MatchDLVLANPCP(MatchField):
66
    """Match for VLAN Priority Code Point."""
67
68 1
    name = 'dl_vlan_pcp'
69 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_VLAN_PCP
70
71 1
    def as_of_tlv(self):
72
        """Return a pyof OXM TLV instance."""
73
        value_bytes = self.value.to_bytes(1, 'big')
74
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
75
76 1
    @classmethod
77
    def from_of_tlv(cls, tlv):
78
        """Return an instance from a pyof OXM TLV."""
79
        priority = int.from_bytes(tlv.oxm_value, 'big')
80
        return cls(priority)
81
82
83 1 View Code Duplication
class MatchDLSrc(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
    """Match for datalink source."""
85
86 1
    name = 'dl_src'
87 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_SRC
88
89 1
    def as_of_tlv(self):
90
        """Return a pyof OXM TLV instance."""
91 1
        if '/' in self.value:
92
            value, mask = self.value.split('/')
93
            mask = mask.upper()
94
            if mask == 'FF:FF:FF:FF:FF:FF':
95
                mask = None
96
                oxm_hasmask = False
97
            else:
98
                oxm_hasmask = True
99
        else:
100 1
            value = self.value
101 1
            mask = None
102 1
            oxm_hasmask = False
103 1
        value_bytes = HWAddress(value).pack()
104 1
        if mask:
105
            value_bytes += HWAddress(mask).pack()
106 1
        return OxmTLV(oxm_field=self.oxm_field,
107
                      oxm_hasmask=oxm_hasmask,
108
                      oxm_value=value_bytes)
109
110 1
    @classmethod
111
    def from_of_tlv(cls, tlv):
112
        """Return an instance from a pyof OXM TLV."""
113
        hw_address = HWAddress()
114
        hw_address.unpack(tlv.oxm_value)
115
        addr_str = str(hw_address)
116
        value = addr_str
117
        if tlv.oxm_hasmask:
118
            hw_mask = HWAddress()
119
            hw_mask.unpack(tlv.oxm_value[6:])
120
            mask_str = str(hw_mask)
121
            value = f'{addr_str}/{mask_str}'
122
        return cls(value)
123
124
125 1 View Code Duplication
class MatchDLDst(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
126
    """Match for datalink destination."""
127
128 1
    name = 'dl_dst'
129 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_DST
130
131 1
    def as_of_tlv(self):
132
        """Return a pyof OXM TLV instance."""
133
        if '/' in self.value:
134
            value, mask = self.value.split('/')
135
            mask = mask.upper()
136
            if mask == 'FF:FF:FF:FF:FF:FF':
137
                mask = None
138
                oxm_hasmask = False
139
            else:
140
                oxm_hasmask = True
141
        else:
142
            value = self.value
143
            mask = None
144
            oxm_hasmask = False
145
        value_bytes = HWAddress(value).pack()
146
        if mask:
147
            value_bytes += HWAddress(mask).pack()
148
        return OxmTLV(oxm_field=self.oxm_field,
149
                      oxm_hasmask=oxm_hasmask,
150
                      oxm_value=value_bytes)
151
152 1
    @classmethod
153
    def from_of_tlv(cls, tlv):
154
        """Return an instance from a pyof OXM TLV."""
155
        hw_address = HWAddress()
156
        hw_address.unpack(tlv.oxm_value)
157
        addr_str = str(hw_address)
158
        value = addr_str
159
        if tlv.oxm_hasmask:
160
            hw_mask = HWAddress()
161
            hw_mask.unpack(tlv.oxm_value[6:])
162
            mask_str = str(hw_mask)
163
            value = f'{addr_str}/{mask_str}'
164
        return cls(value)
165
166
167 1
class MatchDLType(MatchField):
168
    """Match for datalink type."""
169
170 1
    name = 'dl_type'
171 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_TYPE
172
173 1
    def as_of_tlv(self):
174
        """Return a pyof OXM TLV instance."""
175
        value_bytes = self.value.to_bytes(2, 'big')
176
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
177
178 1
    @classmethod
179
    def from_of_tlv(cls, tlv):
180
        """Return an instance from a pyof OXM TLV."""
181
        port = int.from_bytes(tlv.oxm_value, 'big')
182
        return cls(port)
183
184
185 1 View Code Duplication
class MatchNwSrc(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
186
    """Match for IPV4 source."""
187
188 1
    name = 'nw_src'
189 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV4_SRC
190
191 1
    def as_of_tlv(self):
192
        """Return a pyof OXM TLV instance."""
193
        ip_addr = IPAddress(self.value)
194
        value_bytes = ip_addr.pack()
195
        if ip_addr.netmask < 32:
196
            value_bytes += mask_to_bytes(ip_addr.netmask, 32)
197
        return OxmTLV(oxm_field=self.oxm_field,
198
                      oxm_hasmask=ip_addr.netmask < 32,
199
                      oxm_value=value_bytes)
200
201 1
    @classmethod
202
    def from_of_tlv(cls, tlv):
203
        """Return an instance from a pyof OXM TLV."""
204
        ip_address = IPAddress()
205
        ip_address.unpack(tlv.oxm_value)
206
        addr_str = str(ip_address)
207
        value = addr_str
208
        if tlv.oxm_hasmask:
209
            value = f'{addr_str}/{bytes_to_mask(tlv.oxm_value[4:], 32)}'
210
        return cls(value)
211
212
213 1 View Code Duplication
class MatchNwDst(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
214
    """Match for IPV4 destination."""
215
216 1
    name = 'nw_dst'
217 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV4_DST
218
219 1
    def as_of_tlv(self):
220
        """Return a pyof OXM TLV instance."""
221
        ip_addr = IPAddress(self.value)
222
        value_bytes = ip_addr.pack()
223
        if ip_addr.netmask < 32:
224
            value_bytes += mask_to_bytes(ip_addr.netmask, 32)
225
        return OxmTLV(oxm_field=self.oxm_field,
226
                      oxm_hasmask=ip_addr.netmask < 32,
227
                      oxm_value=value_bytes)
228
229 1
    @classmethod
230
    def from_of_tlv(cls, tlv):
231
        """Return an instance from a pyof OXM TLV."""
232
        ip_address = IPAddress()
233
        ip_address.unpack(tlv.oxm_value)
234
        addr_str = str(ip_address)
235
        value = addr_str
236
        if tlv.oxm_hasmask:
237
            value = f'{addr_str}/{bytes_to_mask(tlv.oxm_value[4:], 32)}'
238
        return cls(value)
239
240
241 1
class MatchNwProto(MatchField):
242
    """Match for IP protocol."""
243
244 1
    name = 'nw_proto'
245 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IP_PROTO
246
247 1
    def as_of_tlv(self):
248
        """Return a pyof OXM TLV instance."""
249
        value_bytes = self.value.to_bytes(1, 'big')
250
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
251
252 1
    @classmethod
253
    def from_of_tlv(cls, tlv):
254
        """Return an instance from a pyof OXM TLV."""
255
        priority = int.from_bytes(tlv.oxm_value, 'big')
256
        return cls(priority)
257
258
259 1
class MatchInPort(MatchField):
260
    """Match for input port."""
261
262 1
    name = 'in_port'
263 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IN_PORT
264
265 1
    def as_of_tlv(self):
266
        """Return a pyof OXM TLV instance."""
267
        value_bytes = self.value.to_bytes(4, 'big')
268
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
269
270 1
    @classmethod
271
    def from_of_tlv(cls, tlv):
272
        """Return an instance from a pyof OXM TLV."""
273
        port = int.from_bytes(tlv.oxm_value, 'big')
274
        return cls(port)
275
276
277 1
class MatchTCPSrc(MatchField):
278
    """Match for TCP source."""
279
280 1
    name = 'tp_src'
281 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_TCP_SRC
282
283 1
    def as_of_tlv(self):
284
        """Return a pyof OXM TLV instance."""
285
        value_bytes = self.value.to_bytes(2, 'big')
286
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
287
288 1
    @classmethod
289
    def from_of_tlv(cls, tlv):
290
        """Return an instance from a pyof OXM TLV."""
291
        port = int.from_bytes(tlv.oxm_value, 'big')
292
        return cls(port)
293
294
295 1
class MatchTCPDst(MatchField):
296
    """Match for TCP destination."""
297
298 1
    name = 'tp_dst'
299 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_TCP_DST
300
301 1
    def as_of_tlv(self):
302
        """Return a pyof OXM TLV instance."""
303
        value_bytes = self.value.to_bytes(2, 'big')
304
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
305
306 1
    @classmethod
307
    def from_of_tlv(cls, tlv):
308
        """Return an instance from a pyof OXM TLV."""
309
        port = int.from_bytes(tlv.oxm_value, 'big')
310
        return cls(port)
311
312
313 1
class MatchInPhyPort(MatchField):
314
    """Match for physical input port."""
315
316 1
    name = 'in_phy_port'
317 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT
318
319 1
    def as_of_tlv(self):
320
        """Return a pyof OXM TLV instance."""
321
        value_bytes = self.value.to_bytes(4, 'big')
322
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
323
324 1
    @classmethod
325
    def from_of_tlv(cls, tlv):
326
        """Return an instance from a pyof OXM TLV."""
327
        port = int.from_bytes(tlv.oxm_value, 'big')
328
        return cls(port)
329
330
331 1
class MatchIPDSCP(MatchField):
332
    """Match for IP DSCP."""
333
334 1
    name = 'ip_dscp'
335 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IP_DSCP
336
337 1
    def as_of_tlv(self):
338
        """Return a pyof OXM TLV instance."""
339
        value_bytes = self.value.to_bytes(1, 'big')
340
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
341
342 1
    @classmethod
343
    def from_of_tlv(cls, tlv):
344
        """Return an instance from a pyof OXM TLV."""
345
        value = int.from_bytes(tlv.oxm_value, 'big')
346
        return cls(value)
347
348
349 1
class MatchIPECN(MatchField):
350
    """Match for IP ECN."""
351
352 1
    name = 'ip_ecn'
353 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IP_ECN
354
355 1
    def as_of_tlv(self):
356
        """Return a pyof OXM TLV instance."""
357
        value_bytes = self.value.to_bytes(1, 'big')
358
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
359
360 1
    @classmethod
361
    def from_of_tlv(cls, tlv):
362
        """Return an instance from a pyof OXM TLV."""
363
        value = int.from_bytes(tlv.oxm_value, 'big')
364
        return cls(value)
365
366
367 1
class MatchUDPSrc(MatchField):
368
    """Match for UDP source."""
369
370 1
    name = 'udp_src'
371 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_UDP_SRC
372
373 1
    def as_of_tlv(self):
374
        """Return a pyof OXM TLV instance."""
375
        value_bytes = self.value.to_bytes(2, 'big')
376
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
377
378 1
    @classmethod
379
    def from_of_tlv(cls, tlv):
380
        """Return an instance from a pyof OXM TLV."""
381
        port = int.from_bytes(tlv.oxm_value, 'big')
382
        return cls(port)
383
384
385 1
class MatchUDPDst(MatchField):
386
    """Match for UDP destination."""
387
388 1
    name = 'udp_dst'
389 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_UDP_DST
390
391 1
    def as_of_tlv(self):
392
        """Return a pyof OXM TLV instance."""
393
        value_bytes = self.value.to_bytes(2, 'big')
394
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
395
396 1
    @classmethod
397
    def from_of_tlv(cls, tlv):
398
        """Return an instance from a pyof OXM TLV."""
399
        port = int.from_bytes(tlv.oxm_value, 'big')
400
        return cls(port)
401
402
403 1
class MatchSCTPSrc(MatchField):
404
    """Match for SCTP source."""
405
406 1
    name = 'sctp_src'
407 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_SCTP_SRC
408
409 1
    def as_of_tlv(self):
410
        """Return a pyof OXM TLV instance."""
411
        value_bytes = self.value.to_bytes(2, 'big')
412
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
413
414 1
    @classmethod
415
    def from_of_tlv(cls, tlv):
416
        """Return an instance from a pyof OXM TLV."""
417
        value = int.from_bytes(tlv.oxm_value, 'big')
418
        return cls(value)
419
420
421 1
class MatchSCTPDst(MatchField):
422
    """Match for SCTP destination."""
423
424 1
    name = 'sctp_dst'
425 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_SCTP_DST
426
427 1
    def as_of_tlv(self):
428
        """Return a pyof OXM TLV instance."""
429
        value_bytes = self.value.to_bytes(2, 'big')
430
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
431
432 1
    @classmethod
433
    def from_of_tlv(cls, tlv):
434
        """Return an instance from a pyof OXM TLV."""
435
        value = int.from_bytes(tlv.oxm_value, 'big')
436
        return cls(value)
437
438
439 1
class MatchICMPV4Type(MatchField):
440
    """Match for ICMPV4 type."""
441
442 1
    name = 'icmpv4_type'
443 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ICMPV4_TYPE
444
445 1
    def as_of_tlv(self):
446
        """Return a pyof OXM TLV instance."""
447
        value_bytes = self.value.to_bytes(1, 'big')
448
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
449
450 1
    @classmethod
451
    def from_of_tlv(cls, tlv):
452
        """Return an instance from a pyof OXM TLV."""
453
        port = int.from_bytes(tlv.oxm_value, 'big')
454
        return cls(port)
455
456
457 1
class MatchICMPV4Code(MatchField):
458
    """Match for ICMPV4 code."""
459
460 1
    name = 'icmpv4_code'
461 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ICMPV4_CODE
462
463 1
    def as_of_tlv(self):
464
        """Return a pyof OXM TLV instance."""
465
        value_bytes = self.value.to_bytes(1, 'big')
466
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
467
468 1
    @classmethod
469
    def from_of_tlv(cls, tlv):
470
        """Return an instance from a pyof OXM TLV."""
471
        priority = int.from_bytes(tlv.oxm_value, 'big')
472
        return cls(priority)
473
474
475 1
class MatchARPOP(MatchField):
476
    """Match for ARP opcode."""
477
478 1
    name = 'arp_op'
479 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ARP_OP
480
481 1
    def as_of_tlv(self):
482
        """Return a pyof OXM TLV instance."""
483
        value_bytes = self.value.to_bytes(2, 'big')
484
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
485
486 1
    @classmethod
487
    def from_of_tlv(cls, tlv):
488
        """Return an instance from a pyof OXM TLV."""
489
        opcode = int.from_bytes(tlv.oxm_value, 'big')
490
        return cls(opcode)
491
492
493 1 View Code Duplication
class MatchARPSPA(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
494
    """Match for ARP Sender IP Address."""
495
496 1
    name = 'arp_spa'
497 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ARP_SPA
498
499 1
    def as_of_tlv(self):
500
        """Return a pyof OXM TLV instance."""
501
        ip_addr = IPAddress(self.value)
502
        value_bytes = ip_addr.pack()
503
        if ip_addr.netmask < 32:
504
            value_bytes += mask_to_bytes(ip_addr.netmask, 32)
505
        return OxmTLV(oxm_field=self.oxm_field,
506
                      oxm_hasmask=ip_addr.netmask < 32,
507
                      oxm_value=value_bytes)
508
509 1
    @classmethod
510
    def from_of_tlv(cls, tlv):
511
        """Return an instance from a pyof OXM TLV."""
512
        ip_address = IPAddress()
513
        ip_address.unpack(tlv.oxm_value)
514
        addr_str = str(ip_address)
515
        value = addr_str
516
        if tlv.oxm_hasmask:
517
            value = f'{addr_str}/{bytes_to_mask(tlv.oxm_value[4:], 32)}'
518
        return cls(value)
519
520
521 1 View Code Duplication
class MatchARPTPA(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
522
    """Match for ARP Target IP Address."""
523
524 1
    name = 'arp_tpa'
525 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ARP_TPA
526
527 1
    def as_of_tlv(self):
528
        """Return a pyof OXM TLV instance."""
529
        ip_addr = IPAddress(self.value)
530
        value_bytes = ip_addr.pack()
531
        if ip_addr.netmask < 32:
532
            value_bytes += mask_to_bytes(ip_addr.netmask, 32)
533
        return OxmTLV(oxm_field=self.oxm_field,
534
                      oxm_hasmask=ip_addr.netmask < 32,
535
                      oxm_value=value_bytes)
536
537 1
    @classmethod
538
    def from_of_tlv(cls, tlv):
539
        """Return an instance from a pyof OXM TLV."""
540
        ip_address = IPAddress()
541
        ip_address.unpack(tlv.oxm_value)
542
        addr_str = str(ip_address)
543
        value = addr_str
544
        if tlv.oxm_hasmask:
545
            value = f'{addr_str}/{bytes_to_mask(tlv.oxm_value[4:], 32)}'
546
        return cls(value)
547
548
549 1 View Code Duplication
class MatchARPSHA(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
550
    """Match for ARP Sender MAC Address."""
551
552 1
    name = 'arp_sha'
553 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ARP_SHA
554
555 1
    def as_of_tlv(self):
556
        """Return a pyof OXM TLV instance."""
557
        if '/' in self.value:
558
            value, mask = self.value.split('/')
559
            mask = mask.upper()
560
            if mask == 'FF:FF:FF:FF:FF:FF':
561
                mask = None
562
                oxm_hasmask = False
563
            else:
564
                oxm_hasmask = True
565
        else:
566
            value = self.value
567
            mask = None
568
            oxm_hasmask = False
569
        value_bytes = HWAddress(value).pack()
570
        if mask:
571
            value_bytes += HWAddress(mask).pack()
572
        return OxmTLV(oxm_field=self.oxm_field,
573
                      oxm_hasmask=oxm_hasmask,
574
                      oxm_value=value_bytes)
575
576 1
    @classmethod
577
    def from_of_tlv(cls, tlv):
578
        """Return an instance from a pyof OXM TLV."""
579
        hw_address = HWAddress()
580
        hw_address.unpack(tlv.oxm_value)
581
        addr_str = str(hw_address)
582
        value = addr_str
583
        if tlv.oxm_hasmask:
584
            hw_mask = HWAddress()
585
            hw_mask.unpack(tlv.oxm_value[6:])
586
            mask_str = str(hw_mask)
587
            value = f'{addr_str}/{mask_str}'
588
        return cls(value)
589
590
591 1 View Code Duplication
class MatchARPTHA(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
592
    """Match for ARP Target MAC Address."""
593
594 1
    name = 'arp_tha'
595 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ARP_THA
596
597 1
    def as_of_tlv(self):
598
        """Return a pyof OXM TLV instance."""
599
        if '/' in self.value:
600
            value, mask = self.value.split('/')
601
            mask = mask.upper()
602
            if mask == 'FF:FF:FF:FF:FF:FF':
603
                mask = None
604
                oxm_hasmask = False
605
            else:
606
                oxm_hasmask = True
607
        else:
608
            value = self.value
609
            mask = None
610
            oxm_hasmask = False
611
        value_bytes = HWAddress(value).pack()
612
        if mask:
613
            value_bytes += HWAddress(mask).pack()
614
        return OxmTLV(oxm_field=self.oxm_field,
615
                      oxm_hasmask=oxm_hasmask,
616
                      oxm_value=value_bytes)
617
618 1
    @classmethod
619
    def from_of_tlv(cls, tlv):
620
        """Return an instance from a pyof OXM TLV."""
621
        hw_address = HWAddress()
622
        hw_address.unpack(tlv.oxm_value)
623
        addr_str = str(hw_address)
624
        value = addr_str
625
        if tlv.oxm_hasmask:
626
            hw_mask = HWAddress()
627
            hw_mask.unpack(tlv.oxm_value[6:])
628
            mask_str = str(hw_mask)
629
            value = f'{addr_str}/{mask_str}'
630
        return cls(value)
631
632
633 1
class MatchMPLSLabel(MatchField):
634
    """Match for MPLS Label."""
635
636 1
    name = 'mpls_lab'
637 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_MPLS_LABEL
638
639 1
    def as_of_tlv(self):
640
        """Return a pyof OXM TLV instance."""
641
        value_bytes = self.value.to_bytes(4, 'big')
642
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
643
644 1
    @classmethod
645
    def from_of_tlv(cls, tlv):
646
        """Return an instance from a pyof OXM TLV."""
647
        lab = int.from_bytes(tlv.oxm_value, 'big')
648
        return cls(lab)
649
650
651 1
class MatchMPLSTC(MatchField):
652
    """Match for MPLS TC."""
653
654 1
    name = 'mpls_tc'
655 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_MPLS_TC
656
657 1
    def as_of_tlv(self):
658
        """Return a pyof OXM TLV instance."""
659
        value_bytes = self.value.to_bytes(1, 'big')
660
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
661
662 1
    @classmethod
663
    def from_of_tlv(cls, tlv):
664
        """Return an instance from a pyof OXM TLV."""
665
        value = int.from_bytes(tlv.oxm_value, 'big')
666
        return cls(value)
667
668
669 1
class MatchMPLSBOS(MatchField):
670
    """Match for MPLS BOS."""
671
672 1
    name = 'mpls_bos'
673 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFP_MPLS_BOS
674
675 1
    def as_of_tlv(self):
676
        """Return a pyof OXM TLV instance."""
677
        value_bytes = self.value.to_bytes(1, 'big')
678
        return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes)
679
680 1
    @classmethod
681
    def from_of_tlv(cls, tlv):
682
        """Return an instance from a pyof OXM TLV."""
683
        bos = int.from_bytes(tlv.oxm_value, 'big')
684
        return cls(bos)
685
686
687 1 View Code Duplication
class MatchPBBISID(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
688
    """Match for PBB ISID."""
689
690 1
    name = 'pbb_isid'
691 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_PBB_ISID
692
693 1
    def as_of_tlv(self):
694
        """Return a pyof OXM TLV instance."""
695
        try:
696
            value = int(self.value)
697
            mask = None
698
            oxm_hasmask = False
699
        except ValueError:
700
            value, mask = map(int, self.value.split('/'))
701
            oxm_hasmask = True
702
        value_bytes = value.to_bytes(3, 'big')
703
        if mask:
704
            value_bytes += mask.to_bytes(3, 'big')
705
        return OxmTLV(oxm_field=self.oxm_field,
706
                      oxm_hasmask=oxm_hasmask,
707
                      oxm_value=value_bytes)
708
709 1
    @classmethod
710
    def from_of_tlv(cls, tlv):
711
        """Return an instance from a pyof OXM TLV."""
712
        value = int.from_bytes(tlv.oxm_value[:3], 'big')
713
        if tlv.oxm_hasmask:
714
            pbb_isid_mask = int.from_bytes(tlv.oxm_value[3:], 'big')
715
            value = f'{value}/{pbb_isid_mask}'
716
        return cls(value)
717
718
719 1 View Code Duplication
class MatchMetadata(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
720
    """Match for table metadata."""
721
722 1
    name = 'metadata'
723 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_METADATA
724
725 1
    def as_of_tlv(self):
726
        """Return a pyof OXM TLV instance."""
727
        try:
728
            value = int(self.value)
729
            mask = None
730
            oxm_hasmask = False
731
        except ValueError:
732
            value, mask = map(int, self.value.split('/'))
733
            oxm_hasmask = True
734
        value_bytes = value.to_bytes(8, 'big')
735
        if mask:
736
            value_bytes += mask.to_bytes(8, 'big')
737
        return OxmTLV(oxm_field=self.oxm_field,
738
                      oxm_hasmask=oxm_hasmask,
739
                      oxm_value=value_bytes)
740
741 1
    @classmethod
742
    def from_of_tlv(cls, tlv):
743
        """Return an instance from a pyof OXM TLV."""
744
        value = int.from_bytes(tlv.oxm_value[:8], 'big')
745
        if tlv.oxm_hasmask:
746
            metadata_mask = int.from_bytes(tlv.oxm_value[8:], 'big')
747
            value = f'{value}/{metadata_mask}'
748
        return cls(value)
749
750
751 1 View Code Duplication
class MatchTUNNELID(MatchField):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
752
    """Match for tunnel id."""
753
754 1
    name = 'tun_id'
755 1
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_TUNNEL_ID
756
757 1
    def as_of_tlv(self):
758
        """Return a pyof OXM TLV instance."""
759
        try:
760
            value = int(self.value)
761
            mask = None
762
            oxm_hasmask = False
763
        except ValueError:
764
            value, mask = map(int, self.value.split('/'))
765
            oxm_hasmask = True
766
        value_bytes = value.to_bytes(8, 'big')
767
        if mask:
768
            value_bytes += mask.to_bytes(8, 'big')
769
        return OxmTLV(oxm_field=self.oxm_field,
770
                      oxm_hasmask=oxm_hasmask,
771
                      oxm_value=value_bytes)
772
773 1
    @classmethod
774
    def from_of_tlv(cls, tlv):
775
        """Return an instance from a pyof OXM TLV."""
776
        value = int.from_bytes(tlv.oxm_value[:8], 'big')
777
        if tlv.oxm_hasmask:
778
            tunnel_mask = int.from_bytes(tlv.oxm_value[8:], 'big')
779
            value = f'{value}/{tunnel_mask}'
780
        return cls(value)
781