samanwita pal
Committed by Gerrit Code Review

ONOS-2926 Remove IP instead of host when the IP mapping is released

Change-Id: Ifea3366ce8a18ea068e615636b3069e769221c0e
......@@ -65,7 +65,7 @@ public interface DhcpStore {
*
* @param hostId the host ID for which the mapping needs to be changed
*/
void releaseIP(HostId hostId);
Ip4Address releaseIP(HostId hostId);
/**
* Returns a collection of all the MacAddress to IPAddress mapping assigned to the hosts.
......
......@@ -481,7 +481,10 @@ public class DhcpManager implements DhcpService {
}
}
} else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) {
dhcpStore.releaseIP(hostId);
Ip4Address ip4Address = dhcpStore.releaseIP(hostId);
if (ip4Address != null) {
hostProviderService.removeIpFromHost(hostId, ip4Address);
}
}
}
}
......@@ -666,9 +669,10 @@ public class DhcpManager implements DhcpService {
if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) &&
(ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) {
dhcpStore.releaseIP(entry.getKey());
// TODO remove only the IP from the host entry when the API is in place.
hostProviderService.hostVanished(entry.getKey());
Ip4Address ip4Address = dhcpStore.releaseIP(entry.getKey());
if (ip4Address != null) {
hostProviderService.removeIpFromHost(entry.getKey(), ipAssignment.ipAddress());
}
}
}
timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
......
......@@ -212,7 +212,7 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
public void releaseIP(HostId hostId) {
public Ip4Address releaseIP(HostId hostId) {
if (allocationMap.containsKey(hostId)) {
IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(hostId).value())
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired)
......@@ -222,7 +222,9 @@ public class DistributedDhcpStore implements DhcpStore {
if (ipWithinRange(freeIP)) {
freeIPPool.add(freeIP);
}
return freeIP;
}
return null;
}
@Override
......
......@@ -25,6 +25,7 @@ import org.onlab.packet.DHCPPacketType;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.UDP;
import org.onosproject.core.CoreServiceAdapter;
......@@ -234,7 +235,8 @@ public class DhcpManagerTest {
public void setDefaultTimeoutForPurge(int timeInSeconds) {
}
public void releaseIP(HostId hostId) {
public Ip4Address releaseIP(HostId hostId) {
return null;
}
public Map<HostId, IpAssignment> listAssignedMapping() {
......@@ -338,6 +340,11 @@ public class DhcpManagerTest {
public void hostVanished(HostId hostId) {
}
@Override
public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
}
}
/**
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net.host;
import org.onlab.packet.IpAddress;
import org.onosproject.net.HostId;
import org.onosproject.net.provider.ProviderService;
......@@ -52,4 +53,11 @@ public interface HostProviderService extends ProviderService<HostProvider> {
*/
void hostVanished(HostId hostId);
/**
* Notifies the core when a host is no longer detected on a network.
*
* @param hostId id of the host that vanished
*/
void removeIpFromHost(HostId hostId, IpAddress ipAddress);
}
......
......@@ -55,6 +55,15 @@ public interface HostStore extends Store<HostEvent, HostStoreDelegate> {
HostEvent removeHost(HostId hostId);
/**
* Removes the specified ip from the host entry.
*
* @param hostId host identification
* @param ipAddress ipAddress to be removed
* @return remove event or null if host was not found
*/
HostEvent removeIp(HostId hostId, IpAddress ipAddress);
/**
* Returns the number of hosts in the store.
*
* @return host count
......
......@@ -160,6 +160,11 @@ public class SimpleHostStore
}
@Override
public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
return null;
}
@Override
public int getHostCount() {
return hosts.size();
}
......
......@@ -236,6 +236,16 @@ public class HostManager
post(event);
}
}
@Override
public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
checkNotNull(hostId, HOST_ID_NULL);
checkValidity();
HostEvent event = store.removeIp(hostId, ipAddress);
if (event != null) {
post(event);
}
}
}
// Store delegate to re-post events emitted from the store.
......
......@@ -27,6 +27,7 @@ import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.RE
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
......@@ -197,6 +198,34 @@ public class ECHostStore
}
@Override
public HostEvent removeIp(HostId hostId, IpAddress ipAddress) {
DefaultHost host = hosts.compute(hostId, (id, existingHost) -> {
if (existingHost != null) {
checkState(Objects.equals(hostId.mac(), existingHost.mac()),
"Existing and new MAC addresses differ.");
checkState(Objects.equals(hostId.vlanId(), existingHost.vlan()),
"Existing and new VLANs differ.");
Set<IpAddress> addresses = new HashSet<>(existingHost.ipAddresses());
if (addresses != null && addresses.contains(ipAddress)) {
addresses.remove(ipAddress);
return new DefaultHost(existingHost.providerId(),
hostId,
existingHost.mac(),
existingHost.vlan(),
existingHost.location(),
ImmutableSet.copyOf(addresses),
existingHost.annotations());
} else {
return existingHost;
}
}
return null;
});
return host != null ? new HostEvent(HOST_UPDATED, host) : null;
}
@Override
public int getHostCount() {
return hosts.size();
}
......
......@@ -259,6 +259,10 @@ public class HostLocationProviderTest {
removeCount++;
}
@Override
public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
}
}
private class TestPacketService extends PacketServiceAdapter {
......
......@@ -24,6 +24,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
......@@ -159,6 +160,11 @@ public class OvsdbHostProviderTest {
removeCount++;
}
@Override
public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
}
}
private class OvsdbControllerTest implements OvsdbController {
......