Example #1
0
    def __validateNetwork(self, network: Network) -> None:         \
            # pylint: disable=no-self-use
        """
        Raises:
            InvalidArgument
        """

        optionDict = {}

        if 'address' in network and not network['address']:
            raise InvalidArgument('Network address not set')

        if 'netmask' in network and not network['netmask']:
            raise InvalidArgument('Network mask not set')

        # Get all of the network options
        if network.getOptions():
            for option in network.getOptions().split(';'):
                tokens = option.split('=')
                if len(tokens) == 2:
                    key, value = tokens
                    optionDict[key] = value

        if 'startIp' in network and network['startIp']:
            # Validate that the specified start IP address is on the
            # provided subnet

            startIp = ipaddress.IPv4Address(str(network['startIp']))
        else:
            # Start IP address not provided, fill it in since it can
            # be calculated

            increment = int(network['increment']) \
                if 'increment' in network and network['increment'] else 1

            startIp = ipaddress.IPv4Address(str(
                network['address'])) + increment

        ipaddrNetwork = ipaddress.IPv4Network(
            '%s/%s' % (network['address'], network['netmask']))

        if startIp not in ipaddrNetwork:
            raise InvalidArgument(
                'Starting IP address [%s] is not on network [%s]' %
                (startIp, ipaddrNetwork))

        network['startIp'] = str(startIp)

        # Right now just check the VLAN vid
        if 'vlan' in optionDict:
            try:
                if int(optionDict['vlan']) > 4095 or \
                        int(optionDict['vlan']) < 1:
                    raise InvalidArgument(
                        'The VLAN ID must be an integer in the range'
                        ' 1-4095')
            except ValueError:
                # Convert all exceptions to this one...
                raise InvalidArgument(
                    'The VLAN ID must be an integer in the range 1-4095')
Example #2
0
def populate_network(network: Network,
                     dbNetwork: Optional[NetworkModel] = None) \
        -> NetworkModel:
    if not dbNetwork:
        dbNetwork = NetworkModel()

    dbNetwork.address = network.getAddress()
    dbNetwork.netmask = network.getNetmask()
    dbNetwork.suffix = network.getSuffix()
    dbNetwork.gateway = network.getGateway()
    dbNetwork.options = network.getOptions()
    dbNetwork.name = network.getName()
    dbNetwork.startIp = network.getStartIp()
    dbNetwork.type = network.getType()
    dbNetwork.increment = network.getIncrement()
    dbNetwork.usingDhcp = network.getUsingDhcp()

    return dbNetwork