Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.kubevirtnetworking.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import inet.ipaddr.AddressStringException; |
| 21 | import inet.ipaddr.IPAddress; |
| 22 | import inet.ipaddr.IPAddressSeqRange; |
| 23 | import inet.ipaddr.IPAddressString; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 26 | import java.util.ArrayList; |
| 27 | import java.util.Collections; |
| 28 | import java.util.HashSet; |
| 29 | import java.util.List; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 30 | import java.util.Objects; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 31 | import java.util.Set; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Kubevirt IP Pool. |
| 35 | */ |
| 36 | public class KubevirtIpPool { |
| 37 | |
| 38 | private final IpAddress start; |
| 39 | private final IpAddress end; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 40 | private final Set<IpAddress> allocatedIps; |
| 41 | private final Set<IpAddress> availableIps; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Default constructor. |
| 45 | * |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 46 | * @param start start address of IP pool |
| 47 | * @param end end address of IP pool |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 48 | */ |
| 49 | public KubevirtIpPool(IpAddress start, IpAddress end) { |
| 50 | this.start = start; |
| 51 | this.end = end; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 52 | this.allocatedIps = new HashSet<>(); |
| 53 | this.availableIps = new HashSet<>(); |
| 54 | try { |
| 55 | this.availableIps.addAll(getRangedIps(start.toString(), end.toString())); |
| 56 | } catch (AddressStringException e) { |
| 57 | e.printStackTrace(); |
| 58 | } |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the start address of IP pool. |
| 63 | * |
| 64 | * @return start address of IP pool |
| 65 | */ |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 66 | public IpAddress start() { |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 67 | return start; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the end address of IP pool. |
| 72 | * |
| 73 | * @return end address of IP pool |
| 74 | */ |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 75 | public IpAddress end() { |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 76 | return end; |
| 77 | } |
| 78 | |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 79 | /** |
| 80 | * Returns the set of IP addresses that have been allocated. |
| 81 | * |
| 82 | * @return set of IP addresses that have been allocated |
| 83 | */ |
| 84 | public Set<IpAddress> allocatedIps() { |
| 85 | return ImmutableSet.copyOf(allocatedIps); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the set of IP addresses for allocation. |
| 90 | * |
| 91 | * @return set of IP addresses for allocation. |
| 92 | */ |
| 93 | public Set<IpAddress> availableIps() { |
| 94 | return ImmutableSet.copyOf(availableIps); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Allocates a random IP address. |
| 99 | * |
| 100 | * @return allocated IP address |
| 101 | * @throws Exception exception |
| 102 | */ |
| 103 | public synchronized IpAddress allocateIp() throws Exception { |
| 104 | if (availableIps.size() <= 0) { |
| 105 | throw new Exception("No IP address is available for allocation."); |
| 106 | } |
| 107 | |
| 108 | List<IpAddress> sortedList = new ArrayList<>(availableIps); |
| 109 | Collections.sort(sortedList); |
| 110 | |
| 111 | IpAddress ip = sortedList.get(0); |
| 112 | |
| 113 | availableIps.remove(ip); |
| 114 | allocatedIps.add(ip); |
| 115 | |
| 116 | return ip; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Releases the given IP address. |
| 121 | * |
| 122 | * @param ip IP address to be released |
| 123 | * @throws Exception exception |
| 124 | */ |
| 125 | public synchronized void releaseIp(IpAddress ip) throws Exception { |
| 126 | if (!allocatedIps.contains(ip)) { |
| 127 | throw new Exception("The given IP address is not able to be released."); |
| 128 | } |
| 129 | |
| 130 | allocatedIps.remove(ip); |
| 131 | availableIps.add(ip); |
| 132 | } |
| 133 | |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 134 | @Override |
| 135 | public boolean equals(Object o) { |
| 136 | if (this == o) { |
| 137 | return true; |
| 138 | } |
| 139 | if (o == null || getClass() != o.getClass()) { |
| 140 | return false; |
| 141 | } |
| 142 | KubevirtIpPool that = (KubevirtIpPool) o; |
| 143 | return start.equals(that.start) && end.equals(that.end); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int hashCode() { |
| 148 | return Objects.hash(start, end); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(this) |
| 154 | .add("start", start) |
| 155 | .add("end", end) |
| 156 | .toString(); |
| 157 | } |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame^] | 158 | |
| 159 | /** |
| 160 | * Obtains the IP address list from the given start and end range. |
| 161 | * |
| 162 | * @param start start range |
| 163 | * @param end end range |
| 164 | * @return IP address list from the given start and end range |
| 165 | * @throws AddressStringException exception |
| 166 | */ |
| 167 | public Set<IpAddress> getRangedIps(String start, String end) throws AddressStringException { |
| 168 | Set<IpAddress> ips = new HashSet<>(); |
| 169 | IPAddress lower = new IPAddressString(start).toAddress(); |
| 170 | IPAddress upper = new IPAddressString(end).toAddress(); |
| 171 | IPAddressSeqRange range = lower.toSequentialRange(upper); |
| 172 | for (IPAddress addr : range.getIterable()) { |
| 173 | ips.add(IpAddress.valueOf(addr.toString())); |
| 174 | } |
| 175 | |
| 176 | return ips; |
| 177 | } |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 178 | } |