Code Duplication    Length = 39-39 lines in 2 locations

v0x04/match_fields.py 2 locations

@@ 165-203 (lines=39) @@
162
        return cls(value)
163
164
165
class MatchDLDst(MatchField):
166
    """Match for datalink destination."""
167
168
    name = 'dl_dst'
169
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_DST
170
171
    def as_of_tlv(self):
172
        """Return a pyof OXM TLV instance."""
173
        if '/' in self.value:
174
            value, mask = self.value.split('/')
175
            if mask.upper() == 'FF:FF:FF:FF:FF:FF':
176
                mask = None
177
                oxm_hasmask = False
178
            else:
179
                mask = mask.upper()
180
                oxm_hasmask = True
181
        else:
182
            value = self.value
183
            mask = None
184
            oxm_hasmask = False
185
        value_bytes = HWAddress(value).pack()
186
        if mask:
187
            value_bytes += HWAddress(mask).pack()
188
        return OxmTLV(oxm_field=self.oxm_field,
189
                      oxm_hasmask=oxm_hasmask,
190
                      oxm_value=value_bytes)
191
192
    @classmethod
193
    def from_of_tlv(cls, tlv):
194
        """Return an instance from a pyof OXM TLV."""
195
        hw_address = HWAddress()
196
        hw_address.unpack(tlv.oxm_value)
197
        addr_str = str(hw_address)
198
        value = addr_str
199
        if tlv.oxm_hasmask:
200
            hw_mask = HWAddress()
201
            hw_mask.unpack(tlv.oxm_value[6:])
202
            mask_str = str(hw_mask)
203
            value = f'{addr_str}/{mask_str}'
204
        return cls(value)
205
206
@@ 123-161 (lines=39) @@
120
        return cls(priority)
121
122
123
class MatchDLSrc(MatchField):
124
    """Match for datalink source."""
125
126
    name = 'dl_src'
127
    oxm_field = OxmOfbMatchField.OFPXMT_OFB_ETH_SRC
128
129
    def as_of_tlv(self):
130
        """Return a pyof OXM TLV instance."""
131
        if '/' in self.value:
132
            value, mask = self.value.split('/')
133
            if mask.upper() == 'FF:FF:FF:FF:FF:FF':
134
                mask = None
135
                oxm_hasmask = False
136
            else:
137
                mask = mask.upper()
138
                oxm_hasmask = True
139
        else:
140
            value = self.value
141
            mask = None
142
            oxm_hasmask = False
143
        value_bytes = HWAddress(value).pack()
144
        if mask:
145
            value_bytes += HWAddress(mask).pack()
146
        return OxmTLV(oxm_field=self.oxm_field,
147
                      oxm_hasmask=oxm_hasmask,
148
                      oxm_value=value_bytes)
149
150
    @classmethod
151
    def from_of_tlv(cls, tlv):
152
        """Return an instance from a pyof OXM TLV."""
153
        hw_address = HWAddress()
154
        hw_address.unpack(tlv.oxm_value)
155
        addr_str = str(hw_address)
156
        value = addr_str
157
        if tlv.oxm_hasmask:
158
            hw_mask = HWAddress()
159
            hw_mask.unpack(tlv.oxm_value[6:])
160
            mask_str = str(hw_mask)
161
            value = f'{addr_str}/{mask_str}'
162
        return cls(value)
163
164