blob: fb2906ecc52b16ead14b0f11352ed65dfcd9d688 [file] [log] [blame]
Jian Li8fe714d2021-01-11 02:35:06 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.api;
17
18import com.google.common.base.MoreObjects;
Jian Li3ba5c582021-01-14 11:30:36 +090019import com.google.common.collect.ImmutableSet;
20import inet.ipaddr.AddressStringException;
21import inet.ipaddr.IPAddress;
22import inet.ipaddr.IPAddressSeqRange;
23import inet.ipaddr.IPAddressString;
Jian Li8fe714d2021-01-11 02:35:06 +090024import org.onlab.packet.IpAddress;
25
Jian Li3ba5c582021-01-14 11:30:36 +090026import java.util.ArrayList;
27import java.util.Collections;
28import java.util.HashSet;
29import java.util.List;
Jian Li8fe714d2021-01-11 02:35:06 +090030import java.util.Objects;
Jian Li3ba5c582021-01-14 11:30:36 +090031import java.util.Set;
Jian Li8fe714d2021-01-11 02:35:06 +090032
33/**
34 * Kubevirt IP Pool.
35 */
36public class KubevirtIpPool {
37
38 private final IpAddress start;
39 private final IpAddress end;
Jian Li3ba5c582021-01-14 11:30:36 +090040 private final Set<IpAddress> allocatedIps;
41 private final Set<IpAddress> availableIps;
Jian Li8fe714d2021-01-11 02:35:06 +090042
43 /**
44 * Default constructor.
45 *
Jian Li3ba5c582021-01-14 11:30:36 +090046 * @param start start address of IP pool
47 * @param end end address of IP pool
Jian Li8fe714d2021-01-11 02:35:06 +090048 */
49 public KubevirtIpPool(IpAddress start, IpAddress end) {
50 this.start = start;
51 this.end = end;
Jian Li3ba5c582021-01-14 11:30:36 +090052 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 Li8fe714d2021-01-11 02:35:06 +090059 }
60
61 /**
62 * Returns the start address of IP pool.
63 *
64 * @return start address of IP pool
65 */
Jian Lifc0b8902021-01-12 16:34:49 +090066 public IpAddress start() {
Jian Li8fe714d2021-01-11 02:35:06 +090067 return start;
68 }
69
70 /**
71 * Returns the end address of IP pool.
72 *
73 * @return end address of IP pool
74 */
Jian Lifc0b8902021-01-12 16:34:49 +090075 public IpAddress end() {
Jian Li8fe714d2021-01-11 02:35:06 +090076 return end;
77 }
78
Jian Li3ba5c582021-01-14 11:30:36 +090079 /**
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 Li8fe714d2021-01-11 02:35:06 +0900134 @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 Li3ba5c582021-01-14 11:30:36 +0900158
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 Li8fe714d2021-01-11 02:35:06 +0900178}