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