Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 42d1821

Browse files
authored
Merge pull request #504 from diraol/fix_header_xid
Fix repeated header XID
2 parents 1eeec4a + 5d8a5b4 commit 42d1821

4 files changed

Lines changed: 5 additions & 15 deletions

File tree

pyof/foundation/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
from collections import OrderedDict
2323
from copy import deepcopy
2424
from enum import Enum, IntEnum
25+
from random import randint
2526

26-
# Local source tree imports
27+
from pyof.foundation.constants import UBINT32_MAX_VALUE as MAXID
2728
from pyof.foundation.exceptions import (
2829
BadValueException, PackException, UnpackException, ValidationError)
2930

@@ -747,8 +748,7 @@ class GenericMessage(GenericStruct):
747748
def __init__(self, xid=None):
748749
"""Initialize header's xid."""
749750
super().__init__()
750-
if xid is not None:
751-
self.header.xid = xid
751+
self.header.xid = randint(0, MAXID) if xid is None else xid
752752

753753
def __init_subclass__(cls, **kwargs):
754754
if cls.header is None or cls.header.__class__.__name__ != 'Header':

pyof/v0x01/common/header.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
# System imports
44
from enum import IntEnum
5-
from random import randint
65

76
# Local source tree imports
87
from pyof.foundation.base import GenericStruct
98
from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32
10-
from pyof.foundation.constants import UBINT32_MAX_VALUE as MAXID
119
from pyof.v0x01.common.constants import OFP_VERSION
1210

1311
# Third-party imports
@@ -84,4 +82,4 @@ def __init__(self, message_type=None, length=None, xid=None):
8482
super().__init__()
8583
self.message_type = message_type
8684
self.length = length
87-
self.xid = randint(0, MAXID) if xid is None else xid
85+
self.xid = xid

pyof/v0x04/common/header.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
# System imports
44
from enum import IntEnum
5-
from random import randint
65

76
# Local source tree imports
87
from pyof.foundation.base import GenericStruct
98
from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32
10-
from pyof.foundation.constants import UBINT32_MAX_VALUE as MAXID
119
from pyof.v0x04.common.constants import OFP_VERSION
1210

1311
# Third-party imports
@@ -90,7 +88,7 @@ class Header(GenericStruct):
9088
length = UBInt16()
9189
xid = UBInt32()
9290

93-
def __init__(self, message_type=None, length=None, xid=randint(0, MAXID)):
91+
def __init__(self, message_type=None, length=None, xid=None):
9492
"""Create a Header with the optional parameters below.
9593
9694
Args:

tests/v0x01/test_common/test_header.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,3 @@ def test_unpack(self):
4444
self.assertEqual(self.message.version, 1)
4545

4646
f.close()
47-
48-
@patch('pyof.v0x01.common.header.randint')
49-
def test_random_xid(self, m):
50-
"""Each Header instantiations without xid should call randint."""
51-
Header(), Header() # noqa
52-
self.assertEqual(m.call_count, 2)

0 commit comments

Comments
 (0)