Hyunsun Moon
Committed by Gerrit Code Review

Added static DHCP lease for a new host

Change-Id: Ib3c614bb008d65accec3570ff526f541b1332382
...@@ -45,7 +45,8 @@ ...@@ -45,7 +45,8 @@
45 <api.package>org.onosproject.cordvtn.rest</api.package> 45 <api.package>org.onosproject.cordvtn.rest</api.package>
46 <onos.app.requires> 46 <onos.app.requires>
47 org.onosproject.ovsdb-base, 47 org.onosproject.ovsdb-base,
48 - org.onosproject.openstackswitching 48 + org.onosproject.openstackswitching,
49 + org.onosproject.dhcp
49 </onos.app.requires> 50 </onos.app.requires>
50 </properties> 51 </properties>
51 52
...@@ -110,6 +111,11 @@ ...@@ -110,6 +111,11 @@
110 <artifactId>onos-app-openstackswitching-api</artifactId> 111 <artifactId>onos-app-openstackswitching-api</artifactId>
111 <version>${project.version}</version> 112 <version>${project.version}</version>
112 </dependency> 113 </dependency>
114 + <dependency>
115 + <groupId>org.onosproject</groupId>
116 + <artifactId>onos-app-dhcp-api</artifactId>
117 + <version>${project.version}</version>
118 + </dependency>
113 </dependencies> 119 </dependencies>
114 120
115 <build> 121 <build>
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.cordvtn; 16 package org.onosproject.cordvtn;
17 17
18 +import com.google.common.collect.Lists;
18 import com.google.common.collect.Sets; 19 import com.google.common.collect.Sets;
19 import org.apache.felix.scr.annotations.Activate; 20 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 21 import org.apache.felix.scr.annotations.Component;
...@@ -23,11 +24,13 @@ import org.apache.felix.scr.annotations.Reference; ...@@ -23,11 +24,13 @@ import org.apache.felix.scr.annotations.Reference;
23 import org.apache.felix.scr.annotations.ReferenceCardinality; 24 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 import org.apache.felix.scr.annotations.Service; 25 import org.apache.felix.scr.annotations.Service;
25 import org.onlab.packet.Ethernet; 26 import org.onlab.packet.Ethernet;
27 +import org.onlab.packet.Ip4Address;
26 import org.onlab.packet.IpAddress; 28 import org.onlab.packet.IpAddress;
27 import org.onlab.packet.MacAddress; 29 import org.onlab.packet.MacAddress;
28 import org.onlab.packet.VlanId; 30 import org.onlab.packet.VlanId;
29 import org.onosproject.core.ApplicationId; 31 import org.onosproject.core.ApplicationId;
30 import org.onosproject.core.CoreService; 32 import org.onosproject.core.CoreService;
33 +import org.onosproject.dhcp.DhcpService;
31 import org.onosproject.mastership.MastershipService; 34 import org.onosproject.mastership.MastershipService;
32 import org.onosproject.net.ConnectPoint; 35 import org.onosproject.net.ConnectPoint;
33 import org.onosproject.net.DefaultAnnotations; 36 import org.onosproject.net.DefaultAnnotations;
...@@ -65,6 +68,7 @@ import org.onosproject.openstackswitching.OpenstackSubnet; ...@@ -65,6 +68,7 @@ import org.onosproject.openstackswitching.OpenstackSubnet;
65 import org.onosproject.openstackswitching.OpenstackSwitchingService; 68 import org.onosproject.openstackswitching.OpenstackSwitchingService;
66 import org.slf4j.Logger; 69 import org.slf4j.Logger;
67 70
71 +import java.util.List;
68 import java.util.Map; 72 import java.util.Map;
69 import java.util.Set; 73 import java.util.Set;
70 import java.util.concurrent.ExecutorService; 74 import java.util.concurrent.ExecutorService;
...@@ -121,6 +125,9 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro ...@@ -121,6 +125,9 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected OpenstackSwitchingService openstackService; 126 protected OpenstackSwitchingService openstackService;
123 127
128 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 + protected DhcpService dhcpService;
130 +
124 private final ConfigFactory configFactory = 131 private final ConfigFactory configFactory =
125 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") { 132 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
126 @Override 133 @Override
...@@ -130,6 +137,7 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro ...@@ -130,6 +137,7 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro
130 }; 137 };
131 138
132 private static final String DEFAULT_TUNNEL = "vxlan"; 139 private static final String DEFAULT_TUNNEL = "vxlan";
140 + private static final Ip4Address DEFAULT_DNS = Ip4Address.valueOf("8.8.8.8");
133 private static final String SERVICE_ID = "serviceId"; 141 private static final String SERVICE_ID = "serviceId";
134 private static final String LOCATION_IP = "locationIp"; 142 private static final String LOCATION_IP = "locationIp";
135 private static final String OPENSTACK_VM_ID = "openstackVmId"; 143 private static final String OPENSTACK_VM_ID = "openstackVmId";
...@@ -244,9 +252,13 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro ...@@ -244,9 +252,13 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro
244 if (host != null) { 252 if (host != null) {
245 // Host is already known to the system, no HOST_ADDED event is triggered in this case. 253 // Host is already known to the system, no HOST_ADDED event is triggered in this case.
246 // It happens when the application is restarted. 254 // It happens when the application is restarted.
247 - // TODO check host description if it has all the information 255 + String vmId = host.annotations().value(OPENSTACK_VM_ID);
248 - serviceVmAdded(host); 256 + if (vmId != null && vmId.equals(vPort.deviceId())) {
249 - return; 257 + serviceVmAdded(host);
258 + return;
259 + } else {
260 + hostProvider.hostVanished(host.id());
261 + }
250 } 262 }
251 263
252 Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values()); 264 Set<IpAddress> ip = Sets.newHashSet(vPort.fixedIps().values());
...@@ -382,6 +394,26 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro ...@@ -382,6 +394,26 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro
382 } 394 }
383 395
384 /** 396 /**
397 + * 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 + /**
385 * Handles VM detected situation. 417 * Handles VM detected situation.
386 * 418 *
387 * @param host host 419 * @param host host
...@@ -420,6 +452,7 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro ...@@ -420,6 +452,7 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro
420 } 452 }
421 453
422 ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet); 454 ruleInstaller.populateBasicConnectionRules(host, getTunnelIp(host), vNet);
455 + registerDhcpLease(host, service);
423 } 456 }
424 457
425 /** 458 /**
...@@ -428,6 +461,11 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro ...@@ -428,6 +461,11 @@ public class CordVtn extends AbstractProvider implements CordVtnService, HostPro
428 * @param host host 461 * @param host host
429 */ 462 */
430 private void serviceVmRemoved(Host host) { 463 private void serviceVmRemoved(Host host) {
464 + if (host.annotations().value(OPENSTACK_VM_ID) == null) {
465 + // this host was not injected from CordVtn, just return
466 + return;
467 + }
468 +
431 String vNetId = host.annotations().value(SERVICE_ID); 469 String vNetId = host.annotations().value(SERVICE_ID);
432 OpenstackNetwork vNet = openstackService.network(host.annotations().value(SERVICE_ID)); 470 OpenstackNetwork vNet = openstackService.network(host.annotations().value(SERVICE_ID));
433 if (vNet == null) { 471 if (vNet == null) {
......