blob: 159997e44a0241ef0eada7a63d56e4fd44d440eb [file] [log] [blame]
Hyunsun Moond0e932a2015-09-15 22:39:16 -07001/*
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 */
16package org.onosproject.cordvtn;
17
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080018import com.google.common.collect.Lists;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080019import com.google.common.collect.Maps;
Hyunsun Moon1f145552015-10-08 22:25:30 -070020import com.google.common.collect.Sets;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -080027import org.onlab.packet.Ethernet;
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080028import org.onlab.packet.Ip4Address;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080029import org.onlab.packet.IpAddress;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080030import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
Hyunsun Moon1f145552015-10-08 22:25:30 -070032import org.onosproject.core.ApplicationId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070033import org.onosproject.core.CoreService;
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080034import org.onosproject.dhcp.DhcpService;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080035import org.onosproject.mastership.MastershipService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080036import org.onosproject.net.ConnectPoint;
Hyunsun Moond772f342015-10-28 20:28:16 -070037import org.onosproject.net.DefaultAnnotations;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070038import org.onosproject.net.Host;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080039import org.onosproject.net.HostId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080040import org.onosproject.net.HostLocation;
Hyunsun Moon8539b042015-11-07 22:08:43 -080041import org.onosproject.net.Port;
Hyunsun Moon746956f2016-01-24 21:47:06 -080042import org.onosproject.net.config.ConfigFactory;
43import org.onosproject.net.config.NetworkConfigEvent;
44import org.onosproject.net.config.NetworkConfigListener;
45import org.onosproject.net.config.NetworkConfigRegistry;
46import org.onosproject.net.config.NetworkConfigService;
47import org.onosproject.net.config.basics.SubjectFactories;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070048import org.onosproject.net.device.DeviceService;
Hyunsun Moond772f342015-10-28 20:28:16 -070049import org.onosproject.net.driver.DriverService;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080050import org.onosproject.net.flow.FlowRuleService;
51import org.onosproject.net.group.GroupService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080052import org.onosproject.net.host.DefaultHostDescription;
53import org.onosproject.net.host.HostDescription;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070054import org.onosproject.net.host.HostEvent;
55import org.onosproject.net.host.HostListener;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080056import org.onosproject.net.host.HostProvider;
57import org.onosproject.net.host.HostProviderRegistry;
58import org.onosproject.net.host.HostProviderService;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070059import org.onosproject.net.host.HostService;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -080060import org.onosproject.net.packet.PacketContext;
61import org.onosproject.net.packet.PacketProcessor;
62import org.onosproject.net.packet.PacketService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080063import org.onosproject.net.provider.AbstractProvider;
64import org.onosproject.net.provider.ProviderId;
sangho93447f12016-02-24 00:33:22 +090065import org.onosproject.openstackinterface.OpenstackInterfaceService;
66import org.onosproject.openstackinterface.OpenstackNetwork;
67import org.onosproject.openstackinterface.OpenstackPort;
68import org.onosproject.openstackinterface.OpenstackSubnet;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070069import org.slf4j.Logger;
70
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080071import java.util.List;
Hyunsun Moon1f145552015-10-08 22:25:30 -070072import java.util.Map;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080073import java.util.Objects;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080074import java.util.Set;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070075import java.util.concurrent.ExecutorService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080076import java.util.stream.Collectors;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080077import java.util.stream.StreamSupport;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070078
Hyunsun Moon1f145552015-10-08 22:25:30 -070079import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon746956f2016-01-24 21:47:06 -080080import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070081import static org.onlab.util.Tools.groupedThreads;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070082import static org.slf4j.LoggerFactory.getLogger;
83
84/**
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080085 * Provisions virtual tenant networks with service chaining capability
86 * in OpenStack environment.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070087 */
88@Component(immediate = true)
89@Service
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080090public class CordVtn extends AbstractProvider implements CordVtnService, HostProvider {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070091
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 Moon746956f2016-01-24 21:47:06 -080098 protected NetworkConfigRegistry configRegistry;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected NetworkConfigService configService;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800104 protected HostProviderRegistry hostProviderRegistry;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700107 protected DeviceService deviceService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected HostService hostService;
111
Hyunsun Moon1f145552015-10-08 22:25:30 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moond772f342015-10-28 20:28:16 -0700113 protected DriverService driverService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800116 protected FlowRuleService flowRuleService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800119 protected PacketService packetService;
120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800122 protected MastershipService mastershipService;
123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected GroupService groupService;
126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
sangho93447f12016-02-24 00:33:22 +0900128 protected OpenstackInterfaceService openstackService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800129
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected DhcpService dhcpService;
132
Hyunsun Moon746956f2016-01-24 21:47:06 -0800133 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 Moonb77b60f2016-01-15 20:03:18 -0800141 private static final String DEFAULT_TUNNEL = "vxlan";
142 private static final String SERVICE_ID = "serviceId";
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800143 private static final String OPENSTACK_VM_ID = "openstackVmId";
Hyunsun Moon6d247342016-02-12 12:48:47 -0800144 private static final String OPENSTACK_PORT_ID = "openstackPortId";
145 private static final String DATA_PLANE_IP = "dataPlaneIp";
146 private static final String DATA_PLANE_INTF = "dataPlaneIntf";
147 private static final String S_TAG = "stag";
Hyunsun Moon98025542016-03-08 04:36:02 -0800148 private static final String VSG_HOST_ID = "vsgHostId";
Hyunsun Moon6d247342016-02-12 12:48:47 -0800149
150 private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8");
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800151
Hyunsun Moon746956f2016-01-24 21:47:06 -0800152 private final ExecutorService eventExecutor =
153 newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler"));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700154
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800155 private final PacketProcessor packetProcessor = new InternalPacketProcessor();
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800156 private final HostListener hostListener = new InternalHostListener();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800157 private final NetworkConfigListener configListener = new InternalConfigListener();
Hyunsun Moon2b530322015-09-23 13:24:35 -0700158
Hyunsun Moon746956f2016-01-24 21:47:06 -0800159 private ApplicationId appId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800160 private HostProviderService hostProvider;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800161 private CordVtnRuleInstaller ruleInstaller;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800162 private CordVtnArpProxy arpProxy;
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800163 private volatile MacAddress privateGatewayMac = MacAddress.NONE;
Hyunsun Moon8539b042015-11-07 22:08:43 -0800164
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800165 /**
166 * Creates an cordvtn host location provider.
167 */
168 public CordVtn() {
169 super(new ProviderId("host", CORDVTN_APP_ID));
Hyunsun Moon8539b042015-11-07 22:08:43 -0800170 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700171
172 @Activate
173 protected void activate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800174 appId = coreService.registerApplication("org.onosproject.cordvtn");
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800175 ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService,
176 deviceService,
177 driverService,
178 groupService,
Hyunsun Moonfae776d2016-03-08 18:07:52 -0800179 configRegistry,
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800180 DEFAULT_TUNNEL);
181
Hyunsun Moon9cf43db2016-02-12 15:59:53 -0800182 arpProxy = new CordVtnArpProxy(appId, packetService, hostService);
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800183 packetService.addProcessor(packetProcessor, PacketProcessor.director(0));
184 arpProxy.requestPacket();
185
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700186 hostService.addListener(hostListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800187 hostProvider = hostProviderRegistry.register(this);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700188
Hyunsun Moon746956f2016-01-24 21:47:06 -0800189 configRegistry.registerConfigFactory(configFactory);
190 configService.addListener(configListener);
191 readConfiguration();
192
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700193 log.info("Started");
194 }
195
196 @Deactivate
197 protected void deactivate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800198 hostProviderRegistry.unregister(this);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700199 hostService.removeListener(hostListener);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800200
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800201 packetService.removeProcessor(packetProcessor);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700202
Hyunsun Moon746956f2016-01-24 21:47:06 -0800203 configRegistry.unregisterConfigFactory(configFactory);
204 configService.removeListener(configListener);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700205
Hyunsun Moon746956f2016-01-24 21:47:06 -0800206 eventExecutor.shutdown();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700207 log.info("Stopped");
208 }
209
210 @Override
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800211 public void triggerProbe(Host host) {
212 /*
213 * Note: In CORD deployment, we assume that all hosts are configured.
214 * Therefore no probe is required.
215 */
Hyunsun Moonb219fc42016-01-14 03:42:47 -0800216 }
217
218 @Override
Hyunsun Moon640f183e2016-02-10 17:02:37 -0800219 public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId,
220 boolean isBidirectional) {
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800221 CordService tService = getCordService(tServiceId);
222 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800223
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800224 if (tService == null || pService == null) {
225 log.error("Failed to create CordService for {}", tServiceId.id());
226 return;
227 }
228
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800229 log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id());
Hyunsun Moon640f183e2016-02-10 17:02:37 -0800230 ruleInstaller.populateServiceDependencyRules(tService, pService, isBidirectional);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800231 }
232
233 @Override
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800234 public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) {
235 CordService tService = getCordService(tServiceId);
236 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800237
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800238 if (tService == null || pService == null) {
239 log.error("Failed to create CordService for {}", tServiceId.id());
240 return;
241 }
242
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800243 log.info("Service dependency from {} to {} removed.", tService.id().id(), pService.id().id());
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800244 ruleInstaller.removeServiceDependencyRules(tService, pService);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800245 }
246
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800247 @Override
248 public void addServiceVm(CordVtnNode node, ConnectPoint connectPoint) {
249 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
250 OpenstackPort vPort = openstackService.port(port);
251 if (vPort == null) {
252 log.warn("Failed to get OpenstackPort for {}", getPortName(port));
Hyunsun Moon8539b042015-11-07 22:08:43 -0800253 return;
254 }
255
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800256 MacAddress mac = vPort.macAddress();
257 HostId hostId = HostId.hostId(mac);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800258
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800259 Host host = hostService.getHost(hostId);
260 if (host != null) {
261 // Host is already known to the system, no HOST_ADDED event is triggered in this case.
262 // It happens when the application is restarted.
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800263 String vmId = host.annotations().value(OPENSTACK_VM_ID);
264 if (vmId != null && vmId.equals(vPort.deviceId())) {
265 serviceVmAdded(host);
266 return;
267 } else {
268 hostProvider.hostVanished(host.id());
269 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800270 }
271
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800272 Set<IpAddress> fixedIp = Sets.newHashSet(vPort.fixedIps().values());
Hyunsun Moon6d247342016-02-12 12:48:47 -0800273 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800274 .set(SERVICE_ID, vPort.networkId())
Hyunsun Moon6d247342016-02-12 12:48:47 -0800275 .set(OPENSTACK_VM_ID, vPort.deviceId())
276 .set(OPENSTACK_PORT_ID, vPort.id())
277 .set(DATA_PLANE_IP, node.dpIp().ip().toString())
278 .set(DATA_PLANE_INTF, node.dpIntf());
279
280 String serviceVlan = getServiceVlan(vPort);
281 if (serviceVlan != null) {
282 annotations.set(S_TAG, serviceVlan);
283 }
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800284
285 HostDescription hostDesc = new DefaultHostDescription(
286 mac,
287 VlanId.NONE,
288 new HostLocation(connectPoint, System.currentTimeMillis()),
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800289 fixedIp,
Hyunsun Moon6d247342016-02-12 12:48:47 -0800290 annotations.build());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800291
292 hostProvider.hostDetected(hostId, hostDesc, false);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800293 }
294
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800295 @Override
296 public void removeServiceVm(ConnectPoint connectPoint) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800297 hostService.getConnectedHosts(connectPoint)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800298 .stream()
Hyunsun Moon2a225162016-02-17 19:00:50 -0800299 .forEach(host -> hostProvider.hostVanished(host.id()));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800300 }
301
Hyunsun Moon6d247342016-02-12 12:48:47 -0800302 @Override
303 public void updateVirtualSubscriberGateways(HostId vSgHostId, String serviceVlan,
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800304 Map<IpAddress, MacAddress> vSgs) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800305 Host vSgHost = hostService.getHost(vSgHostId);
306 if (vSgHost == null || !vSgHost.annotations().value(S_TAG).equals(serviceVlan)) {
Hyunsun Moon6d247342016-02-12 12:48:47 -0800307 log.debug("Invalid vSG updates for {}", serviceVlan);
308 return;
309 }
310
Hyunsun Moon98025542016-03-08 04:36:02 -0800311 log.info("Updates vSGs in {} with {}", vSgHost.id(), vSgs.toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800312 vSgs.entrySet().stream()
Hyunsun Moon98025542016-03-08 04:36:02 -0800313 .filter(entry -> hostService.getHostsByMac(entry.getValue()).isEmpty())
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800314 .forEach(entry -> addVirtualSubscriberGateway(
Hyunsun Moon98025542016-03-08 04:36:02 -0800315 vSgHost,
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800316 entry.getKey(),
317 entry.getValue(),
318 serviceVlan));
319
Hyunsun Moon98025542016-03-08 04:36:02 -0800320 hostService.getConnectedHosts(vSgHost.location()).stream()
321 .filter(host -> !host.mac().equals(vSgHost.mac()))
Hyunsun Moon2a225162016-02-17 19:00:50 -0800322 .filter(host -> !vSgs.values().contains(host.mac()))
323 .forEach(host -> {
324 log.info("Removed vSG {}", host.toString());
325 hostProvider.hostVanished(host.id());
326 });
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800327 }
328
329 /**
330 * Adds virtual subscriber gateway to the system.
331 *
332 * @param vSgHost host virtual machine of this vSG
333 * @param vSgIp vSG ip address
334 * @param vSgMac vSG mac address
335 * @param serviceVlan service vlan
336 */
Hyunsun Moon2a225162016-02-17 19:00:50 -0800337 private void addVirtualSubscriberGateway(Host vSgHost, IpAddress vSgIp, MacAddress vSgMac,
338 String serviceVlan) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800339 log.info("vSG with IP({}) MAC({}) added", vSgIp.toString(), vSgMac.toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800340
Hyunsun Moon98025542016-03-08 04:36:02 -0800341 HostId hostId = HostId.hostId(vSgMac);
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800342 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
Hyunsun Moon98025542016-03-08 04:36:02 -0800343 .set(S_TAG, serviceVlan)
344 .set(VSG_HOST_ID, vSgHost.id().toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800345
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 Moon6d247342016-02-12 12:48:47 -0800354 }
355
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800356 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800357 * 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 Moonc71231d2015-12-16 20:53:23 -0800381 * Returns CordService by service ID.
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800382 *
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 Moonc71231d2015-12-16 20:53:23 -0800393 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 Moonb77b60f2016-01-15 20:03:18 -0800406 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800407
408 return new CordService(vNet, subnet, hosts, tServices);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800409 }
410
411 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800412 * Returns CordService by OpenStack network.
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800413 *
414 * @param vNet OpenStack network
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800415 * @return cord service
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800416 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800417 private CordService getCordService(OpenstackNetwork vNet) {
418 checkNotNull(vNet);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800419
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800420 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 Moon9f0814b2015-11-04 17:34:35 -0800427 }
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800428
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800429 Set<CordServiceId> tServices = Sets.newHashSet();
430 // TODO get tenant services from XOS
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800431
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800432 Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet)
433 .stream()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800434 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800435
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800436 return new CordService(vNet, subnet, hosts, tServices);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800437 }
438
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800439 /**
440 * Returns IP address for tunneling for a given host.
441 *
442 * @param host host
Hyunsun Moon6d247342016-02-12 12:48:47 -0800443 * @return ip address, or null
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800444 */
445 private IpAddress getTunnelIp(Host host) {
Hyunsun Moon6d247342016-02-12 12:48:47 -0800446 String ip = host.annotations().value(DATA_PLANE_IP);
447 return ip == null ? null : IpAddress.valueOf(ip);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800448 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700449
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800450 /**
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 Moon8539b042015-11-07 22:08:43 -0800459
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800460 /**
Hyunsun Moon6d247342016-02-12 12:48:47 -0800461 * 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 Moon32f3b8e2016-03-02 19:27:26 -0800477 * 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 Moonb77b60f2016-01-15 20:03:18 -0800487 * 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 Moond0e932a2015-09-15 22:39:16 -0700494
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800495 String vNetId = vNet.id();
496 return StreamSupport.stream(hostService.getHosts().spliterator(), false)
497 .filter(host -> Objects.equals(vNetId, getServiceId(host)))
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800498 .collect(Collectors.toSet());
Hyunsun Moon6d247342016-02-12 12:48:47 -0800499 }
500
501 /**
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800502 * 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 Moonb77b60f2016-01-15 20:03:18 -0800522 * Handles VM detected situation.
523 *
524 * @param host host
525 */
526 private void serviceVmAdded(Host host) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800527 String serviceVlan = host.annotations().value(S_TAG);
528 if (serviceVlan != null) {
529 virtualSubscriberGatewayAdded(host, serviceVlan);
530 }
531
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800532 String vNetId = host.annotations().value(SERVICE_ID);
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800533 if (vNetId == null) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800534 // ignore this host, it is not the service VM, or it's a vSG
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800535 return;
536 }
537
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800538 OpenstackNetwork vNet = openstackService.network(vNetId);
539 if (vNet == null) {
540 log.warn("Failed to get OpenStack network {} for VM {}({}).",
Hyunsun Moon98025542016-03-08 04:36:02 -0800541 vNetId, host.id(),
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800542 host.annotations().value(OPENSTACK_VM_ID));
543 return;
544 }
545
546 log.info("VM {} is detected, MAC: {} IP: {}",
547 host.annotations().value(OPENSTACK_VM_ID),
548 host.mac(),
549 host.ipAddresses().stream().findFirst().get());
550
551 CordService service = getCordService(vNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800552 if (service == null) {
553 return;
554 }
555
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800556 switch (service.serviceType()) {
557 case MANAGEMENT:
558 ruleInstaller.populateManagementNetworkRules(host, service);
559 break;
560 case PRIVATE:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800561 arpProxy.addGateway(service.serviceIp(), privateGatewayMac);
Hyunsun Moon098cda82016-03-03 13:27:44 -0800562 case PUBLIC:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800563 default:
564 // TODO check if the service needs an update on its group buckets after done CORD-433
565 ruleInstaller.updateServiceGroup(service);
566 // sends gratuitous ARP here for the case of adding existing VMs
567 // when ONOS or cordvtn app is restarted
568 arpProxy.sendGratuitousArpForGateway(service.serviceIp(), Sets.newHashSet(host));
569 break;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800570 }
571
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800572 registerDhcpLease(host, service);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800573 ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800574 }
575
576 /**
577 * Handles VM removed situation.
578 *
579 * @param host host
580 */
581 private void serviceVmRemoved(Host host) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800582 String serviceVlan = host.annotations().value(S_TAG);
583 if (serviceVlan != null) {
584 virtualSubscriberGatewayRemoved(host);
585 }
586
Hyunsun Moon2a225162016-02-17 19:00:50 -0800587 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
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800590 return;
591 }
592
Hyunsun Moon6d247342016-02-12 12:48:47 -0800593 OpenstackNetwork vNet = openstackService.network(vNetId);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800594 if (vNet == null) {
595 log.warn("Failed to get OpenStack network {} for VM {}({}).",
Hyunsun Moon98025542016-03-08 04:36:02 -0800596 vNetId, host.id(),
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800597 host.annotations().value(OPENSTACK_VM_ID));
598 return;
599 }
600
601 log.info("VM {} is vanished, MAC: {} IP: {}",
602 host.annotations().value(OPENSTACK_VM_ID),
603 host.mac(),
604 host.ipAddresses().stream().findFirst().get());
605
606 ruleInstaller.removeBasicConnectionRules(host);
Hyunsun Moonfb631b42016-02-03 14:44:06 -0800607 dhcpService.removeStaticMapping(host.mac());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800608
609 CordService service = getCordService(vNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800610 if (service == null) {
611 return;
612 }
613
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800614 switch (service.serviceType()) {
615 case MANAGEMENT:
616 ruleInstaller.removeManagementNetworkRules(host, service);
617 break;
618 case PRIVATE:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800619 if (getHostsWithOpenstackNetwork(vNet).isEmpty()) {
620 arpProxy.removeGateway(service.serviceIp());
621 }
Hyunsun Moon098cda82016-03-03 13:27:44 -0800622 case PUBLIC:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800623 default:
624 // TODO check if the service needs an update on its group buckets after done CORD-433
625 ruleInstaller.updateServiceGroup(service);
626 break;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700627 }
628 }
629
Hyunsun Moon98025542016-03-08 04:36:02 -0800630
631 /**
632 * Handles virtual subscriber gateway VM or container.
633 *
634 * @param host new host with stag, it can be vsg VM or vsg
635 * @param serviceVlan service vlan
636 */
637 private void virtualSubscriberGatewayAdded(Host host, String serviceVlan) {
638 Map<IpAddress, MacAddress> vSgs;
639 Host vSgHost;
640
641 String vSgHostId = host.annotations().value(VSG_HOST_ID);
642 if (vSgHostId == null) {
643 log.debug("vSG VM detected {}", host.id());
644
645 vSgHost = host;
646 vSgs = getSubscriberGateways(vSgHost);
647 vSgs.entrySet().stream().forEach(entry -> addVirtualSubscriberGateway(
648 vSgHost,
649 entry.getKey(),
650 entry.getValue(),
651 serviceVlan));
652 } else {
653 vSgHost = hostService.getHost(HostId.hostId(vSgHostId));
654 if (vSgHost == null) {
655 return;
656 }
657
658 log.debug("vSG detected {}", host.id());
659 vSgs = getSubscriberGateways(vSgHost);
660 }
661
662 ruleInstaller.populateSubscriberGatewayRules(vSgHost, vSgs.keySet());
663 }
664
665 /**
666 * Handles virtual subscriber gateway removed.
667 *
668 * @param vSg vsg host to remove
669 */
670 private void virtualSubscriberGatewayRemoved(Host vSg) {
671 String vSgHostId = vSg.annotations().value(VSG_HOST_ID);
672 if (vSgHostId == null) {
673 return;
674 }
675
676 Host vSgHost = hostService.getHost(HostId.hostId(vSgHostId));
677 if (vSgHost == null) {
678 return;
679 }
680
681 log.info("vSG removed {}", vSg.id());
682 Map<IpAddress, MacAddress> vSgs = getSubscriberGateways(vSgHost);
683 ruleInstaller.populateSubscriberGatewayRules(vSgHost, vSgs.keySet());
684 }
685
Hyunsun Moon746956f2016-01-24 21:47:06 -0800686 /**
687 * Sets service network gateway MAC address and sends out gratuitous ARP to all
688 * VMs to update the gateway MAC address.
689 *
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800690 * @param newMac mac address to update
Hyunsun Moon746956f2016-01-24 21:47:06 -0800691 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800692 private void setPrivateGatewayMac(MacAddress newMac) {
693 if (newMac == null || newMac.equals(privateGatewayMac)) {
694 // no updates, do nothing
695 return;
Hyunsun Moon746956f2016-01-24 21:47:06 -0800696 }
697
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800698 privateGatewayMac = newMac;
699 log.debug("Set service gateway MAC address to {}", privateGatewayMac.toString());
700
Hyunsun Moon746956f2016-01-24 21:47:06 -0800701 // TODO get existing service list from XOS and replace the loop below
702 Set<String> vNets = Sets.newHashSet();
703 hostService.getHosts().forEach(host -> vNets.add(host.annotations().value(SERVICE_ID)));
704 vNets.remove(null);
705
706 vNets.stream().forEach(vNet -> {
707 CordService service = getCordService(CordServiceId.of(vNet));
708 if (service != null) {
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800709 arpProxy.addGateway(service.serviceIp(), privateGatewayMac);
710 arpProxy.sendGratuitousArpForGateway(service.serviceIp(), service.hosts().keySet());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800711 }
712 });
713 }
714
715 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800716 * Sets public gateway MAC address.
717 *
718 * @param publicGateways gateway ip and mac address pairs
719 */
720 private void setPublicGatewayMac(Map<IpAddress, MacAddress> publicGateways) {
721 publicGateways.entrySet()
722 .stream()
723 .forEach(entry -> {
724 arpProxy.addGateway(entry.getKey(), entry.getValue());
725 log.debug("Added public gateway IP {}, MAC {}",
726 entry.getKey().toString(), entry.getValue().toString());
727 });
728 // TODO notice gateway MAC change to VMs holds this gateway IP
729 }
730
731 /**
Hyunsun Moon746956f2016-01-24 21:47:06 -0800732 * Updates configurations.
733 */
734 private void readConfiguration() {
735 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
736 if (config == null) {
737 log.debug("No configuration found");
738 return;
739 }
740
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800741 setPrivateGatewayMac(config.privateGatewayMac());
742 setPublicGatewayMac(config.publicGateways());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800743 }
744
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700745 private class InternalHostListener implements HostListener {
746
747 @Override
748 public void event(HostEvent event) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800749 Host host = event.subject();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700750
751 switch (event.type()) {
752 case HOST_ADDED:
Hyunsun Moon98025542016-03-08 04:36:02 -0800753 if (mastershipService.isLocalMaster(host.location().deviceId())) {
754 eventExecutor.submit(() -> serviceVmAdded(host));
755 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700756 break;
757 case HOST_REMOVED:
Hyunsun Moon98025542016-03-08 04:36:02 -0800758 if (mastershipService.isLocalMaster(host.location().deviceId())) {
759 eventExecutor.submit(() -> serviceVmRemoved(host));
760 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700761 break;
762 default:
763 break;
764 }
765 }
766 }
767
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800768 private class InternalPacketProcessor implements PacketProcessor {
769
770 @Override
771 public void process(PacketContext context) {
772 if (context.isHandled()) {
773 return;
774 }
775
776 Ethernet ethPacket = context.inPacket().parsed();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800777 if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800778 return;
779 }
780
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800781 arpProxy.processArpPacket(context, ethPacket);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800782 }
783 }
784
785 private class InternalConfigListener implements NetworkConfigListener {
786
787 @Override
788 public void event(NetworkConfigEvent event) {
789 if (!event.configClass().equals(CordVtnConfig.class)) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800790 return;
791 }
792
Hyunsun Moon746956f2016-01-24 21:47:06 -0800793 switch (event.type()) {
794 case CONFIG_ADDED:
795 case CONFIG_UPDATED:
796 log.info("Network configuration changed");
797 eventExecutor.execute(CordVtn.this::readConfiguration);
798 break;
799 default:
800 break;
801 }
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800802 }
803 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700804}