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; |
| 32 | import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil; |
| 34 | import org.onosproject.openstacknode.api.NodeState; |
| 35 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 36 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 37 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 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 | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 55 | import static org.onlab.util.Tools.nullIsIllegal; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 56 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 57 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode; |
| 58 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue; |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 59 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER; |
| 60 | |
| 61 | /** |
| 62 | * REST interface for synchronizing openstack network states and rules. |
| 63 | */ |
| 64 | @Path("management") |
| 65 | public class OpenstackManagementWebResource extends AbstractWebResource { |
| 66 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 67 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 68 | private static final String FLOATINGIPS = "floatingips"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 69 | private static final String ARP_MODE_NAME = "arpMode"; |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 70 | |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 71 | private static final String DEVICE_OWNER_IFACE = "network:router_interface"; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 72 | |
| 73 | private static final String ARP_MODE_REQUIRED = "ARP mode is not specified"; |
| 74 | |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 75 | private final ObjectNode root = mapper().createObjectNode(); |
| 76 | private final ArrayNode floatingipsNode = root.putArray(FLOATINGIPS); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 77 | |
| 78 | private final OpenstackSecurityGroupAdminService osSgAdminService = |
| 79 | get(OpenstackSecurityGroupAdminService.class); |
| 80 | private final OpenstackNetworkAdminService osNetAdminService = |
| 81 | get(OpenstackNetworkAdminService.class); |
| 82 | private final OpenstackRouterAdminService osRouterAdminService = |
| 83 | get(OpenstackRouterAdminService.class); |
| 84 | private final OpenstackNodeService osNodeService = |
| 85 | get(OpenstackNodeService.class); |
| 86 | private final OpenstackNodeAdminService osNodeAdminService = |
| 87 | get(OpenstackNodeAdminService.class); |
| 88 | private final FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 89 | private final CoreService coreService = get(CoreService.class); |
| 90 | |
| 91 | /** |
| 92 | * Synchronizes the network states with openstack. |
| 93 | * |
| 94 | * @return 200 OK with sync result, 404 not found |
| 95 | */ |
| 96 | @GET |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
| 98 | @Path("sync/states") |
| 99 | public Response syncStates() { |
| 100 | |
| 101 | Optional<OpenstackNode> node = osNodeService.nodes(CONTROLLER).stream().findFirst(); |
| 102 | if (!node.isPresent()) { |
| 103 | throw new ItemNotFoundException("Auth info is not found"); |
| 104 | } |
| 105 | |
| 106 | OSClient osClient = OpenstackNetworkingUtil.getConnectedClient(node.get()); |
| 107 | |
| 108 | if (osClient == null) { |
| 109 | throw new ItemNotFoundException("Auth info is not correct"); |
| 110 | } |
| 111 | |
| 112 | osClient.networking().securitygroup().list().forEach(osSg -> { |
| 113 | if (osSgAdminService.securityGroup(osSg.getId()) != null) { |
| 114 | osSgAdminService.updateSecurityGroup(osSg); |
| 115 | } else { |
| 116 | osSgAdminService.createSecurityGroup(osSg); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | osClient.networking().network().list().forEach(osNet -> { |
| 121 | if (osNetAdminService.network(osNet.getId()) != null) { |
| 122 | osNetAdminService.updateNetwork(osNet); |
| 123 | } else { |
| 124 | osNetAdminService.createNetwork(osNet); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | osClient.networking().subnet().list().forEach(osSubnet -> { |
| 129 | if (osNetAdminService.subnet(osSubnet.getId()) != null) { |
| 130 | osNetAdminService.updateSubnet(osSubnet); |
| 131 | } else { |
| 132 | osNetAdminService.createSubnet(osSubnet); |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | osClient.networking().port().list().forEach(osPort -> { |
| 137 | if (osNetAdminService.port(osPort.getId()) != null) { |
| 138 | osNetAdminService.updatePort(osPort); |
| 139 | } else { |
| 140 | osNetAdminService.createPort(osPort); |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | osClient.networking().router().list().forEach(osRouter -> { |
| 145 | if (osRouterAdminService.router(osRouter.getId()) != null) { |
| 146 | osRouterAdminService.updateRouter(osRouter); |
| 147 | } else { |
| 148 | osRouterAdminService.createRouter(osRouter); |
| 149 | } |
| 150 | |
| 151 | osNetAdminService.ports().stream() |
| 152 | .filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && |
| 153 | Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_IFACE)) |
| 154 | .forEach(osPort -> addRouterIface(osPort, osRouterAdminService)); |
| 155 | }); |
| 156 | |
| 157 | osClient.networking().floatingip().list().forEach(osFloating -> { |
| 158 | if (osRouterAdminService.floatingIp(osFloating.getId()) != null) { |
| 159 | osRouterAdminService.updateFloatingIp(osFloating); |
| 160 | } else { |
| 161 | osRouterAdminService.createFloatingIp(osFloating); |
| 162 | } |
| 163 | }); |
| 164 | |
| 165 | return ok(mapper().createObjectNode()).build(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Synchronizes the flow rules. |
| 170 | * |
| 171 | * @return 200 OK with sync result, 404 not found |
| 172 | */ |
| 173 | @GET |
| 174 | @Produces(MediaType.APPLICATION_JSON) |
| 175 | @Path("sync/rules") |
| 176 | public Response syncRules() { |
| 177 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 178 | syncRulesBase(); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 179 | return ok(mapper().createObjectNode()).build(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Purges the network states. |
| 184 | * |
| 185 | * @return 200 OK with purge result, 404 not found |
| 186 | */ |
| 187 | @GET |
| 188 | @Produces(MediaType.APPLICATION_JSON) |
| 189 | @Path("purge/states") |
| 190 | public Response purgeStates() { |
| 191 | |
| 192 | osRouterAdminService.clear(); |
| 193 | osNetAdminService.clear(); |
| 194 | osSgAdminService.clear(); |
| 195 | |
| 196 | return ok(mapper().createObjectNode()).build(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Purges the flow rules installed by openstacknetworking. |
| 201 | * |
| 202 | * @return 200 OK with purge result, 404 not found |
| 203 | */ |
| 204 | @GET |
| 205 | @Produces(MediaType.APPLICATION_JSON) |
| 206 | @Path("purge/rules") |
| 207 | public Response purgeRules() { |
| 208 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 209 | purgeRulesBase(); |
| 210 | return ok(mapper().createObjectNode()).build(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Configures the ARP mode (proxy | broadcast). |
| 215 | * |
| 216 | * @param arpmode ARP mode |
| 217 | * @return 200 OK with config result, 404 not found |
| 218 | */ |
| 219 | @GET |
| 220 | @Produces(MediaType.APPLICATION_JSON) |
| 221 | @Path("config/arpmode/{arpmode}") |
| 222 | public Response configArpMode(@PathParam("arpmode") String arpmode) { |
| 223 | |
| 224 | String arpModeStr = nullIsIllegal(arpmode, ARP_MODE_REQUIRED); |
| 225 | if (checkArpMode(arpModeStr)) { |
| 226 | configArpModeBase(arpModeStr); |
| 227 | |
| 228 | ComponentConfigService service = get(ComponentConfigService.class); |
| 229 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 230 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 231 | |
| 232 | // we check the arpMode configured in each component, and purge and |
| 233 | // reinstall all rules only if the arpMode is changed to the configured one |
| 234 | while (true) { |
| 235 | String switchingValue = |
| 236 | getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME); |
| 237 | String routingValue = |
| 238 | getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME); |
| 239 | |
| 240 | if (arpModeStr.equals(switchingValue) && arpModeStr.equals(routingValue)) { |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | purgeRulesBase(); |
| 246 | syncRulesBase(); |
| 247 | } else { |
| 248 | throw new IllegalArgumentException("The ARP mode is not valid"); |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 249 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 250 | |
| 251 | return ok(mapper().createObjectNode()).build(); |
| 252 | } |
Jian Li | 803a1d5 | 2018-06-21 21:47:48 +0900 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * Obtains a collection of all floating IPs. |
| 256 | * |
| 257 | * @return 200 OK with a collection of floating IPs, 404 not found |
| 258 | */ |
| 259 | @GET |
| 260 | @Produces(MediaType.APPLICATION_JSON) |
| 261 | @Path("floatingips/all") |
| 262 | public Response allFloatingIps() { |
| 263 | |
| 264 | List<NetFloatingIP> floatingIps = |
| 265 | Lists.newArrayList(osRouterAdminService.floatingIps()); |
| 266 | floatingIps.stream() |
| 267 | .sorted(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)) |
| 268 | .forEach(fip -> floatingipsNode.add(fip.getFloatingIpAddress())); |
| 269 | |
| 270 | return ok(root).build(); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Obtains a collection of all floating IPs mapped with fixed IPs. |
| 275 | * |
| 276 | * @return 200 OK with a collection of floating IPs mapped with fixed IPs, |
| 277 | * 404 not found |
| 278 | */ |
| 279 | @GET |
| 280 | @Produces(MediaType.APPLICATION_JSON) |
| 281 | @Path("floatingips/mapped") |
| 282 | public Response mappedFloatingIps() { |
| 283 | |
| 284 | List<NetFloatingIP> floatingIps = |
| 285 | Lists.newArrayList(osRouterAdminService.floatingIps()); |
| 286 | |
| 287 | floatingIps.stream() |
| 288 | .filter(fip -> !Strings.isNullOrEmpty(fip.getFixedIpAddress())) |
| 289 | .sorted(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)) |
| 290 | .forEach(fip -> floatingipsNode.add(fip.getFloatingIpAddress())); |
| 291 | |
| 292 | return ok(root).build(); |
| 293 | } |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame^] | 294 | |
| 295 | private void syncRulesBase() { |
| 296 | osNodeService.completeNodes().forEach(osNode -> { |
| 297 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 298 | osNodeAdminService.updateNode(updated); |
| 299 | }); |
| 300 | } |
| 301 | |
| 302 | private void purgeRulesBase() { |
| 303 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 304 | if (appId == null) { |
| 305 | throw new ItemNotFoundException("application not found"); |
| 306 | } |
| 307 | flowRuleService.removeFlowRulesById(appId); |
| 308 | } |
| 309 | |
| 310 | private void configArpModeBase(String arpMode) { |
| 311 | ComponentConfigService service = get(ComponentConfigService.class); |
| 312 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 313 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 314 | |
| 315 | service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode); |
| 316 | service.setProperty(routingComponent, ARP_MODE_NAME, arpMode); |
| 317 | } |
Jian Li | 0b56428 | 2018-06-20 00:50:53 +0900 | [diff] [blame] | 318 | } |