Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.routing; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.util.KryoNamespace; |
| 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Host; |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 40 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 41 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 42 | import org.onosproject.net.host.HostService; |
| 43 | import org.onosproject.openstackinterface.OpenstackFloatingIP; |
sangho | a2d9f49 | 2016-08-10 17:08:17 +0900 | [diff] [blame] | 44 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
| 45 | import org.onosproject.openstacknetworking.AbstractVmHandler; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 46 | import org.onosproject.openstacknetworking.Constants; |
| 47 | import org.onosproject.openstacknetworking.OpenstackFloatingIpService; |
| 48 | import org.onosproject.openstacknetworking.RulePopulatorUtil; |
| 49 | import org.onosproject.openstacknode.OpenstackNode; |
| 50 | import org.onosproject.openstacknode.OpenstackNodeEvent; |
| 51 | import org.onosproject.openstacknode.OpenstackNodeListener; |
| 52 | import org.onosproject.openstacknode.OpenstackNodeService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 53 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
| 54 | import org.onosproject.store.serializers.KryoNamespaces; |
| 55 | import org.onosproject.store.service.ConsistentMap; |
| 56 | import org.onosproject.store.service.Serializer; |
| 57 | import org.onosproject.store.service.StorageService; |
| 58 | import org.onosproject.store.service.Versioned; |
| 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
| 62 | import java.util.Objects; |
| 63 | import java.util.Optional; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | |
| 66 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 67 | import static org.onlab.util.Tools.groupedThreads; |
| 68 | import static org.onosproject.openstacknetworking.Constants.*; |
| 69 | import static org.onosproject.openstacknetworking.RulePopulatorUtil.buildExtension; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | @Service |
| 73 | @Component(immediate = true) |
sangho | a2d9f49 | 2016-08-10 17:08:17 +0900 | [diff] [blame] | 74 | public class OpenstackFloatingIpManager extends AbstractVmHandler implements OpenstackFloatingIpService { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 75 | |
| 76 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected CoreService coreService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected HostService hostService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected DeviceService deviceService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected FlowObjectiveService flowObjectiveService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected StorageService storageService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected OpenstackNodeService nodeService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected ScalableGatewayService gatewayService; |
| 98 | |
sangho | a2d9f49 | 2016-08-10 17:08:17 +0900 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected OpenstackInterfaceService openstackService; |
| 101 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 102 | private static final String NOT_ASSOCIATED = "null"; |
| 103 | private static final KryoNamespace.Builder FLOATING_IP_SERIALIZER = |
| 104 | KryoNamespace.newBuilder().register(KryoNamespaces.API); |
| 105 | |
| 106 | private final ExecutorService eventExecutor = newSingleThreadScheduledExecutor( |
| 107 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 108 | private final InternalNodeListener nodeListener = new InternalNodeListener(); |
| 109 | private ConsistentMap<IpAddress, Host> floatingIpMap; |
| 110 | |
| 111 | private ApplicationId appId; |
| 112 | |
| 113 | @Activate |
| 114 | protected void activate() { |
sangho | a2d9f49 | 2016-08-10 17:08:17 +0900 | [diff] [blame] | 115 | super.activate(); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 116 | appId = coreService.registerApplication(ROUTING_APP_ID); |
| 117 | nodeService.addListener(nodeListener); |
| 118 | floatingIpMap = storageService.<IpAddress, Host>consistentMapBuilder() |
| 119 | .withSerializer(Serializer.using(FLOATING_IP_SERIALIZER.build())) |
| 120 | .withName("openstackrouting-floatingip") |
| 121 | .withApplicationId(appId) |
| 122 | .build(); |
| 123 | |
| 124 | log.info("Started"); |
| 125 | } |
| 126 | |
| 127 | @Deactivate |
| 128 | protected void deactivate() { |
sangho | a2d9f49 | 2016-08-10 17:08:17 +0900 | [diff] [blame] | 129 | super.deactivate(); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 130 | nodeService.removeListener(nodeListener); |
| 131 | log.info("Stopped"); |
| 132 | } |
| 133 | |
| 134 | @Override |
sangho | a2d9f49 | 2016-08-10 17:08:17 +0900 | [diff] [blame] | 135 | protected void hostDetected(Host host) { |
| 136 | IpAddress hostIp = host.ipAddresses().stream().findFirst().get(); |
| 137 | Optional<OpenstackFloatingIP> floatingIp = openstackService.floatingIps().stream() |
| 138 | .filter(fip -> fip.fixedIpAddress() != null && fip.fixedIpAddress().equals(hostIp)) |
| 139 | .findFirst(); |
| 140 | if (floatingIp.isPresent()) { |
| 141 | eventExecutor.execute(() -> associateFloatingIp(floatingIp.get())); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void hostRemoved(Host host) { |
| 147 | IpAddress hostIp = host.ipAddresses().stream().findFirst().get(); |
| 148 | Optional<OpenstackFloatingIP> floatingIp = openstackService.floatingIps().stream() |
| 149 | .filter(fip -> fip.fixedIpAddress() != null && fip.fixedIpAddress().equals(hostIp)) |
| 150 | .findFirst(); |
| 151 | if (floatingIp.isPresent()) { |
| 152 | eventExecutor.execute(() -> disassociateFloatingIp(floatingIp.get())); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 157 | public void reinstallVmFlow(Host host) { |
| 158 | if (host == null) { |
| 159 | hostService.getHosts().forEach(h -> { |
| 160 | hostDetected(h); |
| 161 | log.info("Re-Install data plane flow of virtual machine {}", h); |
| 162 | }); |
| 163 | } else { |
| 164 | hostDetected(host); |
| 165 | log.info("Re-Install data plane flow of virtual machine {}", host); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void purgeVmFlow(Host host) { |
| 171 | if (host == null) { |
| 172 | hostService.getHosts().forEach(h -> { |
| 173 | hostRemoved(h); |
| 174 | log.info("Purge data plane flow of virtual machine {}", h); |
| 175 | }); |
| 176 | } else { |
| 177 | hostRemoved(host); |
| 178 | log.info("Purge data plane flow of virtual machine {}", host); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | @Override |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 183 | public void createFloatingIp(OpenstackFloatingIP floatingIp) { |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void updateFloatingIp(OpenstackFloatingIP floatingIp) { |
| 188 | if (Strings.isNullOrEmpty(floatingIp.portId()) || |
| 189 | floatingIp.portId().equals(NOT_ASSOCIATED)) { |
| 190 | eventExecutor.execute(() -> disassociateFloatingIp(floatingIp)); |
| 191 | } else { |
| 192 | eventExecutor.execute(() -> associateFloatingIp(floatingIp)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void deleteFloatingIp(String floatingIpId) { |
| 198 | } |
| 199 | |
| 200 | private void associateFloatingIp(OpenstackFloatingIP floatingIp) { |
| 201 | Optional<Host> associatedVm = Tools.stream(hostService.getHosts()) |
| 202 | .filter(host -> Objects.equals( |
| 203 | host.annotations().value(PORT_ID), |
| 204 | floatingIp.portId())) |
| 205 | .findAny(); |
| 206 | if (!associatedVm.isPresent()) { |
| 207 | log.warn("Failed to associate floating IP({}) to port:{}", |
| 208 | floatingIp.floatingIpAddress(), |
| 209 | floatingIp.portId()); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | floatingIpMap.put(floatingIp.floatingIpAddress(), associatedVm.get()); |
| 214 | populateFloatingIpRules(floatingIp.floatingIpAddress(), associatedVm.get()); |
| 215 | |
| 216 | log.info("Associated floating IP {} to fixed IP {}", |
| 217 | floatingIp.floatingIpAddress(), floatingIp.fixedIpAddress()); |
| 218 | } |
| 219 | |
| 220 | private void disassociateFloatingIp(OpenstackFloatingIP floatingIp) { |
| 221 | Versioned<Host> associatedVm = floatingIpMap.remove(floatingIp.floatingIpAddress()); |
| 222 | if (associatedVm == null) { |
| 223 | log.warn("Failed to disassociate floating IP({})", |
| 224 | floatingIp.floatingIpAddress()); |
| 225 | // No VM is actually associated with the floating IP, do nothing |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | removeFloatingIpRules(floatingIp.floatingIpAddress(), associatedVm.value()); |
| 230 | log.info("Disassociated floating IP {} from fixed IP {}", |
| 231 | floatingIp.floatingIpAddress(), |
| 232 | associatedVm.value().ipAddresses()); |
| 233 | } |
| 234 | |
| 235 | private void populateFloatingIpRules(IpAddress floatingIp, Host associatedVm) { |
| 236 | populateFloatingIpIncomingRules(floatingIp, associatedVm); |
| 237 | populateFloatingIpOutgoingRules(floatingIp, associatedVm); |
| 238 | } |
| 239 | |
| 240 | private void removeFloatingIpRules(IpAddress floatingIp, Host associatedVm) { |
| 241 | Optional<IpAddress> fixedIp = associatedVm.ipAddresses().stream().findFirst(); |
| 242 | if (!fixedIp.isPresent()) { |
| 243 | log.warn("Failed to remove floating IP({}) from {}", |
| 244 | floatingIp, associatedVm); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | TrafficSelector.Builder sOutgoingBuilder = DefaultTrafficSelector.builder(); |
| 249 | TrafficSelector.Builder sIncomingBuilder = DefaultTrafficSelector.builder(); |
| 250 | |
| 251 | sOutgoingBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 252 | .matchTunnelId(Long.valueOf(associatedVm.annotations().value(VXLAN_ID))) |
| 253 | .matchIPSrc(fixedIp.get().toIpPrefix()); |
| 254 | |
| 255 | sIncomingBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 256 | .matchIPDst(floatingIp.toIpPrefix()); |
| 257 | |
Sho SHIMIZU | 8ebb04a | 2016-10-06 15:58:29 -0700 | [diff] [blame] | 258 | gatewayService.getGatewayDeviceIds().forEach(deviceId -> { |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 259 | TrafficSelector.Builder sForTrafficFromVmBuilder = DefaultTrafficSelector.builder() |
| 260 | .matchEthType(Ethernet.TYPE_IPV4) |
| 261 | .matchIPDst(floatingIp.toIpPrefix()) |
| 262 | .matchInPort(nodeService.tunnelPort(deviceId).get()); |
| 263 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 264 | RulePopulatorUtil.setRule( |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 265 | flowObjectiveService, |
| 266 | appId, |
| 267 | deviceId, |
| 268 | sOutgoingBuilder.build(), |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 269 | DefaultTrafficTreatment.builder().build(), |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 270 | ForwardingObjective.Flag.VERSATILE, |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 271 | FLOATING_RULE_PRIORITY, false); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 272 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 273 | RulePopulatorUtil.setRule( |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 274 | flowObjectiveService, |
| 275 | appId, |
| 276 | deviceId, |
| 277 | sIncomingBuilder.build(), |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 278 | DefaultTrafficTreatment.builder().build(), |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 279 | ForwardingObjective.Flag.VERSATILE, |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 280 | FLOATING_RULE_PRIORITY, false); |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 281 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 282 | RulePopulatorUtil.setRule( |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 283 | flowObjectiveService, |
| 284 | appId, |
| 285 | deviceId, |
| 286 | sForTrafficFromVmBuilder.build(), |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 287 | DefaultTrafficTreatment.builder().build(), |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 288 | ForwardingObjective.Flag.VERSATILE, |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame] | 289 | FLOATING_RULE_FOR_TRAFFIC_FROM_VM_PRIORITY, false); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 290 | }); |
| 291 | } |
| 292 | |
| 293 | private void populateFloatingIpIncomingRules(IpAddress floatingIp, Host associatedVm) { |
| 294 | DeviceId cnodeId = associatedVm.location().deviceId(); |
| 295 | Optional<IpAddress> dataIp = nodeService.dataIp(cnodeId); |
| 296 | Optional<IpAddress> fixedIp = associatedVm.ipAddresses().stream().findFirst(); |
| 297 | |
| 298 | if (!fixedIp.isPresent() || !dataIp.isPresent()) { |
| 299 | log.warn("Failed to associate floating IP({})", floatingIp); |
| 300 | return; |
| 301 | } |
| 302 | |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 303 | TrafficSelector selectorForTrafficFromExternal = DefaultTrafficSelector.builder() |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 304 | .matchEthType(Ethernet.TYPE_IPV4) |
| 305 | .matchIPDst(floatingIp.toIpPrefix()) |
| 306 | .build(); |
| 307 | |
Sho SHIMIZU | 8ebb04a | 2016-10-06 15:58:29 -0700 | [diff] [blame] | 308 | gatewayService.getGatewayDeviceIds().forEach(gnodeId -> { |
| 309 | TrafficTreatment treatmentForTrafficFromExternal = DefaultTrafficTreatment.builder() |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 310 | .setEthSrc(Constants.DEFAULT_GATEWAY_MAC) |
| 311 | .setEthDst(associatedVm.mac()) |
| 312 | .setIpDst(associatedVm.ipAddresses().stream().findFirst().get()) |
| 313 | .setTunnelId(Long.valueOf(associatedVm.annotations().value(VXLAN_ID))) |
sangho | 4d28773 | 2016-08-04 23:24:13 +0900 | [diff] [blame] | 314 | .extension(buildExtension(deviceService, gnodeId, dataIp.get().getIp4Address()), |
| 315 | gnodeId) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 316 | .setOutput(nodeService.tunnelPort(gnodeId).get()) |
| 317 | .build(); |
| 318 | |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 319 | ForwardingObjective forwardingObjectiveForTrafficFromExternal = DefaultForwardingObjective.builder() |
| 320 | .withSelector(selectorForTrafficFromExternal) |
| 321 | .withTreatment(treatmentForTrafficFromExternal) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 322 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 323 | .withPriority(FLOATING_RULE_PRIORITY) |
| 324 | .fromApp(appId) |
| 325 | .add(); |
| 326 | |
Daniel Park | 1b0cb26 | 2016-09-07 16:31:53 +0900 | [diff] [blame] | 327 | flowObjectiveService.forward(gnodeId, forwardingObjectiveForTrafficFromExternal); |
| 328 | |
| 329 | |
| 330 | TrafficSelector selectorForTrafficFromVm = DefaultTrafficSelector.builder() |
| 331 | .matchEthType(Ethernet.TYPE_IPV4) |
| 332 | .matchIPDst(floatingIp.toIpPrefix()) |
| 333 | .matchInPort(nodeService.tunnelPort(gnodeId).get()) |
| 334 | .build(); |
| 335 | |
| 336 | TrafficTreatment treatmentForTrafficFromVm = DefaultTrafficTreatment.builder() |
| 337 | .setEthSrc(Constants.DEFAULT_GATEWAY_MAC) |
| 338 | .setEthDst(associatedVm.mac()) |
| 339 | .setIpDst(associatedVm.ipAddresses().stream().findFirst().get()) |
| 340 | .setTunnelId(Long.valueOf(associatedVm.annotations().value(VXLAN_ID))) |
| 341 | .extension(buildExtension(deviceService, gnodeId, dataIp.get().getIp4Address()), |
| 342 | gnodeId) |
| 343 | .setOutput(PortNumber.IN_PORT) |
| 344 | .build(); |
| 345 | |
| 346 | ForwardingObjective forwardingObjectiveForTrafficFromVm = DefaultForwardingObjective.builder() |
| 347 | .withSelector(selectorForTrafficFromVm) |
| 348 | .withTreatment(treatmentForTrafficFromVm) |
| 349 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 350 | .withPriority(FLOATING_RULE_FOR_TRAFFIC_FROM_VM_PRIORITY) |
| 351 | .fromApp(appId) |
| 352 | .add(); |
| 353 | |
| 354 | flowObjectiveService.forward(gnodeId, forwardingObjectiveForTrafficFromVm); |
| 355 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 356 | }); |
| 357 | } |
| 358 | |
| 359 | private void populateFloatingIpOutgoingRules(IpAddress floatingIp, Host associatedVm) { |
| 360 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 361 | .matchEthType(Ethernet.TYPE_IPV4) |
| 362 | .matchTunnelId(Long.valueOf(associatedVm.annotations().value(VXLAN_ID))) |
| 363 | .matchIPSrc(associatedVm.ipAddresses().stream().findFirst().get().toIpPrefix()) |
| 364 | .build(); |
| 365 | |
Sho SHIMIZU | 8ebb04a | 2016-10-06 15:58:29 -0700 | [diff] [blame] | 366 | gatewayService.getGatewayDeviceIds().forEach(gnodeId -> { |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 367 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 368 | .setIpSrc(floatingIp) |
| 369 | .setEthSrc(Constants.DEFAULT_GATEWAY_MAC) |
| 370 | .setEthDst(Constants.DEFAULT_EXTERNAL_ROUTER_MAC) |
| 371 | .setOutput(gatewayService.getUplinkPort(gnodeId)) |
| 372 | .build(); |
| 373 | |
| 374 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 375 | .withSelector(selector) |
| 376 | .withTreatment(treatment) |
| 377 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 378 | .withPriority(FLOATING_RULE_PRIORITY) |
| 379 | .fromApp(appId) |
| 380 | .add(); |
| 381 | |
| 382 | flowObjectiveService.forward(gnodeId, fo); |
| 383 | }); |
| 384 | } |
| 385 | |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 386 | // TODO consider the case that port with associated floating IP is attached to a VM |
| 387 | |
| 388 | private class InternalNodeListener implements OpenstackNodeListener { |
| 389 | |
| 390 | @Override |
| 391 | public void event(OpenstackNodeEvent event) { |
| 392 | OpenstackNode node = event.node(); |
| 393 | |
| 394 | switch (event.type()) { |
| 395 | case COMPLETE: |
sangho | 9e5f0f5 | 2016-12-23 13:59:02 +0900 | [diff] [blame^] | 396 | reinstallVmFlow(null); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 397 | break; |
| 398 | case INIT: |
| 399 | case DEVICE_CREATED: |
| 400 | case INCOMPLETE: |
| 401 | default: |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |