blob: 4c0bf728935228503612a862dbd1d2be96a23c8f [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 Moon1f145552015-10-08 22:25:30 -070019import com.google.common.collect.Sets;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -080026import org.onlab.packet.Ethernet;
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080027import org.onlab.packet.Ip4Address;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080028import org.onlab.packet.IpAddress;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080029import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
Hyunsun Moon1f145552015-10-08 22:25:30 -070031import org.onosproject.core.ApplicationId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070032import org.onosproject.core.CoreService;
Hyunsun Moon1d3eac92016-02-03 00:11:11 -080033import org.onosproject.dhcp.DhcpService;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080034import org.onosproject.mastership.MastershipService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080035import org.onosproject.net.ConnectPoint;
Hyunsun Moond772f342015-10-28 20:28:16 -070036import org.onosproject.net.DefaultAnnotations;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070037import org.onosproject.net.Host;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080038import org.onosproject.net.HostId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080039import org.onosproject.net.HostLocation;
Hyunsun Moon8539b042015-11-07 22:08:43 -080040import org.onosproject.net.Port;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080041import org.onosproject.net.SparseAnnotations;
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;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080065import org.onosproject.openstackswitching.OpenstackNetwork;
66import org.onosproject.openstackswitching.OpenstackPort;
Hyunsun Moonc71231d2015-12-16 20:53:23 -080067import org.onosproject.openstackswitching.OpenstackSubnet;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080068import org.onosproject.openstackswitching.OpenstackSwitchingService;
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 Moon9f0814b2015-11-04 17:34:35 -080073import java.util.Set;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070074import java.util.concurrent.ExecutorService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080075import java.util.stream.Collectors;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070076
Hyunsun Moon1f145552015-10-08 22:25:30 -070077import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon746956f2016-01-24 21:47:06 -080078import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070079import static org.onlab.util.Tools.groupedThreads;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070080import static org.slf4j.LoggerFactory.getLogger;
81
82/**
Hyunsun Moon9f0814b2015-11-04 17:34:35 -080083 * Provisions virtual tenant networks with service chaining capability
84 * in OpenStack environment.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070085 */
86@Component(immediate = true)
87@Service
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080088public class CordVtn extends AbstractProvider implements CordVtnService, HostProvider {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070089
90 protected final Logger log = getLogger(getClass());
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected CoreService coreService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon746956f2016-01-24 21:47:06 -080096 protected NetworkConfigRegistry configRegistry;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected NetworkConfigService configService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800102 protected HostProviderRegistry hostProviderRegistry;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700105 protected DeviceService deviceService;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected HostService hostService;
109
Hyunsun Moon1f145552015-10-08 22:25:30 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moond772f342015-10-28 20:28:16 -0700111 protected DriverService driverService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800114 protected FlowRuleService flowRuleService;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800117 protected PacketService packetService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800120 protected MastershipService mastershipService;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected GroupService groupService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800126 protected OpenstackSwitchingService openstackService;
127
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected DhcpService dhcpService;
130
Hyunsun Moon746956f2016-01-24 21:47:06 -0800131 private final ConfigFactory configFactory =
132 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
133 @Override
134 public CordVtnConfig createConfig() {
135 return new CordVtnConfig();
136 }
137 };
138
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800139 private static final String DEFAULT_TUNNEL = "vxlan";
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800140 private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8");
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800141 private static final String SERVICE_ID = "serviceId";
142 private static final String LOCATION_IP = "locationIp";
143 private static final String OPENSTACK_VM_ID = "openstackVmId";
144
Hyunsun Moon746956f2016-01-24 21:47:06 -0800145 private final ExecutorService eventExecutor =
146 newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler"));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700147
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800148 private final PacketProcessor packetProcessor = new InternalPacketProcessor();
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800149 private final HostListener hostListener = new InternalHostListener();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800150 private final NetworkConfigListener configListener = new InternalConfigListener();
Hyunsun Moon2b530322015-09-23 13:24:35 -0700151
Hyunsun Moon746956f2016-01-24 21:47:06 -0800152 private ApplicationId appId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800153 private HostProviderService hostProvider;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800154 private CordVtnRuleInstaller ruleInstaller;
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800155 private CordVtnArpProxy arpProxy;
Hyunsun Moon746956f2016-01-24 21:47:06 -0800156 private volatile MacAddress gatewayMac = MacAddress.NONE;
Hyunsun Moon8539b042015-11-07 22:08:43 -0800157
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800158 /**
159 * Creates an cordvtn host location provider.
160 */
161 public CordVtn() {
162 super(new ProviderId("host", CORDVTN_APP_ID));
Hyunsun Moon8539b042015-11-07 22:08:43 -0800163 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700164
165 @Activate
166 protected void activate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800167 appId = coreService.registerApplication("org.onosproject.cordvtn");
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800168 ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService,
169 deviceService,
170 driverService,
171 groupService,
172 mastershipService,
173 DEFAULT_TUNNEL);
174
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800175 arpProxy = new CordVtnArpProxy(appId, packetService);
176 packetService.addProcessor(packetProcessor, PacketProcessor.director(0));
177 arpProxy.requestPacket();
178
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700179 hostService.addListener(hostListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800180 hostProvider = hostProviderRegistry.register(this);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700181
Hyunsun Moon746956f2016-01-24 21:47:06 -0800182 configRegistry.registerConfigFactory(configFactory);
183 configService.addListener(configListener);
184 readConfiguration();
185
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700186 log.info("Started");
187 }
188
189 @Deactivate
190 protected void deactivate() {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800191 hostProviderRegistry.unregister(this);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700192 hostService.removeListener(hostListener);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800193
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800194 packetService.removeProcessor(packetProcessor);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700195
Hyunsun Moon746956f2016-01-24 21:47:06 -0800196 configRegistry.unregisterConfigFactory(configFactory);
197 configService.removeListener(configListener);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700198
Hyunsun Moon746956f2016-01-24 21:47:06 -0800199 eventExecutor.shutdown();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700200 log.info("Stopped");
201 }
202
203 @Override
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800204 public void triggerProbe(Host host) {
205 /*
206 * Note: In CORD deployment, we assume that all hosts are configured.
207 * Therefore no probe is required.
208 */
Hyunsun Moonb219fc42016-01-14 03:42:47 -0800209 }
210
211 @Override
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800212 public void createServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) {
213 CordService tService = getCordService(tServiceId);
214 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800215
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800216 if (tService == null || pService == null) {
217 log.error("Failed to create CordService for {}", tServiceId.id());
218 return;
219 }
220
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800221 log.info("Service dependency from {} to {} created.", tService.id().id(), pService.id().id());
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800222 ruleInstaller.populateServiceDependencyRules(tService, pService);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800223 }
224
225 @Override
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800226 public void removeServiceDependency(CordServiceId tServiceId, CordServiceId pServiceId) {
227 CordService tService = getCordService(tServiceId);
228 CordService pService = getCordService(pServiceId);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800229
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800230 if (tService == null || pService == null) {
231 log.error("Failed to create CordService for {}", tServiceId.id());
232 return;
233 }
234
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800235 log.info("Service dependency from {} to {} removed.", tService.id().id(), pService.id().id());
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800236 ruleInstaller.removeServiceDependencyRules(tService, pService);
Hyunsun Moon699f46b2015-12-04 11:35:25 -0800237 }
238
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800239 @Override
240 public void addServiceVm(CordVtnNode node, ConnectPoint connectPoint) {
241 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
242 OpenstackPort vPort = openstackService.port(port);
243 if (vPort == null) {
244 log.warn("Failed to get OpenstackPort for {}", getPortName(port));
Hyunsun Moon8539b042015-11-07 22:08:43 -0800245 return;
246 }
247
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800248 MacAddress mac = vPort.macAddress();
249 HostId hostId = HostId.hostId(mac);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800250
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800251 Host host = hostService.getHost(hostId);
252 if (host != null) {
253 // Host is already known to the system, no HOST_ADDED event is triggered in this case.
254 // It happens when the application is restarted.
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800255 String vmId = host.annotations().value(OPENSTACK_VM_ID);
256 if (vmId != null && vmId.equals(vPort.deviceId())) {
257 serviceVmAdded(host);
258 return;
259 } else {
260 hostProvider.hostVanished(host.id());
261 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800262 }
263
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800264 Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values());
265 SparseAnnotations annotations = DefaultAnnotations.builder()
266 .set(OPENSTACK_VM_ID, vPort.deviceId())
267 .set(SERVICE_ID, vPort.networkId())
268 .set(LOCATION_IP, node.localIp().toString())
269 .build();
270
271 HostDescription hostDesc = new DefaultHostDescription(
272 mac,
273 VlanId.NONE,
274 new HostLocation(connectPoint, System.currentTimeMillis()),
275 ip,
276 annotations);
277
278 hostProvider.hostDetected(hostId, hostDesc, false);
Hyunsun Moon8539b042015-11-07 22:08:43 -0800279 }
280
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800281 @Override
282 public void removeServiceVm(ConnectPoint connectPoint) {
283 Host host = hostService.getConnectedHosts(connectPoint)
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800284 .stream()
285 .findFirst()
286 .orElse(null);
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800287
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800288 if (host == null) {
289 log.debug("No host is connected on {}", connectPoint.toString());
290 return;
291 }
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800292
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800293 hostProvider.hostVanished(host.id());
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800294 }
295
296 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800297 * Returns CordService by service ID.
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800298 *
299 * @param serviceId service id
300 * @return cord service, or null if it fails to get network from OpenStack
301 */
302 private CordService getCordService(CordServiceId serviceId) {
303 OpenstackNetwork vNet = openstackService.network(serviceId.id());
304 if (vNet == null) {
305 log.warn("Couldn't find OpenStack network for service {}", serviceId.id());
306 return null;
307 }
308
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800309 OpenstackSubnet subnet = vNet.subnets().stream()
310 .findFirst()
311 .orElse(null);
312 if (subnet == null) {
313 log.warn("Couldn't find OpenStack subnet for service {}", serviceId.id());
314 return null;
315 }
316
317 Set<CordServiceId> tServices = Sets.newHashSet();
318 // TODO get tenant services from XOS
319
320 Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet)
321 .stream()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800322 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800323
324 return new CordService(vNet, subnet, hosts, tServices);
Hyunsun Moonbfc47d12015-12-07 14:06:28 -0800325 }
326
327 /**
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800328 * Returns CordService by OpenStack network.
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800329 *
330 * @param vNet OpenStack network
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800331 * @return cord service
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800332 */
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800333 private CordService getCordService(OpenstackNetwork vNet) {
334 checkNotNull(vNet);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800335
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800336 CordServiceId serviceId = CordServiceId.of(vNet.id());
337 OpenstackSubnet subnet = vNet.subnets().stream()
338 .findFirst()
339 .orElse(null);
340 if (subnet == null) {
341 log.warn("Couldn't find OpenStack subnet for service {}", serviceId);
342 return null;
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800343 }
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800344
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800345 Set<CordServiceId> tServices = Sets.newHashSet();
346 // TODO get tenant services from XOS
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800347
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800348 Map<Host, IpAddress> hosts = getHostsWithOpenstackNetwork(vNet)
349 .stream()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800350 .collect(Collectors.toMap(host -> host, this::getTunnelIp));
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800351
Hyunsun Moonc71231d2015-12-16 20:53:23 -0800352 return new CordService(vNet, subnet, hosts, tServices);
Hyunsun Moon9f0814b2015-11-04 17:34:35 -0800353 }
354
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800355 /**
356 * Returns IP address for tunneling for a given host.
357 *
358 * @param host host
359 * @return ip address
360 */
361 private IpAddress getTunnelIp(Host host) {
362 return IpAddress.valueOf(host.annotations().value(LOCATION_IP));
363 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700364
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800365 /**
366 * Returns port name.
367 *
368 * @param port port
369 * @return port name
370 */
371 private String getPortName(Port port) {
372 return port.annotations().value("portName");
373 }
Hyunsun Moon8539b042015-11-07 22:08:43 -0800374
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800375 /**
376 * Returns hosts associated with a given OpenStack network.
377 *
378 * @param vNet openstack network
379 * @return set of hosts
380 */
381 private Set<Host> getHostsWithOpenstackNetwork(OpenstackNetwork vNet) {
382 checkNotNull(vNet);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700383
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800384 Set<Host> hosts = openstackService.ports(vNet.id()).stream()
385 .filter(port -> port.deviceOwner().contains("compute"))
386 .map(port -> hostService.getHostsByMac(port.macAddress())
387 .stream()
388 .findFirst()
389 .orElse(null))
390 .collect(Collectors.toSet());
391
392 hosts.remove(null);
393 return hosts;
394 }
395
396 /**
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800397 * Registers static DHCP lease for a given host.
398 *
399 * @param host host
400 * @param service cord service
401 */
402 private void registerDhcpLease(Host host, CordService service) {
403 List<Ip4Address> options = Lists.newArrayList();
404 options.add(Ip4Address.makeMaskPrefix(service.serviceIpRange().prefixLength()));
405 options.add(service.serviceIp().getIp4Address());
406 options.add(service.serviceIp().getIp4Address());
407 options.add(DEFAULT_DNS);
408
409 log.debug("Set static DHCP mapping for {}", host.mac());
410 dhcpService.setStaticMapping(host.mac(),
411 host.ipAddresses().stream().findFirst().get().getIp4Address(),
412 true,
413 options);
414 }
415
416 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800417 * Handles VM detected situation.
418 *
419 * @param host host
420 */
421 private void serviceVmAdded(Host host) {
422 String vNetId = host.annotations().value(SERVICE_ID);
423 OpenstackNetwork vNet = openstackService.network(vNetId);
424 if (vNet == null) {
425 log.warn("Failed to get OpenStack network {} for VM {}({}).",
426 vNetId,
427 host.id(),
428 host.annotations().value(OPENSTACK_VM_ID));
429 return;
430 }
431
432 log.info("VM {} is detected, MAC: {} IP: {}",
433 host.annotations().value(OPENSTACK_VM_ID),
434 host.mac(),
435 host.ipAddresses().stream().findFirst().get());
436
437 CordService service = getCordService(vNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800438 if (service == null) {
439 return;
440 }
441
442 if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) {
443 ruleInstaller.populateManagementNetworkRules(host, service);
444 } else {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800445 // TODO check if the service needs an update on its group buckets after done CORD-433
446 ruleInstaller.updateServiceGroup(service);
447 arpProxy.addServiceIp(service.serviceIp());
Hyunsun Moon746956f2016-01-24 21:47:06 -0800448
449 // sends gratuitous ARP here for the case of adding existing VMs
450 // when ONOS or cordvtn app is restarted
451 arpProxy.sendGratuitousArp(service.serviceIp(), gatewayMac, Sets.newHashSet(host));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800452 }
453
454 ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet);
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800455 registerDhcpLease(host, service);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800456 }
457
458 /**
459 * Handles VM removed situation.
460 *
461 * @param host host
462 */
463 private void serviceVmRemoved(Host host) {
Hyunsun Moon1d3eac92016-02-03 00:11:11 -0800464 if (host.annotations().value(OPENSTACK_VM_ID) == null) {
465 // this host was not injected from CordVtn, just return
466 return;
467 }
468
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800469 String vNetId = host.annotations().value(SERVICE_ID);
470 OpenstackNetwork vNet = openstackService.network(host.annotations().value(SERVICE_ID));
471 if (vNet == null) {
472 log.warn("Failed to get OpenStack network {} for VM {}({}).",
473 vNetId,
474 host.id(),
475 host.annotations().value(OPENSTACK_VM_ID));
476 return;
477 }
478
479 log.info("VM {} is vanished, MAC: {} IP: {}",
480 host.annotations().value(OPENSTACK_VM_ID),
481 host.mac(),
482 host.ipAddresses().stream().findFirst().get());
483
484 ruleInstaller.removeBasicConnectionRules(host);
485
486 CordService service = getCordService(vNet);
Hyunsun Moond52bffc2016-01-29 18:57:05 -0800487 if (service == null) {
488 return;
489 }
490
491 if (service.serviceType().equals(CordService.ServiceType.MANAGEMENT)) {
492 ruleInstaller.removeManagementNetworkRules(host, service);
493 } else {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800494 // TODO check if the service needs an update on its group buckets after done CORD-433
495 ruleInstaller.updateServiceGroup(service);
496
497 if (getHostsWithOpenstackNetwork(vNet).isEmpty()) {
498 arpProxy.removeServiceIp(service.serviceIp());
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700499 }
500 }
501 }
502
Hyunsun Moon746956f2016-01-24 21:47:06 -0800503 /**
504 * Sets service network gateway MAC address and sends out gratuitous ARP to all
505 * VMs to update the gateway MAC address.
506 *
507 * @param mac mac address
508 */
509 private void setServiceGatewayMac(MacAddress mac) {
510 if (mac != null && !mac.equals(gatewayMac)) {
511 gatewayMac = mac;
512 log.debug("Set service gateway MAC address to {}", gatewayMac.toString());
513 }
514
515 // TODO get existing service list from XOS and replace the loop below
516 Set<String> vNets = Sets.newHashSet();
517 hostService.getHosts().forEach(host -> vNets.add(host.annotations().value(SERVICE_ID)));
518 vNets.remove(null);
519
520 vNets.stream().forEach(vNet -> {
521 CordService service = getCordService(CordServiceId.of(vNet));
522 if (service != null) {
523 arpProxy.sendGratuitousArp(
524 service.serviceIp(),
525 gatewayMac,
526 service.hosts().keySet());
527 }
528 });
529 }
530
531 /**
532 * Updates configurations.
533 */
534 private void readConfiguration() {
535 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
536 if (config == null) {
537 log.debug("No configuration found");
538 return;
539 }
540
541 setServiceGatewayMac(config.gatewayMac());
542 }
543
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700544 private class InternalHostListener implements HostListener {
545
546 @Override
547 public void event(HostEvent event) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800548 Host host = event.subject();
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700549
550 switch (event.type()) {
551 case HOST_ADDED:
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800552 eventExecutor.submit(() -> serviceVmAdded(host));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700553 break;
554 case HOST_REMOVED:
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800555 eventExecutor.submit(() -> serviceVmRemoved(host));
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700556 break;
557 default:
558 break;
559 }
560 }
561 }
562
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800563 private class InternalPacketProcessor implements PacketProcessor {
564
565 @Override
566 public void process(PacketContext context) {
567 if (context.isHandled()) {
568 return;
569 }
570
571 Ethernet ethPacket = context.inPacket().parsed();
Hyunsun Moon746956f2016-01-24 21:47:06 -0800572 if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800573 return;
574 }
575
Hyunsun Moon746956f2016-01-24 21:47:06 -0800576 arpProxy.processArpPacket(context, ethPacket, gatewayMac);
577 }
578 }
579
580 private class InternalConfigListener implements NetworkConfigListener {
581
582 @Override
583 public void event(NetworkConfigEvent event) {
584 if (!event.configClass().equals(CordVtnConfig.class)) {
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800585 return;
586 }
587
Hyunsun Moon746956f2016-01-24 21:47:06 -0800588 switch (event.type()) {
589 case CONFIG_ADDED:
590 case CONFIG_UPDATED:
591 log.info("Network configuration changed");
592 eventExecutor.execute(CordVtn.this::readConfiguration);
593 break;
594 default:
595 break;
596 }
Hyunsun Moon42c7b4e2016-01-11 15:30:42 -0800597 }
598 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700599}