use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class DistributedDhcpStore method removeStaticIP.
@Override
public boolean removeStaticIP(MacAddress macID) {
HostId host = HostId.hostId(macID);
if (allocationMap.containsKey(host)) {
IpAssignment assignment = allocationMap.get(host).value();
if (assignment.assignmentStatus().equals(Option_RangeNotEnforced)) {
allocationMap.remove(host);
return true;
}
Ip4Address freeIP = assignment.ipAddress();
if (assignment.leasePeriod() < 0) {
allocationMap.remove(host);
if (ipWithinRange(freeIP)) {
freeIPPool.add(freeIP);
}
return true;
}
}
return false;
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class DistributedDhcpStore method assignIP.
@Override
public boolean assignIP(HostId hostId, IpAssignment ipAssignment) {
log.trace("Assign IP Called HostId: {}, ipAssignment: {}", hostId, ipAssignment);
IpAssignment newAssignment = null;
Versioned<IpAssignment> versionedAssignment = allocationMap.get(hostId);
Ip4Address requestedIp = ipAssignment.ipAddress();
if (versionedAssignment == null) {
// dynamic assignment is done in suggestIP
if (ipAssignment.assignmentStatus().equals(Option_RangeNotEnforced)) {
newAssignment = ipAssignment;
} else if (freeIPPool.remove(requestedIp)) {
newAssignment = IpAssignment.builder(ipAssignment).assignmentStatus(Option_Assigned).timestamp(new Date()).build();
} else {
log.trace("Failed to assign IP for {}", ipAssignment);
return false;
}
log.trace("Assigned {}", newAssignment);
return allocationMap.putIfAbsent(hostId, newAssignment) == null;
// TODO: handle the case where map changed.
} else {
// this is lease renew or rebinding
// update assignment status and time stamp, and keep the others
IpAssignment existingAssignment = versionedAssignment.value();
if (!existingAssignment.ipAddress().equals(requestedIp)) {
// return false if existing assignment is not for the
// requested host
log.trace("Failed to assign IP for {}", ipAssignment);
return false;
}
switch(existingAssignment.assignmentStatus()) {
case Option_RangeNotEnforced:
newAssignment = IpAssignment.builder(existingAssignment).timestamp(new Date()).build();
break;
case Option_Expired:
if (!freeIPPool.remove(requestedIp)) {
// requested IP is expired for this host and reserved to the other host
return false;
}
case Option_Assigned:
case Option_Requested:
newAssignment = IpAssignment.builder(existingAssignment).timestamp(new Date()).assignmentStatus(Option_Assigned).build();
break;
default:
break;
}
log.trace("Assigned {}", newAssignment);
return allocationMap.replace(hostId, versionedAssignment.version(), newAssignment);
}
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class DhcpWebResource method listAvailableIPs.
/**
* Get all available IPs.
* Shows all the IPs in the free pool of the DHCP Server.
*
* @onos.rsModel DhcpConfigGetAvailable
* @return 200 OK
*/
@GET
@Path("available")
public Response listAvailableIPs() {
Iterable<Ip4Address> availableIPList = service.getAvailableIPs();
ObjectNode root = mapper().createObjectNode();
ArrayNode arrayNode = root.putArray("availableIP");
availableIPList.forEach(i -> arrayNode.add(i.toString()));
return ok(root).build();
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class DhcpSetStaticMapping method doExecute.
@Override
protected void doExecute() {
DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class);
try {
MacAddress macID = MacAddress.valueOf(macAddr);
Ip4Address ipAddress = Ip4Address.valueOf(ipAddr);
IpAssignment ipAssignment = IpAssignment.builder().ipAddress(ipAddress).leasePeriod(dhcpService.getLeaseTime()).timestamp(new Date()).assignmentStatus(Option_Requested).build();
if (dhcpService.setStaticMapping(macID, ipAssignment)) {
print(DHCP_SUCCESS);
} else {
print(DHCP_FAILURE);
}
} catch (IllegalArgumentException e) {
print(e.getMessage());
}
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class FreeIpCompleter method complete.
@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
// Delegate string completer
StringsCompleter delegate = new StringsCompleter();
DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class);
Iterator<Ip4Address> it = dhcpService.getAvailableIPs().iterator();
SortedSet<String> strings = delegate.getStrings();
while (it.hasNext()) {
strings.add(it.next().toString());
}
// Now let the completer do the work for figuring out what to offer.
return delegate.complete(session, commandLine, candidates);
}
Aggregations