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"

$script:InternetSwitchDefaultName = "turnkeysdn-internet"
$script:SdnSwitchDefaultName = "turnkeysdn-sdnswitch"

# 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 ($null -ne $csv) {
        $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 {

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

    if (-not $($SkipConfigPath.IsPresent)) {
        $infraconfig = Get-DeploymentConfig
        $sdnExpPath = $infraconfig.sdnexpressPath
    
        if (-not [String]::IsNullOrEmpty($sdnExpPath)) {
            $sdnExpPSM1 = Join-Path $sdnExpPath "SDNExpress.psm1"
            if ($(Test-Path $sdnExpPSM1)) {
                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 {
    
    param (
        [parameter(Mandatory = $false)][switch] $SkipConfigPath
    )

    if (-not $($SkipConfigPath.IsPresent)) {
        $infraconfig = Get-DeploymentConfig
        $restoreReplayScriptPath = $infraconfig.restoreReplayScriptPath

        if (-not [String]::IsNullOrEmpty($restoreReplayScriptPath)) {
            $restoreReplayScript = Join-Path $restoreReplayScriptPath RestoreReplay.ps1
            if ($(Test-Path $restoreReplayScript)) {
                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 Get-TorVmConfig {
    $config = Get-DeploymentConfig
    return $config.torVmConfig
}

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

    $depConfig = Get-DeploymentConfig
    $depConfig.torVmConfig = $Config
    Set-DeploymentConfig -deploymentConfig $depConfig
}

function Update-MacAddress {
    param($mac)

    $net = Get-MgmtNetworkConfig
    # Generate a mac address based on the managment network.
    $prefixElements = $net.properties.subnets[0].properties.addressPrefix.Split("/")
    $prefixLen = [int]$prefixElements[1]
    $ip = $prefixElements[0]
    if ($prefixLen -gt 24) {
         $secLast_hex = "{0:X2}" -f [int]($ip.Split('.')[-2])
         $last_hex = "{0:X2}" -f [int]($ip.Split('.')[-1])
    } elseif ($prefixLen -gt 8 -and $prefixLen -le 24) {
         $secLast_hex = "{0:X2}" -f [int]($ip.Split('.')[-3])
         $last_hex = "{0:X2}" -f [int]($ip.Split('.')[-2])
    } else {
         $secLast_hex = "{0:X2}" -f [int]($ip.Split('.')[-4])
         $last_hex = "{0:X2}" -f [int]($ip.Split('.')[-3])
    }

    $macEle = $mac.Split("-")
    
    $macEle[3] = $secLast_hex
    $macEle[4] = $last_hex
    $result = $macEle -join '-'

    return $result
}

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

    # Always randomize mac addresses, if needed expose a separate flag to disable.
    $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"
}

function Get-DefaultCtsTrafficFolder {
    if (-not (Test-IsCluster)) {
        return "$env:SystemDrive\Tools" 
    }

    $csvPath = Get-CSV
    return $(Join-Path -Path $csvPath -ChildPath "WorkloadTools")
}

function Get-DefaultCtsTrafficFile {
    return $(Join-Path -Path (Get-DefaultCtsTrafficFolder) -ChildPath "CtsTraffic.exe")
}

function Get-CtsTrafficGitHubUri {
    return "https://github.com/microsoft/ctsTraffic/raw/master/Releases/2.0.3.2/x64/ctsTraffic.exe"
}

function Copy-OrGetCtsTraffficExe {
    param(
        [parameter(Mandatory = $false)][string] $SourceFolder
    )

    
    $defaultCtsTrafficFolder = Get-DefaultCtsTrafficFolder

    if (-not (Test-Path $defaultCtsTrafficFolder)) {
        New-item -Path $defaultCtsTrafficFolder -ItemType Directory -Force -ErrorAction Stop | Out-Null
    }

    $ctsTraffic = Get-DefaultCtsTrafficFile

    $source = Join-Path $SourceFolder "CtsTraffic.exe"

    if (-not $(Test-Path $source) -and -not $(Test-Path $ctsTraffic)) {
        Write-TraceLog "Copy-OrGetCtsTraffficExe: Ctstraffic not found at $source, downloading from github and copying to $ctsTraffic"
        Start-BitsTransfer $(Get-CtsTrafficGitHubUri) $ctsTraffic
        return
    }

    if ($(Test-Path $source)) {
        Write-TraceLog "Copy-OrGetCtsTraffficExe: Ctstraffic found locally at $source, copying to $ctsTraffic"
        Copy-Item -Path $source -Destination $ctsTraffic -Force 
        return
    }   

    Write-TraceLog "Copy-OrGetCtsTraffficExe: Using existing ctstraffic from $ctsTraffic"
}
# SIG # Begin signature block
# MIInwQYJKoZIhvcNAQcCoIInsjCCJ64CAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCCW3JVOnEuvazGM
# zuXikmqpeKrmNdzDh4nlxPq1SClPgKCCDXYwggX0MIID3KADAgECAhMzAAADrzBA
# DkyjTQVBAAAAAAOvMA0GCSqGSIb3DQEBCwU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