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 | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 22 | import org.onlab.util.ItemNotFoundException; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 23 | import org.onosproject.cfg.ComponentConfigService; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.flow.FlowRuleService; |
| 27 | import org.onosproject.openstacknetworking.api.Constants; |
| 28 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
| 29 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
| 30 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler; |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 32 | import org.onosproject.openstacknetworking.impl.OpenstackSecurityGroupHandler; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 34 | import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil; |
| 35 | import org.onosproject.openstacknode.api.NodeState; |
| 36 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 37 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 38 | import org.onosproject.rest.AbstractWebResource; |
| 39 | import org.openstack4j.api.OSClient; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 40 | import org.openstack4j.model.network.NetFloatingIP; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | import javax.ws.rs.GET; |
| 45 | import javax.ws.rs.Path; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 46 | import javax.ws.rs.PathParam; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 47 | import javax.ws.rs.Produces; |
| 48 | import javax.ws.rs.core.MediaType; |
| 49 | import javax.ws.rs.core.Response; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 50 | import java.util.Comparator; |
| 51 | import java.util.List; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 52 | import java.util.Objects; |
| 53 | import java.util.Optional; |
| 54 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 55 | import static java.lang.Thread.sleep; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 56 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame^] | 57 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface; |
| 58 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkActivationFlag; |
| 59 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode; |
| 60 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue; |
| 61 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 62 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER; |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame^] | 63 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * REST interface for synchronizing openstack network states and rules. |
| 67 | */ |
| 68 | @Path("management") |
| 69 | public class OpenstackManagementWebResource extends AbstractWebResource { |
| 70 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 71 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 72 | private static final String FLOATINGIPS = "floatingips"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 73 | private static final String ARP_MODE_NAME = "arpMode"; |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 74 | private static final String USE_SECURITY_GROUP_NAME = "useSecurityGroup"; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 75 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 76 | private static final long SLEEP_MS = 3000; // we wait 3s for init each node |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 77 | |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 78 | private static final String DEVICE_OWNER_IFACE = "network:router_interface"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 79 | |
| 80 | private static final String ARP_MODE_REQUIRED = "ARP mode is not specified"; |
| 81 | |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 82 | private static final String SECURITY_GROUP_FLAG_REQUIRED = "Security Group flag is not specified"; |
| 83 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 84 | private final ObjectNode root = mapper().createObjectNode(); |
| 85 | private final ArrayNode floatingipsNode = root.putArray(FLOATINGIPS); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 86 | |
| 87 | private final OpenstackSecurityGroupAdminService osSgAdminService = |
| 88 | get(OpenstackSecurityGroupAdminService.class); |
| 89 | private final OpenstackNetworkAdminService osNetAdminService = |
| 90 | get(OpenstackNetworkAdminService.class); |
| 91 | private final OpenstackRouterAdminService osRouterAdminService = |
| 92 | get(OpenstackRouterAdminService.class); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 93 | private final OpenstackNodeAdminService osNodeAdminService = |
| 94 | get(OpenstackNodeAdminService.class); |
| 95 | private final FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 96 | private final CoreService coreService = get(CoreService.class); |
| 97 | |
| 98 | /** |
| 99 | * Synchronizes the network states with openstack. |
| 100 | * |
| 101 | * @return 200 OK with sync result, 404 not found |
| 102 | */ |
| 103 | @GET |
| 104 | @Produces(MediaType.APPLICATION_JSON) |
| 105 | @Path("sync/states") |
| 106 | public Response syncStates() { |
| 107 | |
Jian Li | 78ac065 | 2018-07-17 18:10:16 +0900 | [diff] [blame] | 108 | Optional<OpenstackNode> node = osNodeAdminService.nodes(CONTROLLER).stream().findFirst(); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 109 | if (!node.isPresent()) { |
| 110 | throw new ItemNotFoundException("Auth info is not found"); |
| 111 | } |
| 112 | |
| 113 | OSClient osClient = OpenstackNetworkingUtil.getConnectedClient(node.get()); |
| 114 | |
| 115 | if (osClient == null) { |
| 116 | throw new ItemNotFoundException("Auth info is not correct"); |
| 117 | } |
| 118 | |
| 119 | osClient.networking().securitygroup().list().forEach(osSg -> { |
| 120 | if (osSgAdminService.securityGroup(osSg.getId()) != null) { |
| 121 | osSgAdminService.updateSecurityGroup(osSg); |
| 122 | } else { |
| 123 | osSgAdminService.createSecurityGroup(osSg); |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | osClient.networking().network().list().forEach(osNet -> { |
| 128 | if (osNetAdminService.network(osNet.getId()) != null) { |
| 129 | osNetAdminService.updateNetwork(osNet); |
| 130 | } else { |
| 131 | osNetAdminService.createNetwork(osNet); |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | osClient.networking().subnet().list().forEach(osSubnet -> { |
| 136 | if (osNetAdminService.subnet(osSubnet.getId()) != null) { |
| 137 | osNetAdminService.updateSubnet(osSubnet); |
| 138 | } else { |
| 139 | osNetAdminService.createSubnet(osSubnet); |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | osClient.networking().port().list().forEach(osPort -> { |
| 144 | if (osNetAdminService.port(osPort.getId()) != null) { |
| 145 | osNetAdminService.updatePort(osPort); |
| 146 | } else { |
| 147 | osNetAdminService.createPort(osPort); |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | osClient.networking().router().list().forEach(osRouter -> { |
| 152 | if (osRouterAdminService.router(osRouter.getId()) != null) { |
| 153 | osRouterAdminService.updateRouter(osRouter); |
| 154 | } else { |
| 155 | osRouterAdminService.createRouter(osRouter); |
| 156 | } |
| 157 | |
| 158 | osNetAdminService.ports().stream() |
| 159 | .filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && |
| 160 | Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_IFACE)) |
| 161 | .forEach(osPort -> addRouterIface(osPort, osRouterAdminService)); |
| 162 | }); |
| 163 | |
| 164 | osClient.networking().floatingip().list().forEach(osFloating -> { |
| 165 | if (osRouterAdminService.floatingIp(osFloating.getId()) != null) { |
| 166 | osRouterAdminService.updateFloatingIp(osFloating); |
| 167 | } else { |
| 168 | osRouterAdminService.createFloatingIp(osFloating); |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | return ok(mapper().createObjectNode()).build(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Synchronizes the flow rules. |
| 177 | * |
| 178 | * @return 200 OK with sync result, 404 not found |
| 179 | */ |
| 180 | @GET |
| 181 | @Produces(MediaType.APPLICATION_JSON) |
| 182 | @Path("sync/rules") |
| 183 | public Response syncRules() { |
| 184 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 185 | syncRulesBase(); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 186 | return ok(mapper().createObjectNode()).build(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Purges the network states. |
| 191 | * |
| 192 | * @return 200 OK with purge result, 404 not found |
| 193 | */ |
| 194 | @GET |
| 195 | @Produces(MediaType.APPLICATION_JSON) |
| 196 | @Path("purge/states") |
| 197 | public Response purgeStates() { |
| 198 | |
| 199 | osRouterAdminService.clear(); |
| 200 | osNetAdminService.clear(); |
| 201 | osSgAdminService.clear(); |
| 202 | |
| 203 | return ok(mapper().createObjectNode()).build(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Purges the flow rules installed by openstacknetworking. |
| 208 | * |
| 209 | * @return 200 OK with purge result, 404 not found |
| 210 | */ |
| 211 | @GET |
| 212 | @Produces(MediaType.APPLICATION_JSON) |
| 213 | @Path("purge/rules") |
| 214 | public Response purgeRules() { |
| 215 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 216 | purgeRulesBase(); |
| 217 | return ok(mapper().createObjectNode()).build(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Configures the ARP mode (proxy | broadcast). |
| 222 | * |
| 223 | * @param arpmode ARP mode |
| 224 | * @return 200 OK with config result, 404 not found |
| 225 | */ |
| 226 | @GET |
| 227 | @Produces(MediaType.APPLICATION_JSON) |
| 228 | @Path("config/arpmode/{arpmode}") |
| 229 | public Response configArpMode(@PathParam("arpmode") String arpmode) { |
| 230 | |
| 231 | String arpModeStr = nullIsIllegal(arpmode, ARP_MODE_REQUIRED); |
| 232 | if (checkArpMode(arpModeStr)) { |
| 233 | configArpModeBase(arpModeStr); |
| 234 | |
| 235 | ComponentConfigService service = get(ComponentConfigService.class); |
| 236 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 237 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 238 | |
| 239 | // we check the arpMode configured in each component, and purge and |
| 240 | // reinstall all rules only if the arpMode is changed to the configured one |
| 241 | while (true) { |
| 242 | String switchingValue = |
| 243 | getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME); |
| 244 | String routingValue = |
| 245 | getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME); |
| 246 | |
| 247 | if (arpModeStr.equals(switchingValue) && arpModeStr.equals(routingValue)) { |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | purgeRulesBase(); |
| 253 | syncRulesBase(); |
| 254 | } else { |
| 255 | throw new IllegalArgumentException("The ARP mode is not valid"); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 256 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 257 | |
| 258 | return ok(mapper().createObjectNode()).build(); |
| 259 | } |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 260 | |
| 261 | /** |
Jian Li | cad36c7 | 2018-09-13 17:44:54 +0900 | [diff] [blame] | 262 | * Configures the security group (enable | disable). |
| 263 | * |
| 264 | * @param securityGroup security group activation flag |
| 265 | * @return 200 OK with config result, 404 not found |
| 266 | */ |
| 267 | @GET |
| 268 | @Produces(MediaType.APPLICATION_JSON) |
| 269 | @Path("config/securityGroup/{securityGroup}") |
| 270 | public Response configSecurityGroup(@PathParam("securityGroup") String securityGroup) { |
| 271 | String securityGroupStr = nullIsIllegal(securityGroup, SECURITY_GROUP_FLAG_REQUIRED); |
| 272 | |
| 273 | boolean flag = checkActivationFlag(securityGroupStr); |
| 274 | |
| 275 | ComponentConfigService service = get(ComponentConfigService.class); |
| 276 | String securityGroupComponent = OpenstackSecurityGroupHandler.class.getName(); |
| 277 | |
| 278 | service.setProperty(securityGroupComponent, USE_SECURITY_GROUP_NAME, String.valueOf(flag)); |
| 279 | |
| 280 | return ok(mapper().createObjectNode()).build(); |
| 281 | } |
| 282 | |
| 283 | /** |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 284 | * Obtains a collection of all floating IPs. |
| 285 | * |
| 286 | * @return 200 OK with a collection of floating IPs, 404 not found |
| 287 | */ |
| 288 | @GET |
| 289 | @Produces(MediaType.APPLICATION_JSON) |
| 290 | @Path("floatingips/all") |
| 291 | public Response allFloatingIps() { |
| 292 | |
| 293 | List<NetFloatingIP> floatingIps = |
| 294 | Lists.newArrayList(osRouterAdminService.floatingIps()); |
| 295 | floatingIps.stream() |
| 296 | .sorted(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)) |
| 297 | .forEach(fip -> floatingipsNode.add(fip.getFloatingIpAddress())); |
| 298 | |
| 299 | return ok(root).build(); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Obtains a collection of all floating IPs mapped with fixed IPs. |
| 304 | * |
| 305 | * @return 200 OK with a collection of floating IPs mapped with fixed IPs, |
| 306 | * 404 not found |
| 307 | */ |
| 308 | @GET |
| 309 | @Produces(MediaType.APPLICATION_JSON) |
| 310 | @Path("floatingips/mapped") |
| 311 | public Response mappedFloatingIps() { |
| 312 | |
| 313 | List<NetFloatingIP> floatingIps = |
| 314 | Lists.newArrayList(osRouterAdminService.floatingIps()); |
| 315 | |
| 316 | floatingIps.stream() |
| 317 | .filter(fip -> !Strings.isNullOrEmpty(fip.getFixedIpAddress())) |
| 318 | .sorted(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)) |
| 319 | .forEach(fip -> floatingipsNode.add(fip.getFloatingIpAddress())); |
| 320 | |
| 321 | return ok(root).build(); |
| 322 | } |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 323 | |
| 324 | private void syncRulesBase() { |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame^] | 325 | // we first initialize the COMPUTE node, in order to feed all instance ports |
| 326 | // by referring to ports' information obtained from neutron server |
| 327 | osNodeAdminService.completeNodes(COMPUTE).forEach(this::syncRulesBaseForNode); |
| 328 | osNodeAdminService.completeNodes(GATEWAY).forEach(this::syncRulesBaseForNode); |
| 329 | } |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 330 | |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame^] | 331 | private void syncRulesBaseForNode(OpenstackNode osNode) { |
| 332 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 333 | osNodeAdminService.updateNode(updated); |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 334 | |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame^] | 335 | try { |
| 336 | sleep(SLEEP_MS); |
| 337 | } catch (InterruptedException e) { |
| 338 | log.error("Exception caused during node synchronization..."); |
| 339 | } |
| 340 | |
| 341 | if (osNodeAdminService.node(osNode.hostname()).state() == NodeState.COMPLETE) { |
| 342 | log.info("Finished sync rules for node {}", osNode.hostname()); |
| 343 | } else { |
| 344 | log.info("Failed to sync rules for node {}", osNode.hostname()); |
| 345 | } |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | private void purgeRulesBase() { |
| 349 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 350 | if (appId == null) { |
| 351 | throw new ItemNotFoundException("application not found"); |
| 352 | } |
| 353 | flowRuleService.removeFlowRulesById(appId); |
| 354 | } |
| 355 | |
| 356 | private void configArpModeBase(String arpMode) { |
| 357 | ComponentConfigService service = get(ComponentConfigService.class); |
| 358 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 359 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 360 | |
| 361 | service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode); |
| 362 | service.setProperty(routingComponent, ARP_MODE_NAME, arpMode); |
| 363 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 364 | } |