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