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