Exemple #1
0
    def pack(self, value=None):
        """Pack the struct in a binary representation.

        Iterate over the class attributes, according to the
        order of definition, and then convert each attribute to its byte
        representation using its own ``pack`` method.

        Returns:
            bytes: Binary representation of the struct object.

        Raises:
            :exc:`~.exceptions.ValidationError`: If validation fails.

        """
        if value is None:
            if not self.is_valid():
                error_msg = "Error on validation prior to pack() on class "
                error_msg += "{}.".format(type(self).__name__)
                raise ValidationError(error_msg)
            else:
                message = b''
                # pylint: disable=no-member
                for instance_attr, class_attr in self._get_attributes():
                    message += class_attr.pack(instance_attr)
                return message
        elif isinstance(value, type(self)):
            return value.pack()
        else:
            msg = "{} is not an instance of {}".format(value,
                                                       type(self).__name__)
            raise PackException(msg)
Exemple #2
0
 def _validate_in_port(self):
     port = self.in_port
     valid = True
     if isinstance(port, int) and (port < 1 or port >= Port.OFPP_MAX.value):
         valid = False
     elif isinstance(port, Port) and port not in _VIRT_IN_PORTS:
         valid = False
     if not valid:
         raise ValidationError('{} is not a valid input port.'.format(port))
Exemple #3
0
    def _validate_in_port(self):
        port = self.in_port

        if isinstance(port, PortNo):
            valid = port in _VIRT_IN_PORTS
        elif isinstance(port, int):
            # Must use "else" because PortNo is an IntEnum and (int instance)
            valid = port >= 1 and port < PortNo.OFPP_MAX.value
        elif isinstance(port, Port):
            valid = port.is_valid()
        else:  # unknown value
            valid = False

        if not valid:
            raise ValidationError('{} is not a valid input port.'.format(port))
Exemple #4
0
    def _validate_in_port(self):
        """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.')
Exemple #5
0
    def _validate_in_port(self):
        port = self.in_port

        if isinstance(port, PortNo):
            valid = port in _VIRT_IN_PORTS
        elif isinstance(port, int):
            # Must use "else" because PortNo is an IntEnum and (int instance)
            valid = port >= 1 and port < PortNo.OFPP_MAX.value
        elif isinstance(port, Port):
            valid = port.is_valid()
        #: This check is created because it doesn't allow to accept the value from the unpack
        elif isinstance(port, UBInt32):
            valid = port in _VIRT_IN_PORTS
        else:  # unknown value
            valid = False

        if not valid:
            raise ValidationError('{} is not a valid input port.'.format(port))
Exemple #6
0
 def validate(self):
     """Validate the entire message."""
     if not super().is_valid():
         raise ValidationError()
     self._validate_in_port()
Exemple #7
0
    def _validate_in_port(self):
        is_valid_range = self.in_port > 0 and self.in_port <= PortNo.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.')