Search in sources :

Example 1 with LabelResource

use of org.onosproject.incubator.net.resource.label.LabelResource in project onos by opennetworkinglab.

the class DistributedLabelResourceStore method internalApply.

private Collection<LabelResource> internalApply(LabelResourceRequest request) {
    DeviceId deviceId = request.deviceId();
    long applyNum = request.applyNum();
    Versioned<LabelResourcePool> poolOld = resourcePool.get(deviceId);
    if (poolOld == null) {
        log.info("label resource pool not allocated for deviceId {}.", deviceId);
        return Collections.emptyList();
    }
    LabelResourcePool pool = poolOld.value();
    Collection<LabelResource> result = new HashSet<>();
    long freeNum = this.getFreeNumOfDevicePool(deviceId);
    if (applyNum > freeNum) {
        log.info("the free number of the label resource pool of deviceId {} is not enough.");
        return Collections.emptyList();
    }
    Set<LabelResource> releaseLabels = new HashSet<>(pool.releaseLabelId());
    long tmp = releaseLabels.size() > applyNum ? applyNum : releaseLabels.size();
    LabelResource resource = null;
    for (int i = 0; i < tmp; i++) {
        Iterator<LabelResource> it = releaseLabels.iterator();
        if (it.hasNext()) {
            resource = it.next();
            releaseLabels.remove(resource);
        }
        result.add(resource);
    }
    for (long j = pool.currentUsedMaxLabelId().labelId(); j < pool.currentUsedMaxLabelId().labelId() + applyNum - tmp; j++) {
        resource = new DefaultLabelResource(deviceId, LabelResourceId.labelResourceId(j));
        result.add(resource);
    }
    long beginLabel = pool.beginLabel().labelId();
    long endLabel = pool.endLabel().labelId();
    long totalNum = pool.totalNum();
    long current = pool.currentUsedMaxLabelId().labelId() + applyNum - tmp;
    long usedNum = pool.usedNum() + applyNum;
    ImmutableSet<LabelResource> freeLabel = ImmutableSet.copyOf(releaseLabels);
    LabelResourcePool newPool = new LabelResourcePool(deviceId.toString(), beginLabel, endLabel, totalNum, usedNum, current, freeLabel);
    resourcePool.put(deviceId, newPool);
    log.info("success to apply label resource");
    return result;
}
Also used : DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) LabelResource(org.onosproject.incubator.net.resource.label.LabelResource) DeviceId(org.onosproject.net.DeviceId) LabelResourcePool(org.onosproject.incubator.net.resource.label.LabelResourcePool) DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) HashSet(java.util.HashSet)

Example 2 with LabelResource

use of org.onosproject.incubator.net.resource.label.LabelResource in project onos by opennetworkinglab.

the class DistributedLabelResourceStore method releaseToDevicePool.

@Override
public boolean releaseToDevicePool(Multimap<DeviceId, LabelResource> release) {
    Map<DeviceId, Collection<LabelResource>> maps = release.asMap();
    Set<DeviceId> deviceIdSet = maps.keySet();
    LabelResourceRequest request = null;
    for (Iterator<DeviceId> it = deviceIdSet.iterator(); it.hasNext(); ) {
        DeviceId deviceId = it.next();
        Device device = deviceService.getDevice(deviceId);
        if (device == null) {
            continue;
        }
        ImmutableSet<LabelResource> collection = ImmutableSet.copyOf(maps.get(deviceId));
        request = new LabelResourceRequest(deviceId, LabelResourceRequest.Type.RELEASE, 0, collection);
        NodeId master = mastershipService.getMasterFor(deviceId);
        if (master == null) {
            log.warn("Failed to releaseToDevicePool: No master for {}", deviceId);
            return false;
        }
        if (master.equals(clusterService.getLocalNode().id())) {
            return internalRelease(request);
        }
        log.trace("Forwarding request to {}, which is the primary (master) for device {}", master, deviceId);
        return complete(clusterCommunicator.sendAndReceive(request, LabelResourceMessageSubjects.LABEL_POOL_RELEASE, SERIALIZER::encode, SERIALIZER::decode, master));
    }
    return false;
}
Also used : DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) LabelResource(org.onosproject.incubator.net.resource.label.LabelResource) DeviceId(org.onosproject.net.DeviceId) LabelResourceRequest(org.onosproject.incubator.net.resource.label.LabelResourceRequest) Device(org.onosproject.net.Device) NodeId(org.onosproject.cluster.NodeId) Collection(java.util.Collection)

Example 3 with LabelResource

use of org.onosproject.incubator.net.resource.label.LabelResource in project onos by opennetworkinglab.

the class LabelReleaseCommand method doExecute.

@Override
protected void doExecute() {
    LabelResourceService lrs = get(LabelResourceService.class);
    Multimap<DeviceId, LabelResource> map = ArrayListMultimap.create();
    String[] labelIds = releaseLabelIds.split(",");
    DefaultLabelResource resource = null;
    for (int i = 0; i < labelIds.length; i++) {
        resource = new DefaultLabelResource(DeviceId.deviceId(deviceId), LabelResourceId.labelResourceId(Long.parseLong(labelIds[i])));
        map.put(DeviceId.deviceId(deviceId), resource);
    }
    lrs.releaseToDevicePool(map);
}
Also used : DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) LabelResource(org.onosproject.incubator.net.resource.label.LabelResource) LabelResourceService(org.onosproject.incubator.net.resource.label.LabelResourceService) DeviceId(org.onosproject.net.DeviceId) DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource)

Example 4 with LabelResource

use of org.onosproject.incubator.net.resource.label.LabelResource in project onos by opennetworkinglab.

the class DistributedLabelResourceStore method internalRelease.

private boolean internalRelease(LabelResourceRequest request) {
    DeviceId deviceId = request.deviceId();
    Collection<LabelResource> release = request.releaseCollection();
    Versioned<LabelResourcePool> poolOld = resourcePool.get(deviceId);
    if (poolOld == null) {
        log.info("the label resource pool of device id {} not allocated");
        return false;
    }
    LabelResourcePool pool = poolOld.value();
    if (pool == null) {
        log.info("the label resource pool of device id {} does not exist");
        return false;
    }
    Set<LabelResource> storeSet = new HashSet<>(pool.releaseLabelId());
    LabelResource labelResource = null;
    long realReleasedNum = 0;
    for (Iterator<LabelResource> it = release.iterator(); it.hasNext(); ) {
        labelResource = it.next();
        if (labelResource.labelResourceId().labelId() < pool.beginLabel().labelId() || labelResource.labelResourceId().labelId() > pool.endLabel().labelId()) {
            continue;
        }
        if (pool.currentUsedMaxLabelId().labelId() > labelResource.labelResourceId().labelId() || !storeSet.contains(labelResource)) {
            storeSet.add(labelResource);
            realReleasedNum++;
        }
    }
    long beginNum = pool.beginLabel().labelId();
    long endNum = pool.endLabel().labelId();
    long totalNum = pool.totalNum();
    long usedNum = pool.usedNum() - realReleasedNum;
    long current = pool.currentUsedMaxLabelId().labelId();
    ImmutableSet<LabelResource> s = ImmutableSet.copyOf(storeSet);
    LabelResourcePool newPool = new LabelResourcePool(deviceId.toString(), beginNum, endNum, totalNum, usedNum, current, s);
    resourcePool.put(deviceId, newPool);
    log.info("success to release label resource");
    return true;
}
Also used : DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) LabelResource(org.onosproject.incubator.net.resource.label.LabelResource) DeviceId(org.onosproject.net.DeviceId) LabelResourcePool(org.onosproject.incubator.net.resource.label.LabelResourcePool) HashSet(java.util.HashSet)

Example 5 with LabelResource

use of org.onosproject.incubator.net.resource.label.LabelResource in project onos by opennetworkinglab.

the class DistributedLabelResourceStore method releaseToGlobalPool.

@Override
public boolean releaseToGlobalPool(Set<LabelResourceId> release) {
    Set<LabelResource> set = new HashSet<>();
    DefaultLabelResource resource = null;
    for (LabelResourceId labelResource : release) {
        resource = new DefaultLabelResource(DeviceId.deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID), labelResource);
        set.add(resource);
    }
    LabelResourceRequest request = new LabelResourceRequest(DeviceId.deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID), LabelResourceRequest.Type.RELEASE, 0, ImmutableSet.copyOf(set));
    return this.internalRelease(request);
}
Also used : DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) LabelResource(org.onosproject.incubator.net.resource.label.LabelResource) LabelResourceRequest(org.onosproject.incubator.net.resource.label.LabelResourceRequest) DefaultLabelResource(org.onosproject.incubator.net.resource.label.DefaultLabelResource) LabelResourceId(org.onosproject.incubator.net.resource.label.LabelResourceId) HashSet(java.util.HashSet)

Aggregations

DefaultLabelResource (org.onosproject.incubator.net.resource.label.DefaultLabelResource)7 LabelResource (org.onosproject.incubator.net.resource.label.LabelResource)7 DeviceId (org.onosproject.net.DeviceId)4 HashSet (java.util.HashSet)3 LabelResourceService (org.onosproject.incubator.net.resource.label.LabelResourceService)3 LabelResourcePool (org.onosproject.incubator.net.resource.label.LabelResourcePool)2 LabelResourceRequest (org.onosproject.incubator.net.resource.label.LabelResourceRequest)2 Collection (java.util.Collection)1 NodeId (org.onosproject.cluster.NodeId)1 LabelResourceId (org.onosproject.incubator.net.resource.label.LabelResourceId)1 Device (org.onosproject.net.Device)1