Hyunsun Moon | d0e932a | 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 | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 19 | import com.google.common.collect.Maps; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 33 | import org.onosproject.core.CoreService; |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 34 | import org.onosproject.dhcp.DhcpService; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipService; |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 37 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 38 | import org.onosproject.net.Host; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 39 | import org.onosproject.net.HostId; |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 40 | import org.onosproject.net.HostLocation; |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.Port; |
Hyunsun Moon | 746956f | 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 | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 49 | import org.onosproject.net.driver.DriverService; |
Hyunsun Moon | c71231d | 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 | b77b60f | 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 | d0e932a | 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 | b77b60f | 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 | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 59 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 42c7b4e | 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 | b77b60f | 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 | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 65 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 66 | import org.onosproject.openstackswitching.OpenstackPort; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 67 | import org.onosproject.openstackswitching.OpenstackSubnet; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 68 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 71 | import java.util.List; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 72 | import java.util.Map; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 73 | import java.util.Set; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 74 | import java.util.concurrent.ExecutorService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 75 | import java.util.stream.Collectors; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 76 | |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 77 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 78 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 79 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 80 | import static org.slf4j.LoggerFactory.getLogger; |
| 81 | |
| 82 | /** |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 83 | * Provisions virtual tenant networks with service chaining capability |
| 84 | * in OpenStack environment. |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 85 | */ |
| 86 | @Component(immediate = true) |
| 87 | @Service |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 88 | public class CordVtn extends AbstractProvider implements CordVtnService, HostProvider { |
Hyunsun Moon | d0e932a | 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 | 746956f | 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 | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 102 | protected HostProviderRegistry hostProviderRegistry; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | d0e932a | 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 | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 111 | protected DriverService driverService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 114 | protected FlowRuleService flowRuleService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 117 | protected PacketService packetService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | c71231d | 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 | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 126 | protected OpenstackSwitchingService openstackService; |
| 127 | |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected DhcpService dhcpService; |
| 130 | |
Hyunsun Moon | 746956f | 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 | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 139 | private static final String DEFAULT_TUNNEL = "vxlan"; |
| 140 | private static final String SERVICE_ID = "serviceId"; |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 141 | private static final String OPENSTACK_VM_ID = "openstackVmId"; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 142 | private static final String OPENSTACK_PORT_ID = "openstackPortId"; |
| 143 | private static final String DATA_PLANE_IP = "dataPlaneIp"; |
| 144 | private static final String DATA_PLANE_INTF = "dataPlaneIntf"; |
| 145 | private static final String S_TAG = "stag"; |
| 146 | |
| 147 | private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8"); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 148 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 149 | private final ExecutorService eventExecutor = |
| 150 | newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler")); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 151 | |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 152 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 153 | private final HostListener hostListener = new InternalHostListener(); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 154 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 155 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 156 | private ApplicationId appId; |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 157 | private HostProviderService hostProvider; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 158 | private CordVtnRuleInstaller ruleInstaller; |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 159 | private CordVtnArpProxy arpProxy; |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 160 | private volatile MacAddress privateGatewayMac = MacAddress.NONE; |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 161 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Creates an cordvtn host location provider. |
| 164 | */ |
| 165 | public CordVtn() { |
| 166 | super(new ProviderId("host", CORDVTN_APP_ID)); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 167 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 168 | |
| 169 | @Activate |
| 170 | protected void activate() { |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 171 | appId = coreService.registerApplication("org.onosproject.cordvtn"); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 172 | ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService, |
| 173 | deviceService, |
| 174 | driverService, |
| 175 | groupService, |
| 176 | mastershipService, |
| 177 | DEFAULT_TUNNEL); |
| 178 | |
Hyunsun Moon | 9cf43db | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 179 | arpProxy = new CordVtnArpProxy(appId, packetService, hostService); |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 180 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 181 | arpProxy.requestPacket(); |
| 182 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 183 | hostService.addListener(hostListener); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 184 | hostProvider = hostProviderRegistry.register(this); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 185 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 186 | configRegistry.registerConfigFactory(configFactory); |
| 187 | configService.addListener(configListener); |
| 188 | readConfiguration(); |
| 189 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 190 | log.info("Started"); |
| 191 | } |
| 192 | |
| 193 | @Deactivate |
| 194 | protected void deactivate() { |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 195 | hostProviderRegistry.unregister(this); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 196 | hostService.removeListener(hostListener); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 197 | |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 198 | packetService.removeProcessor(packetProcessor); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 199 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 200 | configRegistry.unregisterConfigFactory(configFactory); |
| 201 | configService.removeListener(configListener); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 202 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 203 | eventExecutor.shutdown(); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 204 | log.info("Stopped"); |
| 205 | } |
| 206 | |
| 207 | @Override |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 208 | public void triggerProbe(Host host) { |
| 209 | /* |
| 210 | * Note: In CORD deployment, we assume that all hosts are configured. |
| 211 | * Therefore no probe is required. |
| 212 | */ |
Hyunsun Moon | b219fc4 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Hyunsun Moon | 640f183e | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 216 | public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId, |
| 217 | boolean isBidirectional) { |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 218 | CordService tService = getCordService(tServiceId); |
| 219 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | bfc47d1 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 220 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 221 | if (tService == null || pService == null) { |
| 222 | log.error("Failed to create CordService for {}", tServiceId.id()); |
| 223 | return; |
| 224 | } |
| 225 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 226 | log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id()); |
Hyunsun Moon | 640f183e | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 227 | ruleInstaller.populateServiceDependencyRules(tService, pService, isBidirectional); |
Hyunsun Moon | 699f46b | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 231 | public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) { |
| 232 | CordService tService = getCordService(tServiceId); |
| 233 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | bfc47d1 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 234 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 235 | if (tService == null || pService == null) { |
| 236 | log.error("Failed to create CordService for {}", tServiceId.id()); |
| 237 | return; |
| 238 | } |
| 239 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 240 | log.info("Service dependency from {} to {} removed.", tService.id().id(), pService.id().id()); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 241 | ruleInstaller.removeServiceDependencyRules(tService, pService); |
Hyunsun Moon | 699f46b | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 244 | @Override |
| 245 | public void addServiceVm(CordVtnNode node, ConnectPoint connectPoint) { |
| 246 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 247 | OpenstackPort vPort = openstackService.port(port); |
| 248 | if (vPort == null) { |
| 249 | log.warn("Failed to get OpenstackPort for {}", getPortName(port)); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 253 | MacAddress mac = vPort.macAddress(); |
| 254 | HostId hostId = HostId.hostId(mac); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 255 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 256 | Host host = hostService.getHost(hostId); |
| 257 | if (host != null) { |
| 258 | // Host is already known to the system, no HOST_ADDED event is triggered in this case. |
| 259 | // It happens when the application is restarted. |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 260 | String vmId = host.annotations().value(OPENSTACK_VM_ID); |
| 261 | if (vmId != null && vmId.equals(vPort.deviceId())) { |
| 262 | serviceVmAdded(host); |
| 263 | return; |
| 264 | } else { |
| 265 | hostProvider.hostVanished(host.id()); |
| 266 | } |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 269 | Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values()); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 270 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 271 | .set(SERVICE_ID, vPort.networkId()) |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 272 | .set(OPENSTACK_VM_ID, vPort.deviceId()) |
| 273 | .set(OPENSTACK_PORT_ID, vPort.id()) |
| 274 | .set(DATA_PLANE_IP, node.dpIp().ip().toString()) |
| 275 | .set(DATA_PLANE_INTF, node.dpIntf()); |
| 276 | |
| 277 | String serviceVlan = getServiceVlan(vPort); |
| 278 | if (serviceVlan != null) { |
| 279 | annotations.set(S_TAG, serviceVlan); |
| 280 | } |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 281 | |
| 282 | HostDescription hostDesc = new DefaultHostDescription( |
| 283 | mac, |
| 284 | VlanId.NONE, |
| 285 | new HostLocation(connectPoint, System.currentTimeMillis()), |
| 286 | ip, |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 287 | annotations.build()); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 288 | |
| 289 | hostProvider.hostDetected(hostId, hostDesc, false); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 292 | @Override |
| 293 | public void removeServiceVm(ConnectPoint connectPoint) { |
| 294 | Host host = hostService.getConnectedHosts(connectPoint) |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 295 | .stream() |
| 296 | .findFirst() |
| 297 | .orElse(null); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 298 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 299 | if (host == null) { |
| 300 | log.debug("No host is connected on {}", connectPoint.toString()); |
| 301 | return; |
| 302 | } |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 303 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 304 | hostProvider.hostVanished(host.id()); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 307 | @Override |
| 308 | public void updateVirtualSubscriberGateways(HostId vSgHostId, String serviceVlan, |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 309 | Map<IpAddress, MacAddress> vSgs) { |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 310 | Host vSgVm = hostService.getHost(vSgHostId); |
| 311 | |
| 312 | if (vSgVm == null || !vSgVm.annotations().value(S_TAG).equals(serviceVlan)) { |
| 313 | log.debug("Invalid vSG updates for {}", serviceVlan); |
| 314 | return; |
| 315 | } |
| 316 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 317 | log.info("Updates vSGs in {} with {}", vSgVm.id(), vSgs.toString()); |
| 318 | vSgs.entrySet().stream() |
| 319 | .forEach(entry -> addVirtualSubscriberGateway( |
| 320 | vSgVm, |
| 321 | entry.getKey(), |
| 322 | entry.getValue(), |
| 323 | serviceVlan)); |
| 324 | |
| 325 | ruleInstaller.populateSubscriberGatewayRules(vSgVm, vSgs.keySet()); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Adds virtual subscriber gateway to the system. |
| 330 | * |
| 331 | * @param vSgHost host virtual machine of this vSG |
| 332 | * @param vSgIp vSG ip address |
| 333 | * @param vSgMac vSG mac address |
| 334 | * @param serviceVlan service vlan |
| 335 | */ |
| 336 | public void addVirtualSubscriberGateway(Host vSgHost, IpAddress vSgIp, MacAddress vSgMac, String serviceVlan) { |
| 337 | HostId hostId = HostId.hostId(vSgMac); |
| 338 | Host host = hostService.getHost(hostId); |
| 339 | if (host != null) { |
| 340 | log.debug("vSG with {} already exists", vSgMac.toString()); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | log.info("vSG with IP({}) MAC({}) detected", vSgIp.toString(), vSgMac.toString()); |
| 345 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 346 | .set(S_TAG, serviceVlan); |
| 347 | |
| 348 | HostDescription hostDesc = new DefaultHostDescription( |
| 349 | vSgMac, |
| 350 | VlanId.NONE, |
| 351 | vSgHost.location(), |
| 352 | Sets.newHashSet(vSgIp), |
| 353 | annotations.build()); |
| 354 | |
| 355 | hostProvider.hostDetected(hostId, hostDesc, false); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 358 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 359 | * Returns CordService by service ID. |
Hyunsun Moon | bfc47d1 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 360 | * |
| 361 | * @param serviceId service id |
| 362 | * @return cord service, or null if it fails to get network from OpenStack |
| 363 | */ |
| 364 | private CordService getCordService(CordServiceId serviceId) { |
| 365 | OpenstackNetwork vNet = openstackService.network(serviceId.id()); |
| 366 | if (vNet == null) { |
| 367 | log.warn("Couldn't find OpenStack network for service {}", serviceId.id()); |
| 368 | return null; |
| 369 | } |
| 370 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 371 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 372 | .findFirst() |
| 373 | .orElse(null); |
| 374 | if (subnet == null) { |
| 375 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id()); |
| 376 | return null; |
| 377 | } |
| 378 | |
| 379 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 380 | // TODO get tenant services from XOS |
| 381 | |
| 382 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 383 | .stream() |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 384 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 385 | |
| 386 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | bfc47d1 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 390 | * Returns CordService by OpenStack network. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 391 | * |
| 392 | * @param vNet OpenStack network |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 393 | * @return cord service |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 394 | */ |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 395 | private CordService getCordService(OpenstackNetwork vNet) { |
| 396 | checkNotNull(vNet); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 397 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 398 | CordServiceId serviceId = CordServiceId.of(vNet.id()); |
| 399 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 400 | .findFirst() |
| 401 | .orElse(null); |
| 402 | if (subnet == null) { |
| 403 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId); |
| 404 | return null; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 405 | } |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 406 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 407 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 408 | // TODO get tenant services from XOS |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 409 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 410 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 411 | .stream() |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 412 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 413 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 414 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 417 | /** |
| 418 | * Returns IP address for tunneling for a given host. |
| 419 | * |
| 420 | * @param host host |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 421 | * @return ip address, or null |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 422 | */ |
| 423 | private IpAddress getTunnelIp(Host host) { |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 424 | String ip = host.annotations().value(DATA_PLANE_IP); |
| 425 | return ip == null ? null : IpAddress.valueOf(ip); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 426 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 427 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 428 | /** |
| 429 | * Returns port name. |
| 430 | * |
| 431 | * @param port port |
| 432 | * @return port name |
| 433 | */ |
| 434 | private String getPortName(Port port) { |
| 435 | return port.annotations().value("portName"); |
| 436 | } |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 437 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 438 | /** |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 439 | * Returns s-tag from a given OpenStack port. |
| 440 | * |
| 441 | * @param vPort openstack port |
| 442 | * @return s-tag string |
| 443 | */ |
| 444 | private String getServiceVlan(OpenstackPort vPort) { |
| 445 | checkNotNull(vPort); |
| 446 | |
| 447 | if (vPort.name() != null && vPort.name().startsWith(S_TAG)) { |
| 448 | return vPort.name().split("-")[1]; |
| 449 | } else { |
| 450 | return null; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 455 | * Returns hosts associated with a given OpenStack network. |
| 456 | * |
| 457 | * @param vNet openstack network |
| 458 | * @return set of hosts |
| 459 | */ |
| 460 | private Set<Host> getHostsWithOpenstackNetwork(OpenstackNetwork vNet) { |
| 461 | checkNotNull(vNet); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 462 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 463 | Set<Host> hosts = openstackService.ports(vNet.id()).stream() |
| 464 | .filter(port -> port.deviceOwner().contains("compute")) |
| 465 | .map(port -> hostService.getHostsByMac(port.macAddress()) |
| 466 | .stream() |
| 467 | .findFirst() |
| 468 | .orElse(null)) |
| 469 | .collect(Collectors.toSet()); |
| 470 | |
| 471 | hosts.remove(null); |
| 472 | return hosts; |
| 473 | } |
| 474 | |
| 475 | /** |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 476 | * Returns public ip addresses of vSGs running inside a give vSG host. |
| 477 | * |
| 478 | * @param vSgHost vSG host |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 479 | * @return map of ip and mac address, or empty map |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 480 | */ |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 481 | private Map<IpAddress, MacAddress> getSubscriberGateways(Host vSgHost) { |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 482 | String vPortId = vSgHost.annotations().value(OPENSTACK_PORT_ID); |
| 483 | String serviceVlan = vSgHost.annotations().value(S_TAG); |
| 484 | |
| 485 | OpenstackPort vPort = openstackService.port(vPortId); |
| 486 | if (vPort == null) { |
| 487 | log.warn("Failed to get OpenStack port {} for VM {}", vPortId, vSgHost.id()); |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 488 | return Maps.newHashMap(); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (!serviceVlan.equals(getServiceVlan(vPort))) { |
| 492 | log.error("Host({}) s-tag does not match with vPort s-tag", vSgHost.id()); |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 493 | return Maps.newHashMap(); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 496 | return vPort.allowedAddressPairs(); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 500 | * Registers static DHCP lease for a given host. |
| 501 | * |
| 502 | * @param host host |
| 503 | * @param service cord service |
| 504 | */ |
| 505 | private void registerDhcpLease(Host host, CordService service) { |
| 506 | List<Ip4Address> options = Lists.newArrayList(); |
| 507 | options.add(Ip4Address.makeMaskPrefix(service.serviceIpRange().prefixLength())); |
| 508 | options.add(service.serviceIp().getIp4Address()); |
| 509 | options.add(service.serviceIp().getIp4Address()); |
| 510 | options.add(DEFAULT_DNS); |
| 511 | |
| 512 | log.debug("Set static DHCP mapping for {}", host.mac()); |
| 513 | dhcpService.setStaticMapping(host.mac(), |
| 514 | host.ipAddresses().stream().findFirst().get().getIp4Address(), |
| 515 | true, |
| 516 | options); |
| 517 | } |
| 518 | |
| 519 | /** |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 520 | * Handles VM detected situation. |
| 521 | * |
| 522 | * @param host host |
| 523 | */ |
| 524 | private void serviceVmAdded(Host host) { |
| 525 | String vNetId = host.annotations().value(SERVICE_ID); |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 526 | if (vNetId == null) { |
| 527 | // ignore this host, it not a VM we injected or a vSG |
| 528 | return; |
| 529 | } |
| 530 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 531 | OpenstackNetwork vNet = openstackService.network(vNetId); |
| 532 | if (vNet == null) { |
| 533 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 534 | vNetId, |
| 535 | host.id(), |
| 536 | host.annotations().value(OPENSTACK_VM_ID)); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | log.info("VM {} is detected, MAC: {} IP: {}", |
| 541 | host.annotations().value(OPENSTACK_VM_ID), |
| 542 | host.mac(), |
| 543 | host.ipAddresses().stream().findFirst().get()); |
| 544 | |
| 545 | CordService service = getCordService(vNet); |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 546 | if (service == null) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) { |
| 551 | ruleInstaller.populateManagementNetworkRules(host, service); |
| 552 | } else { |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 553 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 554 | ruleInstaller.updateServiceGroup(service); |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 555 | arpProxy.addGateway(service.serviceIp(), privateGatewayMac); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 556 | |
| 557 | // sends gratuitous ARP here for the case of adding existing VMs |
| 558 | // when ONOS or cordvtn app is restarted |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 559 | arpProxy.sendGratuitousArpForGateway(service.serviceIp(), Sets.newHashSet(host)); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 562 | registerDhcpLease(host, service); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 563 | ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet); |
| 564 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 565 | String serviceVlan = host.annotations().value(S_TAG); |
| 566 | if (serviceVlan != null) { |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 567 | log.debug("vSG VM detected {}", host.id()); |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 568 | Map<IpAddress, MacAddress> vSgs = getSubscriberGateways(host); |
| 569 | vSgs.entrySet().stream() |
| 570 | .forEach(entry -> addVirtualSubscriberGateway( |
| 571 | host, |
| 572 | entry.getKey(), |
| 573 | entry.getValue(), |
| 574 | serviceVlan)); |
| 575 | |
| 576 | ruleInstaller.populateSubscriberGatewayRules(host, vSgs.keySet()); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 577 | } |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Handles VM removed situation. |
| 582 | * |
| 583 | * @param host host |
| 584 | */ |
| 585 | private void serviceVmRemoved(Host host) { |
Hyunsun Moon | 1d3eac9 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 586 | if (host.annotations().value(OPENSTACK_VM_ID) == null) { |
| 587 | // this host was not injected from CordVtn, just return |
| 588 | return; |
| 589 | } |
| 590 | |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 591 | String vNetId = host.annotations().value(SERVICE_ID); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 592 | OpenstackNetwork vNet = openstackService.network(vNetId); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 593 | if (vNet == null) { |
| 594 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 595 | vNetId, |
| 596 | host.id(), |
| 597 | host.annotations().value(OPENSTACK_VM_ID)); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | log.info("VM {} is vanished, MAC: {} IP: {}", |
| 602 | host.annotations().value(OPENSTACK_VM_ID), |
| 603 | host.mac(), |
| 604 | host.ipAddresses().stream().findFirst().get()); |
| 605 | |
| 606 | ruleInstaller.removeBasicConnectionRules(host); |
Hyunsun Moon | fb631b4 | 2016-02-03 14:44:06 -0800 | [diff] [blame] | 607 | dhcpService.removeStaticMapping(host.mac()); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 608 | |
| 609 | CordService service = getCordService(vNet); |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 610 | if (service == null) { |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) { |
| 615 | ruleInstaller.removeManagementNetworkRules(host, service); |
| 616 | } else { |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 617 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 618 | ruleInstaller.updateServiceGroup(service); |
| 619 | |
| 620 | if (getHostsWithOpenstackNetwork(vNet).isEmpty()) { |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 621 | arpProxy.removeGateway(service.serviceIp()); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 626 | /** |
| 627 | * Sets service network gateway MAC address and sends out gratuitous ARP to all |
| 628 | * VMs to update the gateway MAC address. |
| 629 | * |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 630 | * @param newMac mac address to update |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 631 | */ |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 632 | private void setPrivateGatewayMac(MacAddress newMac) { |
| 633 | if (newMac == null || newMac.equals(privateGatewayMac)) { |
| 634 | // no updates, do nothing |
| 635 | return; |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 638 | privateGatewayMac = newMac; |
| 639 | log.debug("Set service gateway MAC address to {}", privateGatewayMac.toString()); |
| 640 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 641 | // TODO get existing service list from XOS and replace the loop below |
| 642 | Set<String> vNets = Sets.newHashSet(); |
| 643 | hostService.getHosts().forEach(host -> vNets.add(host.annotations().value(SERVICE_ID))); |
| 644 | vNets.remove(null); |
| 645 | |
| 646 | vNets.stream().forEach(vNet -> { |
| 647 | CordService service = getCordService(CordServiceId.of(vNet)); |
| 648 | if (service != null) { |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 649 | arpProxy.addGateway(service.serviceIp(), privateGatewayMac); |
| 650 | arpProxy.sendGratuitousArpForGateway(service.serviceIp(), service.hosts().keySet()); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 651 | } |
| 652 | }); |
| 653 | } |
| 654 | |
| 655 | /** |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 656 | * Sets public gateway MAC address. |
| 657 | * |
| 658 | * @param publicGateways gateway ip and mac address pairs |
| 659 | */ |
| 660 | private void setPublicGatewayMac(Map<IpAddress, MacAddress> publicGateways) { |
| 661 | publicGateways.entrySet() |
| 662 | .stream() |
| 663 | .forEach(entry -> { |
| 664 | arpProxy.addGateway(entry.getKey(), entry.getValue()); |
| 665 | log.debug("Added public gateway IP {}, MAC {}", |
| 666 | entry.getKey().toString(), entry.getValue().toString()); |
| 667 | }); |
| 668 | // TODO notice gateway MAC change to VMs holds this gateway IP |
| 669 | } |
| 670 | |
| 671 | /** |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 672 | * Updates configurations. |
| 673 | */ |
| 674 | private void readConfiguration() { |
| 675 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 676 | if (config == null) { |
| 677 | log.debug("No configuration found"); |
| 678 | return; |
| 679 | } |
| 680 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 681 | setPrivateGatewayMac(config.privateGatewayMac()); |
| 682 | setPublicGatewayMac(config.publicGateways()); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 685 | private class InternalHostListener implements HostListener { |
| 686 | |
| 687 | @Override |
| 688 | public void event(HostEvent event) { |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 689 | Host host = event.subject(); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 690 | |
| 691 | switch (event.type()) { |
| 692 | case HOST_ADDED: |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 693 | eventExecutor.submit(() -> serviceVmAdded(host)); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 694 | break; |
| 695 | case HOST_REMOVED: |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 696 | eventExecutor.submit(() -> serviceVmRemoved(host)); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 697 | break; |
| 698 | default: |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 704 | private class InternalPacketProcessor implements PacketProcessor { |
| 705 | |
| 706 | @Override |
| 707 | public void process(PacketContext context) { |
| 708 | if (context.isHandled()) { |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | Ethernet ethPacket = context.inPacket().parsed(); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 713 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) { |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | |
Hyunsun Moon | ae39ae8 | 2016-02-17 15:02:06 -0800 | [diff] [blame^] | 717 | arpProxy.processArpPacket(context, ethPacket); |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | private class InternalConfigListener implements NetworkConfigListener { |
| 722 | |
| 723 | @Override |
| 724 | public void event(NetworkConfigEvent event) { |
| 725 | if (!event.configClass().equals(CordVtnConfig.class)) { |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 726 | return; |
| 727 | } |
| 728 | |
Hyunsun Moon | 746956f | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 729 | switch (event.type()) { |
| 730 | case CONFIG_ADDED: |
| 731 | case CONFIG_UPDATED: |
| 732 | log.info("Network configuration changed"); |
| 733 | eventExecutor.execute(CordVtn.this::readConfiguration); |
| 734 | break; |
| 735 | default: |
| 736 | break; |
| 737 | } |
Hyunsun Moon | 42c7b4e | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 738 | } |
| 739 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 740 | } |