blob: e852cf9fa8157b79fe5d38c82e17d3735ee5d79b [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 Moonc71231d2015-12-16 20:53:23 -0800179 DEFAULT_TUNNEL);
180
Hyunsun Moon9cf43db2016-02-12 15:59:53 -0800181 arpProxy = new CordVtnArpProxy(appId, packetService, hostService);
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800182 packetService.addProcessor(packetProcessor, PacketProcessor.director(0));
183 arpProxy.requestPacket();
184
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700185 hostService.addListener(hostListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800186 hostProvider = hostProviderRegistry.register(this);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700187
Hyunsun Moon746956f2016-01-24 21:47:06 -0800188 configRegistry.registerConfigFactory(configFactory);
189 configService.addListener(configListener);
190 readConfiguration();
191
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700192 log.info("Started");
193 }
194
195 @Deactivate
196 protected void deactivate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800197 hostProviderRegistry.unregister(this);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700198 hostService.removeListener(hostListener);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800199
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800200 packetService.removeProcessor(packetProcessor);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700201
Hyunsun Moon746956f2016-01-24 21:47:06 -0800202 configRegistry.unregisterConfigFactory(configFactory);
203 configService.removeListener(configListener);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700204
Hyunsun Moon746956f2016-01-24 21:47:06 -0800205 eventExecutor.shutdown();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700206 log.info("Stopped");
207 }
208
209 @Override
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800210 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 Moonb219fc42016-01-14 03:42:47 -0800215 }
216
217 @Override
Hyunsun Moon640f183e2016-02-10 17:02:37 -0800218 public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId,
219 boolean isBidirectional) {
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800220 CordService tService = getCordService(tServiceId);
221 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800222
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800223 if (tService == null || pService == null) {
224 log.error("Failed to create CordService for {}", tServiceId.id());
225 return;
226 }
227
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800228 log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id());
Hyunsun Moon640f183e2016-02-10 17:02:37 -0800229 ruleInstaller.populateServiceDependencyRules(tService, pService, isBidirectional);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800230 }
231
232 @Override
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800233 public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) {
234 CordService tService = getCordService(tServiceId);
235 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800236
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800237 if (tService == null || pService == null) {
238 log.error("Failed to create CordService for {}", tServiceId.id());
239 return;
240 }
241
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800242 log.info("Service dependency from {} to {} removed.", tService.id().id(), pService.id().id());
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800243 ruleInstaller.removeServiceDependencyRules(tService, pService);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800244 }
245
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800246 @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 Moon8539b042015-11-07 22:08:43 -0800252 return;
253 }
254
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800255 MacAddress mac = vPort.macAddress();
256 HostId hostId = HostId.hostId(mac);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800257
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800258 Host host = hostService.getHost(hostId);
259 if (host != null) {
260 // Host is already known to the system, no HOST_ADDED event is triggered in this case.
261 // It happens when the application is restarted.
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800262 String vmId = host.annotations().value(OPENSTACK_VM_ID);
263 if (vmId != null && vmId.equals(vPort.deviceId())) {
264 serviceVmAdded(host);
265 return;
266 } else {
267 hostProvider.hostVanished(host.id());
268 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800269 }
270
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800271 Set<IpAddress> fixedIp = Sets.newHashSet(vPort.fixedIps().values());
Hyunsun Moon6d247342016-02-12 12:48:47 -0800272 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800273 .set(SERVICE_ID, vPort.networkId())
Hyunsun Moon6d247342016-02-12 12:48:47 -0800274 .set(OPENSTACK_VM_ID, vPort.deviceId())
275 .set(OPENSTACK_PORT_ID, vPort.id())
276 .set(DATA_PLANE_IP, node.dpIp().ip().toString())
277 .set(DATA_PLANE_INTF, node.dpIntf());
278
279 String serviceVlan = getServiceVlan(vPort);
280 if (serviceVlan != null) {
281 annotations.set(S_TAG, serviceVlan);
282 }
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800283
284 HostDescription hostDesc = new DefaultHostDescription(
285 mac,
286 VlanId.NONE,
287 new HostLocation(connectPoint, System.currentTimeMillis()),
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800288 fixedIp,
Hyunsun Moon6d247342016-02-12 12:48:47 -0800289 annotations.build());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800290
291 hostProvider.hostDetected(hostId, hostDesc, false);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800292 }
293
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800294 @Override
295 public void removeServiceVm(ConnectPoint connectPoint) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800296 hostService.getConnectedHosts(connectPoint)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800297 .stream()
Hyunsun Moon2a225162016-02-17 19:00:50 -0800298 .forEach(host -> hostProvider.hostVanished(host.id()));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800299 }
300
Hyunsun Moon6d247342016-02-12 12:48:47 -0800301 @Override
302 public void updateVirtualSubscriberGateways(HostId vSgHostId, String serviceVlan,
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800303 Map<IpAddress, MacAddress> vSgs) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800304 Host vSgHost = hostService.getHost(vSgHostId);
305 if (vSgHost == null || !vSgHost.annotations().value(S_TAG).equals(serviceVlan)) {
Hyunsun Moon6d247342016-02-12 12:48:47 -0800306 log.debug("Invalid vSG updates for {}", serviceVlan);
307 return;
308 }
309
Hyunsun Moon98025542016-03-08 04:36:02 -0800310 log.info("Updates vSGs in {} with {}", vSgHost.id(), vSgs.toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800311 vSgs.entrySet().stream()
Hyunsun Moon98025542016-03-08 04:36:02 -0800312 .filter(entry -> hostService.getHostsByMac(entry.getValue()).isEmpty())
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800313 .forEach(entry -> addVirtualSubscriberGateway(
Hyunsun Moon98025542016-03-08 04:36:02 -0800314 vSgHost,
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800315 entry.getKey(),
316 entry.getValue(),
317 serviceVlan));
318
Hyunsun Moon98025542016-03-08 04:36:02 -0800319 hostService.getConnectedHosts(vSgHost.location()).stream()
320 .filter(host -> !host.mac().equals(vSgHost.mac()))
Hyunsun Moon2a225162016-02-17 19:00:50 -0800321 .filter(host -> !vSgs.values().contains(host.mac()))
322 .forEach(host -> {
323 log.info("Removed vSG {}", host.toString());
324 hostProvider.hostVanished(host.id());
325 });
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800326 }
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 Moon2a225162016-02-17 19:00:50 -0800336 private void addVirtualSubscriberGateway(Host vSgHost, IpAddress vSgIp, MacAddress vSgMac,
337 String serviceVlan) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800338 log.info("vSG with IP({}) MAC({}) added", vSgIp.toString(), vSgMac.toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800339
Hyunsun Moon98025542016-03-08 04:36:02 -0800340 HostId hostId = HostId.hostId(vSgMac);
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800341 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
Hyunsun Moon98025542016-03-08 04:36:02 -0800342 .set(S_TAG, serviceVlan)
343 .set(VSG_HOST_ID, vSgHost.id().toString());
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800344
345 HostDescription hostDesc = new DefaultHostDescription(
346 vSgMac,
347 VlanId.NONE,
348 vSgHost.location(),
349 Sets.newHashSet(vSgIp),
350 annotations.build());
351
352 hostProvider.hostDetected(hostId, hostDesc, false);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800353 }
354
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800355 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800356 * Returns public ip addresses of vSGs running inside a give vSG host.
357 *
358 * @param vSgHost vSG host
359 * @return map of ip and mac address, or empty map
360 */
361 private Map<IpAddress, MacAddress> getSubscriberGateways(Host vSgHost) {
362 String vPortId = vSgHost.annotations().value(OPENSTACK_PORT_ID);
363 String serviceVlan = vSgHost.annotations().value(S_TAG);
364
365 OpenstackPort vPort = openstackService.port(vPortId);
366 if (vPort == null) {
367 log.warn("Failed to get OpenStack port {} for VM {}", vPortId, vSgHost.id());
368 return Maps.newHashMap();
369 }
370
371 if (!serviceVlan.equals(getServiceVlan(vPort))) {
372 log.error("Host({}) s-tag does not match with vPort s-tag", vSgHost.id());
373 return Maps.newHashMap();
374 }
375
376 return vPort.allowedAddressPairs();
377 }
378
379 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800380 * Returns CordService by service ID.
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800381 *
382 * @param serviceId service id
383 * @return cord service, or null if it fails to get network from OpenStack
384 */
385 private CordService getCordService(CordServiceId serviceId) {
386 OpenstackNetwork vNet = openstackService.network(serviceId.id());
387 if (vNet == null) {
388 log.warn("Couldn't find OpenStack network for service {}", serviceId.id());
389 return null;
390 }
391
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800392 OpenstackSubnet subnet = vNet.subnets().stream()
393 .findFirst()
394 .orElse(null);
395 if (subnet == null) {
396 log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id());
397 return null;
398 }
399
400 Set<CordServiceId> tServices = Sets.newHashSet();
401 // TODO get tenant services from XOS
402
403 Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet)
404 .stream()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800405 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800406
407 return new CordService(vNet, subnet, hosts, tServices);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800408 }
409
410 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800411 * Returns CordService by OpenStack network.
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800412 *
413 * @param vNet OpenStack network
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800414 * @return cord service
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800415 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800416 private CordService getCordService(OpenstackNetwork vNet) {
417 checkNotNull(vNet);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800418
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800419 CordServiceId serviceId = CordServiceId.of(vNet.id());
420 OpenstackSubnet subnet = vNet.subnets().stream()
421 .findFirst()
422 .orElse(null);
423 if (subnet == null) {
424 log.warn("Couldn't find OpenStack subnet for service {}", serviceId);
425 return null;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800426 }
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800427
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800428 Set<CordServiceId> tServices = Sets.newHashSet();
429 // TODO get tenant services from XOS
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800430
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800431 Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet)
432 .stream()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800433 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800434
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800435 return new CordService(vNet, subnet, hosts, tServices);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800436 }
437
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800438 /**
439 * Returns IP address for tunneling for a given host.
440 *
441 * @param host host
Hyunsun Moon6d247342016-02-12 12:48:47 -0800442 * @return ip address, or null
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800443 */
444 private IpAddress getTunnelIp(Host host) {
Hyunsun Moon6d247342016-02-12 12:48:47 -0800445 String ip = host.annotations().value(DATA_PLANE_IP);
446 return ip == null ? null : IpAddress.valueOf(ip);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800447 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700448
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800449 /**
450 * Returns port name.
451 *
452 * @param port port
453 * @return port name
454 */
455 private String getPortName(Port port) {
456 return port.annotations().value("portName");
457 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800458
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800459 /**
Hyunsun Moon6d247342016-02-12 12:48:47 -0800460 * Returns s-tag from a given OpenStack port.
461 *
462 * @param vPort openstack port
463 * @return s-tag string
464 */
465 private String getServiceVlan(OpenstackPort vPort) {
466 checkNotNull(vPort);
467
468 if (vPort.name() != null && vPort.name().startsWith(S_TAG)) {
469 return vPort.name().split("-")[1];
470 } else {
471 return null;
472 }
473 }
474
475 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800476 * Returns service ID of this host.
477 *
478 * @param host host
479 * @return service id, or null if not found
480 */
481 private String getServiceId(Host host) {
482 return host.annotations().value(SERVICE_ID);
483 }
484
485 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800486 * Returns hosts associated with a given OpenStack network.
487 *
488 * @param vNet openstack network
489 * @return set of hosts
490 */
491 private Set<Host> getHostsWithOpenstackNetwork(OpenstackNetwork vNet) {
492 checkNotNull(vNet);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700493
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800494 String vNetId = vNet.id();
495 return StreamSupport.stream(hostService.getHosts().spliterator(), false)
496 .filter(host -> Objects.equals(vNetId, getServiceId(host)))
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800497 .collect(Collectors.toSet());
Hyunsun Moon6d247342016-02-12 12:48:47 -0800498 }
499
500 /**
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800501 * Registers static DHCP lease for a given host.
502 *
503 * @param host host
504 * @param service cord service
505 */
506 private void registerDhcpLease(Host host, CordService service) {
507 List<Ip4Address> options = Lists.newArrayList();
508 options.add(Ip4Address.makeMaskPrefix(service.serviceIpRange().prefixLength()));
509 options.add(service.serviceIp().getIp4Address());
510 options.add(service.serviceIp().getIp4Address());
511 options.add(DEFAULT_DNS);
512
513 log.debug("Set static DHCP mapping for {}", host.mac());
514 dhcpService.setStaticMapping(host.mac(),
515 host.ipAddresses().stream().findFirst().get().getIp4Address(),
516 true,
517 options);
518 }
519
520 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800521 * Handles VM detected situation.
522 *
523 * @param host host
524 */
525 private void serviceVmAdded(Host host) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800526 String serviceVlan = host.annotations().value(S_TAG);
527 if (serviceVlan != null) {
528 virtualSubscriberGatewayAdded(host, serviceVlan);
529 }
530
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800531 String vNetId = host.annotations().value(SERVICE_ID);
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800532 if (vNetId == null) {
Hyunsun Moon2a225162016-02-17 19:00:50 -0800533 // ignore this host, it is not the service VM, or it's a vSG
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800534 return;
535 }
536
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800537 OpenstackNetwork vNet = openstackService.network(vNetId);
538 if (vNet == null) {
539 log.warn("Failed to get OpenStack network {} for VM {}({}).",
Hyunsun Moon98025542016-03-08 04:36:02 -0800540 vNetId, host.id(),
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800541 host.annotations().value(OPENSTACK_VM_ID));
542 return;
543 }
544
545 log.info("VM {} is detected, MAC: {} IP: {}",
546 host.annotations().value(OPENSTACK_VM_ID),
547 host.mac(),
548 host.ipAddresses().stream().findFirst().get());
549
550 CordService service = getCordService(vNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800551 if (service == null) {
552 return;
553 }
554
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800555 switch (service.serviceType()) {
556 case MANAGEMENT:
557 ruleInstaller.populateManagementNetworkRules(host, service);
558 break;
559 case PRIVATE:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800560 arpProxy.addGateway(service.serviceIp(), privateGatewayMac);
Hyunsun Moon098cda82016-03-03 13:27:44 -0800561 case PUBLIC:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800562 default:
563 // TODO check if the service needs an update on its group buckets after done CORD-433
564 ruleInstaller.updateServiceGroup(service);
565 // sends gratuitous ARP here for the case of adding existing VMs
566 // when ONOS or cordvtn app is restarted
567 arpProxy.sendGratuitousArpForGateway(service.serviceIp(), Sets.newHashSet(host));
568 break;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800569 }
570
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800571 registerDhcpLease(host, service);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800572 ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800573 }
574
575 /**
576 * Handles VM removed situation.
577 *
578 * @param host host
579 */
580 private void serviceVmRemoved(Host host) {
Hyunsun Moon98025542016-03-08 04:36:02 -0800581 String serviceVlan = host.annotations().value(S_TAG);
582 if (serviceVlan != null) {
583 virtualSubscriberGatewayRemoved(host);
584 }
585
Hyunsun Moon2a225162016-02-17 19:00:50 -0800586 String vNetId = host.annotations().value(SERVICE_ID);
587 if (vNetId == null) {
588 // ignore it, it's not the service VM or it's a vSG
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800589 return;
590 }
591
Hyunsun Moon6d247342016-02-12 12:48:47 -0800592 OpenstackNetwork vNet = openstackService.network(vNetId);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800593 if (vNet == null) {
594 log.warn("Failed to get OpenStack network {} for VM {}({}).",
Hyunsun Moon98025542016-03-08 04:36:02 -0800595 vNetId, host.id(),
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800596 host.annotations().value(OPENSTACK_VM_ID));
597 return;
598 }
599
600 log.info("VM {} is vanished, MAC: {} IP: {}",
601 host.annotations().value(OPENSTACK_VM_ID),
602 host.mac(),
603 host.ipAddresses().stream().findFirst().get());
604
605 ruleInstaller.removeBasicConnectionRules(host);
Hyunsun Moonfb631b42016-02-03 14:44:06 -0800606 dhcpService.removeStaticMapping(host.mac());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800607
608 CordService service = getCordService(vNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800609 if (service == null) {
610 return;
611 }
612
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800613 switch (service.serviceType()) {
614 case MANAGEMENT:
615 ruleInstaller.removeManagementNetworkRules(host, service);
616 break;
617 case PRIVATE:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800618 if (getHostsWithOpenstackNetwork(vNet).isEmpty()) {
619 arpProxy.removeGateway(service.serviceIp());
620 }
Hyunsun Moon098cda82016-03-03 13:27:44 -0800621 case PUBLIC:
Hyunsun Moon1e5caeb2016-03-01 16:36:23 -0800622 default:
623 // TODO check if the service needs an update on its group buckets after done CORD-433
624 ruleInstaller.updateServiceGroup(service);
625 break;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700626 }
627 }
628
Hyunsun Moon98025542016-03-08 04:36:02 -0800629
630 /**
631 * Handles virtual subscriber gateway VM or container.
632 *
633 * @param host new host with stag, it can be vsg VM or vsg
634 * @param serviceVlan service vlan
635 */
636 private void virtualSubscriberGatewayAdded(Host host, String serviceVlan) {
637 Map<IpAddress, MacAddress> vSgs;
638 Host vSgHost;
639
640 String vSgHostId = host.annotations().value(VSG_HOST_ID);
641 if (vSgHostId == null) {
642 log.debug("vSG VM detected {}", host.id());
643
644 vSgHost = host;
645 vSgs = getSubscriberGateways(vSgHost);
646 vSgs.entrySet().stream().forEach(entry -> addVirtualSubscriberGateway(
647 vSgHost,
648 entry.getKey(),
649 entry.getValue(),
650 serviceVlan));
651 } else {
652 vSgHost = hostService.getHost(HostId.hostId(vSgHostId));
653 if (vSgHost == null) {
654 return;
655 }
656
657 log.debug("vSG detected {}", host.id());
658 vSgs = getSubscriberGateways(vSgHost);
659 }
660
661 ruleInstaller.populateSubscriberGatewayRules(vSgHost, vSgs.keySet());
662 }
663
664 /**
665 * Handles virtual subscriber gateway removed.
666 *
667 * @param vSg vsg host to remove
668 */
669 private void virtualSubscriberGatewayRemoved(Host vSg) {
670 String vSgHostId = vSg.annotations().value(VSG_HOST_ID);
671 if (vSgHostId == null) {
672 return;
673 }
674
675 Host vSgHost = hostService.getHost(HostId.hostId(vSgHostId));
676 if (vSgHost == null) {
677 return;
678 }
679
680 log.info("vSG removed {}", vSg.id());
681 Map<IpAddress, MacAddress> vSgs = getSubscriberGateways(vSgHost);
682 ruleInstaller.populateSubscriberGatewayRules(vSgHost, vSgs.keySet());
683 }
684
Hyunsun Moon746956f2016-01-24 21:47:06 -0800685 /**
686 * Sets service network gateway MAC address and sends out gratuitous ARP to all
687 * VMs to update the gateway MAC address.
688 *
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800689 * @param newMac mac address to update
Hyunsun Moon746956f2016-01-24 21:47:06 -0800690 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800691 private void setPrivateGatewayMac(MacAddress newMac) {
692 if (newMac == null || newMac.equals(privateGatewayMac)) {
693 // no updates, do nothing
694 return;
Hyunsun Moon746956f2016-01-24 21:47:06 -0800695 }
696
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800697 privateGatewayMac = newMac;
698 log.debug("Set service gateway MAC address to {}", privateGatewayMac.toString());
699
Hyunsun Moon746956f2016-01-24 21:47:06 -0800700 // TODO get existing service list from XOS and replace the loop below
701 Set<String> vNets = Sets.newHashSet();
702 hostService.getHosts().forEach(host -> vNets.add(host.annotations().value(SERVICE_ID)));
703 vNets.remove(null);
704
705 vNets.stream().forEach(vNet -> {
706 CordService service = getCordService(CordServiceId.of(vNet));
707 if (service != null) {
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800708 arpProxy.addGateway(service.serviceIp(), privateGatewayMac);
709 arpProxy.sendGratuitousArpForGateway(service.serviceIp(), service.hosts().keySet());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800710 }
711 });
712 }
713
714 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800715 * Sets public gateway MAC address.
716 *
717 * @param publicGateways gateway ip and mac address pairs
718 */
719 private void setPublicGatewayMac(Map<IpAddress, MacAddress> publicGateways) {
720 publicGateways.entrySet()
721 .stream()
722 .forEach(entry -> {
723 arpProxy.addGateway(entry.getKey(), entry.getValue());
724 log.debug("Added public gateway IP {}, MAC {}",
725 entry.getKey().toString(), entry.getValue().toString());
726 });
727 // TODO notice gateway MAC change to VMs holds this gateway IP
728 }
729
730 /**
Hyunsun Moon746956f2016-01-24 21:47:06 -0800731 * Updates configurations.
732 */
733 private void readConfiguration() {
734 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
735 if (config == null) {
736 log.debug("No configuration found");
737 return;
738 }
739
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800740 setPrivateGatewayMac(config.privateGatewayMac());
741 setPublicGatewayMac(config.publicGateways());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800742 }
743
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700744 private class InternalHostListener implements HostListener {
745
746 @Override
747 public void event(HostEvent event) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800748 Host host = event.subject();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700749
750 switch (event.type()) {
751 case HOST_ADDED:
Hyunsun Moon98025542016-03-08 04:36:02 -0800752 if (mastershipService.isLocalMaster(host.location().deviceId())) {
753 eventExecutor.submit(() -> serviceVmAdded(host));
754 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700755 break;
756 case HOST_REMOVED:
Hyunsun Moon98025542016-03-08 04:36:02 -0800757 if (mastershipService.isLocalMaster(host.location().deviceId())) {
758 eventExecutor.submit(() -> serviceVmRemoved(host));
759 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700760 break;
761 default:
762 break;
763 }
764 }
765 }
766
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800767 private class InternalPacketProcessor implements PacketProcessor {
768
769 @Override
770 public void process(PacketContext context) {
771 if (context.isHandled()) {
772 return;
773 }
774
775 Ethernet ethPacket = context.inPacket().parsed();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800776 if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800777 return;
778 }
779
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800780 arpProxy.processArpPacket(context, ethPacket);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800781 }
782 }
783
784 private class InternalConfigListener implements NetworkConfigListener {
785
786 @Override
787 public void event(NetworkConfigEvent event) {
788 if (!event.configClass().equals(CordVtnConfig.class)) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800789 return;
790 }
791
Hyunsun Moon746956f2016-01-24 21:47:06 -0800792 switch (event.type()) {
793 case CONFIG_ADDED:
794 case CONFIG_UPDATED:
795 log.info("Network configuration changed");
796 eventExecutor.execute(CordVtn.this::readConfiguration);
797 break;
798 default:
799 break;
800 }
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800801 }
802 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700803}