Code Duplication    Length = 30-30 lines in 5 locations

v0x04/match_fields.py 3 locations

@@ 748-777 (lines=30) @@
745
        return cls(value)
746
747
748
class MatchTUNNELID(MatchField):
749
    """Match for tunnel id."""
750
751
    name = 'tun_id'
752
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_TUNNEL_ID
753
754
    def as_of_tlv(self):
755
        """Return a pyof OXM TLV instance."""
756
        try:
757
            value = int(self.value)
758
            mask = None
759
            oxm_hasmask = False
760
        except ValueError:
761
            value, mask = map(int, self.value.split('/'))
762
            oxm_hasmask = True
763
        value_bytes = value.to_bytes(8, 'big')
764
        if mask:
765
            value_bytes += mask.to_bytes(8, 'big')
766
        return OxmTLV(oxm_field=self.oxm_field,
767
                      oxm_hasmask=oxm_hasmask,
768
                      oxm_value=value_bytes)
769
770
    @classmethod
771
    def from_of_tlv(cls, tlv):
772
        """Return an instance from a pyof OXM TLV."""
773
        value = int.from_bytes(tlv.oxm_value[:8], 'big')
774
        if tlv.oxm_hasmask:
775
            tunnel_mask = int.from_bytes(tlv.oxm_value[8:], 'big')
776
            value = f'{value}/{tunnel_mask}'
777
        return cls(value)
778
@@ 716-745 (lines=30) @@
713
714
715
716
class MatchMetadata(MatchField):
717
    """Match for table metadata."""
718
719
    name = 'metadata'
720
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_METADATA
721
722
    def as_of_tlv(self):
723
        """Return a pyof OXM TLV instance."""
724
        try:
725
            value = int(self.value)
726
            mask = None
727
            oxm_hasmask = False
728
        except ValueError:
729
            value, mask = map(int, self.value.split('/'))
730
            oxm_hasmask = True
731
        value_bytes = value.to_bytes(8, 'big')
732
        if mask:
733
            value_bytes += mask.to_bytes(8, 'big')
734
        return OxmTLV(oxm_field=self.oxm_field,
735
                      oxm_hasmask=oxm_hasmask,
736
                      oxm_value=value_bytes)
737
738
    @classmethod
739
    def from_of_tlv(cls, tlv):
740
        """Return an instance from a pyof OXM TLV."""
741
        value = int.from_bytes(tlv.oxm_value[:8], 'big')
742
        if tlv.oxm_hasmask:
743
            metadata_mask = int.from_bytes(tlv.oxm_value[8:], 'big')
744
            value = f'{value}/{metadata_mask}'
745
        return cls(value)
746
747
748
class MatchTUNNELID(MatchField):
@@ 682-711 (lines=30) @@
679
        return cls(bos)
680
681
682
class MatchPBBISID(MatchField):
683
    """Match for PBB ISID."""
684
685
    name = 'pbb_isid'
686
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_PBB_ISID
687
688
    def as_of_tlv(self):
689
        """Return a pyof OXM TLV instance."""
690
        try:
691
            value = int(self.value)
692
            mask = None
693
            oxm_hasmask = False
694
        except ValueError:
695
            value, mask = map(int, self.value.split('/'))
696
            oxm_hasmask = True
697
        value_bytes = value.to_bytes(3, 'big')
698
        if mask:
699
            value_bytes += mask.to_bytes(3, 'big')
700
        return OxmTLV(oxm_field=self.oxm_field,
701
                      oxm_hasmask=oxm_hasmask,
702
                      oxm_value=value_bytes)
703
704
    @classmethod
705
    def from_of_tlv(cls, tlv):
706
        """Return an instance from a pyof OXM TLV."""
707
        value = int.from_bytes(tlv.oxm_value[:3], 'big')
708
        if tlv.oxm_hasmask:
709
            pbb_isid_mask = int.from_bytes(tlv.oxm_value[3:], 'big')
710
            value = f'{value}/{pbb_isid_mask}'
711
        return cls(value)
712
713
714

v0x04/match_fields_ipv6.py 2 locations

@@ 188-217 (lines=30) @@
185
        return cls(tll)
186
187
188
class MatchEXTHDR(MatchField):
189
    """Match for IPv6 EXTHDR."""
190
191
    name = 'v6_hdr'
192
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_EXTHDR
193
194
    def as_of_tlv(self):
195
        """Return a pyof OXM TLV instance."""
196
        try:
197
            value = int(self.value)
198
            mask = None
199
            oxm_hasmask = False
200
        except ValueError:
201
            value, mask = map(int, self.value.split('/'))
202
            oxm_hasmask = True
203
        value_bytes = value.to_bytes(2, 'big')
204
        if mask:
205
            value_bytes += mask.to_bytes(2, 'big')
206
        return OxmTLV(oxm_field=self.oxm_field,
207
                      oxm_hasmask=oxm_hasmask,
208
                      oxm_value=value_bytes)
209
210
    @classmethod
211
    def from_of_tlv(cls, tlv):
212
        """Return an instance from a pyof OXM TLV."""
213
        value = int.from_bytes(tlv.oxm_value[:2], 'big')
214
        if tlv.oxm_hasmask:
215
            exhead_mask = int.from_bytes(tlv.oxm_value[2:], 'big')
216
            value = f'{value}/{exhead_mask}'
217
        return cls(value)
218
@@ 66-95 (lines=30) @@
63
        return cls(value)
64
65
66
class MatchIPv6FLabel(MatchField):
67
    """Match for IPv6 Flow Label."""
68
69
    name = 'ipv6_flabel'
70
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_FLABEL
71
72
    def as_of_tlv(self):
73
        """Return a pyof OXM TLV instance."""
74
        try:
75
            value = int(self.value)
76
            mask = None
77
            oxm_hasmask = False
78
        except ValueError:
79
            value, mask = map(int, self.value.split('/'))
80
            oxm_hasmask = True
81
        value_bytes = value.to_bytes(4, 'big')
82
        if mask:
83
            value_bytes += mask.to_bytes(4, 'big')
84
        return OxmTLV(oxm_field=self.oxm_field,
85
                      oxm_hasmask=oxm_hasmask,
86
                      oxm_value=value_bytes)
87
88
    @classmethod
89
    def from_of_tlv(cls, tlv):
90
        """Return an instance from a pyof OXM TLV."""
91
        value = int.from_bytes(tlv.oxm_value[:4], 'big')
92
        if tlv.oxm_hasmask:
93
            flabel_mask = int.from_bytes(tlv.oxm_value[4:], 'big')
94
            value = f'{value}/{flabel_mask}'
95
        return cls(value)
96
97
98
class MatchICMPV6Type(MatchField):