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 | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | e9d7599 | 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 | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 33 | import org.onosproject.core.CoreService; |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 34 | import org.onosproject.dhcp.DhcpService; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 37 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 38 | import org.onosproject.net.Host; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 39 | import org.onosproject.net.HostId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 40 | import org.onosproject.net.HostLocation; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.Port; |
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"; |
| 140 | private static final String SERVICE_ID = "serviceId"; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 141 | private static final String OPENSTACK_VM_ID = "openstackVmId"; |
Hyunsun Moon | 01556a5 | 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 | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 148 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 149 | private final ExecutorService eventExecutor = |
| 150 | newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler")); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 151 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 152 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 153 | private final HostListener hostListener = new InternalHostListener(); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 154 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 155 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 156 | private ApplicationId appId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 157 | private HostProviderService hostProvider; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 158 | private CordVtnRuleInstaller ruleInstaller; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 159 | private CordVtnArpProxy arpProxy; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 160 | private volatile MacAddress privateGatewayMac = MacAddress.NONE; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 161 | |
Hyunsun Moon | cb79944 | 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 | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 167 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 168 | |
| 169 | @Activate |
| 170 | protected void activate() { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 171 | appId = coreService.registerApplication("org.onosproject.cordvtn"); |
Hyunsun Moon | ba29007 | 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 | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 179 | arpProxy = new CordVtnArpProxy(appId, packetService, hostService); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 180 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 181 | arpProxy.requestPacket(); |
| 182 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 183 | hostService.addListener(hostListener); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 184 | hostProvider = hostProviderRegistry.register(this); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 185 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 186 | configRegistry.registerConfigFactory(configFactory); |
| 187 | configService.addListener(configListener); |
| 188 | readConfiguration(); |
| 189 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 190 | log.info("Started"); |
| 191 | } |
| 192 | |
| 193 | @Deactivate |
| 194 | protected void deactivate() { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 195 | hostProviderRegistry.unregister(this); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 196 | hostService.removeListener(hostListener); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 197 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 198 | packetService.removeProcessor(packetProcessor); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 199 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 200 | configRegistry.unregisterConfigFactory(configFactory); |
| 201 | configService.removeListener(configListener); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 202 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 203 | eventExecutor.shutdown(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 204 | log.info("Stopped"); |
| 205 | } |
| 206 | |
| 207 | @Override |
Hyunsun Moon | cb79944 | 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 | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Hyunsun Moon | 5f7ed8a | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 216 | public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId, |
| 217 | boolean isBidirectional) { |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 218 | CordService tService = getCordService(tServiceId); |
| 219 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 220 | |
Hyunsun Moon | ba29007 | 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 | cb79944 | 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 | 5f7ed8a | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 227 | ruleInstaller.populateServiceDependencyRules(tService, pService, isBidirectional); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
Hyunsun Moon | ba29007 | 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 | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 234 | |
Hyunsun Moon | ba29007 | 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 | cb79944 | 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 | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 241 | ruleInstaller.removeServiceDependencyRules(tService, pService); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Hyunsun Moon | cb79944 | 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 | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 253 | MacAddress mac = vPort.macAddress(); |
| 254 | HostId hostId = HostId.hostId(mac); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 255 | |
Hyunsun Moon | cb79944 | 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 | 8b530e3 | 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 | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 269 | Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values()); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 270 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 271 | .set(SERVICE_ID, vPort.networkId()) |
Hyunsun Moon | 01556a5 | 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 | cb79944 | 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 | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 287 | annotations.build()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 288 | |
| 289 | hostProvider.hostDetected(hostId, hostDesc, false); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 292 | @Override |
| 293 | public void removeServiceVm(ConnectPoint connectPoint) { |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 294 | hostService.getConnectedHosts(connectPoint) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 295 | .stream() |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 296 | .forEach(host -> hostProvider.hostVanished(host.id())); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 299 | @Override |
| 300 | public void updateVirtualSubscriberGateways(HostId vSgHostId, String serviceVlan, |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 301 | Map<IpAddress, MacAddress> vSgs) { |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 302 | Host vSgVm = hostService.getHost(vSgHostId); |
| 303 | |
| 304 | if (vSgVm == null || !vSgVm.annotations().value(S_TAG).equals(serviceVlan)) { |
| 305 | log.debug("Invalid vSG updates for {}", serviceVlan); |
| 306 | return; |
| 307 | } |
| 308 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 309 | log.info("Updates vSGs in {} with {}", vSgVm.id(), vSgs.toString()); |
| 310 | vSgs.entrySet().stream() |
| 311 | .forEach(entry -> addVirtualSubscriberGateway( |
| 312 | vSgVm, |
| 313 | entry.getKey(), |
| 314 | entry.getValue(), |
| 315 | serviceVlan)); |
| 316 | |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 317 | hostService.getConnectedHosts(vSgVm.location()).stream() |
| 318 | .filter(host -> !host.mac().equals(vSgVm.mac())) |
| 319 | .filter(host -> !vSgs.values().contains(host.mac())) |
| 320 | .forEach(host -> { |
| 321 | log.info("Removed vSG {}", host.toString()); |
| 322 | hostProvider.hostVanished(host.id()); |
| 323 | }); |
| 324 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 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 | */ |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 336 | private void addVirtualSubscriberGateway(Host vSgHost, IpAddress vSgIp, MacAddress vSgMac, |
| 337 | String serviceVlan) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 338 | HostId hostId = HostId.hostId(vSgMac); |
| 339 | Host host = hostService.getHost(hostId); |
| 340 | if (host != null) { |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 341 | log.trace("vSG with {} already exists", vSgMac.toString()); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | log.info("vSG with IP({}) MAC({}) detected", vSgIp.toString(), vSgMac.toString()); |
| 346 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 347 | .set(S_TAG, serviceVlan); |
| 348 | |
| 349 | HostDescription hostDesc = new DefaultHostDescription( |
| 350 | vSgMac, |
| 351 | VlanId.NONE, |
| 352 | vSgHost.location(), |
| 353 | Sets.newHashSet(vSgIp), |
| 354 | annotations.build()); |
| 355 | |
| 356 | hostProvider.hostDetected(hostId, hostDesc, false); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 359 | /** |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 360 | * Returns CordService by service ID. |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 361 | * |
| 362 | * @param serviceId service id |
| 363 | * @return cord service, or null if it fails to get network from OpenStack |
| 364 | */ |
| 365 | private CordService getCordService(CordServiceId serviceId) { |
| 366 | OpenstackNetwork vNet = openstackService.network(serviceId.id()); |
| 367 | if (vNet == null) { |
| 368 | log.warn("Couldn't find OpenStack network for service {}", serviceId.id()); |
| 369 | return null; |
| 370 | } |
| 371 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 372 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 373 | .findFirst() |
| 374 | .orElse(null); |
| 375 | if (subnet == null) { |
| 376 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id()); |
| 377 | return null; |
| 378 | } |
| 379 | |
| 380 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 381 | // TODO get tenant services from XOS |
| 382 | |
| 383 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 384 | .stream() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 385 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 386 | |
| 387 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 391 | * Returns CordService by OpenStack network. |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 392 | * |
| 393 | * @param vNet OpenStack network |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 394 | * @return cord service |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 395 | */ |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 396 | private CordService getCordService(OpenstackNetwork vNet) { |
| 397 | checkNotNull(vNet); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 398 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 399 | CordServiceId serviceId = CordServiceId.of(vNet.id()); |
| 400 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 401 | .findFirst() |
| 402 | .orElse(null); |
| 403 | if (subnet == null) { |
| 404 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId); |
| 405 | return null; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 406 | } |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 407 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 408 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 409 | // TODO get tenant services from XOS |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 410 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 411 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 412 | .stream() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 413 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 414 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 415 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 418 | /** |
| 419 | * Returns IP address for tunneling for a given host. |
| 420 | * |
| 421 | * @param host host |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 422 | * @return ip address, or null |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 423 | */ |
| 424 | private IpAddress getTunnelIp(Host host) { |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 425 | String ip = host.annotations().value(DATA_PLANE_IP); |
| 426 | return ip == null ? null : IpAddress.valueOf(ip); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 427 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 428 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 429 | /** |
| 430 | * Returns port name. |
| 431 | * |
| 432 | * @param port port |
| 433 | * @return port name |
| 434 | */ |
| 435 | private String getPortName(Port port) { |
| 436 | return port.annotations().value("portName"); |
| 437 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 438 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 439 | /** |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 440 | * Returns s-tag from a given OpenStack port. |
| 441 | * |
| 442 | * @param vPort openstack port |
| 443 | * @return s-tag string |
| 444 | */ |
| 445 | private String getServiceVlan(OpenstackPort vPort) { |
| 446 | checkNotNull(vPort); |
| 447 | |
| 448 | if (vPort.name() != null && vPort.name().startsWith(S_TAG)) { |
| 449 | return vPort.name().split("-")[1]; |
| 450 | } else { |
| 451 | return null; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 456 | * Returns hosts associated with a given OpenStack network. |
| 457 | * |
| 458 | * @param vNet openstack network |
| 459 | * @return set of hosts |
| 460 | */ |
| 461 | private Set<Host> getHostsWithOpenstackNetwork(OpenstackNetwork vNet) { |
| 462 | checkNotNull(vNet); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 463 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 464 | Set<Host> hosts = openstackService.ports(vNet.id()).stream() |
| 465 | .filter(port -> port.deviceOwner().contains("compute")) |
| 466 | .map(port -> hostService.getHostsByMac(port.macAddress()) |
| 467 | .stream() |
| 468 | .findFirst() |
| 469 | .orElse(null)) |
| 470 | .collect(Collectors.toSet()); |
| 471 | |
| 472 | hosts.remove(null); |
| 473 | return hosts; |
| 474 | } |
| 475 | |
| 476 | /** |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 477 | * Returns public ip addresses of vSGs running inside a give vSG host. |
| 478 | * |
| 479 | * @param vSgHost vSG host |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 480 | * @return map of ip and mac address, or empty map |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 481 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 482 | private Map<IpAddress, MacAddress> getSubscriberGateways(Host vSgHost) { |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 483 | String vPortId = vSgHost.annotations().value(OPENSTACK_PORT_ID); |
| 484 | String serviceVlan = vSgHost.annotations().value(S_TAG); |
| 485 | |
| 486 | OpenstackPort vPort = openstackService.port(vPortId); |
| 487 | if (vPort == null) { |
| 488 | log.warn("Failed to get OpenStack port {} for VM {}", vPortId, vSgHost.id()); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 489 | return Maps.newHashMap(); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | if (!serviceVlan.equals(getServiceVlan(vPort))) { |
| 493 | log.error("Host({}) s-tag does not match with vPort s-tag", vSgHost.id()); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 494 | return Maps.newHashMap(); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 497 | return vPort.allowedAddressPairs(); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 501 | * Registers static DHCP lease for a given host. |
| 502 | * |
| 503 | * @param host host |
| 504 | * @param service cord service |
| 505 | */ |
| 506 | private void registerDhcpLease(Host host, CordService service) { |
| 507 | List<Ip4Address> options = Lists.newArrayList(); |
| 508 | options.add(Ip4Address.makeMaskPrefix(service.serviceIpRange().prefixLength())); |
| 509 | options.add(service.serviceIp().getIp4Address()); |
| 510 | options.add(service.serviceIp().getIp4Address()); |
| 511 | options.add(DEFAULT_DNS); |
| 512 | |
| 513 | log.debug("Set static DHCP mapping for {}", host.mac()); |
| 514 | dhcpService.setStaticMapping(host.mac(), |
| 515 | host.ipAddresses().stream().findFirst().get().getIp4Address(), |
| 516 | true, |
| 517 | options); |
| 518 | } |
| 519 | |
| 520 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 521 | * Handles VM detected situation. |
| 522 | * |
| 523 | * @param host host |
| 524 | */ |
| 525 | private void serviceVmAdded(Host host) { |
| 526 | String vNetId = host.annotations().value(SERVICE_ID); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 527 | if (vNetId == null) { |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 528 | // ignore this host, it is not the service VM, or it's a vSG |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 532 | OpenstackNetwork vNet = openstackService.network(vNetId); |
| 533 | if (vNet == null) { |
| 534 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 535 | vNetId, |
| 536 | host.id(), |
| 537 | host.annotations().value(OPENSTACK_VM_ID)); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | log.info("VM {} is detected, MAC: {} IP: {}", |
| 542 | host.annotations().value(OPENSTACK_VM_ID), |
| 543 | host.mac(), |
| 544 | host.ipAddresses().stream().findFirst().get()); |
| 545 | |
| 546 | CordService service = getCordService(vNet); |
Hyunsun Moon | 64ec614 | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 547 | if (service == null) { |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) { |
| 552 | ruleInstaller.populateManagementNetworkRules(host, service); |
| 553 | } else { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 554 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 555 | ruleInstaller.updateServiceGroup(service); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 556 | arpProxy.addGateway(service.serviceIp(), privateGatewayMac); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 557 | |
| 558 | // sends gratuitous ARP here for the case of adding existing VMs |
| 559 | // when ONOS or cordvtn app is restarted |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 560 | arpProxy.sendGratuitousArpForGateway(service.serviceIp(), Sets.newHashSet(host)); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 563 | registerDhcpLease(host, service); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 564 | ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet); |
| 565 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 566 | String serviceVlan = host.annotations().value(S_TAG); |
| 567 | if (serviceVlan != null) { |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 568 | log.debug("vSG VM detected {}", host.id()); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 569 | Map<IpAddress, MacAddress> vSgs = getSubscriberGateways(host); |
| 570 | vSgs.entrySet().stream() |
| 571 | .forEach(entry -> addVirtualSubscriberGateway( |
| 572 | host, |
| 573 | entry.getKey(), |
| 574 | entry.getValue(), |
| 575 | serviceVlan)); |
| 576 | |
| 577 | ruleInstaller.populateSubscriberGatewayRules(host, vSgs.keySet()); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 578 | } |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Handles VM removed situation. |
| 583 | * |
| 584 | * @param host host |
| 585 | */ |
| 586 | private void serviceVmRemoved(Host host) { |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 587 | String vNetId = host.annotations().value(SERVICE_ID); |
| 588 | if (vNetId == null) { |
| 589 | // ignore it, it's not the service VM or it's a vSG |
| 590 | String serviceVlan = host.annotations().value(S_TAG); |
| 591 | if (serviceVlan != null) { |
| 592 | log.info("vSG {} removed", host.id()); |
| 593 | } |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 597 | OpenstackNetwork vNet = openstackService.network(vNetId); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 598 | if (vNet == null) { |
| 599 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 600 | vNetId, |
| 601 | host.id(), |
| 602 | host.annotations().value(OPENSTACK_VM_ID)); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | log.info("VM {} is vanished, MAC: {} IP: {}", |
| 607 | host.annotations().value(OPENSTACK_VM_ID), |
| 608 | host.mac(), |
| 609 | host.ipAddresses().stream().findFirst().get()); |
| 610 | |
| 611 | ruleInstaller.removeBasicConnectionRules(host); |
Hyunsun Moon | 1b2355e | 2016-02-03 14:44:06 -0800 | [diff] [blame] | 612 | dhcpService.removeStaticMapping(host.mac()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 613 | |
| 614 | CordService service = getCordService(vNet); |
Hyunsun Moon | 64ec614 | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 615 | if (service == null) { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) { |
| 620 | ruleInstaller.removeManagementNetworkRules(host, service); |
| 621 | } else { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 622 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 623 | ruleInstaller.updateServiceGroup(service); |
| 624 | |
| 625 | if (getHostsWithOpenstackNetwork(vNet).isEmpty()) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 626 | arpProxy.removeGateway(service.serviceIp()); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 631 | /** |
| 632 | * Sets service network gateway MAC address and sends out gratuitous ARP to all |
| 633 | * VMs to update the gateway MAC address. |
| 634 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 635 | * @param newMac mac address to update |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 636 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 637 | private void setPrivateGatewayMac(MacAddress newMac) { |
| 638 | if (newMac == null || newMac.equals(privateGatewayMac)) { |
| 639 | // no updates, do nothing |
| 640 | return; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 643 | privateGatewayMac = newMac; |
| 644 | log.debug("Set service gateway MAC address to {}", privateGatewayMac.toString()); |
| 645 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 646 | // TODO get existing service list from XOS and replace the loop below |
| 647 | Set<String> vNets = Sets.newHashSet(); |
| 648 | hostService.getHosts().forEach(host -> vNets.add(host.annotations().value(SERVICE_ID))); |
| 649 | vNets.remove(null); |
| 650 | |
| 651 | vNets.stream().forEach(vNet -> { |
| 652 | CordService service = getCordService(CordServiceId.of(vNet)); |
| 653 | if (service != null) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 654 | arpProxy.addGateway(service.serviceIp(), privateGatewayMac); |
| 655 | arpProxy.sendGratuitousArpForGateway(service.serviceIp(), service.hosts().keySet()); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 656 | } |
| 657 | }); |
| 658 | } |
| 659 | |
| 660 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 661 | * Sets public gateway MAC address. |
| 662 | * |
| 663 | * @param publicGateways gateway ip and mac address pairs |
| 664 | */ |
| 665 | private void setPublicGatewayMac(Map<IpAddress, MacAddress> publicGateways) { |
| 666 | publicGateways.entrySet() |
| 667 | .stream() |
| 668 | .forEach(entry -> { |
| 669 | arpProxy.addGateway(entry.getKey(), entry.getValue()); |
| 670 | log.debug("Added public gateway IP {}, MAC {}", |
| 671 | entry.getKey().toString(), entry.getValue().toString()); |
| 672 | }); |
| 673 | // TODO notice gateway MAC change to VMs holds this gateway IP |
| 674 | } |
| 675 | |
| 676 | /** |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 677 | * Updates configurations. |
| 678 | */ |
| 679 | private void readConfiguration() { |
| 680 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 681 | if (config == null) { |
| 682 | log.debug("No configuration found"); |
| 683 | return; |
| 684 | } |
| 685 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 686 | setPrivateGatewayMac(config.privateGatewayMac()); |
| 687 | setPublicGatewayMac(config.publicGateways()); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 690 | private class InternalHostListener implements HostListener { |
| 691 | |
| 692 | @Override |
| 693 | public void event(HostEvent event) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 694 | Host host = event.subject(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 695 | |
| 696 | switch (event.type()) { |
| 697 | case HOST_ADDED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 698 | eventExecutor.submit(() -> serviceVmAdded(host)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 699 | break; |
| 700 | case HOST_REMOVED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 701 | eventExecutor.submit(() -> serviceVmRemoved(host)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 702 | break; |
| 703 | default: |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 709 | private class InternalPacketProcessor implements PacketProcessor { |
| 710 | |
| 711 | @Override |
| 712 | public void process(PacketContext context) { |
| 713 | if (context.isHandled()) { |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | Ethernet ethPacket = context.inPacket().parsed(); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 718 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 719 | return; |
| 720 | } |
| 721 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 722 | arpProxy.processArpPacket(context, ethPacket); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | private class InternalConfigListener implements NetworkConfigListener { |
| 727 | |
| 728 | @Override |
| 729 | public void event(NetworkConfigEvent event) { |
| 730 | if (!event.configClass().equals(CordVtnConfig.class)) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 731 | return; |
| 732 | } |
| 733 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 734 | switch (event.type()) { |
| 735 | case CONFIG_ADDED: |
| 736 | case CONFIG_UPDATED: |
| 737 | log.info("Network configuration changed"); |
| 738 | eventExecutor.execute(CordVtn.this::readConfiguration); |
| 739 | break; |
| 740 | default: |
| 741 | break; |
| 742 | } |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 745 | } |