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