'Check that port_range is valid.'

def _validate_port_range(self, rule):

    if ((rule['port_range_min'] is None) and (rule['port_range_max'] is None)):
        return
    if (not rule['protocol']):
        raise ext_sg.SecurityGroupProtocolRequiredWithPorts()
    ip_proto = self._get_ip_proto_number(rule['protocol'])
    if (ip_proto in [constants.PROTO_NUM_TCP, constants.PROTO_NUM_UDP]):
        if ((rule['port_range_min'] == 0) or (rule['port_range_max'] == 0)):
            raise ext_sg.SecurityGroupInvalidPortValue(port=0)
        elif ((rule['port_range_min'] is not None) and (rule['port_range_max'] is not None) and (rule['port_range_min'] <= rule['port_range_max'])):
            pass
        else:
            raise ext_sg.SecurityGroupInvalidPortRange()
    elif (ip_proto in [constants.PROTO_NUM_ICMP, constants.PROTO_NUM_IPV6_ICMP]):
        for (attr, field) in [('port_range_min', 'type'), ('port_range_max', 'code')]:
            if ((rule[attr] is not None) and (not (0 <= rule[attr] <= 255))):
                raise ext_sg.SecurityGroupInvalidIcmpValue(field=field, attr=attr, value=rule[attr])
        if ((rule['port_range_min'] is None) and (rule['port_range_max'] is not None)):
            raise ext_sg.SecurityGroupMissingIcmpType(value=rule['port_range_max'])
