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; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.packet.Ethernet; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 28 | import org.onlab.packet.IpAddress; |
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; |
| 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 | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 45 | import org.onosproject.net.flow.FlowEntry; |
| 46 | import org.onosproject.net.flow.FlowRuleService; |
| 47 | import org.onosproject.net.flow.criteria.Criterion; |
| 48 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 49 | import org.onosproject.net.flow.instructions.Instruction; |
| 50 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 52 | import org.onosproject.net.host.HostEvent; |
| 53 | import org.onosproject.net.host.HostListener; |
| 54 | import org.onosproject.net.host.HostService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 55 | import org.onosproject.net.packet.InboundPacket; |
| 56 | import org.onosproject.net.packet.PacketContext; |
| 57 | import org.onosproject.net.packet.PacketProcessor; |
| 58 | import org.onosproject.net.packet.PacketService; |
| 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 61 | import java.util.List; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 62 | import java.util.Collection; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 63 | import java.util.NoSuchElementException; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 64 | import java.util.Set; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 65 | import java.util.concurrent.ExecutorService; |
| 66 | import java.util.concurrent.Executors; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 67 | import java.util.stream.Collectors; |
| 68 | |
| 69 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
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 | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 106 | protected FlowRuleService flowRuleService; |
| 107 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 108 | private ApplicationId appId; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 109 | private boolean doNotPushFlows; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 110 | private Ip4Address neutronServer; |
| 111 | private Ip4Address keystoneServer; |
| 112 | private String userName; |
| 113 | private String password; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 114 | private OpenstackArpHandler arpHandler; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 115 | private OpenstackRestHandler restHandler; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 116 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 117 | private ExecutorService deviceEventExcutorService = Executors.newFixedThreadPool(10); |
| 118 | |
| 119 | private InternalPacketProcessor internalPacketProcessor = new InternalPacketProcessor(); |
| 120 | private InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 121 | private InternalConfigListener internalConfigListener = new InternalConfigListener(); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 122 | private InternalHostListener internalHostListener = new InternalHostListener(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 123 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 124 | new ConfigFactory<ApplicationId, OpenstackSwitchingConfig>(APP_SUBJECT_FACTORY, |
| 125 | OpenstackSwitchingConfig.class, |
| 126 | "openstackswitching") { |
| 127 | @Override |
| 128 | public OpenstackSwitchingConfig createConfig() { |
| 129 | return new OpenstackSwitchingConfig(); |
| 130 | } |
| 131 | } |
| 132 | ); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 133 | |
| 134 | @Activate |
| 135 | protected void activate() { |
| 136 | appId = coreService |
| 137 | .registerApplication("org.onosproject.openstackswitching"); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 138 | |
| 139 | factories.forEach(cfgService::registerConfigFactory); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 140 | packetService.addProcessor(internalPacketProcessor, PacketProcessor.director(1)); |
| 141 | deviceService.addListener(internalDeviceListener); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 142 | hostService.addListener(internalHostListener); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 143 | cfgService.addListener(internalConfigListener); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 144 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 145 | internalConfigListener.configureNetwork(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 146 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 147 | log.info("Started"); |
| 148 | } |
| 149 | |
| 150 | @Deactivate |
| 151 | protected void deactivate() { |
| 152 | packetService.removeProcessor(internalPacketProcessor); |
| 153 | deviceService.removeListener(internalDeviceListener); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 154 | cfgService.removeListener(internalConfigListener); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 155 | factories.forEach(cfgService::unregisterConfigFactory); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 156 | |
| 157 | deviceEventExcutorService.shutdown(); |
| 158 | |
| 159 | log.info("Stopped"); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void createPorts(OpenstackPort openstackPort) { |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 164 | registerDhcpInfo(openstackPort); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 168 | public void deletePort(String uuid) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 169 | |
| 170 | } |
| 171 | |
| 172 | @Override |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 173 | public void updatePort(OpenstackPort openstackPort) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void createNetwork(OpenstackNetwork openstackNetwork) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 178 | } |
| 179 | |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 180 | @Override |
| 181 | public void createSubnet(OpenstackSubnet openstackSubnet) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 182 | } |
| 183 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 184 | @Override |
| 185 | public Collection<OpenstackPort> ports(String networkId) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 186 | Collection<OpenstackPort> ports = restHandler.getPorts(); |
| 187 | List<OpenstackPort> portList = ports.stream() |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 188 | .filter(p -> p.networkId().equals(networkId)) |
| 189 | .collect(Collectors.toList()); |
| 190 | |
| 191 | return portList; |
| 192 | } |
| 193 | |
| 194 | @Override |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 195 | public OpenstackPort port(Port port) { |
| 196 | Collection<OpenstackPort> ports = restHandler.getPorts(); |
| 197 | String uuid = port.annotations().value("portName").substring(3); |
| 198 | return ports.stream() |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 199 | .filter(p -> p.id().startsWith(uuid)) |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 200 | .findFirst().orElse(null); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public OpenstackPort port(String portId) { |
| 205 | Collection<OpenstackPort> ports = restHandler.getPorts(); |
| 206 | return ports.stream() |
| 207 | .filter(p -> p.id().equals(portId)) |
| 208 | .findFirst().orElse(null); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public OpenstackNetwork network(String networkId) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 213 | Collection<OpenstackNetwork> networks = restHandler.getNetworks(); |
| 214 | return networks.stream() |
| 215 | .filter(n -> n.id().equals(networkId)) |
| 216 | .findFirst().orElse(null); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 217 | } |
| 218 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 219 | private void processDeviceAdded(Device device) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 220 | log.debug("device {} is added", device.id()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | private void processPortAdded(Device device, Port port) { |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 224 | if (!port.annotations().value("portName").equals("vxlan") |
| 225 | && port.isEnabled() && !doNotPushFlows) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 226 | OpenstackSwitchingRulePopulator rulePopulator = |
| 227 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 228 | deviceService, restHandler, driverService); |
| 229 | rulePopulator.populateSwitchingRules(device, port); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | private void processPortRemoved(Device device, Port port) { |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 234 | log.debug("port {} is removed", port.toString()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 235 | } |
| 236 | |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 237 | private void initializeFlowRules() { |
| 238 | OpenstackSwitchingRulePopulator rulePopulator = |
| 239 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 240 | deviceService, restHandler, driverService); |
| 241 | |
| 242 | deviceService.getDevices().forEach(device -> { |
| 243 | log.debug("device {} num of ports {} ", device.id(), |
| 244 | deviceService.getPorts(device.id()).size()); |
| 245 | deviceService.getPorts(device.id()).stream() |
| 246 | .filter(port -> port.annotations().value("portName").startsWith("tap")) |
| 247 | .forEach(vmPort -> { |
| 248 | OpenstackPort osPort = rulePopulator.openstackPort(vmPort); |
| 249 | if (osPort != null) { |
| 250 | rulePopulator.populateSwitchingRules(device, vmPort); |
| 251 | registerDhcpInfo(osPort); |
| 252 | } else { |
| 253 | log.warn("No openstackPort information for port {}", vmPort); |
| 254 | } |
| 255 | } |
| 256 | ); |
| 257 | } |
| 258 | ); |
| 259 | } |
| 260 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 261 | private void processHostRemoved(Host host) { |
| 262 | log.debug("host {} was removed", host.toString()); |
| 263 | |
| 264 | try { |
| 265 | if (!doNotPushFlows) { |
| 266 | IpAddress hostIp = host.ipAddresses().stream(). |
| 267 | filter(ip -> ip.isIp4()).findAny().orElse(null); |
| 268 | OpenstackSwitchingRulePopulator rulePopulator = |
| 269 | new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, |
| 270 | deviceService, restHandler, driverService); |
| 271 | rulePopulator.removeSwitchingRules(host.location().deviceId(), |
| 272 | hostIp.getIp4Address()); |
| 273 | } |
| 274 | |
| 275 | dhcpService.removeStaticMapping(host.mac()); |
| 276 | } catch (NoSuchElementException e) { |
| 277 | log.error("No IP address is assigned."); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | private long getVniFromFlowRules(DeviceId deviceId, Ip4Address hostIp) { |
| 282 | |
| 283 | for (FlowEntry flowEntry: flowRuleService.getFlowEntries(deviceId)) { |
| 284 | Criterion c = flowEntry.selector().getCriterion(Criterion.Type.IPV4_DST); |
| 285 | if (c != null) { |
| 286 | IPCriterion destIpCriterion = (IPCriterion) c; |
| 287 | if (destIpCriterion.ip().getIp4Prefix().address().equals(hostIp)) { |
| 288 | for (Instruction i : flowEntry.treatment().immediate()) { |
| 289 | if (i.type().equals(Instruction.Type.L2MODIFICATION)) { |
| 290 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 291 | if (l2m.subtype().equals(L2ModificationInstruction.L2SubType.TUNNEL_ID)) { |
| 292 | L2ModificationInstruction.ModTunnelIdInstruction setTunnelInstr = |
| 293 | (L2ModificationInstruction.ModTunnelIdInstruction) l2m; |
| 294 | return setTunnelInstr.tunnelId(); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 305 | private void registerDhcpInfo(OpenstackPort openstackPort) { |
| 306 | Ip4Address ip4Address; |
| 307 | Ip4Address subnetMask; |
| 308 | Ip4Address dhcpServer; |
| 309 | Ip4Address gatewayIPAddress; |
| 310 | Ip4Address domainServer; |
| 311 | OpenstackSubnet openstackSubnet; |
| 312 | |
| 313 | ip4Address = (Ip4Address) openstackPort.fixedIps().values().toArray()[0]; |
| 314 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 315 | openstackSubnet = restHandler.getSubnets().stream() |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 316 | .filter(n -> n.networkId().equals(openstackPort.networkId())) |
| 317 | .findFirst().get(); |
| 318 | |
| 319 | subnetMask = Ip4Address.valueOf(buildSubnetMask(openstackSubnet.cidr())); |
| 320 | gatewayIPAddress = Ip4Address.valueOf(openstackSubnet.gatewayIp()); |
| 321 | dhcpServer = gatewayIPAddress; |
| 322 | // TODO: supports multiple DNS servers |
| 323 | if (openstackSubnet.dnsNameservers().isEmpty()) { |
| 324 | domainServer = Ip4Address.valueOf("8.8.8.8"); |
| 325 | } else { |
| 326 | domainServer = openstackSubnet.dnsNameservers().get(0); |
| 327 | } |
| 328 | List<Ip4Address> options = Lists.newArrayList(); |
| 329 | options.add(subnetMask); |
| 330 | options.add(dhcpServer); |
| 331 | options.add(gatewayIPAddress); |
| 332 | options.add(domainServer); |
| 333 | |
| 334 | dhcpService.setStaticMapping(openstackPort.macAddress(), ip4Address, true, options); |
| 335 | } |
| 336 | |
| 337 | private byte[] buildSubnetMask(String cidr) { |
| 338 | int prefix; |
| 339 | String[] parts = cidr.split("/"); |
| 340 | prefix = Integer.parseInt(parts[1]); |
| 341 | int mask = 0xffffffff << (32 - prefix); |
| 342 | byte[] bytes = new byte[]{(byte) (mask >>> 24), |
| 343 | (byte) (mask >> 16 & 0xff), (byte) (mask >> 8 & 0xff), (byte) (mask & 0xff)}; |
| 344 | |
| 345 | return bytes; |
| 346 | } |
| 347 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 348 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 349 | |
| 350 | private class InternalPacketProcessor implements PacketProcessor { |
| 351 | |
| 352 | @Override |
| 353 | public void process(PacketContext context) { |
| 354 | |
| 355 | if (context.isHandled()) { |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | InboundPacket pkt = context.inPacket(); |
| 360 | Ethernet ethernet = pkt.parsed(); |
| 361 | |
| 362 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 363 | arpHandler.processPacketIn(pkt); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 368 | private class InternalHostListener implements HostListener { |
| 369 | |
| 370 | @Override |
| 371 | public void event(HostEvent hostEvent) { |
| 372 | deviceEventExcutorService.execute(new InternalEventHandler(hostEvent)); |
| 373 | } |
| 374 | } |
| 375 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 376 | private class InternalDeviceListener implements DeviceListener { |
| 377 | |
| 378 | @Override |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 379 | public void event(DeviceEvent deviceEvent) { |
| 380 | deviceEventExcutorService.execute(new InternalEventHandler(deviceEvent)); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | private class InternalEventHandler implements Runnable { |
| 385 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 386 | volatile AbstractEvent event; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 387 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 388 | InternalEventHandler(AbstractEvent event) { |
| 389 | this.event = event; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void run() { |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 394 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 395 | if (event instanceof DeviceEvent) { |
| 396 | DeviceEvent deviceEvent = (DeviceEvent) event; |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 397 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 398 | switch (deviceEvent.type()) { |
| 399 | case DEVICE_ADDED: |
| 400 | processDeviceAdded((Device) deviceEvent.subject()); |
| 401 | break; |
| 402 | case DEVICE_UPDATED: |
| 403 | Port port = (Port) deviceEvent.subject(); |
| 404 | if (port.isEnabled()) { |
| 405 | processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); |
| 406 | } |
| 407 | break; |
| 408 | case DEVICE_AVAILABILITY_CHANGED: |
| 409 | Device device = (Device) deviceEvent.subject(); |
| 410 | if (deviceService.isAvailable(device.id())) { |
| 411 | processDeviceAdded(device); |
| 412 | } |
| 413 | break; |
| 414 | case PORT_ADDED: |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 415 | processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame^] | 416 | break; |
| 417 | case PORT_UPDATED: |
| 418 | processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); |
| 419 | break; |
| 420 | case PORT_REMOVED: |
| 421 | processPortRemoved((Device) deviceEvent.subject(), deviceEvent.port()); |
| 422 | break; |
| 423 | default: |
| 424 | break; |
| 425 | } |
| 426 | } else if (event instanceof HostEvent) { |
| 427 | HostEvent hostEvent = (HostEvent) event; |
| 428 | |
| 429 | switch (hostEvent.type()) { |
| 430 | case HOST_REMOVED: |
| 431 | processHostRemoved((Host) hostEvent.subject()); |
| 432 | break; |
| 433 | default: |
| 434 | break; |
| 435 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 440 | private class InternalConfigListener implements NetworkConfigListener { |
| 441 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 442 | public void configureNetwork() { |
| 443 | OpenstackSwitchingConfig cfg = |
| 444 | cfgService.getConfig(appId, OpenstackSwitchingConfig.class); |
| 445 | if (cfg == null) { |
| 446 | log.error("There is no openstack server information in config."); |
| 447 | return; |
| 448 | } |
| 449 | doNotPushFlows = cfg.doNotPushFlows(); |
| 450 | restHandler = new OpenstackRestHandler(cfg); |
| 451 | arpHandler = new OpenstackArpHandler(restHandler, packetService); |
sanghoshin | 075e3e7 | 2015-11-25 16:34:29 +0900 | [diff] [blame] | 452 | initializeFlowRules(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 453 | } |
| 454 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 455 | @Override |
| 456 | public void event(NetworkConfigEvent event) { |
| 457 | if (((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 458 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) && |
| 459 | event.configClass().equals(OpenstackSwitchingConfig.class)) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 460 | configureNetwork(); |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 461 | } |
| 462 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 463 | |
| 464 | } |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 465 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 466 | private final class PortInfo { |
| 467 | DeviceId deviceId; |
| 468 | String portName; |
| 469 | Ip4Address fixedIp; |
| 470 | Ip4Address hostIp; |
| 471 | |
| 472 | private PortInfo(DeviceId deviceId, String portName, Ip4Address fixedIp, |
| 473 | Ip4Address hostIp) { |
| 474 | this.deviceId = deviceId; |
| 475 | this.portName = portName; |
| 476 | this.fixedIp = fixedIp; |
| 477 | this.hostIp = hostIp; |
| 478 | } |
| 479 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 480 | } |