Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacknetworking.util; |
| 17 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.core.JsonParseException; |
| 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 20 | import com.fasterxml.jackson.databind.JsonMappingException; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 24 | import com.google.common.base.Strings; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 25 | import org.onosproject.cfg.ConfigProperty; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 28 | import org.onosproject.openstacknetworking.api.Constants.VnicType; |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknetworking.api.InstancePort; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 30 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 32 | import org.onosproject.openstacknetworking.impl.DefaultInstancePort; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknode.api.OpenstackAuth; |
| 34 | import org.onosproject.openstacknode.api.OpenstackAuth.Perspective; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 35 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 36 | import org.openstack4j.api.OSClient; |
| 37 | import org.openstack4j.api.client.IOSClientBuilder; |
| 38 | import org.openstack4j.api.exceptions.AuthenticationException; |
| 39 | import org.openstack4j.api.types.Facing; |
| 40 | import org.openstack4j.core.transport.Config; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 41 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 42 | import org.openstack4j.model.ModelEntity; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 43 | import org.openstack4j.model.common.Identifier; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 44 | import org.openstack4j.model.network.NetFloatingIP; |
| 45 | import org.openstack4j.model.network.Network; |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 46 | import org.openstack4j.model.network.Port; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 47 | import org.openstack4j.model.network.RouterInterface; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 48 | import org.openstack4j.openstack.OSFactory; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 49 | import org.openstack4j.openstack.networking.domain.NeutronRouterInterface; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 53 | import javax.net.ssl.HostnameVerifier; |
| 54 | import javax.net.ssl.HttpsURLConnection; |
| 55 | import javax.net.ssl.SSLContext; |
| 56 | import javax.net.ssl.TrustManager; |
| 57 | import javax.net.ssl.X509TrustManager; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 58 | import java.io.IOException; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 59 | import java.io.InputStream; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 60 | import java.security.cert.X509Certificate; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 61 | import java.util.HashMap; |
| 62 | import java.util.Iterator; |
| 63 | import java.util.Map; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 64 | import java.util.Objects; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 65 | import java.util.Optional; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 66 | import java.util.Set; |
| 67 | import java.util.TreeMap; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 68 | |
| 69 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 70 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 71 | import static com.google.common.base.Strings.isNullOrEmpty; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 72 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 73 | import static org.onosproject.openstacknetworking.api.Constants.PCISLOT; |
| 74 | import static org.onosproject.openstacknetworking.api.Constants.PCI_VENDOR_INFO; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 75 | import static org.onosproject.openstacknetworking.api.Constants.PORT_NAME_PREFIX_VM; |
| 76 | import static org.onosproject.openstacknetworking.api.Constants.PORT_NAME_VHOST_USER_PREFIX_VM; |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame^] | 77 | import static org.onosproject.openstacknetworking.api.Constants.UNSUPPORTED_VENDOR; |
Ray Milkey | 9dc5739 | 2018-06-08 08:52:31 -0700 | [diff] [blame] | 78 | import static org.onosproject.openstacknetworking.api.Constants.portNamePrefixMap; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 79 | import static org.openstack4j.core.transport.ObjectMapperSingleton.getContext; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * An utility that used in openstack networking app. |
| 83 | */ |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 84 | public final class OpenstackNetworkingUtil { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 85 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 86 | private static final Logger log = LoggerFactory.getLogger(OpenstackNetworkingUtil.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 87 | |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 88 | private static final int HEX_RADIX = 16; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 89 | private static final String ZERO_FUNCTION_NUMBER = "0"; |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 90 | private static final String PREFIX_DEVICE_NUMBER = "s"; |
| 91 | private static final String PREFIX_FUNCTION_NUMBER = "f"; |
| 92 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 93 | // keystone endpoint related variables |
| 94 | private static final String DOMAIN_DEFAULT = "default"; |
| 95 | private static final String KEYSTONE_V2 = "v2.0"; |
| 96 | private static final String KEYSTONE_V3 = "v3"; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 97 | private static final String SSL_TYPE = "SSL"; |
| 98 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 99 | private static final String PROXY_MODE = "proxy"; |
| 100 | private static final String BROADCAST_MODE = "broadcast"; |
| 101 | |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 102 | private static final String ERR_FLOW = "Failed set flows for floating IP %s: "; |
| 103 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 104 | /** |
| 105 | * Prevents object instantiation from external. |
| 106 | */ |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 107 | private OpenstackNetworkingUtil() { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Interprets JSON string to corresponding openstack model entity object. |
| 112 | * |
| 113 | * @param input JSON string |
| 114 | * @param entityClazz openstack model entity class |
| 115 | * @return openstack model entity object |
| 116 | */ |
| 117 | public static ModelEntity jsonToModelEntity(InputStream input, Class entityClazz) { |
| 118 | ObjectMapper mapper = new ObjectMapper(); |
| 119 | try { |
| 120 | JsonNode jsonTree = mapper.enable(INDENT_OUTPUT).readTree(input); |
| 121 | log.trace(new ObjectMapper().writeValueAsString(jsonTree)); |
| 122 | return ObjectMapperSingleton.getContext(entityClazz) |
| 123 | .readerFor(entityClazz) |
| 124 | .readValue(jsonTree); |
| 125 | } catch (Exception e) { |
| 126 | throw new IllegalArgumentException(); |
| 127 | } |
| 128 | } |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Converts openstack model entity object into JSON object. |
| 132 | * |
| 133 | * @param entity openstack model entity object |
| 134 | * @param entityClazz openstack model entity class |
| 135 | * @return JSON object |
| 136 | */ |
| 137 | public static ObjectNode modelEntityToJson(ModelEntity entity, Class entityClazz) { |
| 138 | ObjectMapper mapper = new ObjectMapper(); |
| 139 | try { |
| 140 | String strModelEntity = ObjectMapperSingleton.getContext(entityClazz) |
| 141 | .writerFor(entityClazz) |
| 142 | .writeValueAsString(entity); |
| 143 | log.trace(strModelEntity); |
| 144 | return (ObjectNode) mapper.readTree(strModelEntity.getBytes()); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 145 | } catch (IOException e) { |
| 146 | log.error("IOException occurred because of {}", e.toString()); |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 147 | throw new IllegalStateException(); |
| 148 | } |
| 149 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 150 | |
| 151 | /** |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 152 | * Obtains a floating IP associated with the given instance port. |
| 153 | * |
| 154 | * @param port instance port |
| 155 | * @param fips a collection of floating IPs |
| 156 | * @return associated floating IP |
| 157 | */ |
| 158 | public static NetFloatingIP associatedFloatingIp(InstancePort port, |
| 159 | Set<NetFloatingIP> fips) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 160 | for (NetFloatingIP fip : fips) { |
| 161 | if (Strings.isNullOrEmpty(fip.getFixedIpAddress())) { |
| 162 | continue; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 163 | } |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 164 | if (Strings.isNullOrEmpty(fip.getFloatingIpAddress())) { |
| 165 | continue; |
| 166 | } |
| 167 | if (fip.getFixedIpAddress().equals(port.ipAddress().toString())) { |
| 168 | return fip; |
| 169 | } |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 170 | } |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 171 | |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 172 | return null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Checks whether the given floating IP is associated with a VM. |
| 177 | * |
| 178 | * @param service openstack network service |
| 179 | * @param fip floating IP |
| 180 | * @return true if the given floating IP associated with a VM, false otherwise |
| 181 | */ |
| 182 | public static boolean isAssociatedWithVM(OpenstackNetworkService service, |
| 183 | NetFloatingIP fip) { |
| 184 | Port osPort = service.port(fip.getPortId()); |
| 185 | if (osPort == null) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | if (!Strings.isNullOrEmpty(osPort.getDeviceId())) { |
| 190 | Network osNet = service.network(osPort.getNetworkId()); |
| 191 | if (osNet == null) { |
| 192 | final String errorFormat = ERR_FLOW + "no network(%s) exists"; |
| 193 | final String error = String.format(errorFormat, |
| 194 | fip.getFloatingIpAddress(), osPort.getNetworkId()); |
| 195 | throw new IllegalStateException(error); |
| 196 | } |
| 197 | return true; |
| 198 | } else { |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 204 | * Obtains the gateway node by instance port. |
| 205 | * |
| 206 | * @param gateways a collection of gateway nodes |
| 207 | * @param instPort instance port |
| 208 | * @return a gateway node |
| 209 | */ |
| 210 | public static OpenstackNode getGwByInstancePort(Set<OpenstackNode> gateways, |
| 211 | InstancePort instPort) { |
| 212 | OpenstackNode gw = null; |
| 213 | if (instPort != null && instPort.deviceId() != null) { |
| 214 | gw = getGwByComputeDevId(gateways, instPort.deviceId()); |
| 215 | } |
| 216 | return gw; |
| 217 | } |
| 218 | |
| 219 | /** |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 220 | * Obtains the gateway node by device in compute node. Note that the gateway |
| 221 | * node is determined by device's device identifier. |
| 222 | * |
| 223 | * @param gws a collection of gateway nodes |
| 224 | * @param deviceId device identifier |
| 225 | * @return a gateway node |
| 226 | */ |
| 227 | public static OpenstackNode getGwByComputeDevId(Set<OpenstackNode> gws, DeviceId deviceId) { |
| 228 | int numOfGw = gws.size(); |
| 229 | |
| 230 | if (numOfGw == 0) { |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | int gwIndex = Math.abs(deviceId.hashCode()) % numOfGw; |
| 235 | |
| 236 | return getGwByIndex(gws, gwIndex); |
| 237 | } |
| 238 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 239 | /** |
| 240 | * Obtains a connected openstack client. |
| 241 | * |
| 242 | * @param osNode openstack node |
| 243 | * @return a connected openstack client |
| 244 | */ |
| 245 | public static OSClient getConnectedClient(OpenstackNode osNode) { |
| 246 | OpenstackAuth auth = osNode.authentication(); |
| 247 | String endpoint = buildEndpoint(osNode); |
| 248 | Perspective perspective = auth.perspective(); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 249 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 250 | Config config = getSslConfig(); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 251 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 252 | try { |
| 253 | if (endpoint.contains(KEYSTONE_V2)) { |
| 254 | IOSClientBuilder.V2 builder = OSFactory.builderV2() |
| 255 | .endpoint(endpoint) |
| 256 | .tenantName(auth.project()) |
| 257 | .credentials(auth.username(), auth.password()) |
| 258 | .withConfig(config); |
| 259 | |
| 260 | if (perspective != null) { |
| 261 | builder.perspective(getFacing(perspective)); |
| 262 | } |
| 263 | |
| 264 | return builder.authenticate(); |
| 265 | } else if (endpoint.contains(KEYSTONE_V3)) { |
| 266 | |
| 267 | Identifier project = Identifier.byName(auth.project()); |
| 268 | Identifier domain = Identifier.byName(DOMAIN_DEFAULT); |
| 269 | |
| 270 | IOSClientBuilder.V3 builder = OSFactory.builderV3() |
| 271 | .endpoint(endpoint) |
| 272 | .credentials(auth.username(), auth.password(), domain) |
| 273 | .scopeToProject(project, domain) |
| 274 | .withConfig(config); |
| 275 | |
| 276 | if (perspective != null) { |
| 277 | builder.perspective(getFacing(perspective)); |
| 278 | } |
| 279 | |
| 280 | return builder.authenticate(); |
| 281 | } else { |
| 282 | log.warn("Unrecognized keystone version type"); |
| 283 | return null; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 284 | } |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 285 | } catch (AuthenticationException e) { |
| 286 | log.error("Authentication failed due to {}", e.toString()); |
| 287 | return null; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 288 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 289 | } |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * Extract the interface name with the supplied port. |
| 293 | * |
| 294 | * @param port port |
| 295 | * @return interface name |
| 296 | */ |
| 297 | public static String getIntfNameFromPciAddress(Port port) { |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 298 | if (port.getProfile() == null || port.getProfile().isEmpty()) { |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 299 | log.error("Port profile is not found"); |
| 300 | return null; |
| 301 | } |
| 302 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 303 | if (!port.getProfile().containsKey(PCISLOT) || |
| 304 | Strings.isNullOrEmpty(port.getProfile().get(PCISLOT).toString())) { |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 305 | log.error("Failed to retrieve the interface name because of no pci_slot information from the port"); |
| 306 | return null; |
| 307 | } |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 308 | |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 309 | String busNumHex = port.getProfile().get(PCISLOT).toString().split(":")[1]; |
| 310 | String busNumDecimal = String.valueOf(Integer.parseInt(busNumHex, HEX_RADIX)); |
| 311 | |
| 312 | String deviceNumHex = port.getProfile().get(PCISLOT).toString() |
| 313 | .split(":")[2] |
| 314 | .split("\\.")[0]; |
| 315 | String deviceNumDecimal = String.valueOf(Integer.parseInt(deviceNumHex, HEX_RADIX)); |
| 316 | |
| 317 | String functionNumHex = port.getProfile().get(PCISLOT).toString() |
| 318 | .split(":")[2] |
| 319 | .split("\\.")[1]; |
| 320 | String functionNumDecimal = String.valueOf(Integer.parseInt(functionNumHex, HEX_RADIX)); |
| 321 | |
| 322 | String intfName; |
| 323 | |
| 324 | String vendorInfoForPort = String.valueOf(port.getProfile().get(PCI_VENDOR_INFO)); |
| 325 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 326 | if (!portNamePrefixMap().containsKey(vendorInfoForPort)) { |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame^] | 327 | log.warn("Failed to retrieve the interface name because of unsupported prefix for vendor ID {}", |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 328 | vendorInfoForPort); |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame^] | 329 | return UNSUPPORTED_VENDOR; |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 330 | } |
Ray Milkey | 9dc5739 | 2018-06-08 08:52:31 -0700 | [diff] [blame] | 331 | String portNamePrefix = portNamePrefixMap().get(vendorInfoForPort); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 332 | |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 333 | if (functionNumDecimal.equals(ZERO_FUNCTION_NUMBER)) { |
| 334 | intfName = portNamePrefix + busNumDecimal + PREFIX_DEVICE_NUMBER + deviceNumDecimal; |
| 335 | } else { |
| 336 | intfName = portNamePrefix + busNumDecimal + PREFIX_DEVICE_NUMBER + deviceNumDecimal |
| 337 | + PREFIX_FUNCTION_NUMBER + functionNumDecimal; |
| 338 | } |
| 339 | |
| 340 | return intfName; |
| 341 | } |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 342 | |
| 343 | /** |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 344 | * Check if the given interface is added to the given device or not. |
| 345 | * |
| 346 | * @param deviceId device ID |
| 347 | * @param intfName interface name |
| 348 | * @param deviceService device service |
| 349 | * @return true if the given interface is added to the given device or false otherwise |
| 350 | */ |
| 351 | public static boolean hasIntfAleadyInDevice(DeviceId deviceId, String intfName, DeviceService deviceService) { |
| 352 | checkNotNull(deviceId); |
| 353 | checkNotNull(intfName); |
| 354 | |
| 355 | return deviceService.getPorts(deviceId).stream() |
| 356 | .anyMatch(port -> Objects.equals(port.annotations().value(PORT_NAME), intfName)); |
| 357 | } |
| 358 | |
| 359 | /** |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 360 | * Adds router interfaces to openstack admin service. |
| 361 | * TODO fix the logic to add router interface to router |
| 362 | * |
| 363 | * @param osPort port |
| 364 | * @param adminService openstack admin service |
| 365 | */ |
| 366 | public static void addRouterIface(Port osPort, OpenstackRouterAdminService adminService) { |
| 367 | osPort.getFixedIps().forEach(p -> { |
| 368 | JsonNode jsonTree = new ObjectMapper().createObjectNode() |
| 369 | .put("id", osPort.getDeviceId()) |
| 370 | .put("tenant_id", osPort.getTenantId()) |
| 371 | .put("subnet_id", p.getSubnetId()) |
| 372 | .put("port_id", osPort.getId()); |
| 373 | try { |
| 374 | RouterInterface rIface = getContext(NeutronRouterInterface.class) |
| 375 | .readerFor(NeutronRouterInterface.class) |
| 376 | .readValue(jsonTree); |
| 377 | if (adminService.routerInterface(rIface.getPortId()) != null) { |
| 378 | adminService.updateRouterInterface(rIface); |
| 379 | } else { |
| 380 | adminService.addRouterInterface(rIface); |
| 381 | } |
| 382 | } catch (IOException ignore) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 383 | log.error("Exception occurred because of {}", ignore.toString()); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 384 | } |
| 385 | }); |
| 386 | } |
| 387 | |
| 388 | /** |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 389 | * Obtains the property value with specified property key name. |
| 390 | * |
| 391 | * @param properties a collection of properties |
| 392 | * @param name key name |
| 393 | * @return mapping value |
| 394 | */ |
| 395 | public static String getPropertyValue(Set<ConfigProperty> properties, String name) { |
| 396 | Optional<ConfigProperty> property = |
| 397 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 398 | return property.map(ConfigProperty::value).orElse(null); |
| 399 | } |
| 400 | |
| 401 | /** |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 402 | * Prints out the JSON string in pretty format. |
| 403 | * |
| 404 | * @param mapper Object mapper |
| 405 | * @param jsonString JSON string |
| 406 | * @return pretty formatted JSON string |
| 407 | */ |
| 408 | public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| 409 | try { |
| 410 | Object jsonObject = mapper.readValue(jsonString, Object.class); |
| 411 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 412 | } catch (JsonParseException e) { |
| 413 | log.debug("JsonParseException caused by {}", e); |
| 414 | } catch (JsonMappingException e) { |
| 415 | log.debug("JsonMappingException caused by {}", e); |
| 416 | } catch (JsonProcessingException e) { |
| 417 | log.debug("JsonProcessingException caused by {}", e); |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 418 | } catch (IOException e) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 419 | log.debug("IOException caused by {}", e); |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 420 | } |
| 421 | return null; |
| 422 | } |
| 423 | |
| 424 | /** |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 425 | * Checks the validity of ARP mode. |
| 426 | * |
| 427 | * @param arpMode ARP mode |
| 428 | * @return returns true if the ARP mode is valid, false otherwise |
| 429 | */ |
| 430 | public static boolean checkArpMode(String arpMode) { |
| 431 | |
| 432 | if (isNullOrEmpty(arpMode)) { |
| 433 | return false; |
| 434 | } else { |
| 435 | return arpMode.equals(PROXY_MODE) || arpMode.equals(BROADCAST_MODE); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 440 | * Swaps current location with old location info. |
| 441 | * The revised instance port will be used to mod the flow rules after migration. |
| 442 | * |
| 443 | * @param instPort instance port |
| 444 | * @return location swapped instance port |
| 445 | */ |
| 446 | public static InstancePort swapStaleLocation(InstancePort instPort) { |
| 447 | return DefaultInstancePort.builder() |
| 448 | .deviceId(instPort.oldDeviceId()) |
| 449 | .portNumber(instPort.oldPortNumber()) |
| 450 | .state(instPort.state()) |
| 451 | .ipAddress(instPort.ipAddress()) |
| 452 | .macAddress(instPort.macAddress()) |
| 453 | .networkId(instPort.networkId()) |
| 454 | .portId(instPort.portId()) |
| 455 | .build(); |
| 456 | } |
| 457 | |
| 458 | /** |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 459 | * Compares two router interfaces are equal. |
| 460 | * Will be remove this after Openstack4j implements equals. |
| 461 | * |
| 462 | * @param routerInterface1 router interface |
| 463 | * @param routerInterface2 router interface |
| 464 | * @return returns true if two router interfaces are equal, false otherwise |
| 465 | */ |
| 466 | public static boolean routerInterfacesEquals(RouterInterface routerInterface1, RouterInterface routerInterface2) { |
| 467 | return Objects.equals(routerInterface1.getId(), routerInterface2.getId()) && |
| 468 | Objects.equals(routerInterface1.getPortId(), routerInterface2.getPortId()) && |
| 469 | Objects.equals(routerInterface1.getSubnetId(), routerInterface2.getSubnetId()) && |
| 470 | Objects.equals(routerInterface1.getTenantId(), routerInterface2.getTenantId()); |
| 471 | } |
| 472 | |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 473 | public static VnicType vnicType(String portName) { |
| 474 | if (portName.startsWith(PORT_NAME_PREFIX_VM) || |
| 475 | portName.startsWith(PORT_NAME_VHOST_USER_PREFIX_VM)) { |
| 476 | return VnicType.NORMAL; |
| 477 | } else if (isDirectPort(portName)) { |
| 478 | return VnicType.DIRECT; |
| 479 | } else { |
| 480 | return VnicType.UNSUPPORTED; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | private static boolean isDirectPort(String portName) { |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame^] | 485 | return portNamePrefixMap().values().stream().anyMatch(p -> portName.startsWith(p)); |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 486 | } |
| 487 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 488 | /** |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 489 | * Builds up and a complete endpoint URL from gateway node. |
| 490 | * |
| 491 | * @param node gateway node |
| 492 | * @return a complete endpoint URL |
| 493 | */ |
| 494 | private static String buildEndpoint(OpenstackNode node) { |
| 495 | |
| 496 | OpenstackAuth auth = node.authentication(); |
| 497 | |
| 498 | StringBuilder endpointSb = new StringBuilder(); |
| 499 | endpointSb.append(auth.protocol().name().toLowerCase()); |
| 500 | endpointSb.append("://"); |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 501 | endpointSb.append(node.endpoint()); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 502 | return endpointSb.toString(); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Obtains the SSL config without verifying the certification. |
| 507 | * |
| 508 | * @return SSL config |
| 509 | */ |
| 510 | private static Config getSslConfig() { |
| 511 | // we bypass the SSL certification verification for now |
| 512 | // TODO: verify server side SSL using a given certification |
| 513 | Config config = Config.newConfig().withSSLVerificationDisabled(); |
| 514 | |
| 515 | TrustManager[] trustAllCerts = new TrustManager[]{ |
| 516 | new X509TrustManager() { |
| 517 | public X509Certificate[] getAcceptedIssuers() { |
| 518 | return null; |
| 519 | } |
| 520 | |
| 521 | public void checkClientTrusted(X509Certificate[] certs, |
| 522 | String authType) { |
| 523 | } |
| 524 | |
| 525 | public void checkServerTrusted(X509Certificate[] certs, |
| 526 | String authType) { |
| 527 | } |
| 528 | } |
| 529 | }; |
| 530 | |
| 531 | HostnameVerifier allHostsValid = (hostname, session) -> true; |
| 532 | |
| 533 | try { |
| 534 | SSLContext sc = SSLContext.getInstance(SSL_TYPE); |
| 535 | sc.init(null, trustAllCerts, |
| 536 | new java.security.SecureRandom()); |
| 537 | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
| 538 | HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); |
| 539 | |
| 540 | config.withSSLContext(sc); |
| 541 | } catch (Exception e) { |
| 542 | log.error("Failed to access OpenStack service due to {}", e.toString()); |
| 543 | return null; |
| 544 | } |
| 545 | |
| 546 | return config; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Obtains the facing object with given openstack perspective. |
| 551 | * |
| 552 | * @param perspective keystone perspective |
| 553 | * @return facing object |
| 554 | */ |
| 555 | private static Facing getFacing(Perspective perspective) { |
| 556 | |
| 557 | switch (perspective) { |
| 558 | case PUBLIC: |
| 559 | return Facing.PUBLIC; |
| 560 | case ADMIN: |
| 561 | return Facing.ADMIN; |
| 562 | case INTERNAL: |
| 563 | return Facing.INTERNAL; |
| 564 | default: |
| 565 | return null; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Obtains gateway instance by giving index number. |
| 571 | * |
| 572 | * @param gws a collection of gateway nodes |
| 573 | * @param index index number |
| 574 | * @return gateway instance |
| 575 | */ |
| 576 | private static OpenstackNode getGwByIndex(Set<OpenstackNode> gws, int index) { |
| 577 | Map<String, OpenstackNode> hashMap = new HashMap<>(); |
| 578 | gws.forEach(gw -> hashMap.put(gw.hostname(), gw)); |
| 579 | TreeMap<String, OpenstackNode> treeMap = new TreeMap<>(hashMap); |
| 580 | Iterator<String> iteratorKey = treeMap.keySet().iterator(); |
| 581 | |
| 582 | int intIndex = 0; |
| 583 | OpenstackNode gw = null; |
| 584 | while (iteratorKey.hasNext()) { |
| 585 | String key = iteratorKey.next(); |
| 586 | |
| 587 | if (intIndex == index) { |
| 588 | gw = treeMap.get(key); |
| 589 | } |
| 590 | intIndex++; |
| 591 | } |
| 592 | return gw; |
| 593 | } |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 594 | } |