sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Jian Li | 7f256f5 | 2016-01-24 15:08:05 -0800 | [diff] [blame] | 16 | package org.onosproject.openstackswitching.impl; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 17 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.Ethernet; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 31 | import org.onosproject.dhcp.DhcpService; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 32 | import org.onosproject.event.AbstractEvent; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 33 | import org.onosproject.net.Device; |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 35 | import org.onosproject.net.Host; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 36 | import org.onosproject.net.Port; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 37 | import org.onosproject.net.config.ConfigFactory; |
| 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
| 40 | import org.onosproject.net.config.NetworkConfigRegistry; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceListener; |
| 43 | import org.onosproject.net.device.DeviceService; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 44 | import org.onosproject.net.driver.DriverService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 45 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 46 | import org.onosproject.net.host.HostEvent; |
| 47 | import org.onosproject.net.host.HostListener; |
| 48 | import org.onosproject.net.host.HostService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 49 | import org.onosproject.net.packet.InboundPacket; |
| 50 | import org.onosproject.net.packet.PacketContext; |
| 51 | import org.onosproject.net.packet.PacketProcessor; |
| 52 | import org.onosproject.net.packet.PacketService; |
Jian Li | 7f256f5 | 2016-01-24 15:08:05 -0800 | [diff] [blame] | 53 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 54 | import org.onosproject.openstackswitching.OpenstackPort; |
| 55 | import org.onosproject.openstackswitching.OpenstackSubnet; |
| 56 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 59 | import java.util.List; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 60 | import java.util.Collection; |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 61 | import java.util.Map; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 62 | import java.util.NoSuchElementException; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 63 | import java.util.Set; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 66 | import java.util.stream.Collectors; |
| 67 | |
| 68 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
sanghoshin | f6735b2 | 2015-12-17 18:23:53 +0900 | [diff] [blame] | 69 | import static org.onlab.util.Tools.groupedThreads; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 70 | |
| 71 | @SuppressWarnings("ALL") |
| 72 | @Service |
| 73 | @Component(immediate = true) |
| 74 | /** |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 75 | * Populates forwarding rules for VMs created by Openstack. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 76 | */ |
| 77 | public class OpenstackSwitchingManager implements OpenstackSwitchingService { |
| 78 | |
| 79 | private static Logger log = LoggerFactory |
| 80 | .getLogger(OpenstackSwitchingManager.class); |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected CoreService coreService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected PacketService packetService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected DeviceService deviceService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 92 | protected HostService hostService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 95 | protected FlowObjectiveService flowObjectiveService; |
| 96 | |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 98 | protected DhcpService dhcpService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 101 | protected NetworkConfigRegistry cfgService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 104 | protected DriverService driverService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 105 | |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 106 | public static final String PORTNAME_PREFIX_VM = "tap"; |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 107 | public static final String PORTNAME_PREFIX_ROUTER = "qr-"; |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 108 | public static final String PORTNAME_PREFIX_TUNNEL = "vxlan"; |
| 109 | public static final String PORTNAME = "portName"; |
| 110 | |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 111 | public static final String DEVICE_OWNER_GATEWAY = "network:router_gateway"; |
| 112 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 113 | private ApplicationId appId; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 114 | private boolean doNotPushFlows; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 115 | private Ip4Address neutronServer; |
| 116 | private Ip4Address keystoneServer; |
| 117 | private String userName; |
| 118 | private String password; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 119 | private OpenstackArpHandler arpHandler; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 120 | private OpenstackRestHandler restHandler; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 121 | |
sanghoshin | f6735b2 | 2015-12-17 18:23:53 +0900 | [diff] [blame] | 122 | private ExecutorService deviceEventExcutorService = |
| 123 | Executors.newSingleThreadExecutor(groupedThreads("onos/openstackswitching", "device-event")); |
| 124 | private ExecutorService networkEventExcutorService = |
| 125 | Executors.newSingleThreadExecutor(groupedThreads("onos/openstackswitching", "config-event")); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 126 | |
| 127 | private InternalPacketProcessor internalPacketProcessor = new InternalPacketProcessor(); |
| 128 | private InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 129 | private InternalConfigListener internalConfigListener = new InternalConfigListener(); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 130 | private InternalHostListener internalHostListener = new InternalHostListener(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 131 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 132 | new ConfigFactory<ApplicationId, OpenstackSwitchingConfig>(APP_SUBJECT_FACTORY, |
| 133 | OpenstackSwitchingConfig.class, |
| 134 | "openstackswitching") { |
| 135 | @Override |
| 136 | public OpenstackSwitchingConfig createConfig() { |
| 137 | return new OpenstackSwitchingConfig(); |
| 138 | } |
| 139 | } |
| 140 | ); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 141 | |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 142 | |
| 143 | private Map<String, OpenstackPortInfo> openstackPortInfoMap = Maps.newHashMap(); |
| 144 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 145 | @Activate |
| 146 | protected void activate() { |
| 147 | appId = coreService |
| 148 | .registerApplication("org.onosproject.openstackswitching"); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 149 | |
| 150 | factories.forEach(cfgService::registerConfigFactory); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 151 | packetService.addProcessor(internalPacketProcessor, PacketProcessor.director(1)); |
| 152 | deviceService.addListener(internalDeviceListener); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 153 | hostService.addListener(internalHostListener); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 154 | cfgService.addListener(internalConfigListener); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 155 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 156 | internalConfigListener.configureNetwork(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 157 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 158 | log.info("Started"); |
| 159 | } |
| 160 | |
| 161 | @Deactivate |
| 162 | protected void deactivate() { |
| 163 | packetService.removeProcessor(internalPacketProcessor); |
| 164 | deviceService.removeListener(internalDeviceListener); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 165 | cfgService.removeListener(internalConfigListener); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 166 | factories.forEach(cfgService::unregisterConfigFactory); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 167 | |
| 168 | deviceEventExcutorService.shutdown(); |
| 169 | |
| 170 | log.info("Stopped"); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void createPorts(OpenstackPort openstackPort) { |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 175 | if (!openstackPort.fixedIps().isEmpty() |
| 176 | && !openstackPort.deviceOwner().equals(DEVICE_OWNER_GATEWAY)) { |
sangho | f2ca7e5c | 2015-12-23 16:02:43 +0900 | [diff] [blame] | 177 | registerDhcpInfo(openstackPort); |
| 178 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 182 | public void deletePort(String uuid) { |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 183 | // When VMs are remvoed, the flow rules for the VMs are removed using ONOS port update event. |
| 184 | // But, when router is removed, no ONOS port event occurs and we need to use Neutron port event. |
| 185 | // Here we should not touch any rules for VMs. |
| 186 | log.debug("port {} was removed", uuid); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 187 | |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 188 | String routerPortName = PORTNAME_PREFIX_ROUTER + uuid.substring(0, 11); |
| 189 | OpenstackPortInfo routerPortInfo = openstackPortInfoMap.get(routerPortName); |
| 190 | if (routerPortInfo != null) { |
| 191 | dhcpService.removeStaticMapping(routerPortInfo.mac()); |
| 192 | if (!doNotPushFlows) { |
| 193 | deviceService.getPorts(routerPortInfo.deviceId()).forEach(port -> { |
| 194 | String pName = port.annotations().value("portName"); |
| 195 | if (pName.equals(routerPortName)) { |
| 196 | OpenstackSwitchingRulePopulator rulePopulator = |
| 197 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 198 | deviceService, restHandler, driverService); |
| 199 | |
| 200 | rulePopulator.removeSwitchingRules(doNotPushFlows, port, openstackPortInfoMap); |
| 201 | openstackPortInfoMap.remove(routerPortName); |
| 202 | return; |
| 203 | } |
| 204 | }); |
| 205 | } |
| 206 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 210 | public void updatePort(OpenstackPort openstackPort) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void createNetwork(OpenstackNetwork openstackNetwork) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 215 | } |
| 216 | |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 217 | @Override |
| 218 | public void createSubnet(OpenstackSubnet openstackSubnet) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 219 | } |
| 220 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 221 | @Override |
| 222 | public Collection<OpenstackPort> ports(String networkId) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 223 | Collection<OpenstackPort> ports = restHandler.getPorts(); |
| 224 | List<OpenstackPort> portList = ports.stream() |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 225 | .filter(p -> p.networkId().equals(networkId)) |
| 226 | .collect(Collectors.toList()); |
| 227 | |
| 228 | return portList; |
| 229 | } |
| 230 | |
| 231 | @Override |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 232 | public OpenstackPort port(Port port) { |
| 233 | Collection<OpenstackPort> ports = restHandler.getPorts(); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 234 | String uuid = port.annotations().value(PORTNAME).substring(3); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 235 | return ports.stream() |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 236 | .filter(p -> p.id().startsWith(uuid)) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 237 | .findFirst().orElse(null); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public OpenstackPort port(String portId) { |
| 242 | Collection<OpenstackPort> ports = restHandler.getPorts(); |
| 243 | return ports.stream() |
| 244 | .filter(p -> p.id().equals(portId)) |
| 245 | .findFirst().orElse(null); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public OpenstackNetwork network(String networkId) { |
sanghoshin | f4ffb94 | 2015-12-10 11:28:24 +0900 | [diff] [blame] | 250 | try { |
| 251 | Collection<OpenstackSubnet> subnets = restHandler.getSubnets().stream() |
| 252 | .filter(s -> s.networkId().equals(networkId)) |
| 253 | .collect(Collectors.toList()); |
| 254 | |
| 255 | Collection<OpenstackNetwork> networks = restHandler.getNetworks(); |
| 256 | OpenstackNetwork openstackNetwork = networks.stream() |
| 257 | .filter(n -> n.id().equals(networkId)) |
| 258 | .findFirst().get(); |
| 259 | |
| 260 | return OpenstackNetwork.builder() |
| 261 | .id(openstackNetwork.id()) |
| 262 | .name(openstackNetwork.name()) |
| 263 | .networkType(openstackNetwork.networkType()) |
| 264 | .segmentId(openstackNetwork.segmentId()) |
| 265 | .tenantId(openstackNetwork.tenantId()) |
| 266 | .subnets(subnets) |
| 267 | .build(); |
| 268 | } catch (NoSuchElementException e) { |
| 269 | log.warn("There is no network infor for net ID {}", networkId); |
| 270 | return null; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public OpenstackSubnet subnet(String subnetId) { |
| 276 | Collection<OpenstackSubnet> subnets = restHandler.getSubnets(); |
| 277 | try { |
| 278 | return subnets.stream() |
| 279 | .filter(s -> s.id().equals(subnetId)) |
| 280 | .findFirst().get(); |
| 281 | } catch (NoSuchElementException e) { |
| 282 | log.warn("There is no subnet info for subnet ID {}", subnetId); |
| 283 | return null; |
| 284 | } |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 285 | } |
| 286 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 287 | private void processDeviceAdded(Device device) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 288 | log.debug("device {} is added", device.id()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 289 | } |
| 290 | |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 291 | private void processPortUpdated(Device device, Port port) { |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 292 | if (!port.annotations().value(PORTNAME).equals(PORTNAME_PREFIX_TUNNEL) && !doNotPushFlows) { |
| 293 | if (port.isEnabled() || port.annotations().value(PORTNAME).startsWith(PORTNAME_PREFIX_ROUTER)) { |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 294 | OpenstackSwitchingRulePopulator rulePopulator = |
| 295 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 296 | deviceService, restHandler, driverService); |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 297 | rulePopulator.populateSwitchingRules(doNotPushFlows, device, port); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 298 | updatePortMap(device.id(), port, restHandler.getNetworks(), rulePopulator.openstackPort(port)); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 299 | //In case portupdate event is driven by vm shutoff from openstack |
| 300 | } else if (!port.isEnabled() && openstackPortInfoMap.containsKey(port.annotations().value(PORTNAME))) { |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 301 | log.debug("Flowrules according to the port {} were removed", port.number().toString()); |
| 302 | OpenstackSwitchingRulePopulator rulePopulator = |
| 303 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 304 | deviceService, restHandler, driverService); |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 305 | |
| 306 | rulePopulator.removeSwitchingRules(doNotPushFlows, port, openstackPortInfoMap); |
| 307 | dhcpService.removeStaticMapping(openstackPortInfoMap.get(port.annotations().value(PORTNAME)).mac()); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 308 | openstackPortInfoMap.remove(port.annotations().value(PORTNAME)); |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 309 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | private void processPortRemoved(Device device, Port port) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 314 | log.debug("port {} is removed", port.toString()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 315 | } |
| 316 | |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 317 | private void initializeFlowRules() { |
| 318 | OpenstackSwitchingRulePopulator rulePopulator = |
| 319 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 320 | deviceService, restHandler, driverService); |
| 321 | |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 322 | Collection<OpenstackNetwork> networks = restHandler.getNetworks(); |
| 323 | |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 324 | deviceService.getDevices().forEach(device -> { |
| 325 | log.debug("device {} num of ports {} ", device.id(), |
| 326 | deviceService.getPorts(device.id()).size()); |
| 327 | deviceService.getPorts(device.id()).stream() |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 328 | .filter(port -> port.annotations().value(PORTNAME).startsWith(PORTNAME_PREFIX_VM) || |
| 329 | port.annotations().value(PORTNAME).startsWith(PORTNAME_PREFIX_ROUTER)) |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 330 | .forEach(vmPort -> { |
| 331 | OpenstackPort osPort = rulePopulator.openstackPort(vmPort); |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 332 | if (osPort != null && !osPort.deviceOwner().equals(DEVICE_OWNER_GATEWAY)) { |
Hyunsun Moon | be5c24c | 2015-12-10 22:26:35 -0800 | [diff] [blame] | 333 | if (!doNotPushFlows) { |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 334 | rulePopulator.populateSwitchingRules(doNotPushFlows, device, vmPort); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 335 | updatePortMap(device.id(), vmPort, networks, osPort); |
Hyunsun Moon | be5c24c | 2015-12-10 22:26:35 -0800 | [diff] [blame] | 336 | } |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 337 | registerDhcpInfo(osPort); |
| 338 | } else { |
| 339 | log.warn("No openstackPort information for port {}", vmPort); |
| 340 | } |
| 341 | } |
| 342 | ); |
| 343 | } |
| 344 | ); |
| 345 | } |
| 346 | |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 347 | private void updatePortMap(DeviceId deviceId, Port port, Collection<OpenstackNetwork> networks, |
| 348 | OpenstackPort openstackPort) { |
| 349 | long vni = Long.parseLong(networks.stream() |
| 350 | .filter(n -> n.id().equals(openstackPort.networkId())) |
| 351 | .findAny().orElse(null).segmentId()); |
| 352 | |
| 353 | OpenstackPortInfo.Builder portBuilder = OpenstackPortInfo.builder() |
| 354 | .setDeviceId(deviceId) |
| 355 | .setHostIp((Ip4Address) openstackPort.fixedIps().values().stream().findFirst().orElse(null)) |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 356 | .setHostMac(openstackPort.macAddress()) |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 357 | .setVni(vni); |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 358 | |
| 359 | openstackPortInfoMap.putIfAbsent(port.annotations().value(PORTNAME), |
| 360 | portBuilder.build()); |
| 361 | } |
| 362 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 363 | private void processHostRemoved(Host host) { |
| 364 | log.debug("host {} was removed", host.toString()); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 365 | } |
| 366 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 367 | private void registerDhcpInfo(OpenstackPort openstackPort) { |
| 368 | Ip4Address ip4Address; |
| 369 | Ip4Address subnetMask; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 370 | Ip4Address gatewayIPAddress; |
sanghoshin | 46c6e3e | 2015-12-18 18:42:17 +0900 | [diff] [blame] | 371 | Ip4Address dhcpServer; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 372 | Ip4Address domainServer; |
| 373 | OpenstackSubnet openstackSubnet; |
| 374 | |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 375 | ip4Address = (Ip4Address) openstackPort.fixedIps().values().stream().findFirst().orElse(null); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 376 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 377 | openstackSubnet = restHandler.getSubnets().stream() |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 378 | .filter(n -> n.networkId().equals(openstackPort.networkId())) |
| 379 | .findFirst().get(); |
| 380 | |
| 381 | subnetMask = Ip4Address.valueOf(buildSubnetMask(openstackSubnet.cidr())); |
| 382 | gatewayIPAddress = Ip4Address.valueOf(openstackSubnet.gatewayIp()); |
| 383 | dhcpServer = gatewayIPAddress; |
| 384 | // TODO: supports multiple DNS servers |
| 385 | if (openstackSubnet.dnsNameservers().isEmpty()) { |
| 386 | domainServer = Ip4Address.valueOf("8.8.8.8"); |
| 387 | } else { |
| 388 | domainServer = openstackSubnet.dnsNameservers().get(0); |
| 389 | } |
| 390 | List<Ip4Address> options = Lists.newArrayList(); |
| 391 | options.add(subnetMask); |
| 392 | options.add(dhcpServer); |
| 393 | options.add(gatewayIPAddress); |
| 394 | options.add(domainServer); |
| 395 | |
| 396 | dhcpService.setStaticMapping(openstackPort.macAddress(), ip4Address, true, options); |
| 397 | } |
| 398 | |
| 399 | private byte[] buildSubnetMask(String cidr) { |
| 400 | int prefix; |
| 401 | String[] parts = cidr.split("/"); |
| 402 | prefix = Integer.parseInt(parts[1]); |
| 403 | int mask = 0xffffffff << (32 - prefix); |
| 404 | byte[] bytes = new byte[]{(byte) (mask >>> 24), |
| 405 | (byte) (mask >> 16 & 0xff), (byte) (mask >> 8 & 0xff), (byte) (mask & 0xff)}; |
| 406 | |
| 407 | return bytes; |
| 408 | } |
| 409 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 410 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 411 | |
| 412 | private class InternalPacketProcessor implements PacketProcessor { |
| 413 | |
| 414 | @Override |
| 415 | public void process(PacketContext context) { |
| 416 | |
| 417 | if (context.isHandled()) { |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | InboundPacket pkt = context.inPacket(); |
| 422 | Ethernet ethernet = pkt.parsed(); |
| 423 | |
Hyunsun Moon | c98e7c5 | 2016-01-11 02:54:27 -0800 | [diff] [blame] | 424 | if (ethernet != null && ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 425 | arpHandler.processPacketIn(pkt); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 430 | private class InternalHostListener implements HostListener { |
| 431 | |
| 432 | @Override |
| 433 | public void event(HostEvent hostEvent) { |
| 434 | deviceEventExcutorService.execute(new InternalEventHandler(hostEvent)); |
| 435 | } |
| 436 | } |
| 437 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 438 | private class InternalDeviceListener implements DeviceListener { |
| 439 | |
| 440 | @Override |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 441 | public void event(DeviceEvent deviceEvent) { |
| 442 | deviceEventExcutorService.execute(new InternalEventHandler(deviceEvent)); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | private class InternalEventHandler implements Runnable { |
| 447 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 448 | volatile AbstractEvent event; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 449 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 450 | InternalEventHandler(AbstractEvent event) { |
| 451 | this.event = event; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void run() { |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 456 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 457 | if (event instanceof DeviceEvent) { |
| 458 | DeviceEvent deviceEvent = (DeviceEvent) event; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 459 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 460 | switch (deviceEvent.type()) { |
| 461 | case DEVICE_ADDED: |
| 462 | processDeviceAdded((Device) deviceEvent.subject()); |
| 463 | break; |
| 464 | case DEVICE_UPDATED: |
| 465 | Port port = (Port) deviceEvent.subject(); |
| 466 | if (port.isEnabled()) { |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 467 | processPortUpdated((Device) deviceEvent.subject(), deviceEvent.port()); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 468 | } |
| 469 | break; |
| 470 | case DEVICE_AVAILABILITY_CHANGED: |
| 471 | Device device = (Device) deviceEvent.subject(); |
| 472 | if (deviceService.isAvailable(device.id())) { |
| 473 | processDeviceAdded(device); |
| 474 | } |
| 475 | break; |
| 476 | case PORT_ADDED: |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 477 | processPortUpdated((Device) deviceEvent.subject(), deviceEvent.port()); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 478 | break; |
| 479 | case PORT_UPDATED: |
Daniel Park | 5df6518 | 2016-01-09 00:12:03 +0900 | [diff] [blame] | 480 | processPortUpdated((Device) deviceEvent.subject(), deviceEvent.port()); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 481 | break; |
| 482 | case PORT_REMOVED: |
| 483 | processPortRemoved((Device) deviceEvent.subject(), deviceEvent.port()); |
| 484 | break; |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | } else if (event instanceof HostEvent) { |
| 489 | HostEvent hostEvent = (HostEvent) event; |
| 490 | |
| 491 | switch (hostEvent.type()) { |
| 492 | case HOST_REMOVED: |
| 493 | processHostRemoved((Host) hostEvent.subject()); |
| 494 | break; |
| 495 | default: |
| 496 | break; |
| 497 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 502 | private class InternalConfigListener implements NetworkConfigListener { |
| 503 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 504 | public void configureNetwork() { |
| 505 | OpenstackSwitchingConfig cfg = |
| 506 | cfgService.getConfig(appId, OpenstackSwitchingConfig.class); |
| 507 | if (cfg == null) { |
| 508 | log.error("There is no openstack server information in config."); |
| 509 | return; |
| 510 | } |
| 511 | doNotPushFlows = cfg.doNotPushFlows(); |
| 512 | restHandler = new OpenstackRestHandler(cfg); |
Hyunsun Moon | c98e7c5 | 2016-01-11 02:54:27 -0800 | [diff] [blame] | 513 | arpHandler = new OpenstackArpHandler(restHandler, packetService, hostService); |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 514 | initializeFlowRules(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 515 | } |
| 516 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 517 | @Override |
| 518 | public void event(NetworkConfigEvent event) { |
| 519 | if (((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 520 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) && |
| 521 | event.configClass().equals(OpenstackSwitchingConfig.class)) { |
sanghoshin | f6735b2 | 2015-12-17 18:23:53 +0900 | [diff] [blame] | 522 | networkEventExcutorService.execute(this::configureNetwork); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 523 | } |
| 524 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 525 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 526 | } |