ConfigManager.psm1

Import-Module $PSScriptRoot\Utils.psm1
Import-Module $PSScriptRoot\ClusUtils.psm1

$script:CMap = @{}

$script:CMap["turnKeySdnPath"] = ""
$script:CMap["storePath"] = ""
$script:CMap["state"] = "state.json"
$script:CMap["credential"] = "cred.json"
$script:CMap["workloadcredential"] = "workloadcred.json"
$script:CMap["userConfigPath"] = "$PSScriptRoot\config\deployment"
$script:CMap["deploymentConfigPath"] = "$PSScriptRoot\config\deployment"
$script:CMap["deploymentFile"] = "deployment.json"
$script:CMap["hnvpaNetworkFile"] = "networkconfig\logicalnetworks\hnvpa.json"
$script:CMap["mgmtNetworkFile"] = "networkconfig\logicalnetworks\management.json"
$script:CMap["greNetworkFile"] = "networkconfig\logicalnetworks\grevip.json"
$script:CMap["publicVIPNetworkFile"] = "networkconfig\logicalnetworks\publicvip.json"
$script:CMap["privateVIPNetworkFile"] = "networkconfig\logicalnetworks\privatevip.json"
$script:CMap["macpoolFile"] = "networkconfig\macpools\defaultMacPool.json"
$script:CMap["sdnConfigFile"] = "sdnconfig.json"

$script:CMap["sdnExpressConfigPath"] = "$PSScriptRoot\config\sdnexpress"
$script:CMap["sdnExpressTemplateFile"] = "template.psd1"

$script:CMap["userWorkloadConfigPath"] = "$PSScriptRoot\config\workload"
$script:CMap["workloadConfigPath"] = "$PSScriptRoot\config\workload"
$script:CMap["workload"] = "workloadconfig.json"

$script:CMap["vhdStore"] = ""

$script:MgmtHostNicName = "TurnKeySdn-Mgmt"

$script:DefaultTestVhdShare = "\\skyshare.redmond.corp.microsoft.com\Beanstalk\SDN_ADOArtifacts\TestArtifacts\mariner_workload_vhd"
$script:DefaultTestVhdFile = "full-2.0.20231004.vhdx"

$script:InstallStateInstalled = "installed"
$script:InstallStateInstallFailed = "installFailed"
$script:InstallStateInstalling = "installing"
$script:InstallStateNone = "none"

# Corp network routes, used on the management nic of gateway VMs to allow access to corp network
$script:MSFTCorpRoutes = @("10.0.0.0/8")
function Initialize-StorePath {

    if (-not [String]::IsNullOrEmpty($script:CMap["turnKeySdnPath"]) -and `
            -not ([String]::IsNullOrEmpty($script:CMap["storePath"]))) {
        return
    }

    $csv = Get-CSV

    if ($csv -ne $null) {
        $script:CMap["turnKeySdnPath"] = Join-Path $csv "TurnKeySdn"
        $script:CMap["vhdStore"] = Join-Path $csv "TurnKeySdnVhdStore"
    }
    else {
        $script:CMap["turnKeySdnPath"] = Join-Path $Env:SystemDrive "TurnKeySdn"
        $script:CMap["vhdStore"] = Join-Path $Env:SystemDrive "TurnKeySdnVhdStore"
    }

    $script:CMap["storePath"] = Join-Path $script:CMap["turnKeySdnPath"] ".store"

    if (Test-IsInitialized) {
        $script:CMap["deploymentConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "DeploymentConfig"
        $script:CMap["workloadConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "WorkloadConfig"
    }
    
    if (-not $(Test-Path (Get-TurnKeySdnPath))) {
        New-Item (Get-TurnKeySdnPath) -ItemType Directory -ErrorAction Stop | Out-Null
    }

    if (-not $(Test-Path (Get-StorePath))) {
        New-Item (Get-StorePath) -ItemType Directory -ErrorAction Stop | Out-Null
    }

}

function Undo-StorePathInitialization {
    $script:CMap["turnKeySdnPath"] = ""
    $script:CMap["storePath"] = ""
    $script:CMap["deploymentConfigPath"] = $script:CMap["userConfigPath"] 
    $script:CMap["workloadConfigPath"] = $script:CMap["userWorkloadConfigPath"]
    $script:CMap["vhdStore"] = ""

}

function Get-StorePath {
    Initialize-StorePath
    return $script:CMap["storePath"]
}

function Get-TurnKeySdnPath {
    Initialize-StorePath
    return $script:CMap["turnKeySdnPath"]
}

function Get-InternalDeploymentConfigPath {
    Initialize-StorePath
    return $script:CMap["deploymentConfigPath"]
}

function Get-InternalWorkloadConfigPath {
    Initialize-StorePath
    return $script:CMap["workloadConfigPath"]
}

function Get-VhdStore {
    Initialize-StorePath
    return $script:CMap["vhdStore"]
}

function Get-SdnExpressPath {

    $infraconfig = Get-DeploymentConfig
    $sdnExpPath = $infraconfig.sdnexpressPath

    if (-not [String]::IsNullOrEmpty($sdnExpPath)) {
        return $sdnExpPath
    }

    $sdnExpPath = Join-Path $PSScriptRoot "..\..\SDNExpress"
    $sdnExpPSM1 = Join-Path $sdnExpPath "SDNExpress.psm1"
    if ($(Test-Path $sdnExpPSM1)) {
        return $sdnExpPath
    }

    Import-Module -Name SdnExpress -ErrorAction SilentlyContinue
    $module = Get-Module SdnExpress
    if ($null -ne $module) {
        return $module.ModuleBase
    }

    return $null
}

function Set-SdnExpressPathInternal {
    param(
        [parameter(Mandatory = $true)][string] $sdnExpressPath
    )
 
    $infraconfig = Get-DeploymentConfig
    $infraconfig.sdnexpressPath = $sdnExpressPath
    Set-DeploymentConfig -deploymentConfig $infraconfig
    Write-TraceLog "Set-SdnExpressPath: Updated path to $sdnExpressPath"
}

function Get-SdnExpressModule {
    $sdnExpPath = Get-SdnExpressPath
    $sdnexpress = Join-Path $sdnExpPath SDNExpress.psm1
    return $sdnexpress
}

function Get-SdnExpressScript {
    $sdnExpPath = Get-SdnExpressPath
    $sdnexpress = Join-Path $sdnExpPath SDNExpress.ps1
    return $sdnexpress
}

function Get-RestoreReplayScriptPath {
    $infraconfig = Get-DeploymentConfig
    $restoreReplayScriptPath = $infraconfig.restoreReplayScriptPath

    if (-not [String]::IsNullOrEmpty($restoreReplayScriptPath)) {
        return $restoreReplayScriptPath
    }

    $restoreReplayScriptPath = Join-Path $PSScriptRoot "..\BCDR"
    $restoreReplayScript = Join-Path $restoreReplayScriptPath RestoreReplay.ps1
    if ($(Test-Path $restoreReplayScript)) {
        return $restoreReplayScriptPath
    }

    $script = Get-InstalledScript RestoreReplay -ErrorAction SilentlyContinue

    if ($null -ne $script) {
        return $script.InstalledLocation
    }

    return $null
}

function Get-RestoreReplayScript {
    $restoreReplayScriptPath = Get-RestoreReplayScriptPath
    $restoreReplay = Join-Path $restoreReplayScriptPath RestoreReplay.ps1
    return $restoreReplay
}

function Get-DefaultNetworkControllerPsModulePath {
    return Join-Path $env:SystemRoot "system32\WindowsPowerShell\v1.0\Modules\NetworkControllerFc"
}
function Get-DefaultNetworkControllerPackageLocation {
    return (Join-Path $env:SystemRoot "NetworkController")
}

function Get-DefaultNetworkControllerDBLocation {
    $csv = Get-CSV

    if ($csv -ne $null) {
        return (Join-Path $csv "FCNCDB")
    }

    return (Join-Path $env:SystemDrive "FCNCDB")
}

function Set-AddressRandmoizerSeed {
    param(
        [parameter(Mandatory = $true)][int] $seed
    )

    $internalConfig = Get-TurnKeySdnInternalConfig -configType state
    $internalConfig.randomizerSeed = $seed
    Set-TurnKeySdnInternalConfig -configType state -config $internalConfig
}

function Get-AddressRandmoizerSeed {

    $internalConfig = Get-TurnKeySdnInternalConfig -configType state
    if (-not [String]::IsNullOrEmpty($internalConfig.randomizerSeed)) {
        return [int]$internalConfig.randomizerSeed
    }

    $curhost = (hostname)
    $hostIndex = $curhost.Substring($($curhost.Length - 2))
    $result = 0

    if (-not [int]::TryParse($hostIndex, [ref]$result)) {
        $result = Get-Random -Minimum 1 -Maximum 60
    }

    return $result
}

function Get-RandomizedIPv4Address {
    param(
        $address,
        $randomizer
    )

    $addresses = $address.Split(".")
    $addresses[1] = ([int]$addresses[1] + $randomizer) % 255
    return $addresses -join '.'
}

function Update-IPv4Address {
    param(
        $Address
    )

    $depConfig = Get-DeploymentConfig

    if (-not $depConfig.randomizeAddresses) {
        return $Address
    }

    $randomizer = Get-AddressRandmoizerSeed
    $addr = Get-RandomizedIPv4Address -address $Address -randomizer $randomizer
    return $addr
}
function Update-LogicalNetworkAddresses {
    param(
        $network
    )

    $depConfig = Get-DeploymentConfig

    if (-not $depConfig.randomizeAddresses) {
        return $network
    }

    $isOneNode = $depConfig.hyperVHosts.Count -eq 1
    $randomizer = Get-AddressRandmoizerSeed
    $network.properties.subnets | ForEach-Object {
        $subnetProp = $_.properties
        $subnetProp.addressPrefix = Get-RandomizedIPv4Address -address $subnetProp.addressPrefix -randomizer $randomizer

        $newGateways = @()
        $subnetProp.defaultGateways | ForEach-Object {
            $newGateways += Get-RandomizedIPv4Address -address $_ -randomizer $randomizer
        }

        $subnetProp.defaultGateways = $newGateways

        $subnetProp.ipPools | Foreach-Object {
            $poolProp = $_.Properties
            $poolProp.startIpAddress = Get-RandomizedIPv4Address -address $poolProp.startIpAddress -randomizer $randomizer
            $poolProp.endIpAddress = Get-RandomizedIPv4Address -address $poolProp.endIpAddress -randomizer $randomizer
        }

        if ($isOneNode) {
            $subnetProp.vlanID = $([int]$subnetProp.vlanID) + $randomizer
        }
    }
    return $network
}

function Set-TurnKeySdnInternalConfig {

    param(
        [ValidateSet("workloadcredential","credential", "state")]
        [parameter(Mandatory = $true)][string] $configType,
        [parameter(Mandatory = $true)] $config
    )

    $configFile = Join-Path (Get-StorePath) $script:CMap[$configType]
    $value = $(ConvertTo-Json $config -Depth 10)
    Set-Content -Path $configFile -Value $value -Encoding UTF8
}

function Get-TurnKeySdnInternalConfig {

    param(
        [ValidateSet("workloadcredential","credential", "state")]
        [parameter(Mandatory = $true)][string] $configType
    )

    $configFile = Join-Path (Get-StorePath) $script:CMap[$configType]
    return $(Get-Config $configFile)
}

function Test-TurnKeySdnStateConfigExist {   
    $configFile = Get-StateFileWithoutInit
    return $(Test-Path $configFile)
}

function Get-TurnKeySdnCred {

    if ($Global:SdnCredential -ne $null -and $Global:SdnCredential -ne [pscredential]::Empty) {
        return $Global:SdnCredential
    }

    $configFile = Join-Path (Get-StorePath) $script:CMap["credential"]
    try {
        $c = Get-Config $configFile
    } catch {
    }
    
    if ([String]::IsNullOrEmpty($c.username) -or [String]::IsNullOREmpty($c.password)) {
        Write-TraceLog "Get-TurnKeySdnCred: Failed to get credentials, defaulting to empty cred"    
        return [pscredential]::Empty
    }

    $cred = [pscredential]::new($c.username, $($c.password | ConvertTo-SecureString -ErrorAction Stop))
    return $cred
}
function Get-TurnKeySdnWorkloadVmCred {

    # todo: find a better way to store workload credentials
    # $configFile = Join-Path (Get-StorePath) $script:CMap["workloadcredential"]
    # $c = Get-Config $configFile
    
    # if([string]::IsNullOrEmpty($c.username) -or [string]::IsNullOrEmpty($c.password)) {

    # throw "Workload VMcredentials is not initialized. Please make sure $Env:TURNKEY_WORKLOAD_PASSWORD is set on the machine. Then run initialize again for TurnKey SDN"

    # } else {
    # $vmUserName = $c.username
    # $vmUserPassword = $c.password
    # }
    $vmUserName = $Env:TURNKEY_WORKLOAD_USERNAME
    $vmUserPassword = $Env:TURNKEY_WORKLOAD_PASSWORD
    return  [pscredential]::new($vmUserName, $($vmUserPassword | ConvertTo-SecureString  -AsPlainText -Force))
}


function Get-Config {
    param(
        [parameter(Mandatory = $true)][string] $file
    )

    Test-FileExist -path $file
    $config = Get-Content $file -ErrorAction Stop | ConvertFrom-Json
    return $config
}

function Get-GreVipLogicalNetworkConfig {
    param(
        [Parameter(Mandatory = $false)][bool] $RandomizeAddress = $true
    )

    $greNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["greNetworkFile"]
    $network = Get-Config $greNetworkFile
    if ($RandomizeAddress) {
        $network = Update-LogicalNetworkAddresses $network
    }
    return $network
}

function Set-GreVipNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $greNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["greNetworkFile"]
    $Network | ConvertTo-Json -Depth 100 | Out-File $greNetworkFile -Force
}

function Get-HnvPaNetworkConfig {
    param(
        [Parameter(Mandatory = $false)][bool] $RandomizeAddress = $true
    )

    $hnvpaNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["hnvpaNetworkFile"]
    $hnvPaNetwork = Get-Config $hnvpaNetworkFile
    if ($RandomizeAddress) {
        $hnvPaNetwork = Update-LogicalNetworkAddresses $hnvPaNetwork
    }
    return $hnvPaNetwork
}

function Set-HnvPaNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $hnvpaNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["hnvpaNetworkFile"]
    $Network | ConvertTo-Json -Depth 100 | Out-File $hnvpaNetworkFile -Force
}

function Get-MgmtNetworkConfig {
    param(
        [Parameter(Mandatory = $false)][bool] $RandomizeAddress = $true
    )
    $mgmtNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["mgmtNetworkFile"]
    $mgmtNetwork = Get-Config $mgmtNetworkFile
    if ($RandomizeAddress) {
        $mgmtNetwork = Update-LogicalNetworkAddresses $mgmtNetwork
    }
    return $mgmtNetwork
}
function Set-MgmtNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $mgmtNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["mgmtNetworkFile"]
    $Network | ConvertTo-Json -Depth 100 | Out-File $mgmtNetworkFile -Force
}
function Get-PublicVipNetworkConfig {
    $publicVipNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["publicVipNetworkFile"]
    $publicVipNetwork = Get-Config $publicVipNetworkFile
    return $publicVipNetwork
}

function Set-PublicVipNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $publicVipNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["publicVipNetworkFile"]   
    $Network | ConvertTo-Json -Depth 100 | Out-File $publicVipNetworkFile -Force
}

function Get-PrivateVipNetworkConfig {
    $privateVipNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["privateVipNetworkFile"]
    $privateVipNetwork = Get-Config $privateVipNetworkFile
    return $privateVipNetwork
}

function Set-PrivateVipNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $privateVipNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["privateVipNetworkFile"]   
    $Network | ConvertTo-Json -Depth 100 | Out-File $privateVipNetworkFile -Force
}

function Get-DeploymentConfig {
    $config = Get-Config $(Get-DeploymentFile)
    
    # config upgrade for productkey
    $p = $config | Get-Member -Name productKey -ErrorAction SilentlyContinue
    if ($null -eq $p) {
        $config | add-member -NotePropertyName productKey -NotePropertyValue ""
    }

    return $config
}

function Update-MacAddress {
    param($mac)

    $macEle = $mac.Split("-")
    $rand = Get-AddressRandmoizerSeed
    $strRand = [string]$rand

    if ($strRand.Length -eq 1) {
        $strRand = "0$strRand"
    }

    $macEle[3] = $strRand
    $result = $macEle -join '-'

    return $result
}

function Get-DefaultMacPoolConfig {
    $configFileName = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["macpoolFile"]
    $config = Get-Config $configFileName

    $depConfig = Get-DeploymentConfig

    if ($depConfig.randomizeAddresses) {
        $config.properties.startMacAddress = Update-MacAddress -mac $config.properties.startMacAddress
        $config.properties.endMacAddress = Update-MacAddress -mac $config.properties.endMacAddress
    }

    return $config
}

function Set-MacPoolConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Config
    )

    $configFileName = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["macpoolFile"]
    $Config | ConvertTo-Json -Depth 100 | Out-File $configFileName -Force
}

function Get-SdnConfig {
    $configFileName = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["sdnConfigFile"]
    $config = Get-Config $configFileName
    return $config
}

function Test-ValidDepId {
    param(
        [parameter(Mandatory = $true)][string] $id
    )

    if ($id.Length -gt 9) {
        Write-TraceLog "Test-ValidDepId failed, id $id must be less than 9 characters"
        return $false
    }

    $names = @("NC-$id", "$id-$(Get-RouterVMSuffix)", "$id-$(Get-NCVMSuffix)0", "$id-$(Get-MUXVMSuffix)0", "$id-$(Get-GWVMSuffix)0")
    foreach($name in $names) {
        $result = (Resolve-DnsName $name -ErrorAction SilentlyContinue)

        if ($result -ne $null) {
            Write-TraceLog "Test-ValidDepId failed, another deployment with id $id already exists"
            return $false
        }
    }
   
    Write-TraceLog "Test-ValidDepId $id passed all validations"
    return $true
}
function Get-NewDepId {
    while($true) {
        $id = ((New-Guid).guid).split("-")[0]
        # Skip if the name is already in use
        if ($(Test-ValidDepId -id $id)) {
            return $id
        }        
    }
}

function Set-DeploymentId {
    param(
        [parameter(Mandatory = $false)][string] $id
    )

    $internalConfig = Get-TurnKeySdnInternalConfig -configType state
    $internalConfig.depId = $id

    if ([String]::IsNullOrEmpty($internalConfig.torId)) {
        $internalConfig.torId = $id
    }

    Set-TurnKeySdnInternalConfig -configType state -config $internalConfig
}

function Get-TurnKeySdnDeploymentId {

    $configFileName = Join-Path (Get-StorePath) $script:CMap["state"]
    $config = Get-Config $configFileName
    return $config.depId
}

function Get-TorId {

    $configFileName = Join-Path (Get-StorePath) $script:CMap["state"]
    $config = Get-Config $configFileName
    return $config.torId
}

function Get-StateFileWithoutInit {
    $csv = Get-CSV

    if ($csv -ne $null) {
        $installPath = Join-Path $csv "TurnKeySdn"
    }
    else {
        $installPath = Join-Path $Env:SystemDrive "TurnKeySdn"
    }

    $storePath = Join-Path $installPath ".store"
    $configFileName = Join-Path $storePath $script:CMap["state"]

    return $configFileName
}

function Remove-TurnKeySdnStore {

    param (
        [parameter(Mandatory = $false)][switch] $SkipStore
    )

    try {
        $depConfig = Get-DeploymentConfig
        $deployFolder = $depConfig.vmLocation

        if (-not [string]::IsNullOrEmpty($deployFolder)) {
            Write-TraceLog "Remove-TurnKeySdnStore removing deployment folder $deployFolder"
            Remove-Item $depConfig.vmLocation -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove vmLocation $deployFolder, error $_"
    }

    try {
        $workloadConfig = Get-TurnKeySdnWorkloadConfig
        $workloadDeployFolder = $workloadConfig.deploymentpath

        if (-not [String]::IsNullOrEmpty($workloadDeployFolder)) {
            Write-TraceLog "Remove-TurnKeySdnStore removing workload folder $workloadDeployFolder"
            Remove-Item $workloadDeployFolder -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove deploymentpath $workloadDeployFolder, error $_"
    }

    if (Test-IsInitialized) {
        try {
            $inernalWorkConfig = Get-InternalWorkloadConfigPath
            
            if (-not [String]::IsNullOrEmpty($inernalWorkConfig)) {
                Write-TraceLog "Remove-TurnKeySdnStore removing workload config folder $inernalWorkConfig"
                Remove-Item $inernalWorkConfig -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
        catch {
            Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove deploymentpath $inernalWorkConfig, error $_"
        }


        try {
            $inernalDepConfig = Get-InternalDeploymentConfigPath
            
            if (-not [String]::IsNullOrEmpty($inernalDepConfig)) {
                Write-TraceLog "Remove-TurnKeySdnStore removing deployment config folder $inernalDepConfig"
                Remove-Item $inernalDepConfig -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
        catch {
            Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove deploymentpath $inernalDepConfig, error $_"
        }
    }

    $configPresent = Test-TurnKeySdnStateConfigExist
    if ($($SkipStore.IsPresent) -and $configPresent) {
        Write-TraceLog "Remove-TurnKeySdnStore: Resetting install states"
        $state = Get-StateInternal
        $state.depId = ""
        $state.initialized = $false
        $state.installState = $script:InstallStateNone
        $state.mgmtIPOffset = "0"
        Set-TurnKeySdnInternalConfig -configType state -config $state
        Write-TraceLog "Remove-TurnKeySdnStore: Skipping store path removal"
        return
    }

    try {
        $storePath = Get-TurnKeySdnPath

        if (-not [String]::IsNullOrEmpty($storePath)) {
            Write-TraceLog "Remove-TurnKeySdnStore removing storePath $storePath"
            Remove-Item $storePath -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove storePath $storePath, error $_"
    }
}

function Get-TurnKeySdnRestEndpoint {
    $sdnConfig = Get-SdnConfig

    $endpoint = $sdnConfig.networkController.restName

    if (-not [String]::IsNullOrEmpty($endpoint)) {
        return $endpoint
    }

    if ([String]::IsNullOrEmpty($sdnConfig.networkController.restIpAddress)) {
        throw "Invalid sdn configuration, both restname and IP are empty"
    }

    return $sdnConfig.networkController.restIpAddress.split("/")[0]
}
function Get-TurnKeySdnNCRuntime() {
    $sdnConfig = Get-SdnConfig
    return $sdnConfig.networkController.runtime
}

function Set-TurnKeySdnConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$sdnConfig
    )

    $configString = $sdnConfig | ConvertTo-Json -Depth 10
    $configFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["sdnConfigFile"]
    Set-Content -Path $configFile -Value $configString -Encoding UTF8
}

function Set-DeploymentConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$deploymentConfig
    )    

    $configString = $deploymentConfig | ConvertTo-Json -Depth 10
    Set-Content -Path $(Get-DeploymentFile) -Value $configString -Encoding UTF8
}

function LoadSdnExpressConfig {
    $sdnexpress = Import-LocalizedData `
        -BaseDirectory $script:CMap["sdnExpressConfigPath"] `
        -FileName $script:CMap["sdnExpressTemplateFile"]

    return $sdnexpress
}

function Get-WorkloadConfigPath {
    param(
        [ValidateSet("LoadBalancer", "VNET", "LogicalNetwork", "Gateway", "None","HLK")]
        [Parameter(Mandatory = $false)] $WorkloadType,
        [Parameter(Mandatory = $false)] $WorkloadFolder
    )

    if ($WorkloadType -eq "None" -or [String]::IsNullOrEmpty($WorkloadType)) {
        $WorkloadType = "LoadBalancer"
    }

    if ([String]::IsNullOrEmpty($WorkloadFolder)) {
        $WorkloadFolder = $WorkloadType
    }

    $path = Join-Path(Get-InternalWorkloadConfigPath) $WorkloadFolder

    if (-not (Test-Path $path)) {
        throw "Workload $WorkloadType not found at $path"
    }

    return $path
}
function Get-TurnKeySdnWorkloadConfig {
    $workloadConfigFile = Join-Path (Get-InternalWorkloadConfigPath) $script:CMap["workload"]
    $workloadConfig = Get-Config $workloadConfigFile
    return $workloadConfig
}
function Get-SdnWorkloadDeploymentPath {
    $workloadConfig = Get-TurnKeySdnWorkloadConfig

    if (-not [String]::isnullorempty($workloadConfig.deploymentpath)) {
        return $workloadConfig.deploymentpath
    }
}

function Set-TurnKeySdnWorkloadConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$config
    )    

    $configString = $config | ConvertTo-Json -Depth 10
    $configFile = Join-Path (Get-InternalWorkloadConfigPath) $script:CMap["workload"]
    Set-Content -Path $configFile -Value $configString -Encoding UTF8
}

function Get-SdnWorkloadVirtualGateways {
    param(
        [Parameter(Mandatory = $true)] $WorkloadConfigPath
    )

    $jsonPath = Join-Path $workloadConfigPath "jsons\virtualgateways"

    if (-not $(Test-Path $jsonPath)) {
        return $null
    }

    Get-ChildItem $jsonPath
}
function Get-SdnWorkloadLogicalNetworks {
    param(
        [Parameter(Mandatory = $true)] $WorkloadConfigPath
    )

    $jsonPath = Join-Path $workloadConfigPath "jsons\logicalnetworks"

    if (-not $(Test-Path $jsonPath)) {
        return $null
    }

    Get-ChildItem $jsonPath
}

function Get-SdnWorkloadNetworkInterfaces {
    param(
        [Parameter(Mandatory = $true)] $WorkloadConfigPath
    )

    $nicJsonPath = Join-Path $workloadConfigPath "jsons\networkinterfaces"

    if (-not $(Test-Path $nicJsonPath)) {
        return $null
    }

    Get-ChildItem $nicJsonPath
}


function Get-SdnWorkloadLoadbalancers {
    param(
        [Parameter(Mandatory=$true)] $WorkloadConfigPath
    )

    $lbJsonPath = Join-Path $workloadConfigPath "jsons\loadbalancers"

    if (-not $(Test-Path $lbJsonPath)) {
        return $null
    }

    Get-ChildItem $lbJsonPath
}

function Get-TurnKeySdnHyperVHosts {
    $depConfig = Get-DeploymentConfig

    if ($depConfig.hyperVHosts.Count -gt 0) {
        return $depConfig.hyperVHosts
    }

    if (Test-IsCluster) {
        Write-TraceLog "Get-TurnKeySdnHyperVHosts: No hosts found, defaulting to clusternodes"
        $hypervHosts = (get-cluster | get-clusternode).Name
    }
    else {
        Write-TraceLog "Get-TurnKeySdnHyperVHosts: No hosts found, defaulting to local host"
        $hypervHosts = @($(hostname))
    }

    return $hypervHosts
}

function Get-SdnSwitchName {
    $depConfig = Get-DeploymentConfig
    return $depConfig.sdnSwitchName
}

function Initialize-TurnKeySdnDeploymentId {

    $config = Get-TurnKeySdnInternalConfig -configType state
    $updated = $false

    if ([String]::IsNullOrEmpty($config.depId)) {
        $config.depId = Get-NewDepId
        $updated = $true
    }

    if ([String]::IsNullOrEmpty($config.torId)) {
        $config.torId = $config.depId
        $updated = $true
    }

    if ([String]::IsNullOrEmpty($config.randomizerSeed)) {
        $config.randomizerSeed = Get-AddressRandmoizerSeed
        $updated = $true
    }

    if ($updated) {       
        Set-TurnKeySdnInternalConfig -configType state -config $config
    }
}

function Initialize-Store {

    Initialize-StorePath
    
    $script:CMap["deploymentConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "DeploymentConfig"
    $script:CMap["workloadConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "WorkloadConfig"

    $internalConfigPath = $script:CMap["deploymentConfigPath"]
    $internalWorkloadConfigPath = $script:CMap["workloadConfigPath"]

    if (-not (Test-Path $internalConfigPath)) {
        New-Item $internalConfigPath -ItemType Directory -ErrorAction Stop | Out-Null
    }

    if (-not (Test-Path $internalWorkloadConfigPath)) {
        New-Item $internalWorkloadConfigPath -ItemType Directory -ErrorAction Stop | Out-Null
    }

    $userConfigPath = $script:CMap["userConfigPath"]
    Write-TraceLog "Copying user config from $userConfigPath to $internalConfigPath"

    robocopy $userConfigPath $script:CMap["deploymentConfigPath"] /E /XO /NC /NS /NP /NFL /NDL /NJH /NJS

    $userWorkloadConfigPath = $script:CMap["userWorkloadConfigPath"]
    Write-TraceLog "Copying user workload config from $userWorkloadConfigPath to $internalWorkloadConfigPath"

    robocopy $userWorkloadConfigPath $internalWorkloadConfigPath /E /XO /NC /NS /NP /NFL /NDL /NJH /NJS

    $vhdStore = Get-VhdStore

    if (-not (Test-Path $vhdStore)) {
        New-Item $vhdStore -ItemType Directory -ErrorAction Stop | Out-Null
    }

    $credFile = Join-Path (Get-StorePath) $script:CMap["credential"]
    if (-not $(Test-Path $credFile)) {
        $cred = @{}
        $cred["username"] = ""
        $cred["password"] = ""
        Set-TurnKeySdnInternalConfig -configType credential -config $cred
    }

    $stateFile = Join-Path (Get-StorePath) $script:CMap["state"]
    if (-not $(Test-Path $stateFile)) {
        $state = @{}
        $state["depId"] = ""
        $state["torId"] = ""
        $state["randomizerSeed"] = ""
        $state["initialized"] = $false
        $state["installState"] = $script:InstallStateNone
        $state["mgmtIPOffset"] = "0"
        Set-TurnKeySdnInternalConfig -configType state -config $state
    }

    Initialize-TurnKeySdnDeploymentId
}

function Get-DeploymentFile {
    return Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["deploymentFile"]
}

function Get-MgmtNicName {
    return $script:MgmtHostNicName
}

function Get-DefaultWorkloadVhdShare {
    return $script:DefaultTestVhdShare
}

function Get-DefaultWorkloadVhdFile {
    return $script:DefaultTestVhdFile
}

function Get-StateInternal {
    $stateFile = Get-StateFileWithoutInit
    $config = Get-Config $stateFile
    return $config
}

function Set-InstallState {
    param(
        [ValidateSet("installed", "installFailed", "installing", "none")]
        [parameter(Mandatory = $true)][string] $state
    )

    $config = Get-StateInternal
    $config.installState = $state
    Set-TurnKeySdnInternalConfig -configType state -config $config
}

function Test-IsInstalled {
    try {
        $config = Get-StateInternal
    }
    catch {
        return $false
    }
    return $config.installState -ieq "installed"
}

function Test-IsInitialized {

    try {
        $config = Get-StateInternal
    }
    catch {
        return $false
    }

    return $config.initialized
}

function Set-Initialized {
    $config = Get-TurnKeySdnInternalConfig -configType state
    $config.initialized = $true
    Set-TurnKeySdnInternalConfig -configType state -config $config
}

function Get-NextManagementIPOffset {

    $config = Get-StateInternal
    [int]$offset = $config.mgmtIPOffset
    $config.mgmtIPOffset = $offset + 1
    Set-TurnKeySdnInternalConfig -configType state -config $config
    return $offset
}

function Initialize-ManagementIPOffset {
    $config = Get-StateInternal
    $config.mgmtIPOffset = 0
    Set-TurnKeySdnInternalConfig -configType state -config $config
}

function Get-MsftCorpRoutes {
    return $script:MSFTCorpRoutes
}

function Test-RRASRouterEnabled {
    $depConfig = Get-DeploymentConfig
    return $depConfig.useRRASRouter
}

function Get-DefaultInternetSwitchName {
    return $script:InternetSwitchDefaultName
}

function Get-DefaultSdnSwitchName {
    return $script:SdnSwitchDefaultName
}

function Get-RouterVMSuffix {
    "-TOR"
}

function Get-NCVMSuffix {
    "-NC"
}

function Get-MUXVMSuffix {
    "-MUX"
}

function Get-GWVMSuffix {
    "-GW"
}