"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.networkWideToInfraEnvField = exports.formViewHostsToInfraEnvField = exports.getMapInterfaceMap = void 0;
const js_yaml_1 = require("js-yaml");
const nmstateYaml_1 = require("./nmstateYaml");
const protocolVersion_1 = require("./protocolVersion");
const formDataFromInfraEnvField_1 = require("./formDataFromInfraEnvField");
const fromInfraEnv_1 = require("./fromInfraEnv");
const dummyData_1 = require("./dummyData");
const machineNetwork_1 = require("./machineNetwork");
const emptyData_1 = require("./emptyData");
const REAL_NIC_NAME = 'eth0';
const REAL_NIC_NAME_1 = 'eth1';
const BOND_NIC_NAME = 'bond0';
const getPrefixLength = (networkWide, protocolVersion) => {
    const machineNetwork = networkWide.ipConfigs[protocolVersion].machineNetwork;
    if (machineNetwork.prefixLength === '') {
        throw 'Subnet prefix length not configured';
    }
    return machineNetwork.prefixLength;
};
const toYamlWithComments = (json, comments) => {
    const yamlComments = comments.map((comment) => `${nmstateYaml_1.YAML_COMMENT_CHAR}${comment}`);
    return `${yamlComments.join('\n')}\n${(0, js_yaml_1.dump)(json, { noRefs: true })}`;
};
const getNmstateObject = (networkWide, hostIps, bondType, hasBondsConfigured) => {
    const interfaces = [];
    const routeConfigs = [];
    const dns = (0, nmstateYaml_1.getDnsSection)(networkWide.dns);
    const realProtocolConfigs = {};
    for (const protocolVersion of (0, protocolVersion_1.getShownProtocolVersions)(networkWide.protocolType)) {
        const hostIp = hostIps[protocolVersion];
        let nicName = '';
        if (hostIp) {
            nicName = bondType && hasBondsConfigured ? BOND_NIC_NAME : REAL_NIC_NAME;
            realProtocolConfigs[protocolVersion] = (0, nmstateYaml_1.getNmstateProtocolConfig)(hostIp, getPrefixLength(networkWide, protocolVersion), protocolVersion);
            if (networkWide.useVlan && networkWide.vlanId) {
                if (bondType && hasBondsConfigured) {
                    interfaces.push((0, nmstateYaml_1.getInterface)(BOND_NIC_NAME, realProtocolConfigs, networkWide, bondType, hasBondsConfigured, true));
                }
            }
        }
        else {
            //this happens when a host was particall configured, or not configured at all
            const protocolConfigs = {
                [protocolVersion]: (0, nmstateYaml_1.getNmstateProtocolConfig)(dummyData_1.DUMMY_NMSTATE_ADDRESSES[protocolVersion].ip, dummyData_1.DUMMY_NMSTATE_ADDRESSES[protocolVersion].prefixLength, protocolVersion),
            };
            nicName = (0, dummyData_1.getDummyNicName)(protocolVersion);
            interfaces.push((0, nmstateYaml_1.getInterface)(nicName, protocolConfigs, networkWide, bondType, hasBondsConfigured));
            if (bondType && hasBondsConfigured) {
                interfaces.push((0, nmstateYaml_1.getInterface)(BOND_NIC_NAME, protocolConfigs, networkWide, bondType, hasBondsConfigured, true));
            }
        }
        routeConfigs.push((0, nmstateYaml_1.getRouteConfig)(protocolVersion, networkWide.ipConfigs[protocolVersion].gateway, networkWide.useVlan && networkWide.vlanId
            ? (0, nmstateYaml_1.getVlanNicName)(nicName, networkWide.vlanId)
            : nicName));
    }
    if (Object.keys(realProtocolConfigs).length > 0) {
        interfaces.unshift((0, nmstateYaml_1.getInterface)(bondType && hasBondsConfigured ? BOND_NIC_NAME : REAL_NIC_NAME, realProtocolConfigs, networkWide, bondType, hasBondsConfigured));
    }
    const nmstate = {
        interfaces,
        'dns-resolver': dns,
        routes: { config: routeConfigs },
    };
    return nmstate;
};
const toYaml = (networkWideConfiguration, formHostData) => {
    const comments = [nmstateYaml_1.FORM_VIEW_PREFIX];
    for (const protocolVersion of (0, protocolVersion_1.getShownProtocolVersions)(networkWideConfiguration.protocolType)) {
        comments.push(`${(0, nmstateYaml_1.getMachineNetworkFieldName)(protocolVersion)} ${(0, machineNetwork_1.getMachineNetworkCidr)(networkWideConfiguration.ipConfigs[protocolVersion].machineNetwork)}`);
    }
    const hostIps = formHostData ? formHostData.ips : (0, emptyData_1.getEmptyHostIps)();
    const bondType = formHostData ? formHostData.bondType : '';
    const hasBondsConfigured = formHostData &&
        formHostData.bondPrimaryInterface !== '' &&
        formHostData.bondSecondaryInterface !== '';
    const nmstate = getNmstateObject(networkWideConfiguration, hostIps, bondType, hasBondsConfigured);
    return toYamlWithComments(nmstate, comments);
};
const formDataToInfraEnvField = (formData) => {
    let ret = [];
    if (formData.hosts.length === 0) {
        //user only filled in network wide configuration and didn't fill in host specific configurations
        ret = [
            {
                networkYaml: toYaml(formData.networkWide),
                macInterfaceMap: (0, dummyData_1.getDummyMacInterfaceMap)(formData.networkWide),
            },
        ];
    }
    else {
        for (const host of formData.hosts) {
            ret.push({
                networkYaml: toYaml(formData.networkWide, host),
                macInterfaceMap: (0, exports.getMapInterfaceMap)(host),
            });
        }
    }
    return ret;
};
const getMapInterfaceMap = (host) => {
    if (host.bondPrimaryInterface &&
        host.bondPrimaryInterface !== '' &&
        host.bondSecondaryInterface &&
        host.bondSecondaryInterface !== '') {
        return [
            {
                macAddress: host.bondPrimaryInterface,
                logicalNicName: REAL_NIC_NAME,
            },
            {
                macAddress: host.bondSecondaryInterface,
                logicalNicName: REAL_NIC_NAME_1,
            },
        ];
    }
    else {
        return [
            {
                macAddress: host.macAddress,
                logicalNicName: REAL_NIC_NAME,
            },
        ];
    }
};
exports.getMapInterfaceMap = getMapInterfaceMap;
const formViewHostsToInfraEnvField = (currentInfraEnv, formViewHosts) => {
    const staticNetworkConfig = (0, fromInfraEnv_1.getStaticNetworkConfig)(currentInfraEnv);
    if (!staticNetworkConfig) {
        throw `Infra env doesn't contain static ip values`;
    }
    const currentFormData = (0, formDataFromInfraEnvField_1.formDataFromInfraEnvField)(staticNetworkConfig);
    const networkWide = currentFormData.networkWide;
    return formDataToInfraEnvField({
        networkWide: networkWide,
        hosts: formViewHosts,
    });
};
exports.formViewHostsToInfraEnvField = formViewHostsToInfraEnvField;
const fixHostIps = (networkWide, hosts) => {
    //fix host ips following changes in network wide configuration protocol type
    for (const host of hosts) {
        const newHostIps = (0, emptyData_1.getEmptyHostIps)();
        for (const protocolVersion of (0, protocolVersion_1.getShownProtocolVersions)(networkWide.protocolType)) {
            newHostIps[protocolVersion] = host.ips[protocolVersion];
        }
        host.ips = newHostIps;
    }
};
const networkWideToInfraEnvField = (currentInfraEnv, networkWide) => {
    const staticNetworkConfig = (0, fromInfraEnv_1.getStaticNetworkConfig)(currentInfraEnv);
    if (!staticNetworkConfig) {
        throw `Infra env doesn't contain static ip values`;
    }
    const currentFormData = (0, formDataFromInfraEnvField_1.formDataFromInfraEnvField)(staticNetworkConfig);
    fixHostIps(networkWide, currentFormData.hosts);
    //Transform vlanId to number
    if (networkWide.vlanId !== '') {
        networkWide.vlanId = Number(networkWide.vlanId);
    }
    return formDataToInfraEnvField({ networkWide, hosts: currentFormData.hosts });
};
exports.networkWideToInfraEnvField = networkWideToInfraEnvField;
//# sourceMappingURL=formDataToInfraEnvField.js.map