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; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 24 | import com.google.common.base.Charsets; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 25 | import com.google.common.base.Strings; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 26 | import com.google.common.collect.Lists; |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 27 | import org.apache.commons.codec.binary.Hex; |
Jian Li | 5172870 | 2019-05-17 18:38:56 +0900 | [diff] [blame] | 28 | import org.apache.commons.lang3.StringUtils; |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 29 | import org.apache.http.HttpException; |
| 30 | import org.apache.http.HttpRequest; |
| 31 | import org.apache.http.HttpResponse; |
| 32 | import org.apache.http.impl.io.DefaultHttpRequestParser; |
| 33 | import org.apache.http.impl.io.DefaultHttpRequestWriter; |
| 34 | import org.apache.http.impl.io.DefaultHttpResponseParser; |
| 35 | import org.apache.http.impl.io.DefaultHttpResponseWriter; |
| 36 | import org.apache.http.impl.io.HttpTransportMetricsImpl; |
| 37 | import org.apache.http.impl.io.SessionInputBufferImpl; |
| 38 | import org.apache.http.impl.io.SessionOutputBufferImpl; |
| 39 | import org.apache.http.io.HttpMessageWriter; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 40 | import org.apache.sshd.client.SshClient; |
| 41 | import org.apache.sshd.client.channel.ClientChannel; |
| 42 | import org.apache.sshd.client.channel.ClientChannelEvent; |
| 43 | import org.apache.sshd.client.future.OpenFuture; |
| 44 | import org.apache.sshd.client.session.ClientSession; |
| 45 | import org.apache.sshd.common.util.io.NoCloseInputStream; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 46 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 47 | import org.onlab.packet.ARP; |
| 48 | import org.onlab.packet.Ethernet; |
| 49 | import org.onlab.packet.Ip4Address; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 50 | import org.onlab.packet.IpAddress; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 51 | import org.onlab.packet.MacAddress; |
| 52 | import org.onlab.packet.VlanId; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 53 | import org.onosproject.cfg.ConfigProperty; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 54 | import org.onosproject.net.DeviceId; |
Jian Li | 2d68c19 | 2018-12-13 15:52:59 +0900 | [diff] [blame] | 55 | import org.onosproject.net.PortNumber; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 56 | import org.onosproject.net.device.DeviceService; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 57 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 58 | import org.onosproject.net.flow.TrafficTreatment; |
Jian Li | d572762 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 59 | import org.onosproject.net.group.DefaultGroupKey; |
| 60 | import org.onosproject.net.group.GroupKey; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 61 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 62 | import org.onosproject.net.packet.PacketService; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 63 | import org.onosproject.openstacknetworking.api.Constants.VnicType; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 64 | import org.onosproject.openstacknetworking.api.ExternalPeerRouter; |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 65 | import org.onosproject.openstacknetworking.api.InstancePort; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 66 | import org.onosproject.openstacknetworking.api.OpenstackHaService; |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 67 | import org.onosproject.openstacknetworking.api.OpenstackNetwork.Type; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 68 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 69 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 70 | import org.onosproject.openstacknetworking.api.OpenstackRouterService; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 71 | import org.onosproject.openstacknetworking.impl.DefaultInstancePort; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 72 | import org.onosproject.openstacknode.api.OpenstackAuth; |
| 73 | import org.onosproject.openstacknode.api.OpenstackAuth.Perspective; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 74 | import org.onosproject.openstacknode.api.OpenstackNode; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 75 | import org.onosproject.openstacknode.api.OpenstackSshAuth; |
Jian Li | 5172870 | 2019-05-17 18:38:56 +0900 | [diff] [blame] | 76 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 77 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 78 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 79 | import org.openstack4j.api.OSClient; |
| 80 | import org.openstack4j.api.client.IOSClientBuilder; |
| 81 | import org.openstack4j.api.exceptions.AuthenticationException; |
| 82 | import org.openstack4j.api.types.Facing; |
| 83 | import org.openstack4j.core.transport.Config; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 84 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 85 | import org.openstack4j.model.ModelEntity; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 86 | import org.openstack4j.model.common.Identifier; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 87 | import org.openstack4j.model.network.ExternalGateway; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 88 | import org.openstack4j.model.network.IP; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 89 | import org.openstack4j.model.network.NetFloatingIP; |
| 90 | import org.openstack4j.model.network.Network; |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 91 | import org.openstack4j.model.network.Port; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 92 | import org.openstack4j.model.network.Router; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 93 | import org.openstack4j.model.network.RouterInterface; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 94 | import org.openstack4j.model.network.SecurityGroup; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 95 | import org.openstack4j.model.network.Subnet; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 96 | import org.openstack4j.openstack.OSFactory; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 97 | import org.openstack4j.openstack.networking.domain.NeutronRouterInterface; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 98 | import org.slf4j.Logger; |
| 99 | import org.slf4j.LoggerFactory; |
| 100 | |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 101 | import javax.crypto.Mac; |
| 102 | import javax.crypto.spec.SecretKeySpec; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 103 | import javax.net.ssl.HostnameVerifier; |
| 104 | import javax.net.ssl.HttpsURLConnection; |
| 105 | import javax.net.ssl.SSLContext; |
| 106 | import javax.net.ssl.TrustManager; |
| 107 | import javax.net.ssl.X509TrustManager; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 108 | import javax.ws.rs.client.Client; |
| 109 | import javax.ws.rs.client.ClientBuilder; |
| 110 | import javax.ws.rs.client.Entity; |
| 111 | import javax.ws.rs.client.WebTarget; |
| 112 | import javax.ws.rs.core.Response; |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 113 | import java.io.ByteArrayInputStream; |
| 114 | import java.io.ByteArrayOutputStream; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 115 | import java.io.IOException; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 116 | import java.io.InputStream; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 117 | import java.io.OutputStream; |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 118 | import java.nio.ByteBuffer; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 119 | import java.security.cert.X509Certificate; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 120 | import java.util.Collection; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 121 | import java.util.HashMap; |
| 122 | import java.util.Iterator; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 123 | import java.util.List; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 124 | import java.util.Map; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 125 | import java.util.Objects; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 126 | import java.util.Optional; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 127 | import java.util.Set; |
| 128 | import java.util.TreeMap; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 129 | import java.util.concurrent.TimeUnit; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 130 | import java.util.stream.Collectors; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 131 | |
| 132 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 133 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 134 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 135 | import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; |
| 136 | import static org.apache.commons.io.IOUtils.toInputStream; |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 137 | import static org.onlab.packet.Ip4Address.valueOf; |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 138 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 139 | import static org.onosproject.openstacknetworking.api.Constants.DEFAULT_GATEWAY_MAC_STR; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 140 | import static org.onosproject.openstacknetworking.api.Constants.FLOATING_IP_FORMAT; |
| 141 | import static org.onosproject.openstacknetworking.api.Constants.NETWORK_FORMAT; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 142 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_REST_PATH; |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 143 | import static org.onosproject.openstacknetworking.api.Constants.PCISLOT; |
| 144 | import static org.onosproject.openstacknetworking.api.Constants.PCI_VENDOR_INFO; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 145 | import static org.onosproject.openstacknetworking.api.Constants.PORT_FORMAT; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 146 | import static org.onosproject.openstacknetworking.api.Constants.PORT_NAME_PREFIX_VM; |
| 147 | import static org.onosproject.openstacknetworking.api.Constants.PORT_NAME_VHOST_USER_PREFIX_VM; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 148 | import static org.onosproject.openstacknetworking.api.Constants.REST_PASSWORD; |
| 149 | import static org.onosproject.openstacknetworking.api.Constants.REST_PORT; |
| 150 | import static org.onosproject.openstacknetworking.api.Constants.REST_USER; |
| 151 | import static org.onosproject.openstacknetworking.api.Constants.REST_UTF8; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 152 | import static org.onosproject.openstacknetworking.api.Constants.ROUTER_FORMAT; |
| 153 | import static org.onosproject.openstacknetworking.api.Constants.ROUTER_INTF_FORMAT; |
| 154 | import static org.onosproject.openstacknetworking.api.Constants.SECURITY_GROUP_FORMAT; |
| 155 | import static org.onosproject.openstacknetworking.api.Constants.SUBNET_FORMAT; |
Daniel Park | ec9d113 | 2018-08-19 11:18:03 +0900 | [diff] [blame] | 156 | import static org.onosproject.openstacknetworking.api.Constants.UNSUPPORTED_VENDOR; |
Ray Milkey | 9dc5739 | 2018-06-08 08:52:31 -0700 | [diff] [blame] | 157 | import static org.onosproject.openstacknetworking.api.Constants.portNamePrefixMap; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 158 | import static org.openstack4j.core.transport.ObjectMapperSingleton.getContext; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * An utility that used in openstack networking app. |
| 162 | */ |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 163 | public final class OpenstackNetworkingUtil { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 164 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 165 | private static final Logger log = LoggerFactory.getLogger(OpenstackNetworkingUtil.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 166 | |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 167 | private static final int HEX_RADIX = 16; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 168 | private static final String ZERO_FUNCTION_NUMBER = "0"; |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 169 | private static final String PREFIX_DEVICE_NUMBER = "s"; |
| 170 | private static final String PREFIX_FUNCTION_NUMBER = "f"; |
| 171 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 172 | // keystone endpoint related variables |
| 173 | private static final String DOMAIN_DEFAULT = "default"; |
| 174 | private static final String KEYSTONE_V2 = "v2.0"; |
| 175 | private static final String KEYSTONE_V3 = "v3"; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 176 | private static final String SSL_TYPE = "SSL"; |
| 177 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 178 | private static final String PROXY_MODE = "proxy"; |
| 179 | private static final String BROADCAST_MODE = "broadcast"; |
| 180 | |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 181 | private static final String ENABLE = "enable"; |
| 182 | private static final String DISABLE = "disable"; |
| 183 | |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 184 | private static final int HTTP_PAYLOAD_BUFFER = 8 * 1024; |
| 185 | |
| 186 | private static final String HMAC_SHA256 = "HmacSHA256"; |
| 187 | |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 188 | private static final String ERR_FLOW = "Failed set flows for floating IP %s: "; |
| 189 | |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 190 | private static final String DL_DST = "dl_dst="; |
| 191 | private static final String NW_DST = "nw_dst="; |
| 192 | private static final String DEFAULT_REQUEST_STRING = "sudo ovs-appctl ofproto/trace br-int ip"; |
| 193 | private static final String IN_PORT = "in_port="; |
| 194 | private static final String NW_SRC = "nw_src="; |
| 195 | private static final String COMMA = ","; |
| 196 | private static final String TUN_ID = "tun_id="; |
| 197 | |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 198 | private static final String DEVICE_OWNER_GW = "network:router_gateway"; |
| 199 | private static final String DEVICE_OWNER_IFACE = "network:router_interface"; |
| 200 | |
| 201 | private static final String NOT_AVAILABLE = "N/A"; |
| 202 | |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 203 | private static final long TIMEOUT_MS = 5000; |
| 204 | private static final long WAIT_OUTPUT_STREAM_SECOND = 2; |
| 205 | private static final int SSH_PORT = 22; |
| 206 | |
Jian Li | 5172870 | 2019-05-17 18:38:56 +0900 | [diff] [blame] | 207 | private static final int TAP_PORT_LENGTH = 11; |
Jian Li | a271b3c | 2019-09-03 23:10:20 +0900 | [diff] [blame] | 208 | private static final int PORT_NAME_MAX_LENGTH = 15; |
Jian Li | 5172870 | 2019-05-17 18:38:56 +0900 | [diff] [blame] | 209 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 210 | /** |
| 211 | * Prevents object instantiation from external. |
| 212 | */ |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 213 | private OpenstackNetworkingUtil() { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Interprets JSON string to corresponding openstack model entity object. |
| 218 | * |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 219 | * @param inputStr JSON string |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 220 | * @param entityClazz openstack model entity class |
| 221 | * @return openstack model entity object |
| 222 | */ |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 223 | public static ModelEntity jsonToModelEntity(String inputStr, Class entityClazz) { |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 224 | ObjectMapper mapper = new ObjectMapper(); |
| 225 | try { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 226 | InputStream input = toInputStream(inputStr, REST_UTF8); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 227 | JsonNode jsonTree = mapper.enable(INDENT_OUTPUT).readTree(input); |
| 228 | log.trace(new ObjectMapper().writeValueAsString(jsonTree)); |
| 229 | return ObjectMapperSingleton.getContext(entityClazz) |
| 230 | .readerFor(entityClazz) |
| 231 | .readValue(jsonTree); |
| 232 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 233 | log.error("Exception occurred because of {}", e); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 234 | throw new IllegalArgumentException(); |
| 235 | } |
| 236 | } |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * Converts openstack model entity object into JSON object. |
| 240 | * |
| 241 | * @param entity openstack model entity object |
| 242 | * @param entityClazz openstack model entity class |
| 243 | * @return JSON object |
| 244 | */ |
| 245 | public static ObjectNode modelEntityToJson(ModelEntity entity, Class entityClazz) { |
| 246 | ObjectMapper mapper = new ObjectMapper(); |
| 247 | try { |
| 248 | String strModelEntity = ObjectMapperSingleton.getContext(entityClazz) |
| 249 | .writerFor(entityClazz) |
| 250 | .writeValueAsString(entity); |
| 251 | log.trace(strModelEntity); |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 252 | return (ObjectNode) mapper.readTree(strModelEntity.getBytes(Charsets.UTF_8)); |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 253 | } catch (IOException e) { |
| 254 | log.error("IOException occurred because of {}", e.toString()); |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 255 | throw new IllegalStateException(); |
| 256 | } |
| 257 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 258 | |
| 259 | /** |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 260 | * Obtains a floating IP associated with the given instance port. |
| 261 | * |
| 262 | * @param port instance port |
| 263 | * @param fips a collection of floating IPs |
| 264 | * @return associated floating IP |
| 265 | */ |
| 266 | public static NetFloatingIP associatedFloatingIp(InstancePort port, |
| 267 | Set<NetFloatingIP> fips) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 268 | for (NetFloatingIP fip : fips) { |
| 269 | if (Strings.isNullOrEmpty(fip.getFixedIpAddress())) { |
| 270 | continue; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 271 | } |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 272 | if (Strings.isNullOrEmpty(fip.getFloatingIpAddress())) { |
| 273 | continue; |
| 274 | } |
Jian Li | 6bc29d9 | 2018-10-02 13:55:05 +0900 | [diff] [blame] | 275 | if (fip.getFixedIpAddress().equals(port.ipAddress().toString()) && |
| 276 | fip.getPortId().equals(port.portId())) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 277 | return fip; |
| 278 | } |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 279 | } |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 280 | |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 281 | return null; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Checks whether the given floating IP is associated with a VM. |
| 286 | * |
| 287 | * @param service openstack network service |
| 288 | * @param fip floating IP |
| 289 | * @return true if the given floating IP associated with a VM, false otherwise |
| 290 | */ |
| 291 | public static boolean isAssociatedWithVM(OpenstackNetworkService service, |
| 292 | NetFloatingIP fip) { |
| 293 | Port osPort = service.port(fip.getPortId()); |
| 294 | if (osPort == null) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | if (!Strings.isNullOrEmpty(osPort.getDeviceId())) { |
| 299 | Network osNet = service.network(osPort.getNetworkId()); |
| 300 | if (osNet == null) { |
| 301 | final String errorFormat = ERR_FLOW + "no network(%s) exists"; |
| 302 | final String error = String.format(errorFormat, |
| 303 | fip.getFloatingIpAddress(), osPort.getNetworkId()); |
| 304 | throw new IllegalStateException(error); |
| 305 | } |
| 306 | return true; |
| 307 | } else { |
| 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
Jian Li | a171a43 | 2018-06-11 11:52:11 +0900 | [diff] [blame] | 313 | * Obtains the gateway node by instance port. |
| 314 | * |
| 315 | * @param gateways a collection of gateway nodes |
| 316 | * @param instPort instance port |
| 317 | * @return a gateway node |
| 318 | */ |
| 319 | public static OpenstackNode getGwByInstancePort(Set<OpenstackNode> gateways, |
| 320 | InstancePort instPort) { |
| 321 | OpenstackNode gw = null; |
| 322 | if (instPort != null && instPort.deviceId() != null) { |
| 323 | gw = getGwByComputeDevId(gateways, instPort.deviceId()); |
| 324 | } |
| 325 | return gw; |
| 326 | } |
| 327 | |
| 328 | /** |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 329 | * Obtains the gateway node by device in compute node. Note that the gateway |
| 330 | * node is determined by device's device identifier. |
| 331 | * |
| 332 | * @param gws a collection of gateway nodes |
| 333 | * @param deviceId device identifier |
| 334 | * @return a gateway node |
| 335 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 336 | public static OpenstackNode getGwByComputeDevId(Set<OpenstackNode> gws, |
| 337 | DeviceId deviceId) { |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 338 | int numOfGw = gws.size(); |
| 339 | |
| 340 | if (numOfGw == 0) { |
| 341 | return null; |
| 342 | } |
| 343 | |
| 344 | int gwIndex = Math.abs(deviceId.hashCode()) % numOfGw; |
| 345 | |
| 346 | return getGwByIndex(gws, gwIndex); |
| 347 | } |
| 348 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 349 | /** |
| 350 | * Obtains a connected openstack client. |
| 351 | * |
| 352 | * @param osNode openstack node |
| 353 | * @return a connected openstack client |
| 354 | */ |
| 355 | public static OSClient getConnectedClient(OpenstackNode osNode) { |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 356 | OpenstackAuth auth = osNode.keystoneConfig().authentication(); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 357 | String endpoint = buildEndpoint(osNode); |
| 358 | Perspective perspective = auth.perspective(); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 359 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 360 | Config config = getSslConfig(); |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 361 | |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 362 | try { |
| 363 | if (endpoint.contains(KEYSTONE_V2)) { |
| 364 | IOSClientBuilder.V2 builder = OSFactory.builderV2() |
| 365 | .endpoint(endpoint) |
| 366 | .tenantName(auth.project()) |
| 367 | .credentials(auth.username(), auth.password()) |
| 368 | .withConfig(config); |
| 369 | |
| 370 | if (perspective != null) { |
| 371 | builder.perspective(getFacing(perspective)); |
| 372 | } |
| 373 | |
| 374 | return builder.authenticate(); |
| 375 | } else if (endpoint.contains(KEYSTONE_V3)) { |
| 376 | |
| 377 | Identifier project = Identifier.byName(auth.project()); |
| 378 | Identifier domain = Identifier.byName(DOMAIN_DEFAULT); |
| 379 | |
| 380 | IOSClientBuilder.V3 builder = OSFactory.builderV3() |
| 381 | .endpoint(endpoint) |
| 382 | .credentials(auth.username(), auth.password(), domain) |
| 383 | .scopeToProject(project, domain) |
| 384 | .withConfig(config); |
| 385 | |
| 386 | if (perspective != null) { |
| 387 | builder.perspective(getFacing(perspective)); |
| 388 | } |
| 389 | |
| 390 | return builder.authenticate(); |
| 391 | } else { |
| 392 | log.warn("Unrecognized keystone version type"); |
| 393 | return null; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 394 | } |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 395 | } catch (AuthenticationException e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 396 | log.error("Authentication failed due to {}", e); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 397 | return null; |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 398 | } |
Jian Li | 1064e4f | 2018-05-29 16:16:53 +0900 | [diff] [blame] | 399 | } |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Extract the interface name with the supplied port. |
| 403 | * |
| 404 | * @param port port |
| 405 | * @return interface name |
| 406 | */ |
| 407 | public static String getIntfNameFromPciAddress(Port port) { |
Daniel Park | ff178ba | 2018-11-23 15:57:24 +0900 | [diff] [blame] | 408 | String intfName; |
| 409 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 410 | if (port.getProfile() == null || port.getProfile().isEmpty()) { |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 411 | log.error("Port profile is not found"); |
| 412 | return null; |
| 413 | } |
| 414 | |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 415 | if (!port.getProfile().containsKey(PCISLOT) || |
| 416 | Strings.isNullOrEmpty(port.getProfile().get(PCISLOT).toString())) { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 417 | log.error("Failed to retrieve the interface name because of no " + |
| 418 | "pci_slot information from the port"); |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 419 | return null; |
| 420 | } |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 421 | |
Daniel Park | ff178ba | 2018-11-23 15:57:24 +0900 | [diff] [blame] | 422 | String vendorInfoForPort = String.valueOf(port.getProfile().get(PCI_VENDOR_INFO)); |
| 423 | |
| 424 | if (!portNamePrefixMap().containsKey(vendorInfoForPort)) { |
| 425 | log.debug("{} is an non-smart NIC prefix.", vendorInfoForPort); |
| 426 | return UNSUPPORTED_VENDOR; |
| 427 | } |
| 428 | |
| 429 | String portNamePrefix = portNamePrefixMap().get(vendorInfoForPort); |
| 430 | |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 431 | String busNumHex = port.getProfile().get(PCISLOT).toString().split(":")[1]; |
| 432 | String busNumDecimal = String.valueOf(Integer.parseInt(busNumHex, HEX_RADIX)); |
| 433 | |
| 434 | String deviceNumHex = port.getProfile().get(PCISLOT).toString() |
| 435 | .split(":")[2] |
| 436 | .split("\\.")[0]; |
| 437 | String deviceNumDecimal = String.valueOf(Integer.parseInt(deviceNumHex, HEX_RADIX)); |
| 438 | |
| 439 | String functionNumHex = port.getProfile().get(PCISLOT).toString() |
| 440 | .split(":")[2] |
| 441 | .split("\\.")[1]; |
| 442 | String functionNumDecimal = String.valueOf(Integer.parseInt(functionNumHex, HEX_RADIX)); |
| 443 | |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 444 | if (functionNumDecimal.equals(ZERO_FUNCTION_NUMBER)) { |
| 445 | intfName = portNamePrefix + busNumDecimal + PREFIX_DEVICE_NUMBER + deviceNumDecimal; |
| 446 | } else { |
| 447 | intfName = portNamePrefix + busNumDecimal + PREFIX_DEVICE_NUMBER + deviceNumDecimal |
| 448 | + PREFIX_FUNCTION_NUMBER + functionNumDecimal; |
| 449 | } |
| 450 | |
| 451 | return intfName; |
| 452 | } |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 453 | |
| 454 | /** |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 455 | * Check if the given interface is added to the given device or not. |
| 456 | * |
| 457 | * @param deviceId device ID |
| 458 | * @param intfName interface name |
| 459 | * @param deviceService device service |
| 460 | * @return true if the given interface is added to the given device or false otherwise |
| 461 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 462 | public static boolean hasIntfAleadyInDevice(DeviceId deviceId, |
| 463 | String intfName, |
| 464 | DeviceService deviceService) { |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 465 | checkNotNull(deviceId); |
| 466 | checkNotNull(intfName); |
| 467 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 468 | return deviceService.getPorts(deviceId).stream().anyMatch(port -> |
| 469 | Objects.equals(port.annotations().value(PORT_NAME), intfName)); |
Daniel Park | 95f7331 | 2018-07-31 15:48:34 +0900 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 473 | * Adds router interfaces to openstack admin service. |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 474 | * |
| 475 | * @param osPort port |
| 476 | * @param adminService openstack admin service |
| 477 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 478 | public static void addRouterIface(Port osPort, |
| 479 | OpenstackRouterAdminService adminService) { |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 480 | osPort.getFixedIps().forEach(p -> { |
| 481 | JsonNode jsonTree = new ObjectMapper().createObjectNode() |
| 482 | .put("id", osPort.getDeviceId()) |
| 483 | .put("tenant_id", osPort.getTenantId()) |
| 484 | .put("subnet_id", p.getSubnetId()) |
| 485 | .put("port_id", osPort.getId()); |
| 486 | try { |
| 487 | RouterInterface rIface = getContext(NeutronRouterInterface.class) |
| 488 | .readerFor(NeutronRouterInterface.class) |
| 489 | .readValue(jsonTree); |
| 490 | if (adminService.routerInterface(rIface.getPortId()) != null) { |
| 491 | adminService.updateRouterInterface(rIface); |
| 492 | } else { |
| 493 | adminService.addRouterInterface(rIface); |
| 494 | } |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 495 | } catch (IOException e) { |
| 496 | log.error("IOException occurred because of {}", e); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 497 | } |
| 498 | }); |
| 499 | } |
| 500 | |
| 501 | /** |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 502 | * Prints openstack security group. |
| 503 | * |
| 504 | * @param osSg openstack security group |
| 505 | */ |
| 506 | public static void printSecurityGroup(SecurityGroup osSg) { |
| 507 | print(SECURITY_GROUP_FORMAT, osSg.getId(), osSg.getName()); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Prints openstack network. |
| 512 | * |
| 513 | * @param osNet openstack network |
| 514 | */ |
| 515 | public static void printNetwork(Network osNet) { |
| 516 | final String strNet = String.format(NETWORK_FORMAT, |
| 517 | osNet.getId(), |
| 518 | osNet.getName(), |
| 519 | osNet.getProviderSegID(), |
| 520 | osNet.getSubnets()); |
| 521 | print(strNet); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Prints openstack subnet. |
| 526 | * |
| 527 | * @param osSubnet openstack subnet |
| 528 | * @param osNetService openstack network service |
| 529 | */ |
| 530 | public static void printSubnet(Subnet osSubnet, |
| 531 | OpenstackNetworkService osNetService) { |
| 532 | final Network network = osNetService.network(osSubnet.getNetworkId()); |
| 533 | final String netName = network == null ? NOT_AVAILABLE : network.getName(); |
| 534 | final String strSubnet = String.format(SUBNET_FORMAT, |
| 535 | osSubnet.getId(), |
| 536 | netName, |
| 537 | osSubnet.getCidr()); |
| 538 | print(strSubnet); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Prints openstack port. |
| 543 | * |
| 544 | * @param osPort openstack port |
| 545 | * @param osNetService openstack network service |
| 546 | */ |
| 547 | public static void printPort(Port osPort, |
| 548 | OpenstackNetworkService osNetService) { |
| 549 | List<String> fixedIps = osPort.getFixedIps().stream() |
| 550 | .map(IP::getIpAddress) |
| 551 | .collect(Collectors.toList()); |
| 552 | final Network network = osNetService.network(osPort.getNetworkId()); |
| 553 | final String netName = network == null ? NOT_AVAILABLE : network.getName(); |
| 554 | final String strPort = String.format(PORT_FORMAT, |
| 555 | osPort.getId(), |
| 556 | netName, |
| 557 | osPort.getMacAddress(), |
| 558 | fixedIps.isEmpty() ? "" : fixedIps); |
| 559 | print(strPort); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Prints openstack router. |
| 564 | * |
| 565 | * @param osRouter openstack router |
| 566 | * @param osNetService openstack network service |
| 567 | */ |
| 568 | public static void printRouter(Router osRouter, |
| 569 | OpenstackNetworkService osNetService) { |
| 570 | List<String> externals = osNetService.ports().stream() |
| 571 | .filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && |
| 572 | Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_GW)) |
| 573 | .flatMap(osPort -> osPort.getFixedIps().stream()) |
| 574 | .map(IP::getIpAddress) |
| 575 | .collect(Collectors.toList()); |
| 576 | |
| 577 | List<String> internals = osNetService.ports().stream() |
| 578 | .filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && |
| 579 | Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_IFACE)) |
| 580 | .flatMap(osPort -> osPort.getFixedIps().stream()) |
| 581 | .map(IP::getIpAddress) |
| 582 | .collect(Collectors.toList()); |
| 583 | |
| 584 | final String strRouter = String.format(ROUTER_FORMAT, |
| 585 | osRouter.getId(), |
| 586 | osRouter.getName(), |
| 587 | externals.isEmpty() ? "" : externals, |
| 588 | internals.isEmpty() ? "" : internals); |
| 589 | print(strRouter); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Prints openstack router interface. |
| 594 | * |
| 595 | * @param osRouterIntf openstack router interface |
| 596 | */ |
| 597 | public static void printRouterIntf(RouterInterface osRouterIntf) { |
| 598 | final String strRouterIntf = String.format(ROUTER_INTF_FORMAT, |
| 599 | osRouterIntf.getId(), |
| 600 | osRouterIntf.getTenantId(), |
| 601 | osRouterIntf.getSubnetId()); |
| 602 | print(strRouterIntf); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Prints openstack floating IP. |
| 607 | * |
| 608 | * @param floatingIp floating IP |
| 609 | */ |
| 610 | public static void printFloatingIp(NetFloatingIP floatingIp) { |
| 611 | final String strFloating = String.format(FLOATING_IP_FORMAT, |
| 612 | floatingIp.getId(), |
| 613 | floatingIp.getFloatingIpAddress(), |
| 614 | Strings.isNullOrEmpty(floatingIp.getFixedIpAddress()) ? |
| 615 | "" : floatingIp.getFixedIpAddress()); |
| 616 | print(strFloating); |
| 617 | } |
| 618 | |
| 619 | /** |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 620 | * Obtains the property value with specified property key name. |
| 621 | * |
| 622 | * @param properties a collection of properties |
| 623 | * @param name key name |
| 624 | * @return mapping value |
| 625 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 626 | public static String getPropertyValue(Set<ConfigProperty> properties, |
| 627 | String name) { |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 628 | Optional<ConfigProperty> property = |
| 629 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 630 | return property.map(ConfigProperty::value).orElse(null); |
| 631 | } |
| 632 | |
| 633 | /** |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 634 | * Obtains the boolean property value with specified property key name. |
| 635 | * |
| 636 | * @param properties a collection of properties |
| 637 | * @param name key name |
| 638 | * @return mapping value |
| 639 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 640 | public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties, |
| 641 | String name) { |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 642 | Optional<ConfigProperty> property = |
| 643 | properties.stream().filter(p -> p.name().equals(name)).findFirst(); |
| 644 | |
| 645 | return property.map(ConfigProperty::asBoolean).orElse(false); |
| 646 | } |
| 647 | |
| 648 | /** |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 649 | * Prints out the JSON string in pretty format. |
| 650 | * |
| 651 | * @param mapper Object mapper |
| 652 | * @param jsonString JSON string |
| 653 | * @return pretty formatted JSON string |
| 654 | */ |
| 655 | public static String prettyJson(ObjectMapper mapper, String jsonString) { |
| 656 | try { |
| 657 | Object jsonObject = mapper.readValue(jsonString, Object.class); |
| 658 | return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 659 | } catch (JsonParseException e) { |
| 660 | log.debug("JsonParseException caused by {}", e); |
| 661 | } catch (JsonMappingException e) { |
| 662 | log.debug("JsonMappingException caused by {}", e); |
| 663 | } catch (JsonProcessingException e) { |
| 664 | log.debug("JsonProcessingException caused by {}", e); |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 665 | } catch (IOException e) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 666 | log.debug("IOException caused by {}", e); |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 667 | } |
| 668 | return null; |
| 669 | } |
| 670 | |
| 671 | /** |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 672 | * Checks the validity of ARP mode. |
| 673 | * |
| 674 | * @param arpMode ARP mode |
| 675 | * @return returns true if the ARP mode is valid, false otherwise |
| 676 | */ |
| 677 | public static boolean checkArpMode(String arpMode) { |
| 678 | |
| 679 | if (isNullOrEmpty(arpMode)) { |
| 680 | return false; |
| 681 | } else { |
| 682 | return arpMode.equals(PROXY_MODE) || arpMode.equals(BROADCAST_MODE); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /** |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 687 | * Checks the validity of activation flag. |
| 688 | * |
| 689 | * @param activationFlag activation flag |
| 690 | * @return returns true if the activation flag is valid, false otherwise |
| 691 | */ |
| 692 | public static boolean checkActivationFlag(String activationFlag) { |
| 693 | |
| 694 | switch (activationFlag) { |
| 695 | case ENABLE: |
| 696 | return true; |
| 697 | case DISABLE: |
| 698 | return false; |
| 699 | default: |
| 700 | throw new IllegalArgumentException("The given activation flag is not valid!"); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /** |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 705 | * Swaps current location with old location info. |
| 706 | * The revised instance port will be used to mod the flow rules after migration. |
| 707 | * |
| 708 | * @param instPort instance port |
| 709 | * @return location swapped instance port |
| 710 | */ |
| 711 | public static InstancePort swapStaleLocation(InstancePort instPort) { |
| 712 | return DefaultInstancePort.builder() |
| 713 | .deviceId(instPort.oldDeviceId()) |
| 714 | .portNumber(instPort.oldPortNumber()) |
| 715 | .state(instPort.state()) |
| 716 | .ipAddress(instPort.ipAddress()) |
| 717 | .macAddress(instPort.macAddress()) |
| 718 | .networkId(instPort.networkId()) |
| 719 | .portId(instPort.portId()) |
| 720 | .build(); |
| 721 | } |
| 722 | |
| 723 | /** |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 724 | * Compares two router interfaces are equal. |
| 725 | * Will be remove this after Openstack4j implements equals. |
| 726 | * |
| 727 | * @param routerInterface1 router interface |
| 728 | * @param routerInterface2 router interface |
| 729 | * @return returns true if two router interfaces are equal, false otherwise |
| 730 | */ |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 731 | public static boolean routerInterfacesEquals(RouterInterface routerInterface1, |
| 732 | RouterInterface routerInterface2) { |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 733 | return Objects.equals(routerInterface1.getId(), routerInterface2.getId()) && |
| 734 | Objects.equals(routerInterface1.getPortId(), routerInterface2.getPortId()) && |
| 735 | Objects.equals(routerInterface1.getSubnetId(), routerInterface2.getSubnetId()) && |
| 736 | Objects.equals(routerInterface1.getTenantId(), routerInterface2.getTenantId()); |
| 737 | } |
| 738 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 739 | /** |
| 740 | * Returns the vnic type of given port. |
| 741 | * |
| 742 | * @param portName port name |
| 743 | * @return vnit type |
| 744 | */ |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 745 | public static VnicType vnicType(String portName) { |
| 746 | if (portName.startsWith(PORT_NAME_PREFIX_VM) || |
| 747 | portName.startsWith(PORT_NAME_VHOST_USER_PREFIX_VM)) { |
| 748 | return VnicType.NORMAL; |
| 749 | } else if (isDirectPort(portName)) { |
| 750 | return VnicType.DIRECT; |
| 751 | } else { |
| 752 | return VnicType.UNSUPPORTED; |
| 753 | } |
| 754 | } |
| 755 | |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 756 | /** |
| 757 | * Deserializes raw payload into HttpRequest object. |
| 758 | * |
| 759 | * @param rawData raw http payload |
| 760 | * @return HttpRequest object |
| 761 | */ |
| 762 | public static HttpRequest parseHttpRequest(byte[] rawData) { |
| 763 | SessionInputBufferImpl sessionInputBuffer = |
| 764 | new SessionInputBufferImpl( |
| 765 | new HttpTransportMetricsImpl(), HTTP_PAYLOAD_BUFFER); |
| 766 | sessionInputBuffer.bind(new ByteArrayInputStream(rawData)); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 767 | DefaultHttpRequestParser requestParser = |
| 768 | new DefaultHttpRequestParser(sessionInputBuffer); |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 769 | try { |
| 770 | return requestParser.parse(); |
| 771 | } catch (IOException | HttpException e) { |
| 772 | log.warn("Failed to parse HttpRequest, due to {}", e); |
| 773 | } |
| 774 | |
| 775 | return null; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Serializes HttpRequest object to byte array. |
| 780 | * |
| 781 | * @param request http request object |
| 782 | * @return byte array |
| 783 | */ |
| 784 | public static byte[] unparseHttpRequest(HttpRequest request) { |
| 785 | try { |
| 786 | SessionOutputBufferImpl sessionOutputBuffer = |
| 787 | new SessionOutputBufferImpl( |
| 788 | new HttpTransportMetricsImpl(), HTTP_PAYLOAD_BUFFER); |
| 789 | |
| 790 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 791 | sessionOutputBuffer.bind(baos); |
| 792 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 793 | HttpMessageWriter<HttpRequest> requestWriter = |
| 794 | new DefaultHttpRequestWriter(sessionOutputBuffer); |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 795 | requestWriter.write(request); |
| 796 | sessionOutputBuffer.flush(); |
| 797 | |
| 798 | return baos.toByteArray(); |
| 799 | } catch (HttpException | IOException e) { |
| 800 | log.warn("Failed to unparse HttpRequest, due to {}", e); |
| 801 | } |
| 802 | |
| 803 | return null; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * Deserializes raw payload into HttpResponse object. |
| 808 | * |
| 809 | * @param rawData raw http payload |
| 810 | * @return HttpResponse object |
| 811 | */ |
| 812 | public static HttpResponse parseHttpResponse(byte[] rawData) { |
| 813 | SessionInputBufferImpl sessionInputBuffer = |
| 814 | new SessionInputBufferImpl( |
| 815 | new HttpTransportMetricsImpl(), HTTP_PAYLOAD_BUFFER); |
| 816 | sessionInputBuffer.bind(new ByteArrayInputStream(rawData)); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 817 | DefaultHttpResponseParser responseParser = |
| 818 | new DefaultHttpResponseParser(sessionInputBuffer); |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 819 | try { |
| 820 | return responseParser.parse(); |
| 821 | } catch (IOException | HttpException e) { |
| 822 | log.warn("Failed to parse HttpResponse, due to {}", e); |
| 823 | } |
| 824 | |
| 825 | return null; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Serializes HttpResponse header to byte array. |
| 830 | * |
| 831 | * @param response http response object |
| 832 | * @return byte array |
| 833 | */ |
| 834 | public static byte[] unparseHttpResponseHeader(HttpResponse response) { |
| 835 | try { |
| 836 | SessionOutputBufferImpl sessionOutputBuffer = |
| 837 | new SessionOutputBufferImpl( |
| 838 | new HttpTransportMetricsImpl(), HTTP_PAYLOAD_BUFFER); |
| 839 | |
| 840 | ByteArrayOutputStream headerBaos = new ByteArrayOutputStream(); |
| 841 | sessionOutputBuffer.bind(headerBaos); |
| 842 | |
| 843 | HttpMessageWriter<HttpResponse> responseWriter = |
| 844 | new DefaultHttpResponseWriter(sessionOutputBuffer); |
| 845 | responseWriter.write(response); |
| 846 | sessionOutputBuffer.flush(); |
| 847 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 848 | log.debug(headerBaos.toString(Charsets.UTF_8.name())); |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 849 | |
| 850 | return headerBaos.toByteArray(); |
| 851 | } catch (IOException | HttpException e) { |
| 852 | log.warn("Failed to unparse HttpResponse headers, due to {}", e); |
| 853 | } |
| 854 | |
| 855 | return null; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Serializes HttpResponse object to byte array. |
| 860 | * |
| 861 | * @param response http response object |
| 862 | * @return byte array |
| 863 | */ |
| 864 | public static byte[] unparseHttpResponseBody(HttpResponse response) { |
| 865 | try { |
| 866 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 867 | response.getEntity().writeTo(baos); |
| 868 | |
| 869 | log.debug(response.toString()); |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 870 | log.debug(baos.toString(Charsets.UTF_8.name())); |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 871 | |
| 872 | return baos.toByteArray(); |
| 873 | } catch (IOException e) { |
| 874 | log.warn("Failed to unparse HttpResponse, due to {}", e); |
| 875 | } |
| 876 | |
| 877 | return null; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * Encodes the given data using HmacSHA256 encryption method with given secret key. |
| 882 | * |
| 883 | * @param key secret key |
| 884 | * @param data data to be encrypted |
| 885 | * @return Hmac256 encrypted data |
| 886 | */ |
| 887 | public static String hmacEncrypt(String key, String data) { |
| 888 | try { |
| 889 | Mac sha256Hmac = Mac.getInstance(HMAC_SHA256); |
| 890 | SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), HMAC_SHA256); |
| 891 | sha256Hmac.init(secretKey); |
| 892 | return Hex.encodeHexString(sha256Hmac.doFinal(data.getBytes("UTF-8"))); |
| 893 | } catch (Exception e) { |
| 894 | log.warn("Failed to encrypt data {} using key {}, due to {}", data, key, e); |
| 895 | } |
| 896 | return null; |
| 897 | } |
| 898 | |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 899 | /** |
| 900 | * Creates flow trace request string. |
| 901 | * |
| 902 | * @param srcIp src ip address |
| 903 | * @param dstIp dst ip address |
| 904 | * @param srcInstancePort src instance port |
| 905 | * @param osNetService openstack networking service |
Daniel Park | 5aef982 | 2018-09-20 18:04:18 +0900 | [diff] [blame] | 906 | * @param uplink true if this request is for uplink |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 907 | * @return flow trace request string |
| 908 | */ |
| 909 | public static String traceRequestString(String srcIp, |
| 910 | String dstIp, |
| 911 | InstancePort srcInstancePort, |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 912 | OpenstackNetworkService osNetService, |
| 913 | boolean uplink) { |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 914 | |
| 915 | StringBuilder requestStringBuilder = new StringBuilder(DEFAULT_REQUEST_STRING); |
| 916 | |
| 917 | if (uplink) { |
| 918 | |
| 919 | requestStringBuilder.append(COMMA) |
| 920 | .append(IN_PORT) |
| 921 | .append(srcInstancePort.portNumber().toString()) |
| 922 | .append(COMMA) |
| 923 | .append(NW_SRC) |
| 924 | .append(srcIp) |
| 925 | .append(COMMA); |
| 926 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 927 | String modifiedDstIp = dstIp; |
Jian Li | 621f73c | 2018-12-15 01:49:22 +0900 | [diff] [blame] | 928 | Type netType = osNetService.networkType(srcInstancePort.networkId()); |
| 929 | if (netType == Type.VXLAN || netType == Type.GRE || |
| 930 | netType == Type.VLAN || netType == Type.GENEVE) { |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 931 | if (srcIp.equals(dstIp)) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 932 | modifiedDstIp = osNetService.gatewayIp(srcInstancePort.portId()); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 933 | requestStringBuilder.append(DL_DST) |
| 934 | .append(DEFAULT_GATEWAY_MAC_STR).append(COMMA); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 935 | } else if (!osNetService.ipPrefix(srcInstancePort.portId()).contains( |
| 936 | IpAddress.valueOf(dstIp))) { |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 937 | requestStringBuilder.append(DL_DST) |
| 938 | .append(DEFAULT_GATEWAY_MAC_STR) |
| 939 | .append(COMMA); |
| 940 | } |
| 941 | } else { |
| 942 | if (srcIp.equals(dstIp)) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 943 | modifiedDstIp = osNetService.gatewayIp(srcInstancePort.portId()); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | requestStringBuilder.append(NW_DST) |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 948 | .append(modifiedDstIp) |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 949 | .append("\n"); |
| 950 | } else { |
| 951 | requestStringBuilder.append(COMMA) |
| 952 | .append(NW_SRC) |
| 953 | .append(dstIp) |
| 954 | .append(COMMA); |
| 955 | |
Jian Li | 621f73c | 2018-12-15 01:49:22 +0900 | [diff] [blame] | 956 | Type netType = osNetService.networkType(srcInstancePort.networkId()); |
| 957 | |
| 958 | if (netType == Type.VXLAN || netType == Type.GRE || |
| 959 | netType == Type.VLAN || netType == Type.GENEVE) { |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 960 | requestStringBuilder.append(TUN_ID) |
| 961 | .append(osNetService.segmentId(srcInstancePort.networkId())) |
| 962 | .append(COMMA); |
| 963 | } |
| 964 | requestStringBuilder.append(NW_DST) |
| 965 | .append(srcIp) |
| 966 | .append("\n"); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | return requestStringBuilder.toString(); |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * Sends flow trace string to node. |
| 974 | * |
| 975 | * @param requestString reqeust string |
| 976 | * @param node src node |
| 977 | * @return flow trace result in string format |
| 978 | */ |
| 979 | public static String sendTraceRequestToNode(String requestString, |
| 980 | OpenstackNode node) { |
| 981 | String traceResult = null; |
| 982 | OpenstackSshAuth sshAuth = node.sshAuthInfo(); |
| 983 | |
| 984 | try (SshClient client = SshClient.setUpDefaultClient()) { |
| 985 | client.start(); |
| 986 | |
| 987 | try (ClientSession session = client |
| 988 | .connect(sshAuth.id(), node.managementIp().getIp4Address().toString(), SSH_PORT) |
| 989 | .verify(TIMEOUT_MS, TimeUnit.SECONDS).getSession()) { |
| 990 | session.addPasswordIdentity(sshAuth.password()); |
| 991 | session.auth().verify(TIMEOUT_MS, TimeUnit.SECONDS); |
| 992 | |
| 993 | |
| 994 | try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) { |
| 995 | |
| 996 | log.debug("requestString: {}", requestString); |
| 997 | final InputStream inputStream = |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 998 | new ByteArrayInputStream(requestString.getBytes(Charsets.UTF_8)); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 999 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1000 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 1001 | OutputStream errStream = new ByteArrayOutputStream(); |
| 1002 | |
| 1003 | channel.setIn(new NoCloseInputStream(inputStream)); |
| 1004 | channel.setErr(errStream); |
| 1005 | channel.setOut(outputStream); |
| 1006 | |
| 1007 | Collection<ClientChannelEvent> eventList = Lists.newArrayList(); |
| 1008 | eventList.add(ClientChannelEvent.OPENED); |
| 1009 | |
| 1010 | OpenFuture channelFuture = channel.open(); |
| 1011 | |
| 1012 | if (channelFuture.await(TIMEOUT_MS, TimeUnit.SECONDS)) { |
| 1013 | |
| 1014 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 1015 | |
| 1016 | while (!channelFuture.isOpened()) { |
| 1017 | if ((timeoutExpiredMs - System.currentTimeMillis()) <= 0) { |
| 1018 | log.error("Failed to open channel"); |
| 1019 | return null; |
| 1020 | } |
| 1021 | } |
| 1022 | TimeUnit.SECONDS.sleep(WAIT_OUTPUT_STREAM_SECOND); |
| 1023 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1024 | traceResult = outputStream.toString(Charsets.UTF_8.name()); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 1025 | |
| 1026 | channel.close(); |
| 1027 | } |
| 1028 | } finally { |
| 1029 | session.close(); |
| 1030 | } |
| 1031 | } finally { |
| 1032 | client.stop(); |
| 1033 | } |
| 1034 | |
| 1035 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1036 | log.error("Exception occurred because of {}", e); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | return traceResult; |
| 1040 | } |
| 1041 | |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1042 | /** |
| 1043 | * Returns the floating ip with supplied instance port. |
| 1044 | * |
| 1045 | * @param instancePort instance port |
| 1046 | * @param osRouterAdminService openstack router admin service |
| 1047 | * @return floating ip |
| 1048 | */ |
| 1049 | public static NetFloatingIP floatingIpByInstancePort(InstancePort instancePort, |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1050 | OpenstackRouterAdminService |
| 1051 | osRouterAdminService) { |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1052 | return osRouterAdminService.floatingIps().stream() |
| 1053 | .filter(netFloatingIP -> netFloatingIP.getPortId() != null) |
| 1054 | .filter(netFloatingIP -> netFloatingIP.getPortId().equals(instancePort.portId())) |
| 1055 | .findAny().orElse(null); |
| 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * Sends GARP packet with supplied floating ip information. |
| 1060 | * |
| 1061 | * @param floatingIP floating ip |
| 1062 | * @param instancePort instance port |
| 1063 | * @param vlanId vlain id |
| 1064 | * @param gatewayNode gateway node |
| 1065 | * @param packetService packet service |
| 1066 | */ |
Jian Li | 32b0362 | 2018-11-06 17:54:24 +0900 | [diff] [blame] | 1067 | public static void processGarpPacketForFloatingIp(NetFloatingIP floatingIP, |
| 1068 | InstancePort instancePort, |
| 1069 | VlanId vlanId, |
| 1070 | OpenstackNode gatewayNode, |
| 1071 | PacketService packetService) { |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1072 | Ethernet ethernet = buildGratuitousArpPacket(floatingIP, instancePort, vlanId); |
| 1073 | |
| 1074 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1075 | .setOutput(gatewayNode.uplinkPortNum()).build(); |
| 1076 | |
| 1077 | packetService.emit(new DefaultOutboundPacket(gatewayNode.intgBridge(), treatment, |
| 1078 | ByteBuffer.wrap(ethernet.serialize()))); |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * Returns the external peer router with supplied network information. |
| 1083 | * |
| 1084 | * @param network network |
| 1085 | * @param osNetworkService openstack network service |
| 1086 | * @param osRouterAdminService openstack router admin service |
| 1087 | * @return external peer router |
| 1088 | */ |
| 1089 | public static ExternalPeerRouter externalPeerRouterForNetwork(Network network, |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1090 | OpenstackNetworkService |
| 1091 | osNetworkService, |
| 1092 | OpenstackRouterAdminService |
| 1093 | osRouterAdminService) { |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1094 | if (network == null) { |
| 1095 | return null; |
| 1096 | } |
| 1097 | |
Jian Li | e6e609f | 2019-05-14 17:45:54 +0900 | [diff] [blame] | 1098 | Subnet subnet = osNetworkService.subnets(network.getId()) |
| 1099 | .stream().findAny().orElse(null); |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1100 | |
| 1101 | if (subnet == null) { |
| 1102 | return null; |
| 1103 | } |
| 1104 | |
| 1105 | RouterInterface osRouterIface = osRouterAdminService.routerInterfaces().stream() |
| 1106 | .filter(i -> Objects.equals(i.getSubnetId(), subnet.getId())) |
| 1107 | .findAny().orElse(null); |
| 1108 | if (osRouterIface == null) { |
| 1109 | return null; |
| 1110 | } |
| 1111 | |
| 1112 | Router osRouter = osRouterAdminService.router(osRouterIface.getId()); |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1113 | if (osRouter == null || osRouter.getExternalGatewayInfo() == null) { |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1114 | return null; |
| 1115 | } |
| 1116 | |
| 1117 | ExternalGateway exGatewayInfo = osRouter.getExternalGatewayInfo(); |
| 1118 | return osNetworkService.externalPeerRouter(exGatewayInfo); |
| 1119 | |
| 1120 | } |
| 1121 | |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 1122 | /** |
| 1123 | * Returns the external peer router with specified subnet information. |
| 1124 | * |
| 1125 | * @param subnet openstack subnet |
| 1126 | * @param osRouterService openstack router service |
| 1127 | * @param osNetworkService openstack network service |
| 1128 | * @return external peer router |
| 1129 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1130 | public static ExternalPeerRouter externalPeerRouterFromSubnet(Subnet subnet, |
| 1131 | OpenstackRouterService |
| 1132 | osRouterService, |
| 1133 | OpenstackNetworkService |
| 1134 | osNetworkService) { |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 1135 | Router osRouter = getRouterFromSubnet(subnet, osRouterService); |
| 1136 | if (osRouter == null) { |
| 1137 | return null; |
| 1138 | } |
| 1139 | if (osRouter.getExternalGatewayInfo() == null) { |
| 1140 | // this router does not have external connectivity |
| 1141 | log.trace("router({}) has no external gateway", |
| 1142 | osRouter.getName()); |
| 1143 | return null; |
| 1144 | } |
| 1145 | |
| 1146 | return osNetworkService.externalPeerRouter(osRouter.getExternalGatewayInfo()); |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * Returns the external ip address with specified router information. |
| 1151 | * |
| 1152 | * @param srcSubnet source subnet |
| 1153 | * @param osRouterService openstack router service |
| 1154 | * @param osNetworkService openstack network service |
| 1155 | * @return external ip address |
| 1156 | */ |
| 1157 | public static IpAddress externalIpFromSubnet(Subnet srcSubnet, |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1158 | OpenstackRouterService |
| 1159 | osRouterService, |
| 1160 | OpenstackNetworkService |
| 1161 | osNetworkService) { |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 1162 | |
| 1163 | Router osRouter = getRouterFromSubnet(srcSubnet, osRouterService); |
| 1164 | |
| 1165 | if (osRouter.getExternalGatewayInfo() == null) { |
| 1166 | // this router does not have external connectivity |
| 1167 | log.trace("router({}) has no external gateway", |
| 1168 | osRouter.getName()); |
| 1169 | return null; |
| 1170 | } |
| 1171 | |
| 1172 | return getExternalIp(osRouter, osNetworkService); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Returns the external ip address with specified router information. |
| 1177 | * |
| 1178 | * @param router openstack router |
| 1179 | * @param osNetworkService openstack network service |
| 1180 | * @return external ip address |
| 1181 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1182 | public static IpAddress getExternalIp(Router router, |
| 1183 | OpenstackNetworkService osNetworkService) { |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 1184 | if (router == null) { |
| 1185 | return null; |
| 1186 | } |
| 1187 | |
| 1188 | ExternalGateway externalGateway = router.getExternalGatewayInfo(); |
| 1189 | if (externalGateway == null || !externalGateway.isEnableSnat()) { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1190 | log.trace("Failed to get externalIp for router {} because " + |
| 1191 | "externalGateway is null or SNAT is disabled", |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 1192 | router.getId()); |
| 1193 | return null; |
| 1194 | } |
| 1195 | |
| 1196 | // TODO fix openstack4j for ExternalGateway provides external fixed IP list |
| 1197 | Port exGatewayPort = osNetworkService.ports(externalGateway.getNetworkId()) |
| 1198 | .stream() |
| 1199 | .filter(port -> Objects.equals(port.getDeviceId(), router.getId())) |
| 1200 | .findAny().orElse(null); |
| 1201 | |
| 1202 | if (exGatewayPort == null) { |
| 1203 | return null; |
| 1204 | } |
| 1205 | |
| 1206 | return IpAddress.valueOf(exGatewayPort.getFixedIps().stream() |
| 1207 | .findAny().get().getIpAddress()); |
| 1208 | } |
| 1209 | |
Jian Li | 2d68c19 | 2018-12-13 15:52:59 +0900 | [diff] [blame] | 1210 | /** |
| 1211 | * Returns the tunnel port number with specified net ID and openstack node. |
| 1212 | * |
| 1213 | * @param netId network ID |
| 1214 | * @param netService network service |
| 1215 | * @param osNode openstack node |
| 1216 | * @return tunnel port number |
| 1217 | */ |
| 1218 | public static PortNumber tunnelPortNumByNetId(String netId, |
| 1219 | OpenstackNetworkService netService, |
| 1220 | OpenstackNode osNode) { |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 1221 | Type netType = netService.networkType(netId); |
Jian Li | 2d68c19 | 2018-12-13 15:52:59 +0900 | [diff] [blame] | 1222 | |
| 1223 | if (netType == null) { |
| 1224 | return null; |
| 1225 | } |
| 1226 | |
| 1227 | return tunnelPortNumByNetType(netType, osNode); |
| 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * Returns the tunnel port number with specified net type and openstack node. |
| 1232 | * |
| 1233 | * @param netType network type |
| 1234 | * @param osNode openstack node |
| 1235 | * @return tunnel port number |
| 1236 | */ |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 1237 | public static PortNumber tunnelPortNumByNetType(Type netType, OpenstackNode osNode) { |
Jian Li | 2d68c19 | 2018-12-13 15:52:59 +0900 | [diff] [blame] | 1238 | switch (netType) { |
| 1239 | case VXLAN: |
| 1240 | return osNode.vxlanTunnelPortNum(); |
| 1241 | case GRE: |
| 1242 | return osNode.greTunnelPortNum(); |
Jian Li | 621f73c | 2018-12-15 01:49:22 +0900 | [diff] [blame] | 1243 | case GENEVE: |
| 1244 | return osNode.geneveTunnelPortNum(); |
Jian Li | 2d68c19 | 2018-12-13 15:52:59 +0900 | [diff] [blame] | 1245 | default: |
| 1246 | return null; |
| 1247 | } |
| 1248 | } |
| 1249 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 1250 | /** |
| 1251 | * Returns the REST URL of active node. |
| 1252 | * |
| 1253 | * @param haService openstack HA service |
| 1254 | * @return REST URL of active node |
| 1255 | */ |
| 1256 | public static String getActiveUrl(OpenstackHaService haService) { |
| 1257 | return "http://" + haService.getActiveIp().toString() + ":" + |
| 1258 | REST_PORT + "/" + OPENSTACK_NETWORKING_REST_PATH + "/"; |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * Returns the REST client instance with given resource path. |
| 1263 | * |
| 1264 | * @param haService openstack HA service |
| 1265 | * @param resourcePath resource path |
| 1266 | * @return REST client instance |
| 1267 | */ |
| 1268 | public static WebTarget getActiveClient(OpenstackHaService haService, |
| 1269 | String resourcePath) { |
| 1270 | HttpAuthenticationFeature feature = |
| 1271 | HttpAuthenticationFeature.universal(REST_USER, REST_PASSWORD); |
| 1272 | Client client = ClientBuilder.newClient().register(feature); |
| 1273 | return client.target(getActiveUrl(haService)).path(resourcePath); |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * Returns the post response from the active node. |
| 1278 | * |
| 1279 | * @param haService openstack HA service |
| 1280 | * @param resourcePath resource path |
| 1281 | * @param input input |
| 1282 | * @return post response |
| 1283 | */ |
| 1284 | public static Response syncPost(OpenstackHaService haService, |
| 1285 | String resourcePath, |
| 1286 | String input) { |
| 1287 | |
| 1288 | log.debug("Sync POST request with {} on {}", |
| 1289 | haService.getActiveIp().toString(), resourcePath); |
| 1290 | |
| 1291 | return getActiveClient(haService, resourcePath) |
| 1292 | .request(APPLICATION_JSON_TYPE) |
| 1293 | .post(Entity.json(input)); |
| 1294 | } |
| 1295 | |
| 1296 | /** |
| 1297 | * Returns the put response from the active node. |
| 1298 | * |
| 1299 | * @param haService openstack HA service |
| 1300 | * @param resourcePath resource path |
| 1301 | * @param id resource identifier |
| 1302 | * @param input input |
| 1303 | * @return put response |
| 1304 | */ |
| 1305 | public static Response syncPut(OpenstackHaService haService, |
| 1306 | String resourcePath, |
| 1307 | String id, String input) { |
| 1308 | return syncPut(haService, resourcePath, null, id, input); |
| 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * Returns the put response from the active node. |
| 1313 | * |
| 1314 | * @param haService openstack HA service |
| 1315 | * @param resourcePath resource path |
| 1316 | * @param id resource identifier |
| 1317 | * @param suffix resource suffix |
| 1318 | * @param input input |
| 1319 | * @return put response |
| 1320 | */ |
| 1321 | public static Response syncPut(OpenstackHaService haService, |
| 1322 | String resourcePath, |
| 1323 | String suffix, |
| 1324 | String id, String input) { |
| 1325 | |
| 1326 | log.debug("Sync PUT request with {} on {}", |
| 1327 | haService.getActiveIp().toString(), resourcePath); |
| 1328 | |
| 1329 | String pathStr = "/" + id; |
| 1330 | |
| 1331 | if (suffix != null) { |
| 1332 | pathStr += "/" + suffix; |
| 1333 | } |
| 1334 | |
| 1335 | return getActiveClient(haService, resourcePath) |
| 1336 | .path(pathStr) |
| 1337 | .request(APPLICATION_JSON_TYPE) |
| 1338 | .put(Entity.json(input)); |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * Returns the delete response from the active node. |
| 1343 | * |
| 1344 | * @param haService openstack HA service |
| 1345 | * @param resourcePath resource path |
| 1346 | * @param id resource identifier |
| 1347 | * @return delete response |
| 1348 | */ |
| 1349 | public static Response syncDelete(OpenstackHaService haService, |
| 1350 | String resourcePath, |
| 1351 | String id) { |
| 1352 | |
| 1353 | log.debug("Sync DELETE request with {} on {}", |
| 1354 | haService.getActiveIp().toString(), resourcePath); |
| 1355 | |
| 1356 | return getActiveClient(haService, resourcePath) |
| 1357 | .path("/" + id) |
| 1358 | .request(APPLICATION_JSON_TYPE) |
| 1359 | .delete(); |
| 1360 | } |
| 1361 | |
Jian Li | 5172870 | 2019-05-17 18:38:56 +0900 | [diff] [blame] | 1362 | /** |
| 1363 | * Gets the ovsdb client with supplied openstack node. |
| 1364 | * |
| 1365 | * @param node openstack node |
| 1366 | * @param ovsdbPort openvswitch DB port number |
| 1367 | * @param controller openvswitch DB controller instance |
| 1368 | * @return ovsdb client instance |
| 1369 | */ |
| 1370 | public static OvsdbClientService getOvsdbClient(OpenstackNode node, int ovsdbPort, |
| 1371 | OvsdbController controller) { |
| 1372 | OvsdbNodeId ovsdb = new OvsdbNodeId(node.managementIp(), ovsdbPort); |
| 1373 | return controller.getOvsdbClient(ovsdb); |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Obtains the name of interface attached to the openstack VM. |
| 1378 | * |
| 1379 | * @param portId openstack port identifier |
| 1380 | * @return name of interface |
| 1381 | */ |
| 1382 | public static String ifaceNameFromOsPortId(String portId) { |
| 1383 | if (portId != null) { |
| 1384 | return PORT_NAME_PREFIX_VM + StringUtils.substring(portId, 0, TAP_PORT_LENGTH); |
| 1385 | } |
| 1386 | |
| 1387 | return null; |
| 1388 | } |
| 1389 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1390 | private static Router getRouterFromSubnet(Subnet subnet, |
| 1391 | OpenstackRouterService osRouterService) { |
Jian Li | ebde74d | 2018-11-14 00:18:57 +0900 | [diff] [blame] | 1392 | RouterInterface osRouterIface = osRouterService.routerInterfaces().stream() |
| 1393 | .filter(i -> Objects.equals(i.getSubnetId(), subnet.getId())) |
| 1394 | .findAny().orElse(null); |
| 1395 | if (osRouterIface == null) { |
| 1396 | return null; |
| 1397 | } |
| 1398 | |
| 1399 | return osRouterService.router(osRouterIface.getId()); |
| 1400 | } |
| 1401 | |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 1402 | private static boolean isDirectPort(String portName) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1403 | return portNamePrefixMap().values().stream().anyMatch(portName::startsWith); |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 1404 | } |
| 1405 | |
Daniel Park | 2ff66b4 | 2018-08-01 11:52:45 +0900 | [diff] [blame] | 1406 | /** |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1407 | * Returns GARP packet with supplied floating ip and instance port information. |
| 1408 | * |
| 1409 | * @param floatingIP floating ip |
| 1410 | * @param instancePort instance port |
| 1411 | * @param vlanId vlan id |
| 1412 | * @return GARP packet |
| 1413 | */ |
| 1414 | private static Ethernet buildGratuitousArpPacket(NetFloatingIP floatingIP, |
| 1415 | InstancePort instancePort, |
| 1416 | VlanId vlanId) { |
| 1417 | Ethernet ethernet = new Ethernet(); |
| 1418 | ethernet.setDestinationMACAddress(MacAddress.BROADCAST); |
| 1419 | ethernet.setSourceMACAddress(instancePort.macAddress()); |
| 1420 | ethernet.setEtherType(Ethernet.TYPE_ARP); |
| 1421 | ethernet.setVlanID(vlanId.id()); |
| 1422 | |
| 1423 | ARP arp = new ARP(); |
| 1424 | arp.setOpCode(ARP.OP_REPLY); |
| 1425 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 1426 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
| 1427 | |
| 1428 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
| 1429 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 1430 | |
| 1431 | arp.setSenderHardwareAddress(instancePort.macAddress().toBytes()); |
| 1432 | arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes()); |
| 1433 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 1434 | arp.setSenderProtocolAddress(valueOf(floatingIP.getFloatingIpAddress()).toInt()); |
| 1435 | arp.setTargetProtocolAddress(valueOf(floatingIP.getFloatingIpAddress()).toInt()); |
Daniel Park | 4fa1f5e | 2018-10-17 12:41:52 +0900 | [diff] [blame] | 1436 | |
| 1437 | ethernet.setPayload(arp); |
| 1438 | |
| 1439 | return ethernet; |
| 1440 | } |
| 1441 | |
| 1442 | /** |
Jian Li | a271b3c | 2019-09-03 23:10:20 +0900 | [diff] [blame] | 1443 | * Re-structures the OVS port name. |
| 1444 | * The length of OVS port name should be not large than 15. |
| 1445 | * |
| 1446 | * @param portName original port name |
| 1447 | * @return re-structured OVS port name |
| 1448 | */ |
| 1449 | public static String structurePortName(String portName) { |
| 1450 | |
| 1451 | // The size of OVS port name should not be larger than 15 |
| 1452 | if (portName.length() > PORT_NAME_MAX_LENGTH) { |
| 1453 | return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH); |
| 1454 | } |
| 1455 | |
| 1456 | return portName; |
| 1457 | } |
| 1458 | |
| 1459 | /** |
Jian Li | d572762 | 2019-09-11 11:15:16 +0900 | [diff] [blame] | 1460 | * Obtains flow group key from the given id. |
| 1461 | * |
| 1462 | * @param groupId flow group identifier |
| 1463 | * @return flow group key |
| 1464 | */ |
| 1465 | public static GroupKey getGroupKey(int groupId) { |
| 1466 | return new DefaultGroupKey((Integer.toString(groupId)).getBytes()); |
| 1467 | } |
| 1468 | |
| 1469 | /** |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1470 | * Builds up and a complete endpoint URL from gateway node. |
| 1471 | * |
| 1472 | * @param node gateway node |
| 1473 | * @return a complete endpoint URL |
| 1474 | */ |
| 1475 | private static String buildEndpoint(OpenstackNode node) { |
| 1476 | |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 1477 | OpenstackAuth auth = node.keystoneConfig().authentication(); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1478 | |
| 1479 | StringBuilder endpointSb = new StringBuilder(); |
| 1480 | endpointSb.append(auth.protocol().name().toLowerCase()); |
| 1481 | endpointSb.append("://"); |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 1482 | endpointSb.append(node.keystoneConfig().endpoint()); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1483 | return endpointSb.toString(); |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * Obtains the SSL config without verifying the certification. |
| 1488 | * |
| 1489 | * @return SSL config |
| 1490 | */ |
| 1491 | private static Config getSslConfig() { |
| 1492 | // we bypass the SSL certification verification for now |
| 1493 | // TODO: verify server side SSL using a given certification |
| 1494 | Config config = Config.newConfig().withSSLVerificationDisabled(); |
| 1495 | |
| 1496 | TrustManager[] trustAllCerts = new TrustManager[]{ |
| 1497 | new X509TrustManager() { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1498 | @Override |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1499 | public X509Certificate[] getAcceptedIssuers() { |
| 1500 | return null; |
| 1501 | } |
| 1502 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1503 | @Override |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1504 | public void checkClientTrusted(X509Certificate[] certs, |
| 1505 | String authType) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1506 | return; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1507 | } |
| 1508 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1509 | @Override |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1510 | public void checkServerTrusted(X509Certificate[] certs, |
| 1511 | String authType) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1512 | return; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | }; |
| 1516 | |
| 1517 | HostnameVerifier allHostsValid = (hostname, session) -> true; |
| 1518 | |
| 1519 | try { |
| 1520 | SSLContext sc = SSLContext.getInstance(SSL_TYPE); |
| 1521 | sc.init(null, trustAllCerts, |
| 1522 | new java.security.SecureRandom()); |
| 1523 | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
| 1524 | HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); |
| 1525 | |
| 1526 | config.withSSLContext(sc); |
| 1527 | } catch (Exception e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 1528 | log.error("Failed to access OpenStack service due to {}", e); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 1529 | return null; |
| 1530 | } |
| 1531 | |
| 1532 | return config; |
| 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * Obtains the facing object with given openstack perspective. |
| 1537 | * |
| 1538 | * @param perspective keystone perspective |
| 1539 | * @return facing object |
| 1540 | */ |
| 1541 | private static Facing getFacing(Perspective perspective) { |
| 1542 | |
| 1543 | switch (perspective) { |
| 1544 | case PUBLIC: |
| 1545 | return Facing.PUBLIC; |
| 1546 | case ADMIN: |
| 1547 | return Facing.ADMIN; |
| 1548 | case INTERNAL: |
| 1549 | return Facing.INTERNAL; |
| 1550 | default: |
| 1551 | return null; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * Obtains gateway instance by giving index number. |
| 1557 | * |
| 1558 | * @param gws a collection of gateway nodes |
| 1559 | * @param index index number |
| 1560 | * @return gateway instance |
| 1561 | */ |
| 1562 | private static OpenstackNode getGwByIndex(Set<OpenstackNode> gws, int index) { |
| 1563 | Map<String, OpenstackNode> hashMap = new HashMap<>(); |
| 1564 | gws.forEach(gw -> hashMap.put(gw.hostname(), gw)); |
| 1565 | TreeMap<String, OpenstackNode> treeMap = new TreeMap<>(hashMap); |
| 1566 | Iterator<String> iteratorKey = treeMap.keySet().iterator(); |
| 1567 | |
| 1568 | int intIndex = 0; |
| 1569 | OpenstackNode gw = null; |
| 1570 | while (iteratorKey.hasNext()) { |
| 1571 | String key = iteratorKey.next(); |
| 1572 | |
| 1573 | if (intIndex == index) { |
| 1574 | gw = treeMap.get(key); |
| 1575 | } |
| 1576 | intIndex++; |
| 1577 | } |
| 1578 | return gw; |
| 1579 | } |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame^] | 1580 | |
| 1581 | private static void print(String format, Object... args) { |
| 1582 | System.out.println(String.format(format, args)); |
| 1583 | } |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 1584 | } |