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 | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 30 | import org.onosproject.core.CoreService; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 31 | import org.onosproject.mastership.MastershipService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Host; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 35 | import org.onosproject.net.HostId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 36 | import org.onosproject.net.HostLocation; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 38 | import org.onosproject.net.SparseAnnotations; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 40 | import org.onosproject.net.driver.DriverService; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.FlowRuleService; |
| 42 | import org.onosproject.net.group.GroupService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 43 | import org.onosproject.net.host.DefaultHostDescription; |
| 44 | import org.onosproject.net.host.HostDescription; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 45 | import org.onosproject.net.host.HostEvent; |
| 46 | import org.onosproject.net.host.HostListener; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 47 | import org.onosproject.net.host.HostProvider; |
| 48 | import org.onosproject.net.host.HostProviderRegistry; |
| 49 | import org.onosproject.net.host.HostProviderService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 50 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 51 | import org.onosproject.net.packet.PacketContext; |
| 52 | import org.onosproject.net.packet.PacketProcessor; |
| 53 | import org.onosproject.net.packet.PacketService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 54 | import org.onosproject.net.provider.AbstractProvider; |
| 55 | import org.onosproject.net.provider.ProviderId; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 56 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 57 | import org.onosproject.openstackswitching.OpenstackPort; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 58 | import org.onosproject.openstackswitching.OpenstackSubnet; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 59 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 62 | import java.util.Map; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 63 | import java.util.Set; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 66 | import java.util.stream.Collectors; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 67 | |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 68 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 69 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 70 | import static org.slf4j.LoggerFactory.getLogger; |
| 71 | |
| 72 | /** |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 73 | * Provisions virtual tenant networks with service chaining capability |
| 74 | * in OpenStack environment. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 75 | */ |
| 76 | @Component(immediate = true) |
| 77 | @Service |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 78 | public class CordVtn extends AbstractProvider implements CordVtnService, HostProvider { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 79 | |
| 80 | protected final Logger log = getLogger(getClass()); |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected CoreService coreService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 86 | protected HostProviderRegistry hostProviderRegistry; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 89 | protected DeviceService deviceService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected HostService hostService; |
| 93 | |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 169f938 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 95 | protected DriverService driverService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 98 | protected FlowRuleService flowRuleService; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 101 | protected PacketService packetService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 104 | protected MastershipService mastershipService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected GroupService groupService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 110 | protected OpenstackSwitchingService openstackService; |
| 111 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 112 | private static final int NUM_THREADS = 1; |
| 113 | private static final String DEFAULT_TUNNEL = "vxlan"; |
| 114 | private static final String SERVICE_ID = "serviceId"; |
| 115 | private static final String LOCATION_IP = "locationIp"; |
| 116 | private static final String OPENSTACK_VM_ID = "openstackVmId"; |
| 117 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 118 | private final ExecutorService eventExecutor = Executors |
| 119 | .newFixedThreadPool(NUM_THREADS, groupedThreads("onos/cordvtn", "event-handler")); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 120 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 121 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 122 | private final HostListener hostListener = new InternalHostListener(); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 123 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 124 | private HostProviderService hostProvider; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 125 | private CordVtnRuleInstaller ruleInstaller; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 126 | private CordVtnArpProxy arpProxy; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 127 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Creates an cordvtn host location provider. |
| 130 | */ |
| 131 | public CordVtn() { |
| 132 | super(new ProviderId("host", CORDVTN_APP_ID)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 133 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 134 | |
| 135 | @Activate |
| 136 | protected void activate() { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 137 | ApplicationId appId = coreService.registerApplication("org.onosproject.cordvtn"); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 138 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 139 | ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService, |
| 140 | deviceService, |
| 141 | driverService, |
| 142 | groupService, |
| 143 | mastershipService, |
| 144 | DEFAULT_TUNNEL); |
| 145 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 146 | arpProxy = new CordVtnArpProxy(appId, packetService); |
| 147 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 148 | arpProxy.requestPacket(); |
| 149 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 150 | hostService.addListener(hostListener); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 151 | hostProvider = hostProviderRegistry.register(this); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 152 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 153 | log.info("Started"); |
| 154 | } |
| 155 | |
| 156 | @Deactivate |
| 157 | protected void deactivate() { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 158 | hostService.removeListener(hostListener); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 159 | packetService.removeProcessor(packetProcessor); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 160 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 161 | eventExecutor.shutdown(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 162 | hostProviderRegistry.unregister(this); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 163 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 164 | log.info("Stopped"); |
| 165 | } |
| 166 | |
| 167 | @Override |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 168 | public void triggerProbe(Host host) { |
| 169 | /* |
| 170 | * Note: In CORD deployment, we assume that all hosts are configured. |
| 171 | * Therefore no probe is required. |
| 172 | */ |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 176 | public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) { |
| 177 | CordService tService = getCordService(tServiceId); |
| 178 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 179 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 180 | if (tService == null || pService == null) { |
| 181 | log.error("Failed to create CordService for {}", tServiceId.id()); |
| 182 | return; |
| 183 | } |
| 184 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 185 | log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id()); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 186 | ruleInstaller.populateServiceDependencyRules(tService, pService); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 190 | public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) { |
| 191 | CordService tService = getCordService(tServiceId); |
| 192 | CordService pService = getCordService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 193 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 194 | if (tService == null || pService == null) { |
| 195 | log.error("Failed to create CordService for {}", tServiceId.id()); |
| 196 | return; |
| 197 | } |
| 198 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 199 | 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] | 200 | ruleInstaller.removeServiceDependencyRules(tService, pService); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 203 | @Override |
| 204 | public void addServiceVm(CordVtnNode node, ConnectPoint connectPoint) { |
| 205 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 206 | OpenstackPort vPort = openstackService.port(port); |
| 207 | if (vPort == null) { |
| 208 | log.warn("Failed to get OpenstackPort for {}", getPortName(port)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 212 | MacAddress mac = vPort.macAddress(); |
| 213 | HostId hostId = HostId.hostId(mac); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 214 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 215 | Host host = hostService.getHost(hostId); |
| 216 | if (host != null) { |
| 217 | // Host is already known to the system, no HOST_ADDED event is triggered in this case. |
| 218 | // It happens when the application is restarted. |
| 219 | // TODO check host description if it has all the information |
| 220 | serviceVmAdded(host); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 224 | Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values()); |
| 225 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 226 | .set(OPENSTACK_VM_ID, vPort.deviceId()) |
| 227 | .set(SERVICE_ID, vPort.networkId()) |
| 228 | .set(LOCATION_IP, node.localIp().toString()) |
| 229 | .build(); |
| 230 | |
| 231 | HostDescription hostDesc = new DefaultHostDescription( |
| 232 | mac, |
| 233 | VlanId.NONE, |
| 234 | new HostLocation(connectPoint, System.currentTimeMillis()), |
| 235 | ip, |
| 236 | annotations); |
| 237 | |
| 238 | hostProvider.hostDetected(hostId, hostDesc, false); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 241 | @Override |
| 242 | public void removeServiceVm(ConnectPoint connectPoint) { |
| 243 | Host host = hostService.getConnectedHosts(connectPoint) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 244 | .stream() |
| 245 | .findFirst() |
| 246 | .orElse(null); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 247 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 248 | if (host == null) { |
| 249 | log.debug("No host is connected on {}", connectPoint.toString()); |
| 250 | return; |
| 251 | } |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 252 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 253 | hostProvider.hostVanished(host.id()); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 257 | * Returns CordService by service ID. |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 258 | * |
| 259 | * @param serviceId service id |
| 260 | * @return cord service, or null if it fails to get network from OpenStack |
| 261 | */ |
| 262 | private CordService getCordService(CordServiceId serviceId) { |
| 263 | OpenstackNetwork vNet = openstackService.network(serviceId.id()); |
| 264 | if (vNet == null) { |
| 265 | log.warn("Couldn't find OpenStack network for service {}", serviceId.id()); |
| 266 | return null; |
| 267 | } |
| 268 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 269 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 270 | .findFirst() |
| 271 | .orElse(null); |
| 272 | if (subnet == null) { |
| 273 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id()); |
| 274 | return null; |
| 275 | } |
| 276 | |
| 277 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 278 | // TODO get tenant services from XOS |
| 279 | |
| 280 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 281 | .stream() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 282 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 283 | |
| 284 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 288 | * Returns CordService by OpenStack network. |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 289 | * |
| 290 | * @param vNet OpenStack network |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 291 | * @return cord service |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 292 | */ |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 293 | private CordService getCordService(OpenstackNetwork vNet) { |
| 294 | checkNotNull(vNet); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 295 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 296 | CordServiceId serviceId = CordServiceId.of(vNet.id()); |
| 297 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 298 | .findFirst() |
| 299 | .orElse(null); |
| 300 | if (subnet == null) { |
| 301 | log.warn("Couldn't find OpenStack subnet for service {}", serviceId); |
| 302 | return null; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 303 | } |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 304 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 305 | Set<CordServiceId> tServices = Sets.newHashSet(); |
| 306 | // TODO get tenant services from XOS |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 307 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 308 | Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet) |
| 309 | .stream() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 310 | .collect(Collectors.toMap(host -> host, this::getTunnelIp)); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 311 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 312 | return new CordService(vNet, subnet, hosts, tServices); |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 315 | /** |
| 316 | * Returns IP address for tunneling for a given host. |
| 317 | * |
| 318 | * @param host host |
| 319 | * @return ip address |
| 320 | */ |
| 321 | private IpAddress getTunnelIp(Host host) { |
| 322 | return IpAddress.valueOf(host.annotations().value(LOCATION_IP)); |
| 323 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 324 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Returns port name. |
| 327 | * |
| 328 | * @param port port |
| 329 | * @return port name |
| 330 | */ |
| 331 | private String getPortName(Port port) { |
| 332 | return port.annotations().value("portName"); |
| 333 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 334 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 335 | /** |
| 336 | * Returns hosts associated with a given OpenStack network. |
| 337 | * |
| 338 | * @param vNet openstack network |
| 339 | * @return set of hosts |
| 340 | */ |
| 341 | private Set<Host> getHostsWithOpenstackNetwork(OpenstackNetwork vNet) { |
| 342 | checkNotNull(vNet); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 343 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 344 | Set<Host> hosts = openstackService.ports(vNet.id()).stream() |
| 345 | .filter(port -> port.deviceOwner().contains("compute")) |
| 346 | .map(port -> hostService.getHostsByMac(port.macAddress()) |
| 347 | .stream() |
| 348 | .findFirst() |
| 349 | .orElse(null)) |
| 350 | .collect(Collectors.toSet()); |
| 351 | |
| 352 | hosts.remove(null); |
| 353 | return hosts; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Handles VM detected situation. |
| 358 | * |
| 359 | * @param host host |
| 360 | */ |
| 361 | private void serviceVmAdded(Host host) { |
| 362 | String vNetId = host.annotations().value(SERVICE_ID); |
| 363 | OpenstackNetwork vNet = openstackService.network(vNetId); |
| 364 | if (vNet == null) { |
| 365 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 366 | vNetId, |
| 367 | host.id(), |
| 368 | host.annotations().value(OPENSTACK_VM_ID)); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | log.info("VM {} is detected, MAC: {} IP: {}", |
| 373 | host.annotations().value(OPENSTACK_VM_ID), |
| 374 | host.mac(), |
| 375 | host.ipAddresses().stream().findFirst().get()); |
| 376 | |
| 377 | CordService service = getCordService(vNet); |
| 378 | if (service != null) { |
| 379 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 380 | ruleInstaller.updateServiceGroup(service); |
| 381 | arpProxy.addServiceIp(service.serviceIp()); |
| 382 | } |
| 383 | |
| 384 | ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Handles VM removed situation. |
| 389 | * |
| 390 | * @param host host |
| 391 | */ |
| 392 | private void serviceVmRemoved(Host host) { |
| 393 | String vNetId = host.annotations().value(SERVICE_ID); |
| 394 | OpenstackNetwork vNet = openstackService.network(host.annotations().value(SERVICE_ID)); |
| 395 | if (vNet == null) { |
| 396 | log.warn("Failed to get OpenStack network {} for VM {}({}).", |
| 397 | vNetId, |
| 398 | host.id(), |
| 399 | host.annotations().value(OPENSTACK_VM_ID)); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | log.info("VM {} is vanished, MAC: {} IP: {}", |
| 404 | host.annotations().value(OPENSTACK_VM_ID), |
| 405 | host.mac(), |
| 406 | host.ipAddresses().stream().findFirst().get()); |
| 407 | |
| 408 | ruleInstaller.removeBasicConnectionRules(host); |
| 409 | |
| 410 | CordService service = getCordService(vNet); |
| 411 | if (service != null) { |
| 412 | // TODO check if the service needs an update on its group buckets after done CORD-433 |
| 413 | ruleInstaller.updateServiceGroup(service); |
| 414 | |
| 415 | if (getHostsWithOpenstackNetwork(vNet).isEmpty()) { |
| 416 | arpProxy.removeServiceIp(service.serviceIp()); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | private class InternalHostListener implements HostListener { |
| 422 | |
| 423 | @Override |
| 424 | public void event(HostEvent event) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 425 | Host host = event.subject(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 426 | |
| 427 | switch (event.type()) { |
| 428 | case HOST_ADDED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 429 | eventExecutor.submit(() -> serviceVmAdded(host)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 430 | break; |
| 431 | case HOST_REMOVED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 432 | eventExecutor.submit(() -> serviceVmRemoved(host)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 433 | break; |
| 434 | default: |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 440 | private class InternalPacketProcessor implements PacketProcessor { |
| 441 | |
| 442 | @Override |
| 443 | public void process(PacketContext context) { |
| 444 | if (context.isHandled()) { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | Ethernet ethPacket = context.inPacket().parsed(); |
| 449 | if (ethPacket == null) { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (ethPacket.getEtherType() != Ethernet.TYPE_ARP) { |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | arpProxy.processArpPacket(context, ethPacket); |
| 458 | } |
| 459 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 460 | } |