blob: a60bd0a064873aadecdc7b3fd37e00f9bda30f0b [file] [log] [blame]
Jian Li43244382021-01-09 00:19:02 +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.util;
17
Jian Li0c656f02021-06-07 13:32:39 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Lif97a07e2021-01-13 18:05:00 +090019import com.fasterxml.jackson.databind.ObjectMapper;
Jian Li0c656f02021-06-07 13:32:39 +090020import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Li556709c2021-02-03 17:54:28 +090021import com.google.common.base.Strings;
Jian Lid4296d02021-03-12 18:03:58 +090022import com.google.common.collect.ImmutableSet;
Jian Li034820d2021-01-15 16:58:48 +090023import io.fabric8.kubernetes.client.ConfigBuilder;
24import io.fabric8.kubernetes.client.DefaultKubernetesClient;
25import io.fabric8.kubernetes.client.KubernetesClient;
Jian Li43244382021-01-09 00:19:02 +090026import org.apache.commons.lang.StringUtils;
Jian Li3ba5c582021-01-14 11:30:36 +090027import org.apache.commons.net.util.SubnetUtils;
Jian Lica20b712021-01-18 00:19:31 +090028import org.json.JSONException;
29import org.json.JSONObject;
Jian Li858ccd72021-02-04 17:25:01 +090030import org.onlab.osgi.DefaultServiceDirectory;
Daniel Park157947f2021-04-09 17:50:53 +090031import org.onlab.packet.ARP;
32import org.onlab.packet.Ethernet;
Daniel Park2884b232021-03-04 18:58:47 +090033import org.onlab.packet.Ip4Address;
Jian Li3ba5c582021-01-14 11:30:36 +090034import org.onlab.packet.IpAddress;
Jian Lica20b712021-01-18 00:19:31 +090035import org.onlab.packet.MacAddress;
Jian Li43244382021-01-09 00:19:02 +090036import org.onosproject.cfg.ConfigProperty;
Jian Lica20b712021-01-18 00:19:31 +090037import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort;
Daniel Park05a94582021-05-12 10:57:02 +090038import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
39import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerService;
Jian Lica20b712021-01-18 00:19:31 +090040import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
Daniel Parkf3136042021-03-10 07:49:11 +090041import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService;
Jian Lica20b712021-01-18 00:19:31 +090042import org.onosproject.kubevirtnetworking.api.KubevirtPort;
Daniel Park2884b232021-03-04 18:58:47 +090043import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
44import org.onosproject.kubevirtnetworking.api.KubevirtRouterService;
Jian Li034820d2021-01-15 16:58:48 +090045import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
46import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
Jian Li858ccd72021-02-04 17:25:01 +090047import org.onosproject.kubevirtnode.api.KubevirtNode;
Daniel Park2884b232021-03-04 18:58:47 +090048import org.onosproject.kubevirtnode.api.KubevirtNodeService;
Jian Li858ccd72021-02-04 17:25:01 +090049import org.onosproject.net.DeviceId;
50import org.onosproject.net.Port;
51import org.onosproject.net.PortNumber;
52import org.onosproject.net.device.DeviceService;
Daniel Park05a94582021-05-12 10:57:02 +090053import org.onosproject.net.group.DefaultGroupKey;
54import org.onosproject.net.group.GroupKey;
Jian Li43244382021-01-09 00:19:02 +090055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
Jian Li94b6d162021-04-15 17:09:11 +090057import org.xbill.DNS.Address;
Jian Li43244382021-01-09 00:19:02 +090058
Jian Lif97a07e2021-01-13 18:05:00 +090059import java.io.IOException;
Jian Li94b6d162021-04-15 17:09:11 +090060import java.net.InetAddress;
61import java.net.UnknownHostException;
Jian Li3ba5c582021-01-14 11:30:36 +090062import java.util.Arrays;
63import java.util.HashSet;
Jian Lif97a07e2021-01-13 18:05:00 +090064import java.util.List;
Jian Li858ccd72021-02-04 17:25:01 +090065import java.util.Objects;
Jian Li43244382021-01-09 00:19:02 +090066import java.util.Optional;
67import java.util.Set;
Jian Li3ba5c582021-01-14 11:30:36 +090068import java.util.stream.Collectors;
Jian Li43244382021-01-09 00:19:02 +090069
Jian Li858ccd72021-02-04 17:25:01 +090070import static org.onosproject.kubevirtnetworking.api.Constants.TUNNEL_TO_TENANT_PREFIX;
Daniel Park2884b232021-03-04 18:58:47 +090071import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY;
Jian Li858ccd72021-02-04 17:25:01 +090072import static org.onosproject.net.AnnotationKeys.PORT_NAME;
73
Jian Li43244382021-01-09 00:19:02 +090074/**
75 * An utility that used in KubeVirt networking app.
76 */
77public final class KubevirtNetworkingUtil {
78
79 private static final Logger log = LoggerFactory.getLogger(KubevirtNetworkingUtil.class);
80
81 private static final int PORT_NAME_MAX_LENGTH = 15;
Jian Li034820d2021-01-15 16:58:48 +090082 private static final String COLON_SLASH = "://";
83 private static final String COLON = ":";
Jian Li556709c2021-02-03 17:54:28 +090084 private static final String OF_PREFIX = "of:";
Jian Li43244382021-01-09 00:19:02 +090085
Jian Lica20b712021-01-18 00:19:31 +090086 private static final String NETWORK_STATUS_KEY = "k8s.v1.cni.cncf.io/network-status";
87 private static final String NAME = "name";
88 private static final String NETWORK_PREFIX = "default/";
89 private static final String MAC = "mac";
90 private static final String IPS = "ips";
Daniel Parkbabde9c2021-03-09 13:37:42 +090091 private static final String BR_INT = "br-int";
Jian Li0c656f02021-06-07 13:32:39 +090092 private static final String STATUS = "status";
93 private static final String INTERFACES = "interfaces";
94 private static final String IP_ADDRESS = "ipAddress";
95 private static final String NODE_NAME = "nodeName";
Jian Lica20b712021-01-18 00:19:31 +090096
Jian Li43244382021-01-09 00:19:02 +090097 /**
98 * Prevents object installation from external.
99 */
100 private KubevirtNetworkingUtil() {
101 }
102
103 /**
104 * Obtains the boolean property value with specified property key name.
105 *
Daniel Park2884b232021-03-04 18:58:47 +0900106 * @param properties a collection of properties
107 * @param name key name
Jian Li43244382021-01-09 00:19:02 +0900108 * @return mapping value
109 */
110 public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties,
111 String name) {
112 Optional<ConfigProperty> property =
113 properties.stream().filter(p -> p.name().equals(name)).findFirst();
114
115 return property.map(ConfigProperty::asBoolean).orElse(false);
116 }
117
118 /**
119 * Re-structures the OVS port name.
120 * The length of OVS port name should be not large than 15.
121 *
Daniel Park2884b232021-03-04 18:58:47 +0900122 * @param portName original port name
Jian Li43244382021-01-09 00:19:02 +0900123 * @return re-structured OVS port name
124 */
125 public static String structurePortName(String portName) {
126
127 // The size of OVS port name should not be larger than 15
128 if (portName.length() > PORT_NAME_MAX_LENGTH) {
129 return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH);
130 }
131
132 return portName;
133 }
Jian Lif97a07e2021-01-13 18:05:00 +0900134
135 /**
136 * Generates string format based on the given string length list.
137 *
138 * @param stringLengths a list of string lengths
139 * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
140 */
141 public static String genFormatString(List<Integer> stringLengths) {
142 StringBuilder fsb = new StringBuilder();
143 stringLengths.forEach(length -> {
144 fsb.append("%-");
145 fsb.append(length);
146 fsb.append("s");
147 });
148 return fsb.toString();
149 }
150
151 /**
Jian Li556709c2021-02-03 17:54:28 +0900152 * Auto generates DPID from the given name.
153 *
154 * @param name name
155 * @return auto generated DPID
156 */
157 public static String genDpidFromName(String name) {
158 if (name != null) {
159 String hexString = Integer.toHexString(name.hashCode());
160 return OF_PREFIX + Strings.padStart(hexString, 16, '0');
161 }
162
163 return null;
164 }
165
166 /**
Jian Lif97a07e2021-01-13 18:05:00 +0900167 * Prints out the JSON string in pretty format.
168 *
Daniel Park2884b232021-03-04 18:58:47 +0900169 * @param mapper Object mapper
170 * @param jsonString JSON string
Jian Lif97a07e2021-01-13 18:05:00 +0900171 * @return pretty formatted JSON string
172 */
173 public static String prettyJson(ObjectMapper mapper, String jsonString) {
174 try {
175 Object jsonObject = mapper.readValue(jsonString, Object.class);
176 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
177 } catch (IOException e) {
178 log.debug("Json string parsing exception caused by {}", e);
179 }
180 return null;
181 }
Jian Li3ba5c582021-01-14 11:30:36 +0900182
183 /**
184 * Obtains valid IP addresses of the given subnet.
185 *
186 * @param cidr CIDR
187 * @return set of IP addresses
188 */
189 public static Set<IpAddress> getSubnetIps(String cidr) {
190 SubnetUtils utils = new SubnetUtils(cidr);
191 utils.setInclusiveHostCount(false);
192 SubnetUtils.SubnetInfo info = utils.getInfo();
193 Set<String> allAddresses =
194 new HashSet<>(Arrays.asList(info.getAllAddresses()));
195
196 if (allAddresses.size() > 2) {
197 allAddresses.remove(info.getLowAddress());
198 allAddresses.remove(info.getHighAddress());
199 }
200
201 return allAddresses.stream()
202 .map(IpAddress::valueOf).collect(Collectors.toSet());
203 }
204
205 /**
206 * Calculate the broadcast address from given IP address and subnet prefix length.
207 *
Daniel Park2884b232021-03-04 18:58:47 +0900208 * @param ipAddr IP address
209 * @param prefixLength subnet prefix length
Jian Li3ba5c582021-01-14 11:30:36 +0900210 * @return broadcast address
211 */
212 public static String getBroadcastAddr(String ipAddr, int prefixLength) {
213 String subnet = ipAddr + "/" + prefixLength;
214 SubnetUtils utils = new SubnetUtils(subnet);
215 return utils.getInfo().getBroadcastAddress();
216 }
Daniel Park2884b232021-03-04 18:58:47 +0900217
Jian Li034820d2021-01-15 16:58:48 +0900218 /**
219 * Generates endpoint URL by referring to scheme, ipAddress and port.
220 *
Daniel Park2884b232021-03-04 18:58:47 +0900221 * @param scheme scheme
222 * @param ipAddress IP address
223 * @param port port number
Jian Li034820d2021-01-15 16:58:48 +0900224 * @return generated endpoint URL
225 */
226 public static String endpoint(KubevirtApiConfig.Scheme scheme, IpAddress ipAddress, int port) {
227 StringBuilder endpoint = new StringBuilder();
228 String protocol = org.apache.commons.lang3.StringUtils.lowerCase(scheme.name());
229
230 endpoint.append(protocol);
231 endpoint.append(COLON_SLASH);
232 endpoint.append(ipAddress.toString());
233 endpoint.append(COLON);
234 endpoint.append(port);
235
236 return endpoint.toString();
237 }
238
239 /**
240 * Generates endpoint URL by referring to scheme, ipAddress and port.
241 *
Daniel Park2884b232021-03-04 18:58:47 +0900242 * @param apiConfig kubernetes API config
Jian Li034820d2021-01-15 16:58:48 +0900243 * @return generated endpoint URL
244 */
245 public static String endpoint(KubevirtApiConfig apiConfig) {
246 return endpoint(apiConfig.scheme(), apiConfig.ipAddress(), apiConfig.port());
247 }
248
249 /**
250 * Obtains workable kubernetes client.
251 *
252 * @param config kubernetes API config
253 * @return kubernetes client
254 */
255 public static KubernetesClient k8sClient(KubevirtApiConfig config) {
256 if (config == null) {
257 log.warn("Kubernetes API server config is empty.");
258 return null;
259 }
260
261 String endpoint = endpoint(config);
262
263 ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(endpoint);
264
265 if (config.scheme() == KubevirtApiConfig.Scheme.HTTPS) {
266 configBuilder.withTrustCerts(true)
Jian Li034820d2021-01-15 16:58:48 +0900267 .withCaCertData(config.caCertData())
268 .withClientCertData(config.clientCertData())
269 .withClientKeyData(config.clientKeyData());
270 }
271
272 return new DefaultKubernetesClient(configBuilder.build());
273 }
274
275 /**
276 * Obtains workable kubernetes client.
277 *
278 * @param service kubernetes API service
279 * @return kubernetes client
280 */
281 public static KubernetesClient k8sClient(KubevirtApiConfigService service) {
282 KubevirtApiConfig config = service.apiConfig();
283 if (config == null) {
284 log.error("Failed to find valid kubernetes API configuration.");
285 return null;
286 }
287
288 KubernetesClient client = k8sClient(config);
289
290 if (client == null) {
291 log.error("Failed to connect to kubernetes API server.");
292 return null;
293 }
294
295 return client;
296 }
Jian Lica20b712021-01-18 00:19:31 +0900297
298 /**
Jian Li556709c2021-02-03 17:54:28 +0900299 * Obtains the hex string of the given segment ID with fixed padding.
300 *
301 * @param segIdStr segment identifier string
302 * @return hex string with padding
303 */
304 public static String segmentIdHex(String segIdStr) {
305 int segId = Integer.parseInt(segIdStr);
306 return String.format("%06x", segId).toLowerCase();
307 }
308
309 /**
Jian Li858ccd72021-02-04 17:25:01 +0900310 * Obtains the tunnel port number with the given network and node.
311 *
312 * @param network kubevirt network
Daniel Park2884b232021-03-04 18:58:47 +0900313 * @param node kubevirt node
Jian Li858ccd72021-02-04 17:25:01 +0900314 * @return tunnel port number
315 */
316 public static PortNumber tunnelPort(KubevirtNetwork network, KubevirtNode node) {
317 switch (network.type()) {
318 case VXLAN:
319 return node.vxlanPort();
320 case GRE:
321 return node.grePort();
322 case GENEVE:
323 return node.genevePort();
324 default:
325 break;
326 }
327 return null;
328 }
329
330 /**
Jian Li0c656f02021-06-07 13:32:39 +0900331 * Obtains the kubevirt port from kubevirt VMI.
Jian Lica20b712021-01-18 00:19:31 +0900332 *
Daniel Parkf3136042021-03-10 07:49:11 +0900333 * @param nodeService kubevirt node service
Jian Lica20b712021-01-18 00:19:31 +0900334 * @param networks set of existing kubevirt networks
Jian Li0c656f02021-06-07 13:32:39 +0900335 * @param resource VMI definition
336 * @return kubevirt ports attached to the VMI
Jian Lica20b712021-01-18 00:19:31 +0900337 */
Jian Lib6dc08f2021-03-24 15:24:18 +0900338 public static Set<KubevirtPort> getPorts(KubevirtNodeService nodeService,
Jian Li0c656f02021-06-07 13:32:39 +0900339 Set<KubevirtNetwork> networks,
340 String resource) {
Jian Lica20b712021-01-18 00:19:31 +0900341 try {
Jian Li0c656f02021-06-07 13:32:39 +0900342 ObjectMapper mapper = new ObjectMapper();
343 JsonNode json = mapper.readTree(resource);
344 JsonNode statusJson = json.get(STATUS);
345 ArrayNode interfacesJson = (ArrayNode) statusJson.get(INTERFACES);
Jian Lib6dc08f2021-03-24 15:24:18 +0900346
Daniel Parkf3136042021-03-10 07:49:11 +0900347 KubevirtPort.Builder builder = DefaultKubevirtPort.builder();
Jian Li0c656f02021-06-07 13:32:39 +0900348 String nodeName = parseVmiNodeName(resource);
349 if (nodeName != null && nodeService.node(nodeName) != null) {
350 builder.deviceId(nodeService.node(nodeName).intgBridge());
Daniel Parkf3136042021-03-10 07:49:11 +0900351 }
Jian Lica20b712021-01-18 00:19:31 +0900352
Jian Li0c656f02021-06-07 13:32:39 +0900353 if (interfacesJson == null) {
354 return ImmutableSet.of();
355 }
Jian Lica20b712021-01-18 00:19:31 +0900356
Jian Li0c656f02021-06-07 13:32:39 +0900357 Set<KubevirtPort> ports = new HashSet<>();
358 for (JsonNode interfaceJson : interfacesJson) {
359 String name = interfaceJson.get(NAME).asText();
Jian Lica20b712021-01-18 00:19:31 +0900360 KubevirtNetwork network = networks.stream()
Jian Li0c656f02021-06-07 13:32:39 +0900361 .filter(n -> (NETWORK_PREFIX + n.name()).equals(name) ||
362 (n.name() + "-net").equals(name))
Jian Lica20b712021-01-18 00:19:31 +0900363 .findAny().orElse(null);
364 if (network != null) {
Jian Li0c656f02021-06-07 13:32:39 +0900365 // FIXME: we do not update IP address, as learning IP address
366 // requires much more time due to the lag from VM agent
367 String mac = interfaceJson.get(MAC).asText();
Daniel Parkf3136042021-03-10 07:49:11 +0900368 builder.macAddress(MacAddress.valueOf(mac))
Jian Lica20b712021-01-18 00:19:31 +0900369 .networkId(network.networkId());
Jian Lid4296d02021-03-12 18:03:58 +0900370 ports.add(builder.build());
Jian Lica20b712021-01-18 00:19:31 +0900371 }
372 }
Jian Lid4296d02021-03-12 18:03:58 +0900373 return ports;
Jian Li0c656f02021-06-07 13:32:39 +0900374 } catch (IOException e) {
375 log.error("Failed to parse port info from VMI object", e);
376 }
377 return ImmutableSet.of();
378 }
Jian Lid4296d02021-03-12 18:03:58 +0900379
Jian Li0c656f02021-06-07 13:32:39 +0900380 public static String parseVmiNodeName(String resource) {
381 String nodeName = null;
382 try {
383 ObjectMapper mapper = new ObjectMapper();
384 JsonNode json = mapper.readTree(resource);
385 JsonNode statusJson = json.get(STATUS);
386 JsonNode nodeNameJson = statusJson.get(NODE_NAME);
387 nodeName = nodeNameJson != null ? nodeNameJson.asText() : null;
388 } catch (IOException e) {
389 log.error("Failed to parse kubevirt VMI nodename");
Jian Lica20b712021-01-18 00:19:31 +0900390 }
391
Jian Li0c656f02021-06-07 13:32:39 +0900392 return nodeName;
Jian Lica20b712021-01-18 00:19:31 +0900393 }
Jian Li858ccd72021-02-04 17:25:01 +0900394
395 /**
396 * Obtains the tunnel bridge to tenant bridge patch port number.
397 *
Daniel Park2884b232021-03-04 18:58:47 +0900398 * @param node kubevirt node
Jian Li858ccd72021-02-04 17:25:01 +0900399 * @param network kubevirt network
400 * @return patch port number
401 */
402 public static PortNumber tunnelToTenantPort(KubevirtNode node, KubevirtNetwork network) {
403 if (network.segmentId() == null) {
404 return null;
405 }
406
407 if (node.tunBridge() == null) {
408 return null;
409 }
410
411 String tunToTenantPortName = TUNNEL_TO_TENANT_PREFIX + segmentIdHex(network.segmentId());
412 return portNumber(node.tunBridge(), tunToTenantPortName);
413 }
414
415 /**
416 * Obtains the tunnel port number of the given node.
417 *
Daniel Park2884b232021-03-04 18:58:47 +0900418 * @param node kubevirt node
Jian Li858ccd72021-02-04 17:25:01 +0900419 * @param network kubevirt network
420 * @return tunnel port number
421 */
422 public static PortNumber tunnelPort(KubevirtNode node, KubevirtNetwork network) {
423 if (network.segmentId() == null) {
424 return null;
425 }
426
427 if (node.tunBridge() == null) {
428 return null;
429 }
430
431 switch (network.type()) {
432 case VXLAN:
433 return node.vxlanPort();
434 case GRE:
435 return node.grePort();
436 case GENEVE:
437 return node.genevePort();
438 case FLAT:
Jian Li2ce718e2021-02-17 20:42:15 +0900439 case VLAN:
Jian Li858ccd72021-02-04 17:25:01 +0900440 default:
441 // do nothing
442 return null;
443 }
444 }
445
Jian Li810f58c2021-02-27 01:10:50 +0900446 public static String parseResourceName(String resource) {
447 try {
448 JSONObject json = new JSONObject(resource);
449 return json.getJSONObject("metadata").getString("name");
450 } catch (JSONException e) {
451 log.error("");
452 }
453 return "";
454 }
455
Daniel Parkf3136042021-03-10 07:49:11 +0900456 public static PortNumber portNumber(DeviceId deviceId, String portName) {
Jian Li858ccd72021-02-04 17:25:01 +0900457 DeviceService deviceService = DefaultServiceDirectory.getService(DeviceService.class);
458 Port port = deviceService.getPorts(deviceId).stream()
459 .filter(p -> p.isEnabled() &&
460 Objects.equals(p.annotations().value(PORT_NAME), portName))
461 .findAny().orElse(null);
462 return port != null ? port.number() : null;
463 }
464
Daniel Park2884b232021-03-04 18:58:47 +0900465 /**
466 * Returns the gateway node for the specified kubevirt router.
467 * Among gateways, only one gateway would act as a gateway per perter.
468 * Currently gateway node is selected based on modulo operation with router hashcode.
469 *
470 * @param nodeService kubevirt node service
471 * @param router kubevirt router
472 * @return elected gateway node
473 */
474 public static KubevirtNode gatewayNodeForSpecifiedRouter(KubevirtNodeService nodeService,
475 KubevirtRouter router) {
476 //TODO: enhance election logic for a better load balancing
477
478 int numOfGateways = nodeService.completeNodes(GATEWAY).size();
479 if (numOfGateways == 0) {
480 return null;
481 }
482 return (KubevirtNode) nodeService.completeNodes(GATEWAY).toArray()[router.hashCode() % numOfGateways];
483 }
484
485 /**
Daniel Parkf3136042021-03-10 07:49:11 +0900486 * Returns the mac address of the router.
487 *
488 * @param router kubevirt router
489 * @return macc address of the router
490 */
491 public static MacAddress getRouterMacAddress(KubevirtRouter router) {
492 if (router.mac() == null) {
493 log.warn("Failed to get mac address of router {}", router.name());
494 }
495
496 return router.mac();
Daniel Park2884b232021-03-04 18:58:47 +0900497 }
498
499 /**
500 * Returns the snat ip address with specified router.
501 *
502 * @param routerService router service
503 * @param internalNetworkId internal network id which is associated with the router
504 * @return snat ip address if exist, null otherwise
505 */
506 public static IpAddress getRouterSnatIpAddress(KubevirtRouterService routerService,
507 String internalNetworkId) {
508 KubevirtRouter router = routerService.routers().stream()
509 .filter(r -> r.internal().contains(internalNetworkId))
510 .findAny().orElse(null);
511
512 if (router == null) {
513 return null;
514 }
515
516 String routerSnatIp = router.external().keySet().stream().findAny().orElse(null);
517
518 if (routerSnatIp == null) {
519 return null;
520 }
521
522 return Ip4Address.valueOf(routerSnatIp);
523 }
Daniel Parkbabde9c2021-03-09 13:37:42 +0900524
525 /**
526 * Returns the kubevirt router with specified kubevirt port.
527 *
528 * @param routerService kubevirt router service
529 * @param kubevirtPort kubevirt port
530 * @return kubevirt router
531 */
532 public static KubevirtRouter getRouterForKubevirtPort(KubevirtRouterService routerService,
533 KubevirtPort kubevirtPort) {
534 if (kubevirtPort.ipAddress() != null) {
535 return routerService.routers().stream()
536 .filter(r -> r.internal().contains(kubevirtPort.networkId()))
537 .findAny().orElse(null);
538 }
539 return null;
540 }
541
542 /**
543 * Returns the kubevirt router with specified kubevirt network.
544 *
545 * @param routerService kubevirt router service
546 * @param kubevirtNetwork kubevirt network
547 * @return kubevirt router
548 */
549 public static KubevirtRouter getRouterForKubevirtNetwork(KubevirtRouterService routerService,
550 KubevirtNetwork kubevirtNetwork) {
551 return routerService.routers().stream()
552 .filter(router -> router.internal().contains(kubevirtNetwork.networkId()))
553 .findAny().orElse(null);
554 }
Daniel Parkf3136042021-03-10 07:49:11 +0900555
556 /**
557 * Returns the external patch port number with specified gateway.
558 *
559 * @param deviceService device service
Jian Li9793ec42021-03-19 15:03:32 +0900560 * @param gatewayNode gateway node
Daniel Parkf3136042021-03-10 07:49:11 +0900561 * @return external patch port number
562 */
563 public static PortNumber externalPatchPortNum(DeviceService deviceService, KubevirtNode gatewayNode) {
Jian Li63f191f2021-03-25 17:14:40 +0900564 String gatewayBridgeName = gatewayNode.gatewayBridgeName();
565 if (gatewayBridgeName == null) {
Jian Li9793ec42021-03-19 15:03:32 +0900566 log.warn("No external interface is attached to gateway {}", gatewayNode.hostname());
567 return null;
568 }
569
Jian Li63f191f2021-03-25 17:14:40 +0900570 String patchPortName = "int-to-" + gatewayBridgeName;
Daniel Parkf3136042021-03-10 07:49:11 +0900571 Port port = deviceService.getPorts(gatewayNode.intgBridge()).stream()
572 .filter(p -> p.isEnabled() &&
Jian Li9793ec42021-03-19 15:03:32 +0900573 Objects.equals(p.annotations().value(PORT_NAME), patchPortName))
Daniel Parkf3136042021-03-10 07:49:11 +0900574 .findAny().orElse(null);
575
576 return port != null ? port.number() : null;
577 }
578
Daniel Park157947f2021-04-09 17:50:53 +0900579 /**
580 * Returns the kubevirt external network with specified router.
581 *
582 * @param networkService kubevirt network service
583 * @param router kubevirt router
584 * @return external network
585 */
Daniel Parkf3136042021-03-10 07:49:11 +0900586 public static KubevirtNetwork getExternalNetworkByRouter(KubevirtNetworkService networkService,
587 KubevirtRouter router) {
588 String networkId = router.external().values().stream().findAny().orElse(null);
589 if (networkId == null) {
590 return null;
591 }
592
593 return networkService.network(networkId);
594 }
Daniel Park157947f2021-04-09 17:50:53 +0900595
Jian Li94b6d162021-04-15 17:09:11 +0900596 /**
597 * Resolve a DNS with the given DNS server and hostname.
598 *
599 * @param hostname hostname to be resolved
600 * @return resolved IP address
601 */
602 public static IpAddress resolveHostname(String hostname) {
603 try {
604 InetAddress addr = Address.getByName(hostname);
605 return IpAddress.valueOf(IpAddress.Version.INET, addr.getAddress());
606 } catch (UnknownHostException e) {
607 log.warn("Failed to resolve IP address of host {}", hostname);
608 }
609 return null;
610 }
611
612 /**
613 * Builds a GARP packet using the given source MAC and source IP address.
614 *
615 * @param srcMac source MAC address
616 * @param srcIp source IP address
617 * @return GARP packet
618 */
Daniel Park157947f2021-04-09 17:50:53 +0900619 public static Ethernet buildGarpPacket(MacAddress srcMac, IpAddress srcIp) {
620 if (srcMac == null || srcIp == null) {
621 return null;
622 }
623
624 Ethernet ethernet = new Ethernet();
625 ethernet.setDestinationMACAddress(MacAddress.BROADCAST);
626 ethernet.setSourceMACAddress(srcMac);
627 ethernet.setEtherType(Ethernet.TYPE_ARP);
628
629 ARP arp = new ARP();
630 arp.setOpCode(ARP.OP_REPLY);
631 arp.setProtocolType(ARP.PROTO_TYPE_IP);
632 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
633
634 arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
635 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
636
637 arp.setSenderHardwareAddress(srcMac.toBytes());
638 arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes());
639
640 arp.setSenderProtocolAddress(srcIp.toOctets());
641 arp.setTargetProtocolAddress(srcIp.toOctets());
642
643 ethernet.setPayload(arp);
644
645 return ethernet;
646 }
Daniel Park05a94582021-05-12 10:57:02 +0900647
648 /**
649 * Obtains flow group key from the given id.
650 *
651 * @param groupId flow group identifier
652 * @return flow group key
653 */
654 public static GroupKey getGroupKey(int groupId) {
655 return new DefaultGroupKey((Integer.toString(groupId)).getBytes());
656 }
657
658 /**
659 * Obtains load balancer set from the given router.
660 *
661 * @param router kubevirt router
662 * @param lbService kubevirt loadbalancer service
663 * @return loadbalancer set
664 */
665 public static Set<KubevirtLoadBalancer> getLoadBalancerSetForRouter(KubevirtRouter router,
666 KubevirtLoadBalancerService lbService) {
667
668 return lbService.loadBalancers().stream()
669 .filter(lb -> router.internal().contains(lb.networkId()))
670 .collect(Collectors.toSet());
671 }
Jian Li0c656f02021-06-07 13:32:39 +0900672
673 /**
674 * Waits for the given length of time.
675 *
676 * @param timeSecond the amount of time for wait in second unit
677 */
678 public static void waitFor(int timeSecond) {
679 try {
680 Thread.sleep(timeSecond * 1000L);
681 } catch (Exception e) {
682 log.error(e.toString());
683 }
684 }
Jian Li43244382021-01-09 00:19:02 +0900685}