Changeset 6458


Ignore:
Timestamp:
Dec 15, 2008, 1:27:43 AM (16 years ago)
Author:
roland
Message:

Refactored code and made more unittests.

Location:
trunk/exodus
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/exodus/tests.py

    r6457 r6458  
    4747        ip_size = self.ipcalc(netmask)._ip_size(25)
    4848        self.failUnless(ip_size, 128)
     49
     50    def test_ipcalc_get_used_ips(self):
     51        self.fail("Test not implemented")
     52
     53    def test_ipcalc_get_free_network_addr(self):
     54        self.fail("Test not implemented")
    4955
    5056    def test_ipcalc_out_of_bounds(self):
  • trunk/exodus/wlipcalc.py

    r6457 r6458  
    1313            raise ValueError, 'Netmask out of bounds.'
    1414
    15         master_ip = node.masterip
     15        self.node = node
     16        self.master_ip = node.masterip
    1617        master_netmask = MASTERIP_NETMASK
    17         master_network = wl.get_network(master_ip, master_netmask)
     18        self.master_network = wl.get_network(self.master_ip, master_netmask)
    1819
    1920        # calculate the number of ipaddresses in the subnetmask
    20         master_ip_size = self._ip_size(master_netmask)
    21         new_ip_size = self._ip_size(free_netmask)
     21        self.master_ip_size = self._ip_size(master_netmask)
     22        self.new_ip_size = self._ip_size(free_netmask)
    2223       
    23         # generate a list of all used_ipaddresses
    24         # we take only the ip addresses of the interfaces that are
    25         # linked to itself, because they are 'master' and the slaves
    26         # on other nodes fall in their subnet.
    27         used_list = [master_ip]
    28         for i in node.interface_set.all():
    29             #XXX: rewrite to if i.ip in masterip??
    30             if i.id == i.link_id:
    31                 ip_size = self._ip_size(i.netmask)
    32                 network_addr = wl.network(i.ip, i.netmask)
    33                 for j in range(ip_size):
    34                     used_list.append(wl.show_addr(network_addr + j))
     24        used_list = self._get_used_ips()
     25        network = self._get_free_network_addr(used_list)
    3526
    36         for i in range(master_ip_size/new_ip_size):
    37             network_addr = wl.parse_addr(master_network) + (new_ip_size * i)
    38             k = 0
    39             for j in range(new_ip_size):
    40                 new_ip = wl.show_addr(network_addr + j)
    41                 if new_ip not in used_list:
    42                     k += 1
    43             if k == new_ip_size and \
    44                     wl.show_addr(network_addr) != master_network:
    45                 self.network = wl.show_addr(network_addr)
    46                 break
    47 
    48         if not self.network:
     27        if not network:
    4928            raise Exception, "Not enough space for a /%d subnet" % \
    5029                (free_netmask)
     30        else:
     31            self.network = wl.show_addr(network)
    5132 
    5233        # variables for extenal calling
     
    5940            # broadcast addresses.
    6041            self.ips = []
    61             for i in range(1, new_ip_size-1):
    62                 self.ips.append(wl.show_addr(network_addr + i))                   
     42            for i in range(1, self.new_ip_size-1):
     43                self.ips.append(wl.show_addr(network + i))                   
    6344    def _ip_size(self, netmask):
    6445        return int(pow(2,32 - netmask))
     46   
     47    def _get_used_ips(self):
     48        """ Generate a list of all used_ipaddresses.
     49
     50        we take only the ip addresses of the interfaces that are
     51        linked to itself, because they are 'master' and the slaves
     52        on other nodes fall in their subnet.
     53        """
     54        used_list = [self.master_ip]
     55        for i in self.node.interface_set.all():
     56            #XXX: rewrite to if i.ip in masterip??
     57            if i.id == i.link_id:
     58                ip_size = self._ip_size(i.netmask)
     59                network_addr = wl.network(i.ip, i.netmask)
     60                for j in range(ip_size):
     61                    used_list.append(wl.show_addr(network_addr + j))
     62        return used_list
     63
     64    def _get_free_network_addr(self, used_list):
     65        """Calculate a new network address with a used_list and given netmask.
     66        """
     67        for i in range(self.master_ip_size/self.new_ip_size):
     68            network_addr = wl.parse_addr(self.master_network) + \
     69                    (self.new_ip_size * i)
     70            k = 0
     71            for j in range(self.new_ip_size):
     72                new_ip = wl.show_addr(network_addr + j)
     73                if new_ip not in used_list:
     74                    k += 1
     75            if k == self.new_ip_size and \
     76                    wl.show_addr(network_addr) != self.master_network:
     77                    return  network_addr
     78        return None
     79
Note: See TracChangeset for help on using the changeset viewer.