use of org.onosproject.net.HostId in project onos by opennetworkinglab.
the class Dhcp6HandlerImpl method removeHostOrRoute.
/**
* remove host or route and update dhcp relay record attributes.
*
* @param directConnFlag flag to show that packet is from directly connected client
* @param location client side connect point
* @param dhcp6Packet the dhcp6 payload
* @param clientPacket client's ethernet packet
* @param clientIpv6 client's Ipv6 packet
* @param clientInterface client interfaces
*/
private void removeHostOrRoute(boolean directConnFlag, ConnectPoint location, DHCP6 dhcp6Packet, Ethernet clientPacket, IPv6 clientIpv6, Interface clientInterface) {
log.debug("removeHostOrRoute enters {}", dhcp6Packet);
VlanId vlanId = clientInterface.vlan();
// could be gw or host
MacAddress srcMac = clientPacket.getSourceMAC();
MacAddress leafClientMac;
byte leafMsgType;
log.debug("client mac {} client vlan {}", HexString.toHexString(srcMac.toBytes(), ":"), vlanId);
Dhcp6ClientIdOption clientIdOption = Dhcp6HandlerUtil.extractClientId(directConnFlag, dhcp6Packet);
if (clientIdOption != null) {
if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) || (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) {
leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress());
} else {
log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created.");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL);
return;
}
} else {
log.warn("CLIENTID option NOT found. Don't create DhcpRelay Record.");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL);
return;
}
HostId leafHostId = HostId.hostId(leafClientMac, vlanId);
DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null);
if (record == null) {
record = new DhcpRecord(leafHostId);
} else {
record = record.clone();
}
Boolean isMsgRelease = Dhcp6HandlerUtil.isDhcp6Release(dhcp6Packet);
IpAddressInfo ipInfo;
PdPrefixInfo pdInfo = null;
if (directConnFlag) {
// Add to host store if it is connected to network directly
ipInfo = extractIpAddress(dhcp6Packet);
if (ipInfo != null) {
if (isMsgRelease) {
HostId hostId = HostId.hostId(srcMac, vlanId);
log.debug("remove Host {} ip for directly connected.", hostId.toString());
providerService.removeIpFromHost(hostId, ipInfo.ip6Address);
}
} else {
log.debug("ipAddress not found. Do not remove Host {} for directly connected.", HostId.hostId(srcMac, vlanId).toString());
}
leafMsgType = dhcp6Packet.getMsgType();
} else {
// Remove from route store if it is not connected to network directly
// pick out the first link-local ip address
IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId);
if (nextHopIp == null) {
log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId);
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW);
return;
}
DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Packet);
ipInfo = extractIpAddress(leafDhcp);
if (ipInfo == null) {
log.debug("ip is null");
} else {
if (isMsgRelease) {
Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp);
log.debug("removing route of 128 address for indirectly connected.");
log.debug("128 ip {}, nexthop {}", HexString.toHexString(ipInfo.ip6Address.toOctets(), ":"), HexString.toHexString(nextHopIp.toOctets(), ":"));
routeStore.removeRoute(routeForIP);
}
}
pdInfo = extractPrefix(leafDhcp);
if (pdInfo == null) {
log.debug("ipPrefix is null ");
} else {
if (isMsgRelease) {
Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp);
log.debug("removing route of PD for indirectly connected.");
log.debug("pd ip {}, nexthop {}", HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":"), HexString.toHexString(nextHopIp.toOctets(), ":"));
routeStore.removeRoute(routeForPrefix);
if (this.dhcpFpmEnabled) {
dhcpFpmPrefixStore.removeFpmRecord(pdInfo.pdPrefix);
}
}
}
leafMsgType = leafDhcp.getMsgType();
}
if (isMsgRelease) {
log.debug("DHCP6 RELEASE msg.");
if (record != null) {
if (ipInfo != null) {
log.debug("DhcpRelay Record ip6Address is set to null.");
record.ip6Address(null);
}
if (pdInfo != null) {
log.debug("DhcpRelay Record pdPrefix is set to null.");
}
if (!record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) {
log.warn("IP6 address and IP6 PD both are null. Remove record.");
// do not remove a record. Let timer task handler it.
// dhcpRelayStore.removeDhcpRecord(HostId.hostId(leafClientMac, vlanId));
}
}
}
if (record != null) {
record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
record.addLocation(new HostLocation(location, System.currentTimeMillis()));
record.ip6Status(DHCP6.MsgType.getType(leafMsgType));
record.setDirectlyConnected(directConnFlag);
if (!directConnFlag) {
// Update gateway mac address if the host is not directly connected
record.nextHop(srcMac);
}
record.updateLastSeen();
}
dhcpRelayStore.updateDhcpRecord(leafHostId, record);
/*
// TODO Use AtomicInteger for the counters
try {
recordSemaphore.acquire();
try {
dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
} finally {
// calling release() after a successful acquire()
recordSemaphore.release();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
*/
}
use of org.onosproject.net.HostId in project onos by opennetworkinglab.
the class Dhcp6HandlerImpl method findNextHopIp6FromRelayStore.
public Ip6Address findNextHopIp6FromRelayStore(Ip6Address clientAddress) {
DhcpRecord dr = getDhcpRelayRecordFor(clientAddress);
if (dr != null) {
Optional<MacAddress> nextHopMac = dr.nextHop();
if (nextHopMac.isPresent()) {
// find the local ip6 from the host store
HostId gwHostId = HostId.hostId(nextHopMac.get(), dr.vlanId());
Host gwHost = hostService.getHost(gwHostId);
if (gwHost == null) {
log.warn("Can't find next hop host ID {}", gwHostId);
return null;
}
Ip6Address nextHopIp = gwHost.ipAddresses().stream().filter(IpAddress::isIp6).filter(IpAddress::isLinkLocal).map(IpAddress::getIp6Address).findFirst().orElse(null);
log.info("findNextHopIp6FromRelayStore " + clientAddress + " got mac " + nextHopMac.toString() + " ip6 " + nextHopIp);
return nextHopIp;
}
} else {
log.warn("findNextHopIp6FromRelayStore could NOT find next hop for " + clientAddress);
return null;
}
return null;
}
use of org.onosproject.net.HostId 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.onosproject.net.HostId in project onos by opennetworkinglab.
the class DhcpWebResource method setMapping.
/**
* Post a new static MAC/IP binding.
* Registers a static binding to the DHCP server, and displays the current set of bindings.
*
* @onos.rsModel DhcpConfigPut
* @param stream JSON stream
* @return 200 OK
*/
@POST
@Path("mappings")
@Consumes(MediaType.APPLICATION_JSON)
public Response setMapping(InputStream stream) {
ObjectNode root = mapper().createObjectNode();
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
JsonNode macID = jsonTree.get("mac");
JsonNode ip = jsonTree.get("ip");
if (macID != null && ip != null) {
IpAssignment ipAssignment = IpAssignment.builder().ipAddress(Ip4Address.valueOf(ip.asText())).leasePeriod(service.getLeaseTime()).timestamp(new Date()).assignmentStatus(Option_Requested).build();
if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()), ipAssignment)) {
throw new IllegalArgumentException("Static Mapping Failed. " + "The IP maybe unavailable.");
}
}
final Map<HostId, IpAssignment> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode().put("host", i.getKey().toString()).put("ip", i.getValue().ipAddress().toString())));
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage());
}
return ok(root).build();
}
use of org.onosproject.net.HostId in project onos by opennetworkinglab.
the class DhcpListAllMappings method doExecute.
@Override
protected void doExecute() {
DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class);
Map<HostId, IpAssignment> allocationMap = dhcpService.listMapping();
for (Map.Entry<HostId, IpAssignment> entry : allocationMap.entrySet()) {
print(DHCP_MAPPING_FORMAT, entry.getKey().toString(), entry.getValue().ipAddress().toString());
}
}
Aggregations