Jian Li | 0b56428 | 2018-06-20 00:50:53 +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.web; |
| 17 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.Lists; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 23 | import org.onlab.util.ItemNotFoundException; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 24 | import org.onosproject.cfg.ComponentConfigService; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.flow.FlowRuleService; |
| 28 | import org.onosproject.openstacknetworking.api.Constants; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknetworking.api.OpenstackHaService; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 30 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
| 31 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
| 32 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler; |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 34 | import org.onosproject.openstacknetworking.impl.OpenstackRoutingSnatHandler; |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 35 | import org.onosproject.openstacknetworking.impl.OpenstackSecurityGroupHandler; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 36 | import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 37 | import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil; |
| 38 | import org.onosproject.openstacknode.api.NodeState; |
| 39 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 40 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 41 | import org.onosproject.rest.AbstractWebResource; |
| 42 | import org.openstack4j.api.OSClient; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 43 | import org.openstack4j.model.network.NetFloatingIP; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 47 | import javax.ws.rs.Consumes; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 48 | import javax.ws.rs.GET; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 49 | import javax.ws.rs.PUT; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 50 | import javax.ws.rs.Path; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 51 | import javax.ws.rs.PathParam; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 52 | import javax.ws.rs.Produces; |
| 53 | import javax.ws.rs.core.MediaType; |
| 54 | import javax.ws.rs.core.Response; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 55 | import java.util.Comparator; |
sanghoshin | 2a354e3 | 2018-11-09 12:52:44 +0800 | [diff] [blame] | 56 | import java.util.HashMap; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 57 | import java.util.List; |
sanghoshin | 2a354e3 | 2018-11-09 12:52:44 +0800 | [diff] [blame] | 58 | import java.util.Map; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 59 | import java.util.Objects; |
| 60 | import java.util.Optional; |
| 61 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 62 | import static java.lang.Thread.sleep; |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 63 | import static java.util.stream.StreamSupport.stream; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 64 | import static javax.ws.rs.core.Response.status; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 65 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 66 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface; |
| 67 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkActivationFlag; |
| 68 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode; |
| 69 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue; |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 70 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValueAsBoolean; |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 71 | import static org.onosproject.openstacknode.api.NodeState.COMPLETE; |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 72 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 73 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER; |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 74 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * REST interface for synchronizing openstack network states and rules. |
| 78 | */ |
| 79 | @Path("management") |
| 80 | public class OpenstackManagementWebResource extends AbstractWebResource { |
| 81 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 82 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 83 | private static final String FLOATINGIPS = "floatingips"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 84 | private static final String ARP_MODE_NAME = "arpMode"; |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 85 | private static final String USE_SECURITY_GROUP_NAME = "useSecurityGroup"; |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 86 | private static final String USE_STATEFUL_SNAT_NAME = "useStatefulSnat"; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 87 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 88 | private static final long SLEEP_MS = 3000; // we wait 3s for init each node |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 89 | private static final long TIMEOUT_MS = 10000; // we wait 10s |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 90 | |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 91 | private static final String DEVICE_OWNER_IFACE = "network:router_interface"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 92 | |
| 93 | private static final String ARP_MODE_REQUIRED = "ARP mode is not specified"; |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 94 | private static final String STATEFUL_SNAT_REQUIRED = "Stateful SNAT flag nis not specified"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 95 | |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 96 | private static final String SECURITY_GROUP_FLAG_REQUIRED = "Security Group flag is not specified"; |
| 97 | |
sanghoshin | 2a354e3 | 2018-11-09 12:52:44 +0800 | [diff] [blame] | 98 | private static final String HTTP_HEADER_ACCEPT = "accept"; |
| 99 | private static final String HTTP_HEADER_VALUE_JSON = "application/json"; |
| 100 | |
Jian Li | 812460d | 2019-05-13 15:41:30 +0900 | [diff] [blame] | 101 | private static final String IS_ACTIVE = "isActive"; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 102 | private static final String FLAG_TRUE = "true"; |
| 103 | private static final String FLAG_FALSE = "false"; |
| 104 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 105 | private final ObjectNode root = mapper().createObjectNode(); |
| 106 | private final ArrayNode floatingipsNode = root.putArray(FLOATINGIPS); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 107 | |
| 108 | private final OpenstackSecurityGroupAdminService osSgAdminService = |
| 109 | get(OpenstackSecurityGroupAdminService.class); |
| 110 | private final OpenstackNetworkAdminService osNetAdminService = |
| 111 | get(OpenstackNetworkAdminService.class); |
| 112 | private final OpenstackRouterAdminService osRouterAdminService = |
| 113 | get(OpenstackRouterAdminService.class); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 114 | private final OpenstackNodeAdminService osNodeAdminService = |
| 115 | get(OpenstackNodeAdminService.class); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 116 | private final OpenstackHaService osHaService = get(OpenstackHaService.class); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 117 | private final FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 118 | private final CoreService coreService = get(CoreService.class); |
| 119 | |
| 120 | /** |
| 121 | * Synchronizes the network states with openstack. |
| 122 | * |
| 123 | * @return 200 OK with sync result, 404 not found |
| 124 | */ |
| 125 | @GET |
| 126 | @Produces(MediaType.APPLICATION_JSON) |
| 127 | @Path("sync/states") |
| 128 | public Response syncStates() { |
| 129 | |
sanghoshin | 2a354e3 | 2018-11-09 12:52:44 +0800 | [diff] [blame] | 130 | Map<String, String> headerMap = new HashMap(); |
| 131 | headerMap.put(HTTP_HEADER_ACCEPT, HTTP_HEADER_VALUE_JSON); |
| 132 | |
Jian Li | 78ac065 | 2018-07-17 18:10:16 +0900 | [diff] [blame] | 133 | Optional<OpenstackNode> node = osNodeAdminService.nodes(CONTROLLER).stream().findFirst(); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 134 | if (!node.isPresent()) { |
| 135 | throw new ItemNotFoundException("Auth info is not found"); |
| 136 | } |
| 137 | |
| 138 | OSClient osClient = OpenstackNetworkingUtil.getConnectedClient(node.get()); |
| 139 | |
| 140 | if (osClient == null) { |
| 141 | throw new ItemNotFoundException("Auth info is not correct"); |
| 142 | } |
| 143 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 144 | try { |
| 145 | osClient.headers(headerMap).networking().securitygroup().list().forEach(osSg -> { |
| 146 | if (osSgAdminService.securityGroup(osSg.getId()) != null) { |
| 147 | osSgAdminService.updateSecurityGroup(osSg); |
| 148 | } else { |
| 149 | osSgAdminService.createSecurityGroup(osSg); |
| 150 | } |
| 151 | }); |
| 152 | } catch (Exception e) { |
| 153 | log.warn("Failed to retrieve security group due to {}", e.getMessage()); |
| 154 | return Response.serverError().build(); |
| 155 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 156 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 157 | try { |
| 158 | osClient.headers(headerMap).networking().network().list().forEach(osNet -> { |
| 159 | if (osNetAdminService.network(osNet.getId()) != null) { |
| 160 | osNetAdminService.updateNetwork(osNet); |
| 161 | } else { |
| 162 | osNetAdminService.createNetwork(osNet); |
| 163 | } |
| 164 | }); |
| 165 | } catch (Exception e) { |
| 166 | log.warn("Failed to retrieve network due to {}", e.getMessage()); |
| 167 | return Response.serverError().build(); |
| 168 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 169 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 170 | try { |
| 171 | osClient.headers(headerMap).networking().subnet().list().forEach(osSubnet -> { |
| 172 | if (osNetAdminService.subnet(osSubnet.getId()) != null) { |
| 173 | osNetAdminService.updateSubnet(osSubnet); |
| 174 | } else { |
| 175 | osNetAdminService.createSubnet(osSubnet); |
| 176 | } |
| 177 | }); |
| 178 | } catch (Exception e) { |
| 179 | log.warn("Failed to retrieve subnet due to {}", e.getMessage()); |
| 180 | return Response.serverError().build(); |
| 181 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 182 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 183 | try { |
| 184 | osClient.headers(headerMap).networking().port().list().forEach(osPort -> { |
| 185 | if (osNetAdminService.port(osPort.getId()) != null) { |
| 186 | osNetAdminService.updatePort(osPort); |
| 187 | } else { |
| 188 | osNetAdminService.createPort(osPort); |
| 189 | } |
| 190 | }); |
| 191 | } catch (Exception e) { |
| 192 | log.warn("Failed to retrieve port due to {}", e.getMessage()); |
| 193 | return Response.serverError().build(); |
| 194 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 195 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 196 | try { |
| 197 | osClient.headers(headerMap).networking().router().list().forEach(osRouter -> { |
| 198 | if (osRouterAdminService.router(osRouter.getId()) != null) { |
| 199 | osRouterAdminService.updateRouter(osRouter); |
| 200 | } else { |
| 201 | osRouterAdminService.createRouter(osRouter); |
| 202 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 203 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 204 | osNetAdminService.ports().stream() |
| 205 | .filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && |
| 206 | Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_IFACE)) |
| 207 | .forEach(osPort -> addRouterIface(osPort, osRouterAdminService)); |
| 208 | }); |
| 209 | } catch (Exception e) { |
| 210 | log.warn("Failed to retrieve router due to {}", e.getMessage()); |
| 211 | return Response.serverError().build(); |
| 212 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 213 | |
Jian Li | b6bc15d | 2019-09-24 17:19:05 +0900 | [diff] [blame] | 214 | try { |
| 215 | osClient.headers(headerMap).networking().floatingip().list().forEach(osFloating -> { |
| 216 | if (osRouterAdminService.floatingIp(osFloating.getId()) != null) { |
| 217 | osRouterAdminService.updateFloatingIp(osFloating); |
| 218 | } else { |
| 219 | osRouterAdminService.createFloatingIp(osFloating); |
| 220 | } |
| 221 | }); |
| 222 | } catch (Exception e) { |
| 223 | log.warn("Failed to retrieve floating IP due to {}", e.getMessage()); |
| 224 | return Response.serverError().build(); |
| 225 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 226 | |
| 227 | return ok(mapper().createObjectNode()).build(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Synchronizes the flow rules. |
| 232 | * |
| 233 | * @return 200 OK with sync result, 404 not found |
| 234 | */ |
| 235 | @GET |
| 236 | @Produces(MediaType.APPLICATION_JSON) |
| 237 | @Path("sync/rules") |
| 238 | public Response syncRules() { |
| 239 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 240 | syncRulesBase(); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 241 | return ok(mapper().createObjectNode()).build(); |
| 242 | } |
| 243 | |
| 244 | /** |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 245 | * Purges the flow rules installed by openstacknetworking. |
| 246 | * |
| 247 | * @return 200 OK with purge result, 404 not found |
| 248 | */ |
| 249 | @GET |
| 250 | @Produces(MediaType.APPLICATION_JSON) |
| 251 | @Path("purge/rules") |
| 252 | public Response purgeRules() { |
| 253 | |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 254 | if (purgeRulesBase()) { |
| 255 | return ok(mapper().createObjectNode()).build(); |
| 256 | } else { |
| 257 | return Response.serverError().build(); |
| 258 | } |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Configures the ARP mode (proxy | broadcast). |
| 263 | * |
| 264 | * @param arpmode ARP mode |
| 265 | * @return 200 OK with config result, 404 not found |
| 266 | */ |
| 267 | @GET |
| 268 | @Produces(MediaType.APPLICATION_JSON) |
| 269 | @Path("config/arpmode/{arpmode}") |
| 270 | public Response configArpMode(@PathParam("arpmode") String arpmode) { |
| 271 | |
| 272 | String arpModeStr = nullIsIllegal(arpmode, ARP_MODE_REQUIRED); |
| 273 | if (checkArpMode(arpModeStr)) { |
| 274 | configArpModeBase(arpModeStr); |
| 275 | |
| 276 | ComponentConfigService service = get(ComponentConfigService.class); |
| 277 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 278 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 279 | |
| 280 | // we check the arpMode configured in each component, and purge and |
| 281 | // reinstall all rules only if the arpMode is changed to the configured one |
| 282 | while (true) { |
| 283 | String switchingValue = |
| 284 | getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME); |
| 285 | String routingValue = |
| 286 | getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME); |
| 287 | |
| 288 | if (arpModeStr.equals(switchingValue) && arpModeStr.equals(routingValue)) { |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | purgeRulesBase(); |
| 294 | syncRulesBase(); |
| 295 | } else { |
| 296 | throw new IllegalArgumentException("The ARP mode is not valid"); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 297 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 298 | |
| 299 | return ok(mapper().createObjectNode()).build(); |
| 300 | } |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 301 | |
| 302 | /** |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 303 | * Configures the stateful SNAT flag (enable | disable). |
| 304 | * |
| 305 | * @param statefulSnat stateful SNAT flag |
| 306 | * @return 200 OK with config result, 404 not found |
| 307 | */ |
| 308 | @GET |
| 309 | @Produces(MediaType.APPLICATION_JSON) |
| 310 | @Path("config/statefulSnat/{statefulSnat}") |
| 311 | public Response configStatefulSnat(@PathParam("statefulSnat") String statefulSnat) { |
| 312 | String statefulSnatStr = nullIsIllegal(statefulSnat, STATEFUL_SNAT_REQUIRED); |
| 313 | boolean flag = checkActivationFlag(statefulSnatStr); |
| 314 | configStatefulSnatBase(flag); |
| 315 | |
| 316 | ComponentConfigService service = get(ComponentConfigService.class); |
| 317 | String snatComponent = OpenstackRoutingSnatHandler.class.getName(); |
| 318 | |
| 319 | while (true) { |
| 320 | boolean snatValue = |
| 321 | getPropertyValueAsBoolean( |
| 322 | service.getProperties(snatComponent), USE_STATEFUL_SNAT_NAME); |
| 323 | |
| 324 | if (flag == snatValue) { |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | purgeRulesBase(); |
| 330 | syncRulesBase(); |
| 331 | |
| 332 | return ok(mapper().createObjectNode()).build(); |
| 333 | } |
| 334 | |
| 335 | /** |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 336 | * Configures the security group (enable | disable). |
| 337 | * |
| 338 | * @param securityGroup security group activation flag |
| 339 | * @return 200 OK with config result, 404 not found |
| 340 | */ |
| 341 | @GET |
| 342 | @Produces(MediaType.APPLICATION_JSON) |
| 343 | @Path("config/securityGroup/{securityGroup}") |
| 344 | public Response configSecurityGroup(@PathParam("securityGroup") String securityGroup) { |
| 345 | String securityGroupStr = nullIsIllegal(securityGroup, SECURITY_GROUP_FLAG_REQUIRED); |
| 346 | |
| 347 | boolean flag = checkActivationFlag(securityGroupStr); |
| 348 | |
| 349 | ComponentConfigService service = get(ComponentConfigService.class); |
| 350 | String securityGroupComponent = OpenstackSecurityGroupHandler.class.getName(); |
| 351 | |
| 352 | service.setProperty(securityGroupComponent, USE_SECURITY_GROUP_NAME, String.valueOf(flag)); |
| 353 | |
| 354 | return ok(mapper().createObjectNode()).build(); |
| 355 | } |
| 356 | |
| 357 | /** |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 358 | * Obtains a collection of all floating IPs. |
| 359 | * |
| 360 | * @return 200 OK with a collection of floating IPs, 404 not found |
| 361 | */ |
| 362 | @GET |
| 363 | @Produces(MediaType.APPLICATION_JSON) |
| 364 | @Path("floatingips/all") |
| 365 | public Response allFloatingIps() { |
| 366 | |
| 367 | List<NetFloatingIP> floatingIps = |
| 368 | Lists.newArrayList(osRouterAdminService.floatingIps()); |
| 369 | floatingIps.stream() |
| 370 | .sorted(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)) |
| 371 | .forEach(fip -> floatingipsNode.add(fip.getFloatingIpAddress())); |
| 372 | |
| 373 | return ok(root).build(); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Obtains a collection of all floating IPs mapped with fixed IPs. |
| 378 | * |
| 379 | * @return 200 OK with a collection of floating IPs mapped with fixed IPs, |
| 380 | * 404 not found |
| 381 | */ |
| 382 | @GET |
| 383 | @Produces(MediaType.APPLICATION_JSON) |
| 384 | @Path("floatingips/mapped") |
| 385 | public Response mappedFloatingIps() { |
| 386 | |
| 387 | List<NetFloatingIP> floatingIps = |
| 388 | Lists.newArrayList(osRouterAdminService.floatingIps()); |
| 389 | |
| 390 | floatingIps.stream() |
| 391 | .filter(fip -> !Strings.isNullOrEmpty(fip.getFixedIpAddress())) |
| 392 | .sorted(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)) |
| 393 | .forEach(fip -> floatingipsNode.add(fip.getFloatingIpAddress())); |
| 394 | |
| 395 | return ok(root).build(); |
| 396 | } |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 397 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 398 | /** |
| 399 | * Configures the HA active-standby status. |
| 400 | * |
| 401 | * @param flag active-standby status |
| 402 | * @return 200 OK or 400 BAD_REQUEST |
| 403 | */ |
| 404 | @PUT |
| 405 | @Path("active/status/{flag}") |
| 406 | @Consumes(MediaType.APPLICATION_JSON) |
| 407 | @Produces(MediaType.APPLICATION_JSON) |
| 408 | public Response updateActiveStatus(@PathParam("flag") String flag) { |
| 409 | |
| 410 | if (FLAG_TRUE.equalsIgnoreCase(flag)) { |
| 411 | osHaService.setActive(true); |
| 412 | } |
| 413 | |
| 414 | if (FLAG_FALSE.equalsIgnoreCase(flag)) { |
| 415 | osHaService.setActive(false); |
| 416 | } |
| 417 | |
| 418 | return status(Response.Status.OK).build(); |
| 419 | } |
| 420 | |
| 421 | /** |
Jian Li | 812460d | 2019-05-13 15:41:30 +0900 | [diff] [blame] | 422 | * Configures the HA active-standby status. |
| 423 | * |
| 424 | * @return 200 OK with HA status. |
| 425 | * True if the node runs in active mode, false otherwise |
| 426 | */ |
| 427 | @GET |
| 428 | @Path("active/status") |
| 429 | @Produces(MediaType.APPLICATION_JSON) |
| 430 | public Response getActiveStatus() { |
| 431 | return ok(mapper().createObjectNode().put(IS_ACTIVE, osHaService.isActive())).build(); |
| 432 | } |
| 433 | |
| 434 | /** |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 435 | * Configures the HA active IP address. |
| 436 | * |
| 437 | * @param ip IP address of active node |
| 438 | * @return 200 OK or 400 BAD_REQUEST |
| 439 | */ |
| 440 | @PUT |
| 441 | @Path("active/ip/{ip}") |
| 442 | @Consumes(MediaType.APPLICATION_JSON) |
| 443 | @Produces(MediaType.APPLICATION_JSON) |
| 444 | public Response updateActiveIp(@PathParam("ip") String ip) { |
| 445 | |
| 446 | osHaService.setActiveIp(IpAddress.valueOf(ip)); |
| 447 | |
| 448 | return status(Response.Status.OK).build(); |
| 449 | } |
| 450 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 451 | private void syncRulesBase() { |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 452 | // we first initialize the COMPUTE node, in order to feed all instance ports |
| 453 | // by referring to ports' information obtained from neutron server |
| 454 | osNodeAdminService.completeNodes(COMPUTE).forEach(this::syncRulesBaseForNode); |
| 455 | osNodeAdminService.completeNodes(GATEWAY).forEach(this::syncRulesBaseForNode); |
| 456 | } |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 457 | |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 458 | private void syncRulesBaseForNode(OpenstackNode osNode) { |
| 459 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 460 | osNodeAdminService.updateNode(updated); |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 461 | |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 462 | boolean result = true; |
| 463 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 464 | |
| 465 | while (osNodeAdminService.node(osNode.hostname()).state() != COMPLETE) { |
| 466 | |
| 467 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 468 | |
| 469 | try { |
| 470 | sleep(SLEEP_MS); |
| 471 | } catch (InterruptedException e) { |
| 472 | log.error("Exception caused during node synchronization..."); |
| 473 | } |
| 474 | |
| 475 | if (osNodeAdminService.node(osNode.hostname()).state() == COMPLETE) { |
| 476 | break; |
| 477 | } else { |
| 478 | osNodeAdminService.updateNode(updated); |
| 479 | log.info("Failed to synchronize flow rules, retrying..."); |
| 480 | } |
| 481 | |
| 482 | if (waitMs <= 0) { |
| 483 | result = false; |
| 484 | break; |
| 485 | } |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 486 | } |
| 487 | |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 488 | if (result) { |
| 489 | log.info("Successfully synchronize flow rules for node {}!", osNode.hostname()); |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 490 | } else { |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 491 | log.warn("Failed to synchronize flow rules for node {}.", osNode.hostname()); |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 492 | } |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 493 | } |
| 494 | |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 495 | private boolean purgeRulesBase() { |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 496 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 497 | if (appId == null) { |
| 498 | throw new ItemNotFoundException("application not found"); |
| 499 | } |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 500 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 501 | flowRuleService.removeFlowRulesById(appId); |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 502 | |
| 503 | boolean result = true; |
| 504 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 505 | |
| 506 | // we make sure all flow rules are removed from the store |
| 507 | while (stream(flowRuleService.getFlowEntriesById(appId) |
| 508 | .spliterator(), false).count() > 0) { |
| 509 | |
| 510 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 511 | |
| 512 | try { |
| 513 | sleep(SLEEP_MS); |
| 514 | } catch (InterruptedException e) { |
| 515 | log.error("Exception caused during rule purging..."); |
| 516 | } |
| 517 | |
| 518 | if (stream(flowRuleService.getFlowEntriesById(appId) |
| 519 | .spliterator(), false).count() == 0) { |
| 520 | break; |
| 521 | } else { |
| 522 | flowRuleService.removeFlowRulesById(appId); |
| 523 | log.info("Failed to purging flow rules, retrying rule purging..."); |
| 524 | } |
| 525 | |
| 526 | if (waitMs <= 0) { |
| 527 | result = false; |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (result) { |
| 533 | log.info("Successfully purged flow rules!"); |
| 534 | } else { |
| 535 | log.warn("Failed to purge flow rules."); |
| 536 | } |
| 537 | |
| 538 | return result; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | private void configArpModeBase(String arpMode) { |
| 542 | ComponentConfigService service = get(ComponentConfigService.class); |
| 543 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 544 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 545 | |
| 546 | service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode); |
| 547 | service.setProperty(routingComponent, ARP_MODE_NAME, arpMode); |
| 548 | } |
Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 549 | |
| 550 | private void configStatefulSnatBase(boolean snatFlag) { |
| 551 | ComponentConfigService service = get(ComponentConfigService.class); |
| 552 | String snatComponent = OpenstackRoutingSnatHandler.class.getName(); |
| 553 | |
| 554 | service.setProperty(snatComponent, USE_STATEFUL_SNAT_NAME, String.valueOf(snatFlag)); |
| 555 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 556 | } |