Search in sources :

Example 1 with GroupState

use of org.onosproject.net.group.Group.GroupState in project onos by opennetworkinglab.

the class GroupsListCommand method getSortedGroups.

/**
 * Returns the list of devices sorted using the device ID URIs.
 *
 * @param deviceService device service
 * @param groupService group service
 * @return sorted device list
 */
protected SortedMap<Device, List<Group>> getSortedGroups(DeviceService deviceService, GroupService groupService) {
    final GroupState groupsState = (this.state != null && !"any".equals(this.state)) ? GroupState.valueOf(this.state.toUpperCase()) : null;
    final Iterable<Device> devices = Optional.ofNullable(uri).map(DeviceId::deviceId).map(deviceService::getDevice).map(dev -> (Iterable<Device>) Collections.singletonList(dev)).orElse(deviceService.getDevices());
    SortedMap<Device, List<Group>> sortedGroups = new TreeMap<>(Comparators.ELEMENT_COMPARATOR);
    for (Device d : devices) {
        Stream<Group> groupStream = Lists.newArrayList(groupService.getGroups(d.id())).stream();
        if (groupsState != null) {
            groupStream = groupStream.filter(g -> g.state().equals(groupsState));
        }
        if (referencedOnly) {
            groupStream = groupStream.filter(g -> g.referenceCount() != 0);
        }
        if (type != null && !"any".equals(type)) {
            groupStream = groupStream.filter(g -> g.type().equals(GroupDescription.Type.valueOf(type.toUpperCase())));
        }
        if (unreferencedOnly) {
            groupStream = groupStream.filter(g -> g.referenceCount() == 0);
        }
        sortedGroups.put(d, groupStream.sorted(Comparators.GROUP_COMPARATOR).collect(Collectors.toList()));
    }
    return sortedGroups;
}
Also used : Comparators(org.onosproject.utils.Comparators) DeviceService(org.onosproject.net.device.DeviceService) GroupBucket(org.onosproject.net.group.GroupBucket) Command(org.apache.karaf.shell.api.action.Command) Group(org.onosproject.net.group.Group) Lists(com.google.common.collect.Lists) GroupState(org.onosproject.net.group.Group.GroupState) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Device(org.onosproject.net.Device) GroupService(org.onosproject.net.group.GroupService) Argument(org.apache.karaf.shell.api.action.Argument) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Optional(java.util.Optional) Completion(org.apache.karaf.shell.api.action.Completion) Option(org.apache.karaf.shell.api.action.Option) DeviceId(org.onosproject.net.DeviceId) GroupDescription(org.onosproject.net.group.GroupDescription) Collections(java.util.Collections) SortedMap(java.util.SortedMap) Group(org.onosproject.net.group.Group) Device(org.onosproject.net.Device) List(java.util.List) GroupState(org.onosproject.net.group.Group.GroupState) TreeMap(java.util.TreeMap)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 Lists (com.google.common.collect.Lists)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Command (org.apache.karaf.shell.api.action.Command)1 Completion (org.apache.karaf.shell.api.action.Completion)1 Option (org.apache.karaf.shell.api.action.Option)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 AbstractShellCommand (org.onosproject.cli.AbstractShellCommand)1 Device (org.onosproject.net.Device)1 DeviceId (org.onosproject.net.DeviceId)1 DeviceService (org.onosproject.net.device.DeviceService)1