Beispiel #1
0
    def _check_infra_data(self, infra, infra_orig=None):
        subnetkey = 'infra_subnet'
        startkey = 'infra_start'
        endkey = 'infra_end'

        subnet = None
        mgmt_ip_version = InfraNetworkController.get_management_ip_version()
        ip_version_string = constants.IP_FAMILIES[mgmt_ip_version]

        if subnetkey in infra.keys():
            subnet = infra[subnetkey]
            try:
                subnet = IPNetwork(subnet)
            except AddrFormatError:
                raise wsme.exc.ClientSideError(
                    _("Invalid subnet %s %s. Please configure"
                      "valid %s subnet") %
                    (subnetkey, subnet, ip_version_string))

            utils.is_valid_subnet(subnet, mgmt_ip_version)

            if (infra_orig and infra_orig[subnetkey]
                    and infra[subnetkey] != infra_orig[subnetkey]):
                raise wsme.exc.ClientSideError(
                    _("Infrastructure subnet cannot be modified."))

        if startkey in infra.keys() or endkey in infra.keys():
            if not subnet:
                raise wsme.exc.ClientSideError(
                    _("An infrastructure subnet must be specified"))

        if infra.get(startkey):
            start = infra[startkey]
            try:
                start = IPAddress(infra[startkey])
            except AddrFormatError:
                raise wsme.exc.ClientSideError(
                    _("Invalid infra start address %s %s. Please configure "
                      "valid %s address") %
                    (startkey, start, ip_version_string))

            utils.is_valid_address_within_subnet(start, subnet)
        else:
            infra[startkey] = subnet[2]

        if infra.get(endkey):
            end = infra[endkey]
            try:
                end = IPAddress(infra[endkey])
            except AddrFormatError:
                raise wsme.exc.ClientSideError(
                    _("Invalid infra end address %s %s. Please configure "
                      "valid %s address") % (startkey, end, ip_version_string))

            utils.is_valid_address_within_subnet(end, subnet)
        else:
            infra[endkey] = subnet[-2]

        if IPAddress(infra[endkey]) <= IPAddress(infra[startkey]):
            raise wsme.exc.ClientSideError(
                _("Invalid infra range.  Start address %s must be below end "
                  "address %s") % (infra[startkey], infra[endkey]))

        # regenerate static addresses if start address changed
        if infra_orig and infra[startkey] != infra_orig[startkey]:
            start_address = IPAddress(infra[startkey])
            for index, field in enumerate(InfraNetwork.address_names.keys()):
                infra[field] = str(start_address + index)

        return infra
Beispiel #2
0
def _check_extoam_data(extoam_orig, extoam, region_config=False):

    subnetkey = 'oam_subnet'
    if subnetkey in extoam.keys():
        subnet = extoam[subnetkey]
        try:
            subnet = IPNetwork(subnet)
        except AddrFormatError:
            raise wsme.exc.ClientSideError(_(
                "Invalid subnet %s %s."
                "Please configure a valid subnet"
            ) % (subnetkey, subnet))

        try:
            utils.is_valid_subnet(subnet)
        except Exception:
            raise wsme.exc.ClientSideError(_(
                "Invalid subnet %s %s."
                "Please check and configure a valid OAM Subnet."
            ) % (subnetkey, subnet))

    skip_oam_gateway_ip_check = False
    gateway_ipkey = 'oam_gateway_ip'
    gateway_ip = extoam.get(gateway_ipkey) or ""
    if gateway_ipkey in extoam.keys():
        ogateway_ip = extoam_orig.get(gateway_ipkey) or ""
        osubnet = extoam_orig.get(subnetkey) or ""
        if not ogateway_ip and osubnet:
            if gateway_ip:
                raise wsme.exc.ClientSideError(_(
                    "OAM gateway IP is not allowed to be configured %s %s. "
                    "There is already a management gateway address configured."
                ) % (ogateway_ip, gateway_ip))
            else:
                skip_oam_gateway_ip_check = True

    for k, v in extoam.items():
        if k in extoam_ip_address_keys:

            if skip_oam_gateway_ip_check:
                if k == "oam_gateway_ip":
                    continue
            if utils.get_system_mode() == constants.SYSTEM_MODE_SIMPLEX:
                if k == "oam_c0_ip" or k == 'oam_c1_ip':
                    continue
            try:
                v = IPAddress(v)
            except (AddrFormatError, ValueError):
                raise wsme.exc.ClientSideError(_(
                    "Invalid address %s in %s."
                    " Please configure a valid"
                    " IPv%s address"
                ) % (v, k, str(subnet.version)))

            utils.is_valid_address_within_subnet(v, subnet)

    oam_c0_ip = extoam.get('oam_c0_ip') or ""
    oam_c1_ip = extoam.get('oam_c1_ip') or ""

    # check for unique if not empty
    if oam_c0_ip and oam_c0_ip == oam_c1_ip:
        raise wsme.exc.ClientSideError(_(
            "Invalid address: "
            "oam_c0_ip=%s and oam_c1_ip=%s must be unique. "
        ) % (oam_c0_ip, oam_c1_ip))

    if gateway_ip and (gateway_ip == oam_c0_ip) or (gateway_ip == oam_c1_ip):
        raise wsme.exc.ClientSideError(_(
            "Invalid address: "
            "oam_c0_ip=%s, oam_c1_ip=%s, oam_gateway_ip=%s must be unique."
        ) % (oam_c0_ip, oam_c1_ip, gateway_ip))

    # Region Mode, check if addresses are within start and end range
    # Gateway address is not used in region mode
    subnet = IPNetwork(extoam.get('oam_subnet'))
    floating_address = IPAddress(extoam.get('oam_floating_ip'))
    start_address = IPAddress(extoam.get('oam_start_ip'))
    end_address = IPAddress(extoam.get('oam_end_ip'))
    # check whether start and end addresses are within the oam_subnet range
    if start_address not in subnet:
        if region_config:
            raise wsme.exc.ClientSideError(_(
                "Invalid oam_start_ip=%s. Please configure a valid IP address")
                % start_address)
        LOG.info("Updating oam_start_ip=%s to %s" % (start_address, subnet[1]))
        extoam['oam_start_ip'] = subnet[1]
        start_address = IPAddress(extoam.get('oam_start_ip'))

    if end_address not in subnet:
        if region_config:
            raise wsme.exc.ClientSideError(_(
                "Invalid oam_end_ip=%s. Please configure a valid IP address") %
                end_address)
        LOG.info("Updating oam_end_ip=%s to %s" % (end_address, subnet[-2]))
        extoam['oam_end_ip'] = subnet[-2]
        end_address = IPAddress(extoam.get('oam_end_ip'))

    if floating_address not in IPRange(start_address, end_address):
        raise wsme.exc.ClientSideError(_(
            "Invalid oam_floating_ip=%s. Please configure a valid IP address "
            "in range")
            % floating_address)

    if oam_c0_ip and IPAddress(oam_c0_ip) not in IPRange(start_address, end_address):
        raise wsme.exc.ClientSideError(_(
            "Invalid oam_c0_ip=%s. Please configure a valid IP address "
            "in range")
            % oam_c0_ip)

    if oam_c1_ip and IPAddress(oam_c1_ip) not in IPRange(start_address, end_address):
        raise wsme.exc.ClientSideError(_(
            "Invalid oam_c1_ip=%s. Please configure a valid IP address "
            "in range")
            % oam_c1_ip)

    return extoam