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