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