Search in sources :

Example 16 with HostId

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();
        }
        */
}
Also used : DhcpRecord(org.onosproject.dhcprelay.store.DhcpRecord) MacAddress(org.onlab.packet.MacAddress) HostId(org.onosproject.net.HostId) HostLocation(org.onosproject.net.HostLocation) IpAddress(org.onlab.packet.IpAddress) DHCP6(org.onlab.packet.DHCP6) VlanId(org.onlab.packet.VlanId) Route(org.onosproject.routeservice.Route) Dhcp6ClientIdOption(org.onlab.packet.dhcp.Dhcp6ClientIdOption)

Example 17 with HostId

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;
}
Also used : Ip6Address(org.onlab.packet.Ip6Address) DhcpRecord(org.onosproject.dhcprelay.store.DhcpRecord) Host(org.onosproject.net.Host) IpAddress(org.onlab.packet.IpAddress) MacAddress(org.onlab.packet.MacAddress) HostId(org.onosproject.net.HostId)

Example 18 with HostId

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;
}
Also used : IpAssignment(org.onosproject.dhcp.IpAssignment) Ip4Address(org.onlab.packet.Ip4Address) HostId(org.onosproject.net.HostId)

Example 19 with HostId

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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IpAssignment(org.onosproject.dhcp.IpAssignment) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException) HostId(org.onosproject.net.HostId) Date(java.util.Date) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 20 with HostId

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());
    }
}
Also used : IpAssignment(org.onosproject.dhcp.IpAssignment) DhcpService(org.onosproject.dhcp.DhcpService) HostId(org.onosproject.net.HostId) Map(java.util.Map)

Aggregations

HostId (org.onosproject.net.HostId)86 IpAddress (org.onlab.packet.IpAddress)27 DeviceId (org.onosproject.net.DeviceId)24 VlanId (org.onlab.packet.VlanId)23 HostLocation (org.onosproject.net.HostLocation)23 MacAddress (org.onlab.packet.MacAddress)21 Test (org.junit.Test)18 ConnectPoint (org.onosproject.net.ConnectPoint)17 Host (org.onosproject.net.Host)17 HostDescription (org.onosproject.net.host.HostDescription)16 DefaultHostDescription (org.onosproject.net.host.DefaultHostDescription)14 Set (java.util.Set)13 DhcpRecord (org.onosproject.dhcprelay.store.DhcpRecord)12 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)11 HashSet (java.util.HashSet)11 Logger (org.slf4j.Logger)11 ImmutableSet (com.google.common.collect.ImmutableSet)10 Device (org.onosproject.net.Device)10 Path (org.onosproject.net.Path)10 LoggerFactory (org.slf4j.LoggerFactory)10