Ryan Tidwell | 1964a26 | 2016-05-04 15:13:23 -0700 | [diff] [blame] | 1 | # Copyright 2016 Hewlett Packard Enterprise Development Company |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Ryan Tidwell | 1964a26 | 2016-05-04 15:13:23 -0700 | [diff] [blame] | 15 | import netaddr |
| 16 | |
| 17 | from tempest.lib import exceptions as lib_exc |
| 18 | |
| 19 | |
| 20 | def get_unused_ip_addresses(ports_client, subnets_client, |
| 21 | network_id, subnet_id, count): |
Ryan Tidwell | 1964a26 | 2016-05-04 15:13:23 -0700 | [diff] [blame] | 22 | """Return a list with the specified number of unused IP addresses |
| 23 | |
| 24 | This method uses the given ports_client to find the specified number of |
| 25 | unused IP addresses on the given subnet using the supplied subnets_client |
| 26 | """ |
| 27 | |
| 28 | ports = ports_client.list_ports(network_id=network_id)['ports'] |
| 29 | subnet = subnets_client.show_subnet(subnet_id) |
| 30 | ip_net = netaddr.IPNetwork(subnet['subnet']['cidr']) |
| 31 | subnet_set = netaddr.IPSet(ip_net.iter_hosts()) |
| 32 | alloc_set = netaddr.IPSet() |
| 33 | |
| 34 | # prune out any addresses already allocated to existing ports |
| 35 | for port in ports: |
| 36 | for fixed_ip in port.get('fixed_ips'): |
| 37 | alloc_set.add(fixed_ip['ip_address']) |
| 38 | |
zhufl | 8458dde | 2016-08-25 11:29:13 +0800 | [diff] [blame] | 39 | # exclude gateway_ip of subnet |
| 40 | gateway_ip = subnet['subnet']['gateway_ip'] |
| 41 | if gateway_ip: |
| 42 | alloc_set.add(gateway_ip) |
| 43 | |
Ryan Tidwell | 1964a26 | 2016-05-04 15:13:23 -0700 | [diff] [blame] | 44 | av_set = subnet_set - alloc_set |
Kevin Benton | 6f455aa | 2016-07-13 16:20:06 -0700 | [diff] [blame] | 45 | addrs = [] |
| 46 | for cidr in reversed(av_set.iter_cidrs()): |
| 47 | for ip in reversed(cidr): |
| 48 | addrs.append(str(ip)) |
| 49 | if len(addrs) == count: |
| 50 | return addrs |
| 51 | msg = "Insufficient IP addresses available" |
| 52 | raise lib_exc.BadRequest(message=msg) |
Ihar Hrachyshka | f9227c0 | 2016-09-15 11:16:47 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def get_ping_payload_size(mtu, ip_version): |
| 56 | """Return the maximum size of ping payload that will fit into MTU.""" |
| 57 | if not mtu: |
| 58 | return None |
| 59 | if ip_version == 4: |
| 60 | ip_header = 20 |
| 61 | icmp_header = 8 |
| 62 | else: |
| 63 | ip_header = 40 |
| 64 | icmp_header = 4 |
| 65 | res = mtu - ip_header - icmp_header |
| 66 | if res < 0: |
| 67 | raise lib_exc.BadRequest( |
| 68 | message='MTU = %(mtu)d is too low for IPv%(ip_version)d' % { |
| 69 | 'mtu': mtu, |
| 70 | 'ip_version': ip_version, |
| 71 | }) |
| 72 | return res |