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