Code Duplication    Length = 39-39 lines in 2 locations

v0x04/match_fields.py 2 locations

@@ 183-221 (lines=39) @@
180
        return cls(value)
181
182
183
class MatchDLDst(MatchField):
184
    """Match for datalink destination."""
185
186
    name = 'dl_dst'
187
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_DST
188
189
    def as_of_tlv(self):
190
        """Return a pyof OXM TLV instance."""
191
        if '/' in self.value:
192
            value, mask = self.value.split('/')
193
            if mask.upper() == 'FF:FF:FF:FF:FF:FF':
194
                mask = None
195
                oxm_hasmask = False
196
            else:
197
                mask = mask.upper()
198
                oxm_hasmask = True
199
        else:
200
            value = self.value
201
            mask = None
202
            oxm_hasmask = False
203
        value_bytes = HWAddress(value).pack()
204
        if mask:
205
            value_bytes += HWAddress(mask).pack()
206
        return OxmTLV(oxm_field=self.oxm_field, oxm_hasmask=oxm_hasmask, 
207
                        oxm_value=value_bytes)
208
209
    @classmethod
210
    def from_of_tlv(cls, tlv):
211
        """Return an instance from a pyof OXM TLV."""
212
        hw_address = HWAddress()
213
        hw_address.unpack(tlv.oxm_value)
214
        addr_str = str(hw_address)
215
        value = addr_str
216
        if tlv.oxm_hasmask:
217
            hw_mask = HWAddress()
218
            hw_mask.unpack(tlv.oxm_value[6:])
219
            mask_str = str(hw_mask)
220
            value = f'{addr_str}/{mask_str}'
221
        return cls(value)
222
223
224
class MatchDLType(MatchField):
@@ 142-180 (lines=39) @@
139
        return cls(priority)
140
141
142
class MatchDLSrc(MatchField):
143
    """Match for datalink source."""
144
145
    name = 'dl_src'
146
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_SRC
147
148
    def as_of_tlv(self):
149
        """Return a pyof OXM TLV instance."""
150
        if '/' in self.value:
151
            value, mask = self.value.split('/')
152
            if mask.upper() == 'FF:FF:FF:FF:FF:FF':
153
                mask = None
154
                oxm_hasmask = False
155
            else:
156
                mask = mask.upper()
157
                oxm_hasmask = True
158
        else:
159
            value = self.value
160
            mask = None
161
            oxm_hasmask = False
162
        value_bytes = HWAddress(value).pack()
163
        if mask:
164
            value_bytes += HWAddress(mask).pack()
165
        return OxmTLV(oxm_field=self.oxm_field, oxm_hasmask=oxm_hasmask, 
166
                        oxm_value=value_bytes)
167
168
    @classmethod
169
    def from_of_tlv(cls, tlv):
170
        """Return an instance from a pyof OXM TLV."""
171
        hw_address = HWAddress()
172
        hw_address.unpack(tlv.oxm_value)
173
        addr_str = str(hw_address)
174
        value = addr_str
175
        if tlv.oxm_hasmask:
176
            hw_mask = HWAddress()
177
            hw_mask.unpack(tlv.oxm_value[6:])
178
            mask_str = str(hw_mask)
179
            value = f'{addr_str}/{mask_str}'
180
        return cls(value)
181
182
183
class MatchDLDst(MatchField):