Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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.cordvtn; |
| 17 | |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [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; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 32 | import org.onosproject.core.CoreService; |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 33 | import org.onosproject.dhcp.DhcpService; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 34 | import org.onosproject.mastership.MastershipService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 35 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 36 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Host; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 38 | import org.onosproject.net.HostId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 39 | import org.onosproject.net.HostLocation; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 40 | import org.onosproject.net.Port; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 41 | import org.onosproject.net.SparseAnnotations; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 42 | import org.onosproject.net.config.ConfigFactory; |
| 43 | import org.onosproject.net.config.NetworkConfigEvent; |
| 44 | import org.onosproject.net.config.NetworkConfigListener; |
| 45 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 46 | import org.onosproject.net.config.NetworkConfigService; |
| 47 | import org.onosproject.net.config.basics.SubjectFactories; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 49 | import org.onosproject.net.driver.DriverService; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRuleService; |
| 51 | import org.onosproject.net.group.GroupService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 52 | import org.onosproject.net.host.DefaultHostDescription; |
| 53 | import org.onosproject.net.host.HostDescription; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 54 | import org.onosproject.net.host.HostEvent; |
| 55 | import org.onosproject.net.host.HostListener; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 56 | import org.onosproject.net.host.HostProvider; |
| 57 | import org.onosproject.net.host.HostProviderRegistry; |
| 58 | import org.onosproject.net.host.HostProviderService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 59 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 60 | import org.onosproject.net.packet.PacketContext; |
| 61 | import org.onosproject.net.packet.PacketProcessor; |
| 62 | import org.onosproject.net.packet.PacketService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 63 | import org.onosproject.net.provider.AbstractProvider; |
| 64 | import org.onosproject.net.provider.ProviderId; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 65 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 66 | import org.onosproject.openstackswitching.OpenstackPort; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 67 | import org.onosproject.openstackswitching.OpenstackSubnet; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 68 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 71 | import java.util.List; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 72 | import java.util.Map; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 73 | import java.util.Set; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 74 | import java.util.concurrent.ExecutorService; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 75 | import java.util.stream.Collectors; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 76 | |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 77 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 78 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 79 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 80 | import static org.slf4j.LoggerFactory.getLogger; |
| 81 | |
| 82 | /** |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 83 | * Provisions virtual tenant networks with service chaining capability |
| 84 | * in OpenStack environment. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 85 | */ |
| 86 | @Component(immediate = true) |
| 87 | @Service |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 88 | public class CordVtn extends AbstractProvider implements CordVtnService, HostProvider { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 89 | |
| 90 | protected final Logger log = getLogger(getClass()); |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected CoreService coreService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 96 | protected NetworkConfigRegistry configRegistry; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected NetworkConfigService configService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 102 | protected HostProviderRegistry hostProviderRegistry; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 105 | protected DeviceService deviceService; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected HostService hostService; |
| 109 | |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 111 | protected DriverService driverService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 114 | protected FlowRuleService flowRuleService; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 117 | protected PacketService packetService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 120 | protected MastershipService mastershipService; |
| 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected GroupService groupService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 126 | protected OpenstackSwitchingService openstackService; |
| 127 | |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected DhcpService dhcpService; |
| 130 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 131 | private final ConfigFactory configFactory = |
| 132 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") { |
| 133 | @Override |
| 134 | public CordVtnConfig createConfig() { |
| 135 | return new CordVtnConfig(); |
| 136 | } |
| 137 | }; |
| 138 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 139 | private static final String DEFAULT_TUNNEL = "vxlan"; |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 140 | private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8"); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 141 | private static final String SERVICE_ID = "serviceId"; |
| 142 | private static final String LOCATION_IP = "locationIp"; |
| 143 | private static final String OPENSTACK_VM_ID = "openstackVmId"; |
| 144 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 145 | private final ExecutorService eventExecutor = |
| 146 | newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler")); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 147 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 148 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 149 | private final HostListener hostListener = new InternalHostListener(); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 150 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 151 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 152 | private ApplicationId appId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 153 | private HostProviderService hostProvider; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 154 | private CordVtnRuleInstaller ruleInstaller; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 155 | private CordVtnArpProxy arpProxy; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 156 | private volatile MacAddress gatewayMac = MacAddress.NONE; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 157 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 158 | /** |
| 159 | * Creates an cordvtn host location provider. |
| 160 | */ |
| 161 | public CordVtn() { |
| 162 | super(new ProviderId("host", CORDVTN_APP_ID)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 163 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 164 | |
| 165 | @Activate |
| 166 | protected void activate() { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 167 | appId = coreService.registerApplication("org.onosproject.cordvtn"); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 168 | ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService, |
| 169 | deviceService, |
| 170 | driverService, |
| 171 | groupService, |
| 172 | mastershipService, |
| 173 | DEFAULT_TUNNEL); |
| 174 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 175 | arpProxy = new CordVtnArpProxy(appId, packetService); |
| 176 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 177 | arpProxy.requestPacket(); |
| 178 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 179 | hostService.addListener(hostListener); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 180 | hostProvider = hostProviderRegistry.register(this); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 181 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 182 | configRegistry.registerConfigFactory(configFactory); |
| 183 | configService.addListener(configListener); |
| 184 | readConfiguration(); |
| 185 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 186 | log.info("Started"); |
| 187 | } |
| 188 | |
| 189 | @Deactivate |
| 190 | protected void deactivate() { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 191 | hostProviderRegistry.unregister(this); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 192 | hostService.removeListener(hostListener); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 193 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 194 | packetService.removeProcessor(packetProcessor); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 195 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 196 | configRegistry.unregisterConfigFactory(configFactory); |
| 197 | configService.removeListener(configListener); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 198 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 199 | eventExecutor.shutdown(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 200 | log.info("Stopped"); |
| 201 | } |
| 202 | |
| 203 | @Override |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 204 | public void triggerProbe(Host host) { |
| 205 | /* |
| 206 | * Note: In CORD deployment, we assume that all hosts are configured. |
| 207 | * Therefore no probe is required. |
| 208 | */ |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 212 | public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) { |
| 213 | CordService tService = getCordService(tServiceId); |
| 214 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 215 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 216 | if (tService == null || pService == null) { |
| 217 | log.error("Failed to create CordService for {}", tServiceId.id()); |
| 218 | return; |
| 219 | } |
| 220 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 221 | log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id()); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 222 | ruleInstaller.populateServiceDependencyRules(tService, pService); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 226 | public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) { |
| 227 | CordService tService = getCordService(tServiceId); |
| 228 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 229 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 230 | if (tService == null || pService == null) { |
| 231 | log.error("Failed to create CordService for {}", tServiceId.id()); |
| 232 | return; |
| 233 | } |
| 234 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 235 | log.info("Service dependency from {} to {} removed.", tService.id().id(), pService.id().id()); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 236 | ruleInstaller.removeServiceDependencyRules(tService, pService); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 239 | @Override |
| 240 | public void addServiceVm(CordVtnNode node, ConnectPoint connectPoint) { |
| 241 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 242 | OpenstackPort vPort = openstackService.port(port); |
| 243 | if (vPort == null) { |
| 244 | log.warn("Failed to get OpenstackPort for {}", getPortName(port)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 248 | MacAddress mac = vPort.macAddress(); |
| 249 | HostId hostId = HostId.hostId(mac); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 250 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 251 | Host host = hostService.getHost(hostId); |
| 252 | if (host != null) { |
| 253 | // Host is already known to the system, no HOST_ADDED event is triggered in this case. |
| 254 | // It happens when the application is restarted. |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 255 | String vmId = host.annotations().value(OPENSTACK_VM_ID); |
| 256 | if (vmId != null && vmId.equals(vPort.deviceId())) { |
| 257 | serviceVmAdded(host); |
| 258 | return; |
| 259 | } else { |
| 260 | hostProvider.hostVanished(host.id()); |
| 261 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 264 | Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values()); |
| 265 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 266 | .set(OPENSTACK_VM_ID, vPort.deviceId()) |
| 267 | .set(SERVICE_ID, vPort.networkId()) |
| 268 | .set(LOCATION_IP, node.localIp().toString()) |
| 269 | .build(); |
| 270 | |
| 271 | HostDescription hostDesc = new DefaultHostDescription( |
| 272 | mac, |
| 273 | VlanId.NONE, |
| 274 | new HostLocation(connectPoint, System.currentTimeMillis()), |
| 275 | ip, |
| 276 | annotations); |
| 277 | |
| 278 | hostProvider.hostDetected(hostId, hostDesc, false); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 281 | @Override |
| 282 | public void removeServiceVm(ConnectPoint connectPoint) { |
| 283 | Host host = hostService.getConnectedHosts(connectPoint) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 284 | .stream() |
| 285 | .findFirst() |
| 286 | .orElse(null); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 287 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 288 | if (host == null) { |
| 289 | log.debug("No host is connected on {}", connectPoint.toString()); |
| 290 | return; |
| 291 | } |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 292 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 293 | hostProvider.hostVanished(host.id()); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 297 | * Returns CordService by service ID. |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 298 | * |
| 299 | * @param serviceId service id |
| 300 | * @return cord service, or null if it fails to get network from OpenStack |
| 301 | */ |
| 302 | private CordService getCordService(CordServiceId serviceId) { |
| 303 | OpenstackNetwork vNet = openstackService.network(serviceId.id()); |
| 304 | if (vNet == null) { |
| 305 | log.warn("Couldn't find OpenStack network for service {}", serviceId.id()); |
| 306 | return null; |
| 307 | } |
| 308 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 309 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 310 | .findFirst() |
| 311 | .orElse(null); |
| 312 | if (subnet == null) { |
| 313 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id()); |
| 314 | return null; |
| 315 | } |
| 316 | |
| 317 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 318 | // TODO get tenant services from XOS |
| 319 | |
| 320 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 321 | .stream() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 322 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 323 | |
| 324 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 328 | * Returns CordService by OpenStack network. |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 329 | * |
| 330 | * @param vNet OpenStack network |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 331 | * @return cord service |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 332 | */ |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 333 | private CordService getCordService(OpenstackNetwork vNet) { |
| 334 | checkNotNull(vNet); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 335 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 336 | CordServiceId serviceId = CordServiceId.of(vNet.id()); |
| 337 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 338 | .findFirst() |
| 339 | .orElse(null); |
| 340 | if (subnet == null) { |
| 341 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId); |
| 342 | return null; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 343 | } |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 344 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 345 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 346 | // TODO get tenant services from XOS |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 347 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 348 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 349 | .stream() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 350 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 351 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 352 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 355 | /** |
| 356 | * Returns IP address for tunneling for a given host. |
| 357 | * |
| 358 | * @param host host |
| 359 | * @return ip address |
| 360 | */ |
| 361 | private IpAddress getTunnelIp(Host host) { |
| 362 | return IpAddress.valueOf(host.annotations().value(LOCATION_IP)); |
| 363 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 364 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 365 | /** |
| 366 | * Returns port name. |
| 367 | * |
| 368 | * @param port port |
| 369 | * @return port name |
| 370 | */ |
| 371 | private String getPortName(Port port) { |
| 372 | return port.annotations().value("portName"); |
| 373 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 374 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 375 | /** |
| 376 | * Returns hosts associated with a given OpenStack network. |
| 377 | * |
| 378 | * @param vNet openstack network |
| 379 | * @return set of hosts |
| 380 | */ |
| 381 | private Set<Host> getHostsWithOpenstackNetwork(OpenstackNetwork vNet) { |
| 382 | checkNotNull(vNet); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 383 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 384 | Set<Host> hosts = openstackService.ports(vNet.id()).stream() |
| 385 | .filter(port -> port.deviceOwner().contains("compute")) |
| 386 | .map(port -> hostService.getHostsByMac(port.macAddress()) |
| 387 | .stream() |
| 388 | .findFirst() |
| 389 | .orElse(null)) |
| 390 | .collect(Collectors.toSet()); |
| 391 | |
| 392 | hosts.remove(null); |
| 393 | return hosts; |
| 394 | } |
| 395 | |
| 396 | /** |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 397 | * Registers static DHCP lease for a given host. |
| 398 | * |
| 399 | * @param host host |
| 400 | * @param service cord service |
| 401 | */ |
| 402 | private void registerDhcpLease(Host host, CordService service) { |
| 403 | List<Ip4Address> options = Lists.newArrayList(); |
| 404 | options.add(Ip4Address.makeMaskPrefix(service.serviceIpRange().prefixLength())); |
| 405 | options.add(service.serviceIp().getIp4Address()); |
| 406 | options.add(service.serviceIp().getIp4Address()); |
| 407 | options.add(DEFAULT_DNS); |
| 408 | |
| 409 | log.debug("Set static DHCP mapping for {}", host.mac()); |
| 410 | dhcpService.setStaticMapping(host.mac(), |
| 411 | host.ipAddresses().stream().findFirst().get().getIp4Address(), |
| 412 | true, |
| 413 | options); |
| 414 | } |
| 415 | |
| 416 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 417 | * Handles VM detected situation. |
| 418 | * |
| 419 | * @param host host |
| 420 | */ |
| 421 | private void serviceVmAdded(Host host) { |
| 422 | String vNetId = host.annotations().value(SERVICE_ID); |
| 423 | OpenstackNetwork vNet = openstackService.network(vNetId); |
| 424 | if (vNet == null) { |
| 425 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 426 | vNetId, |
| 427 | host.id(), |
| 428 | host.annotations().value(OPENSTACK_VM_ID)); |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | log.info("VM {} is detected, MAC: {} IP: {}", |
| 433 | host.annotations().value(OPENSTACK_VM_ID), |
| 434 | host.mac(), |
| 435 | host.ipAddresses().stream().findFirst().get()); |
| 436 | |
| 437 | CordService service = getCordService(vNet); |
Hyunsun Moon | 64ec614 | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 438 | if (service == null) { |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) { |
| 443 | ruleInstaller.populateManagementNetworkRules(host, service); |
| 444 | } else { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 445 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 446 | ruleInstaller.updateServiceGroup(service); |
| 447 | arpProxy.addServiceIp(service.serviceIp()); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 448 | |
| 449 | // sends gratuitous ARP here for the case of adding existing VMs |
| 450 | // when ONOS or cordvtn app is restarted |
| 451 | arpProxy.sendGratuitousArp(service.serviceIp(), gatewayMac, Sets.newHashSet(host)); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet); |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 455 | registerDhcpLease(host, service); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Handles VM removed situation. |
| 460 | * |
| 461 | * @param host host |
| 462 | */ |
| 463 | private void serviceVmRemoved(Host host) { |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 464 | if (host.annotations().value(OPENSTACK_VM_ID) == null) { |
| 465 | // this host was not injected from CordVtn, just return |
| 466 | return; |
| 467 | } |
| 468 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 469 | String vNetId = host.annotations().value(SERVICE_ID); |
| 470 | OpenstackNetwork vNet = openstackService.network(host.annotations().value(SERVICE_ID)); |
| 471 | if (vNet == null) { |
| 472 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 473 | vNetId, |
| 474 | host.id(), |
| 475 | host.annotations().value(OPENSTACK_VM_ID)); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | log.info("VM {} is vanished, MAC: {} IP: {}", |
| 480 | host.annotations().value(OPENSTACK_VM_ID), |
| 481 | host.mac(), |
| 482 | host.ipAddresses().stream().findFirst().get()); |
| 483 | |
| 484 | ruleInstaller.removeBasicConnectionRules(host); |
Hyunsun Moon | 1b2355e | 2016-02-03 14:44:06 -0800 | [diff] [blame] | 485 | dhcpService.removeStaticMapping(host.mac()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 486 | |
| 487 | CordService service = getCordService(vNet); |
Hyunsun Moon | 64ec614 | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 488 | if (service == null) { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) { |
| 493 | ruleInstaller.removeManagementNetworkRules(host, service); |
| 494 | } else { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 495 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 496 | ruleInstaller.updateServiceGroup(service); |
| 497 | |
| 498 | if (getHostsWithOpenstackNetwork(vNet).isEmpty()) { |
| 499 | arpProxy.removeServiceIp(service.serviceIp()); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 504 | /** |
| 505 | * Sets service network gateway MAC address and sends out gratuitous ARP to all |
| 506 | * VMs to update the gateway MAC address. |
| 507 | * |
| 508 | * @param mac mac address |
| 509 | */ |
| 510 | private void setServiceGatewayMac(MacAddress mac) { |
| 511 | if (mac != null && !mac.equals(gatewayMac)) { |
| 512 | gatewayMac = mac; |
| 513 | log.debug("Set service gateway MAC address to {}", gatewayMac.toString()); |
| 514 | } |
| 515 | |
| 516 | // TODO get existing service list from XOS and replace the loop below |
| 517 | Set<String> vNets = Sets.newHashSet(); |
| 518 | hostService.getHosts().forEach(host -> vNets.add(host.annotations().value(SERVICE_ID))); |
| 519 | vNets.remove(null); |
| 520 | |
| 521 | vNets.stream().forEach(vNet -> { |
| 522 | CordService service = getCordService(CordServiceId.of(vNet)); |
| 523 | if (service != null) { |
| 524 | arpProxy.sendGratuitousArp( |
| 525 | service.serviceIp(), |
| 526 | gatewayMac, |
| 527 | service.hosts().keySet()); |
| 528 | } |
| 529 | }); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Updates configurations. |
| 534 | */ |
| 535 | private void readConfiguration() { |
| 536 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 537 | if (config == null) { |
| 538 | log.debug("No configuration found"); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | setServiceGatewayMac(config.gatewayMac()); |
| 543 | } |
| 544 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 545 | private class InternalHostListener implements HostListener { |
| 546 | |
| 547 | @Override |
| 548 | public void event(HostEvent event) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 549 | Host host = event.subject(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 550 | |
| 551 | switch (event.type()) { |
| 552 | case HOST_ADDED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 553 | eventExecutor.submit(() -> serviceVmAdded(host)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 554 | break; |
| 555 | case HOST_REMOVED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 556 | eventExecutor.submit(() -> serviceVmRemoved(host)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 557 | break; |
| 558 | default: |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 564 | private class InternalPacketProcessor implements PacketProcessor { |
| 565 | |
| 566 | @Override |
| 567 | public void process(PacketContext context) { |
| 568 | if (context.isHandled()) { |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | Ethernet ethPacket = context.inPacket().parsed(); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 573 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 577 | arpProxy.processArpPacket(context, ethPacket, gatewayMac); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | private class InternalConfigListener implements NetworkConfigListener { |
| 582 | |
| 583 | @Override |
| 584 | public void event(NetworkConfigEvent event) { |
| 585 | if (!event.configClass().equals(CordVtnConfig.class)) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 589 | switch (event.type()) { |
| 590 | case CONFIG_ADDED: |
| 591 | case CONFIG_UPDATED: |
| 592 | log.info("Network configuration changed"); |
| 593 | eventExecutor.execute(CordVtn.this::readConfiguration); |
| 594 | break; |
| 595 | default: |
| 596 | break; |
| 597 | } |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 598 | } |
| 599 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 600 | } |