Search in sources :

Example 1 with Resource

use of org.onosproject.net.resource.Resource in project onos by opennetworkinglab.

the class OpticalOduIntentCompiler method compile.

@Override
public List<Intent> compile(OpticalOduIntent intent, List<Intent> installable) {
    // Check if ports are OduClt ports
    ConnectPoint src = intent.getSrc();
    ConnectPoint dst = intent.getDst();
    Port srcPort = deviceService.getPort(src.deviceId(), src.port());
    Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
    checkArgument(srcPort instanceof OduCltPort);
    checkArgument(dstPort instanceof OduCltPort);
    log.debug("Compiling optical ODU intent between {} and {}", src, dst);
    // Release of intent resources here is only a temporary solution for handling the
    // case of recompiling due to intent restoration (when intent state is FAILED).
    // TODO: try to release intent resources in IntentManager.
    resourceService.release(intent.key());
    // Check OduClt ports availability
    Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
    Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
    // If ports are not available, compilation fails
    if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
        throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
    }
    List<Resource> intentResources = new ArrayList<>();
    intentResources.add(srcPortResource);
    intentResources.add(dstPortResource);
    // Calculate available light paths
    Set<Path> paths = getOpticalPaths(intent);
    if (paths.isEmpty()) {
        throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
    }
    // Use first path that can be successfully reserved
    for (Path path : paths) {
        // Find available Tributary Slots on both directions of path
        Map<LinkKey, Set<TributarySlot>> slotsMap = findAvailableTributarySlots(intent, path);
        if (slotsMap.isEmpty()) {
            continue;
        }
        List<Resource> tributarySlotResources = convertToResources(slotsMap);
        if (!tributarySlotResources.stream().allMatch(resourceService::isAvailable)) {
            continue;
        }
        intentResources.addAll(tributarySlotResources);
        allocateResources(intent, intentResources);
        List<FlowRule> rules = new LinkedList<>();
        // Create rules for forward and reverse path
        rules = createRules(intent, intent.getSrc(), intent.getDst(), path, slotsMap, false);
        if (intent.isBidirectional()) {
            rules.addAll(createRules(intent, intent.getDst(), intent.getSrc(), path, slotsMap, true));
        }
        return Collections.singletonList(new FlowRuleIntent(appId, intent.key(), rules, ImmutableSet.copyOf(path.links()), PathIntent.ProtectionType.PRIMARY, intent.resourceGroup()));
    }
    throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
}
Also used : Path(org.onosproject.net.Path) LinkKey(org.onosproject.net.LinkKey) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Port(org.onosproject.net.Port) OduCltPort(org.onosproject.net.optical.OduCltPort) OtuPort(org.onosproject.net.optical.OtuPort) OduCltPort(org.onosproject.net.optical.OduCltPort) Resource(org.onosproject.net.resource.Resource) ArrayList(java.util.ArrayList) ConnectPoint(org.onosproject.net.ConnectPoint) LinkedList(java.util.LinkedList) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) FlowRuleIntent(org.onosproject.net.intent.FlowRuleIntent)

Example 2 with Resource

use of org.onosproject.net.resource.Resource in project onos by opennetworkinglab.

the class OpticalCircuitIntentCompiler method compile.

private List<Intent> compile(OpticalCircuitIntent intent, ConnectPoint src, ConnectPoint dst, Optional<OpticalConnectivityIntent> existingConnectivity, List<Resource> resources, boolean supportsMultiplexing) {
    OpticalConnectivityIntent connectivityIntent;
    List<Resource> required;
    if (existingConnectivity.isPresent()) {
        connectivityIntent = existingConnectivity.get();
        required = resources;
    } else {
        // Find OCh ports with available resources
        Pair<OchPort, OchPort> ochPorts = findPorts(intent.getSrc(), intent.getDst(), intent.getSignalType());
        if (ochPorts == null) {
            throw new OpticalIntentCompilationException("Unable to find suitable OCH ports for intent " + intent);
        }
        ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
        ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
        // Create optical connectivity intent
        // In this case the channel and path optionally specified in this circuit intent
        // are used to create the connectivity intent
        connectivityIntent = OpticalConnectivityIntent.builder().appId(appId).src(srcCP).dst(dstCP).priority(OPTICAL_CONNECTIVITY_INTENT_PRIORITY).signalType(ochPorts.getLeft().signalType()).bidirectional(intent.isBidirectional()).ochSignal(intent.ochSignal()).suggestedPath(intent.suggestedPath()).resourceGroup(intent.resourceGroup()).build();
        if (!supportsMultiplexing) {
            required = resources;
        } else {
            List<Resource> slots = availableSlotResources(srcCP, dstCP, intent.getSignalType());
            if (slots.isEmpty()) {
                throw new OpticalIntentCompilationException("Unable to find Tributary Slots for intent " + intent);
            }
            required = ImmutableList.<Resource>builder().addAll(resources).addAll(slots).build();
        }
    }
    if (resourceService.allocate(intent.key(), required).isEmpty()) {
        throw new OpticalIntentCompilationException("Unable to allocate resources for intent " + intent + ": resources=" + required);
    }
    intentService.submit(connectivityIntent);
    // Save circuit to connectivity intent mapping
    intentSetMultimap.allocateMapping(connectivityIntent.id(), intent.id());
    FlowRuleIntent circuitIntent = createFlowRule(intent, connectivityIntent, required.stream().flatMap(x -> Tools.stream(x.valueAs(TributarySlot.class))).collect(Collectors.toSet()));
    return ImmutableList.of(circuitIntent);
}
Also used : Resource(org.onosproject.net.resource.Resource) OpticalConnectivityIntent(org.onosproject.net.intent.OpticalConnectivityIntent) ConnectPoint(org.onosproject.net.ConnectPoint) OchPort(org.onosproject.net.optical.OchPort) FlowRuleIntent(org.onosproject.net.intent.FlowRuleIntent)

Example 3 with Resource

use of org.onosproject.net.resource.Resource in project onos by opennetworkinglab.

the class ResourcesCommand method printResource.

private void printResource(Resource resource, int level) {
    // workaround to preserve the original behavior of ResourceService#getRegisteredResources
    Set<Resource> children;
    if (resource instanceof DiscreteResource) {
        children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
    } else {
        children = Collections.emptySet();
    }
    if (resource.equals(Resource.ROOT)) {
        print("ROOT");
    } else {
        String resourceName = resource.simpleTypeName();
        if (resource instanceof ContinuousResource) {
            if (availablesOnly) {
                // Get the total resource
                double total = ((ContinuousResource) resource).value();
                // Get allocated resource
                double allocated = resourceService.getResourceAllocations(resource.id()).stream().mapToDouble(rA -> ((ContinuousResource) rA.resource()).value()).sum();
                // Difference
                double difference = total - allocated;
                print("%s%s: %f", Strings.repeat(" ", level), resourceName, difference);
            } else {
                print("%s%s: %f", Strings.repeat(" ", level), resourceName, ((ContinuousResource) resource).value());
            }
            // Continuous resource is terminal node, stop here
            return;
        } else {
            String availability = "";
            if (availablesOnly && !children.isEmpty()) {
                // intermediate nodes cannot be omitted, print availability
                if (resourceService.isAvailable(resource)) {
                    availability = " ✔";
                } else {
                    availability = " ✘";
                }
            }
            String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
            if (toString.startsWith(resourceName)) {
                print("%s%s%s", Strings.repeat(" ", level), toString, availability);
            } else {
                print("%s%s: %s%s", Strings.repeat(" ", level), resourceName, toString, availability);
            }
        }
    }
    // Classify children into aggregatable terminal resources and everything else
    Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder().add(VlanId.class).add(MplsLabel.class).build();
    // (last() resource name) -> { Resource }
    Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
    List<Resource> nonAggregatable = new ArrayList<>();
    for (Resource r : children) {
        if (!isPrintTarget(r)) {
            // A
            continue;
        }
        if (r instanceof DiscreteResource) {
            if (resourceService.getRegisteredResources(((DiscreteResource) r).id()).isEmpty()) {
                // resource which has children should be printed
                continue;
            }
        }
        if (r instanceof ContinuousResource) {
            // non-aggregatable terminal node
            nonAggregatable.add(r);
        } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
            // aggregatable & terminal node
            String simpleName = r.simpleTypeName();
            aggregatables.put(simpleName, r);
        } else {
            nonAggregatable.add(r);
        }
    }
    // print aggregated (terminal)
    aggregatables.asMap().entrySet().forEach(e -> {
        // for each type...
        String resourceName = e.getKey();
        RangeSet<Long> rangeSet = TreeRangeSet.create();
        // aggregate into RangeSet
        e.getValue().stream().map(res -> {
            if (res.isTypeOf(VlanId.class)) {
                return (long) res.valueAs(VlanId.class).get().toShort();
            } else if (res.isTypeOf(MplsLabel.class)) {
                return (long) res.valueAs(MplsLabel.class).get().toInt();
            } else if (res.isTypeOf(TributarySlot.class)) {
                return res.valueAs(TributarySlot.class).get().index();
            }
            // TODO support Lambda (OchSignal types)
            return 0L;
        }).map(Range::singleton).map(range -> range.canonical(DiscreteDomain.longs())).forEach(rangeSet::add);
        print("%s%s: %s", Strings.repeat(" ", level + 1), resourceName, rangeSet);
    });
    // print non-aggregatables (recurse)
    if (sort) {
        nonAggregatable.stream().sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))).forEach(r -> printResource(r, level + 1));
    } else {
        nonAggregatable.forEach(r -> printResource(r, level + 1));
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) Iterables(com.google.common.collect.Iterables) RangeSet(com.google.common.collect.RangeSet) PortNumber(org.onosproject.net.PortNumber) Multimap(com.google.common.collect.Multimap) TreeRangeSet(com.google.common.collect.TreeRangeSet) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.action.Command) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) DiscreteDomain(com.google.common.collect.DiscreteDomain) DeviceId.deviceId(org.onosproject.net.DeviceId.deviceId) ImmutableSet(com.google.common.collect.ImmutableSet) TributarySlot(org.onosproject.net.TributarySlot) MplsLabel(org.onlab.packet.MplsLabel) Resources(org.onosproject.net.resource.Resources) VlanId(org.onlab.packet.VlanId) Range(com.google.common.collect.Range) Set(java.util.Set) Argument(org.apache.karaf.shell.api.action.Argument) Resource(org.onosproject.net.resource.Resource) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Completion(org.apache.karaf.shell.api.action.Completion) ResourceQueryService(org.onosproject.net.resource.ResourceQueryService) Option(org.apache.karaf.shell.api.action.Option) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) Resource(org.onosproject.net.resource.Resource) ContinuousResource(org.onosproject.net.resource.ContinuousResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) ArrayList(java.util.ArrayList) DiscreteResource(org.onosproject.net.resource.DiscreteResource) Range(com.google.common.collect.Range) TributarySlot(org.onosproject.net.TributarySlot) MplsLabel(org.onlab.packet.MplsLabel) VlanId(org.onlab.packet.VlanId) ContinuousResource(org.onosproject.net.resource.ContinuousResource)

Example 4 with Resource

use of org.onosproject.net.resource.Resource in project onos by opennetworkinglab.

the class ConnectivityIntentCompiler method allocateBandwidth.

/**
 * Allocates the bandwidth specified as intent constraint on each link
 * composing the intent, if a bandwidth constraint is specified.
 *
 * @param intent the intent requesting bandwidth allocation
 * @param connectPoints the connect points composing the intent path computed
 */
protected void allocateBandwidth(ConnectivityIntent intent, List<ConnectPoint> connectPoints) {
    // Retrieve bandwidth constraint if exists
    List<Constraint> constraints = intent.constraints();
    if (constraints == null) {
        return;
    }
    Optional<Constraint> constraint = constraints.stream().filter(c -> c instanceof BandwidthConstraint).findAny();
    // If there is no bandwidth constraint continue
    if (!constraint.isPresent()) {
        return;
    }
    BandwidthConstraint bwConstraint = (BandwidthConstraint) constraint.get();
    double bw = bwConstraint.bandwidth().bps();
    // If a resource group is set on the intent, the resource consumer is
    // set equal to it. Otherwise it's set to the intent key
    ResourceConsumer newResourceConsumer = intent.resourceGroup() != null ? intent.resourceGroup() : intent.key();
    // Get the list of current resource allocations
    Collection<ResourceAllocation> resourceAllocations = resourceService.getResourceAllocations(newResourceConsumer);
    // Get the list of resources already allocated from resource allocations
    List<Resource> resourcesAllocated = resourcesFromAllocations(resourceAllocations);
    // Get the list of resource ids for resources already allocated
    List<ResourceId> idsResourcesAllocated = resourceIds(resourcesAllocated);
    // Create the list of incoming resources requested. Exclude resources
    // already allocated.
    List<Resource> incomingResources = resources(connectPoints, bw).stream().filter(r -> !resourcesAllocated.contains(r)).collect(Collectors.toList());
    if (incomingResources.isEmpty()) {
        return;
    }
    // Create the list of resources to be added, meaning their key is not
    // present in the resources already allocated
    List<Resource> resourcesToAdd = incomingResources.stream().filter(r -> !idsResourcesAllocated.contains(r.id())).collect(Collectors.toList());
    // Resources to updated are all the new valid resources except the
    // resources to be added
    List<Resource> resourcesToUpdate = Lists.newArrayList(incomingResources);
    resourcesToUpdate.removeAll(resourcesToAdd);
    // If there are no resources to update skip update procedures
    if (!resourcesToUpdate.isEmpty()) {
        // Remove old resources that need to be updated
        // TODO: use transaction updates when available in the resource service
        List<ResourceAllocation> resourceAllocationsToUpdate = resourceAllocations.stream().filter(rA -> resourceIds(resourcesToUpdate).contains(rA.resource().id())).collect(Collectors.toList());
        log.debug("Releasing bandwidth for intent {}: {} bps", newResourceConsumer, resourcesToUpdate);
        resourceService.release(resourceAllocationsToUpdate);
        // Update resourcesToAdd with the list of both the new resources and
        // the resources to update
        resourcesToAdd.addAll(resourcesToUpdate);
    }
    // remove them
    if (intent.resourceGroup() != null) {
        // Get the list of current resource allocations made by intent key
        Collection<ResourceAllocation> resourceAllocationsByKey = resourceService.getResourceAllocations(intent.key());
        resourceService.release(Lists.newArrayList(resourceAllocationsByKey));
    }
    // Allocate resources
    log.debug("Allocating bandwidth for intent {}: {} bps", newResourceConsumer, resourcesToAdd);
    List<ResourceAllocation> allocations = resourceService.allocate(newResourceConsumer, resourcesToAdd);
    if (allocations.isEmpty()) {
        log.debug("No resources allocated for intent {}", newResourceConsumer);
    }
    log.debug("Done allocating bandwidth for intent {}", newResourceConsumer);
}
Also used : HashedPathSelectionConstraint(org.onosproject.net.intent.constraint.HashedPathSelectionConstraint) TopologyEdge(org.onosproject.net.topology.TopologyEdge) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) ElementId(org.onosproject.net.ElementId) ResourceService(org.onosproject.net.resource.ResourceService) ConnectPoint(org.onosproject.net.ConnectPoint) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) PathNotFoundException(org.onosproject.net.intent.impl.PathNotFoundException) PathViabilityConstraint(org.onosproject.net.intent.constraint.PathViabilityConstraint) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Bandwidth(org.onlab.util.Bandwidth) IntentExtensionService(org.onosproject.net.intent.IntentExtensionService) Resources(org.onosproject.net.resource.Resources) PathService(org.onosproject.net.topology.PathService) Collection(java.util.Collection) Set(java.util.Set) ProviderId(org.onosproject.net.provider.ProviderId) Resource(org.onosproject.net.resource.Resource) Collectors(java.util.stream.Collectors) Constraint(org.onosproject.net.intent.Constraint) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) DefaultEdgeWeigher(org.onlab.graph.DefaultEdgeWeigher) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint) List(java.util.List) Weight(org.onlab.graph.Weight) ResourceId(org.onosproject.net.resource.ResourceId) TopologyVertex(org.onosproject.net.topology.TopologyVertex) ConnectivityIntent(org.onosproject.net.intent.ConnectivityIntent) IntentCompiler(org.onosproject.net.intent.IntentCompiler) Optional(java.util.Optional) Path(org.onosproject.net.Path) ResourceConsumer(org.onosproject.net.resource.ResourceConsumer) LinkWeigher(org.onosproject.net.topology.LinkWeigher) Reference(org.osgi.service.component.annotations.Reference) ScalarWeight(org.onlab.graph.ScalarWeight) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) DisjointPath(org.onosproject.net.DisjointPath) MarkerConstraint(org.onosproject.net.intent.constraint.MarkerConstraint) HashedPathSelectionConstraint(org.onosproject.net.intent.constraint.HashedPathSelectionConstraint) PathViabilityConstraint(org.onosproject.net.intent.constraint.PathViabilityConstraint) Constraint(org.onosproject.net.intent.Constraint) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint) MarkerConstraint(org.onosproject.net.intent.constraint.MarkerConstraint) Resource(org.onosproject.net.resource.Resource) ResourceConsumer(org.onosproject.net.resource.ResourceConsumer) ResourceAllocation(org.onosproject.net.resource.ResourceAllocation) ResourceId(org.onosproject.net.resource.ResourceId) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint)

Example 5 with Resource

use of org.onosproject.net.resource.Resource in project onos by opennetworkinglab.

the class ResourceDeviceListener method unregisterPortResource.

private void unregisterPortResource(Device device, Port port) {
    DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
    List<Resource> allResources = getDescendantResources(portResource);
    adminService.unregister(Lists.transform(allResources, Resource::id));
}
Also used : Resource(org.onosproject.net.resource.Resource) DiscreteResource(org.onosproject.net.resource.DiscreteResource) DiscreteResource(org.onosproject.net.resource.DiscreteResource)

Aggregations

Resource (org.onosproject.net.resource.Resource)16 Set (java.util.Set)8 DiscreteResource (org.onosproject.net.resource.DiscreteResource)8 ResourceAllocation (org.onosproject.net.resource.ResourceAllocation)8 List (java.util.List)7 ConnectPoint (org.onosproject.net.ConnectPoint)7 Resources (org.onosproject.net.resource.Resources)7 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 Reference (org.osgi.service.component.annotations.Reference)6 ReferenceCardinality (org.osgi.service.component.annotations.ReferenceCardinality)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 Collection (java.util.Collection)5 Collections (java.util.Collections)5 DeviceId (org.onosproject.net.DeviceId)5 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4