import subprocess
import os
import sys
import re
from networkCheck import networkCheck

class checkCustomerNetwork(networkCheck):

    def getHumanName(self):
        return "Check configuration of customer network"
        
    def runCheck(self,config, debug):
        # confirm static assigned customer numbers are not actually active on the net!
        returnCode=0

        # Get the netmask value        
        netmask = config["environment"]["customerNetmask"]
        
        # Get the customer/public interface for cs1
        csip = 'dhcp'
        for server in config["environment"]["servers"]:
            for vm in server['vms']:             
                if vm['name'] == 'cs1':
                    csip = vm['customerIP']

        if csip == 'dhcp':

            for server in config['environment']['servers']:
                for vm in server['vms']:
                    if vm['name'] != 'cs1':
                        ip = vm['customerIP']
                        if ip == 'static':
                            # this is invalid.
                            self.addFailure("Customer IP value 'static' is not supported for KVM hypervisor. Use an actual ipv4 address of the string 'dhcp'")
                        else:
                            if ip != '' and ip != 'dhcp':
                            
                                # then its a static.  make sure its not active...
                                if self.pingIP(ip):
                                    # thats a problem!
                                    self.addFailure('Assigned static customer IP '+ip+' for vm '+vm['name']+' on server '+server['accessIP']+' appears to be alive on the network. Please enter a different IP address.')
                                else:
                                    # warn that it's a bit weird that it is configured as a static IP
                                    self.addWarning('Inconsistent network configuration: installer vm is using DHCP while vm '+vm['name']+' on server '+server['accessIP']+' has been assigned static customer IP '+ip+'.')

        else:
            for server in config['environment']['servers']:
                if "deployed" not in server:
                    for vm in server['vms']:
                        if vm['name'] != 'cs1':
                            ip = vm['customerIP']
                            if ip != '':
                                
                                if ip == 'static':
                                    # this is invalid.
                                    self.addFailure("Customer IP value 'static' is not supported for KVM hypervisor. Use an actual ipv4 address of the string 'dhcp'")

                                else:
                                        
                                    if ip == 'dhcp':

                                        # TODO: how to check that there is a DHCP server to assign an IP address?
                                        pass

                                    else:

                                        if not self.checkNetwork(csip, ip, netmask):
                                            self.addFailure('Invalid static customer IP '+ip+' for vm '+vm['name']+' on server '+server['accessIP']+'.')
                                            
                                        else:

                                            # then its a static.  make sure its not active...
                                            if self.pingIP(ip):
                                                # thats a problem!
                                                self.addFailure('Assigned static customer IP '+ip+' for vm '+vm['name']+' on server '+server['accessIP']+' appears to be alive on the network. Please enter a different IP address.')

        return returnCode
