diff --git a/pyof/v0x01/controller2switch/packet_out.py b/pyof/v0x01/controller2switch/packet_out.py index 5996d4712..ad906e331 100644 --- a/pyof/v0x01/controller2switch/packet_out.py +++ b/pyof/v0x01/controller2switch/packet_out.py @@ -112,13 +112,20 @@ def _update_actions_len(self): self.actions_len = ListOfActions(self.actions).get_size() def _validate_in_port(self): - port = self.in_port - valid = True - if isinstance(port, Port): - if port not in _VIRT_IN_PORTS: - valid = False - elif isinstance(port, int) and (port < 1 or port >= - Port.OFPP_MAX.value): - valid = False - if not valid: - raise ValidationError('{} is not a valid input port.'.format(port)) + """Validate in_port attribute. + + A valid port is either: + + * Greater than 0 and less than or equals to Port.OFPP_MAX + * One of the valid virtual ports: Port.OFPP_LOCAL, + Port.OFPP_CONTROLLER or Port.OFPP_NONE + + Raises: + ValidationError: If in_port is an invalid port. + + """ + is_valid_range = self.in_port > 0 and self.in_port <= Port.OFPP_MAX + is_valid_virtual_in_ports = self.in_port in _VIRT_IN_PORTS + + if (is_valid_range or is_valid_virtual_in_ports) is False: + raise ValidationError(f'{self.in_port} is not a valid input port.') diff --git a/tests/v0x01/test_controller2switch/test_packet_out.py b/tests/v0x01/test_controller2switch/test_packet_out.py index 00ea79783..7a277ee81 100644 --- a/tests/v0x01/test_controller2switch/test_packet_out.py +++ b/tests/v0x01/test_controller2switch/test_packet_out.py @@ -45,14 +45,14 @@ def test_invalid_virtual_in_ports(self): def test_valid_physical_in_ports(self): """Physical port limits from 1.0.0 spec.""" - max_valid = int(Port.OFPP_MAX.value) - 1 + max_valid = int(Port.OFPP_MAX.value) for in_port in (1, max_valid): self.message.in_port = in_port self.assertTrue(self.message.is_valid()) def test_invalid_physical_in_port(self): """Physical port limits from 1.0.0 spec.""" - max_valid = int(Port.OFPP_MAX.value) - 1 + max_valid = int(Port.OFPP_MAX.value) for in_port in (-1, 0, max_valid + 1, max_valid + 2): self.message.in_port = in_port self.assertFalse(self.message.is_valid())