Jian Li | 4324438 | 2021-01-09 00:19:02 +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.util; |
| 17 | |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame] | 21 | import com.google.common.base.Strings; |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 23 | import io.fabric8.kubernetes.client.ConfigBuilder; |
| 24 | import io.fabric8.kubernetes.client.DefaultKubernetesClient; |
| 25 | import io.fabric8.kubernetes.client.KubernetesClient; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 26 | import org.apache.commons.lang.StringUtils; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 27 | import org.apache.commons.net.util.SubnetUtils; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 28 | import org.json.JSONException; |
| 29 | import org.json.JSONObject; |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 30 | import org.onlab.osgi.DefaultServiceDirectory; |
Daniel Park | 157947f | 2021-04-09 17:50:53 +0900 | [diff] [blame] | 31 | import org.onlab.packet.ARP; |
| 32 | import org.onlab.packet.Ethernet; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 33 | import org.onlab.packet.Ip4Address; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 35 | import org.onlab.packet.MacAddress; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 36 | import org.onosproject.cfg.ConfigProperty; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 37 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 38 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer; |
| 39 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerService; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 40 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 41 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 42 | import org.onosproject.kubevirtnetworking.api.KubevirtPort; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 43 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 44 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterService; |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 45 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 46 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 47 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 48 | import org.onosproject.kubevirtnode.api.KubevirtNodeService; |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 49 | import org.onosproject.net.DeviceId; |
| 50 | import org.onosproject.net.Port; |
| 51 | import org.onosproject.net.PortNumber; |
| 52 | import org.onosproject.net.device.DeviceService; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 53 | import org.onosproject.net.group.DefaultGroupKey; |
| 54 | import org.onosproject.net.group.GroupKey; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | import org.slf4j.LoggerFactory; |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 57 | import org.xbill.DNS.Address; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 58 | |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 59 | import java.io.IOException; |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 60 | import java.net.InetAddress; |
| 61 | import java.net.UnknownHostException; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 62 | import java.util.Arrays; |
| 63 | import java.util.HashSet; |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 64 | import java.util.List; |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 65 | import java.util.Objects; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 66 | import java.util.Optional; |
| 67 | import java.util.Set; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 68 | import java.util.stream.Collectors; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 69 | |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 70 | import static org.onosproject.kubevirtnetworking.api.Constants.TUNNEL_TO_TENANT_PREFIX; |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 71 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.GATEWAY; |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 72 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 73 | |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 74 | /** |
| 75 | * An utility that used in KubeVirt networking app. |
| 76 | */ |
| 77 | public 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 Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 82 | private static final String COLON_SLASH = "://"; |
| 83 | private static final String COLON = ":"; |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame] | 84 | private static final String OF_PREFIX = "of:"; |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 85 | |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 86 | 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 Park | babde9c | 2021-03-09 13:37:42 +0900 | [diff] [blame] | 91 | private static final String BR_INT = "br-int"; |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 92 | 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 Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 96 | |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 97 | /** |
| 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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 106 | * @param properties a collection of properties |
| 107 | * @param name key name |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 108 | * @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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 122 | * @param portName original port name |
Jian Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 123 | * @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 Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 134 | |
| 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 Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame] | 152 | * 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 Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 167 | * Prints out the JSON string in pretty format. |
| 168 | * |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 169 | * @param mapper Object mapper |
| 170 | * @param jsonString JSON string |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 171 | * @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 Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 182 | |
| 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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 208 | * @param ipAddr IP address |
| 209 | * @param prefixLength subnet prefix length |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 210 | * @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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 217 | |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 218 | /** |
| 219 | * Generates endpoint URL by referring to scheme, ipAddress and port. |
| 220 | * |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 221 | * @param scheme scheme |
| 222 | * @param ipAddress IP address |
| 223 | * @param port port number |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 224 | * @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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 242 | * @param apiConfig kubernetes API config |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 243 | * @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 Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 267 | .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 Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 297 | |
| 298 | /** |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame] | 299 | * 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 Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 310 | * Obtains the tunnel port number with the given network and node. |
| 311 | * |
| 312 | * @param network kubevirt network |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 313 | * @param node kubevirt node |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 314 | * @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 Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 331 | * Obtains the kubevirt port from kubevirt VMI. |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 332 | * |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 333 | * @param nodeService kubevirt node service |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 334 | * @param networks set of existing kubevirt networks |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 335 | * @param resource VMI definition |
| 336 | * @return kubevirt ports attached to the VMI |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 337 | */ |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 338 | public static Set<KubevirtPort> getPorts(KubevirtNodeService nodeService, |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 339 | Set<KubevirtNetwork> networks, |
| 340 | String resource) { |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 341 | try { |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 342 | ObjectMapper mapper = new ObjectMapper(); |
| 343 | JsonNode json = mapper.readTree(resource); |
| 344 | JsonNode statusJson = json.get(STATUS); |
| 345 | ArrayNode interfacesJson = (ArrayNode) statusJson.get(INTERFACES); |
Jian Li | b6dc08f | 2021-03-24 15:24:18 +0900 | [diff] [blame] | 346 | |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 347 | KubevirtPort.Builder builder = DefaultKubevirtPort.builder(); |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 348 | String nodeName = parseVmiNodeName(resource); |
| 349 | if (nodeName != null && nodeService.node(nodeName) != null) { |
| 350 | builder.deviceId(nodeService.node(nodeName).intgBridge()); |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 351 | } |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 352 | |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 353 | if (interfacesJson == null) { |
| 354 | return ImmutableSet.of(); |
| 355 | } |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 356 | |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 357 | Set<KubevirtPort> ports = new HashSet<>(); |
| 358 | for (JsonNode interfaceJson : interfacesJson) { |
| 359 | String name = interfaceJson.get(NAME).asText(); |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 360 | KubevirtNetwork network = networks.stream() |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 361 | .filter(n -> (NETWORK_PREFIX + n.name()).equals(name) || |
| 362 | (n.name() + "-net").equals(name)) |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 363 | .findAny().orElse(null); |
| 364 | if (network != null) { |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 365 | // 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 Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 368 | builder.macAddress(MacAddress.valueOf(mac)) |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 369 | .networkId(network.networkId()); |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 370 | ports.add(builder.build()); |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 371 | } |
| 372 | } |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 373 | return ports; |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 374 | } catch (IOException e) { |
| 375 | log.error("Failed to parse port info from VMI object", e); |
| 376 | } |
| 377 | return ImmutableSet.of(); |
| 378 | } |
Jian Li | d4296d0 | 2021-03-12 18:03:58 +0900 | [diff] [blame] | 379 | |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 380 | 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 Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 390 | } |
| 391 | |
Jian Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 392 | return nodeName; |
Jian Li | ca20b71 | 2021-01-18 00:19:31 +0900 | [diff] [blame] | 393 | } |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 394 | |
| 395 | /** |
| 396 | * Obtains the tunnel bridge to tenant bridge patch port number. |
| 397 | * |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 398 | * @param node kubevirt node |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 399 | * @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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 418 | * @param node kubevirt node |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 419 | * @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 Li | 2ce718e | 2021-02-17 20:42:15 +0900 | [diff] [blame] | 439 | case VLAN: |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 440 | default: |
| 441 | // do nothing |
| 442 | return null; |
| 443 | } |
| 444 | } |
| 445 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 446 | 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 Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 456 | public static PortNumber portNumber(DeviceId deviceId, String portName) { |
Jian Li | 858ccd7 | 2021-02-04 17:25:01 +0900 | [diff] [blame] | 457 | 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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 465 | /** |
| 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 Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 486 | * 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 Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 497 | } |
| 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 Park | babde9c | 2021-03-09 13:37:42 +0900 | [diff] [blame] | 524 | |
| 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 Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 555 | |
| 556 | /** |
| 557 | * Returns the external patch port number with specified gateway. |
| 558 | * |
| 559 | * @param deviceService device service |
Jian Li | 9793ec4 | 2021-03-19 15:03:32 +0900 | [diff] [blame] | 560 | * @param gatewayNode gateway node |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 561 | * @return external patch port number |
| 562 | */ |
| 563 | public static PortNumber externalPatchPortNum(DeviceService deviceService, KubevirtNode gatewayNode) { |
Jian Li | 63f191f | 2021-03-25 17:14:40 +0900 | [diff] [blame] | 564 | String gatewayBridgeName = gatewayNode.gatewayBridgeName(); |
| 565 | if (gatewayBridgeName == null) { |
Jian Li | 9793ec4 | 2021-03-19 15:03:32 +0900 | [diff] [blame] | 566 | log.warn("No external interface is attached to gateway {}", gatewayNode.hostname()); |
| 567 | return null; |
| 568 | } |
| 569 | |
Jian Li | 63f191f | 2021-03-25 17:14:40 +0900 | [diff] [blame] | 570 | String patchPortName = "int-to-" + gatewayBridgeName; |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 571 | Port port = deviceService.getPorts(gatewayNode.intgBridge()).stream() |
| 572 | .filter(p -> p.isEnabled() && |
Jian Li | 9793ec4 | 2021-03-19 15:03:32 +0900 | [diff] [blame] | 573 | Objects.equals(p.annotations().value(PORT_NAME), patchPortName)) |
Daniel Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 574 | .findAny().orElse(null); |
| 575 | |
| 576 | return port != null ? port.number() : null; |
| 577 | } |
| 578 | |
Daniel Park | 157947f | 2021-04-09 17:50:53 +0900 | [diff] [blame] | 579 | /** |
| 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 Park | f313604 | 2021-03-10 07:49:11 +0900 | [diff] [blame] | 586 | 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 Park | 157947f | 2021-04-09 17:50:53 +0900 | [diff] [blame] | 595 | |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 596 | /** |
| 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 Park | 157947f | 2021-04-09 17:50:53 +0900 | [diff] [blame] | 619 | 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 Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame] | 647 | |
| 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 Li | 0c656f0 | 2021-06-07 13:32:39 +0900 | [diff] [blame] | 672 | |
| 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 Li | 4324438 | 2021-01-09 00:19:02 +0900 | [diff] [blame] | 685 | } |