Search in sources :

Example 6 with GroupService

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

the class GroupsListCommand method doExecute.

@Override
protected void doExecute() {
    DeviceService deviceService = get(DeviceService.class);
    GroupService groupService = get(GroupService.class);
    SortedMap<Device, List<Group>> sortedGroups = getSortedGroups(deviceService, groupService);
    if (referencedOnly && unreferencedOnly) {
        print("Options -r and -u cannot be used at the same time");
        return;
    }
    if (outputJson()) {
        print("%s", json(sortedGroups));
    } else {
        sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
    }
}
Also used : Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) List(java.util.List) GroupService(org.onosproject.net.group.GroupService)

Example 7 with GroupService

use of org.onosproject.net.group.GroupService 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)

Example 8 with GroupService

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

the class K8sPurgeRulesCommand method doExecute.

@Override
protected void doExecute() {
    FlowRuleService flowRuleService = get(FlowRuleService.class);
    GroupService groupService = get(GroupService.class);
    CoreService coreService = get(CoreService.class);
    K8sNodeService k8sNodeService = get(K8sNodeService.class);
    ApplicationId appId = coreService.getAppId(K8S_NETWORKING_APP_ID);
    if (appId == null) {
        error("Failed to purge kubernetes networking flow rules.");
        return;
    }
    flowRuleService.removeFlowRulesById(appId);
    print("Successfully purged flow rules installed by kubernetes networking app.");
    boolean result = true;
    long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
    // we make sure all flow rules are removed from the store
    while (stream(flowRuleService.getFlowEntriesById(appId).spliterator(), false).count() > 0) {
        long waitMs = timeoutExpiredMs - System.currentTimeMillis();
        try {
            sleep(SLEEP_MS);
        } catch (InterruptedException e) {
            log.error("Exception caused during rule purging...");
        }
        if (stream(flowRuleService.getFlowEntriesById(appId).spliterator(), false).count() == 0) {
            break;
        } else {
            flowRuleService.removeFlowRulesById(appId);
            print("Failed to purging flow rules, retrying rule purging...");
        }
        if (waitMs <= 0) {
            result = false;
            break;
        }
    }
    for (K8sNode node : k8sNodeService.completeNodes()) {
        for (Group group : groupService.getGroups(node.intgBridge(), appId)) {
            groupService.removeGroup(node.intgBridge(), group.appCookie(), appId);
        }
    }
    if (result) {
        print("Successfully purged flow rules!");
    } else {
        error("Failed to purge flow rules.");
    }
}
Also used : Group(org.onosproject.net.group.Group) K8sNode(org.onosproject.k8snode.api.K8sNode) K8sNodeService(org.onosproject.k8snode.api.K8sNodeService) CoreService(org.onosproject.core.CoreService) FlowRuleService(org.onosproject.net.flow.FlowRuleService) ApplicationId(org.onosproject.core.ApplicationId) GroupService(org.onosproject.net.group.GroupService)

Example 9 with GroupService

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

the class OFSwitchManager method getGroups.

@Override
public List<Group> getGroups(NetworkId networkId, DeviceId deviceId) {
    GroupService groupService = virtualNetService.get(networkId, GroupService.class);
    Iterable<Group> entries = groupService.getGroups(deviceId);
    return Lists.newArrayList(entries);
}
Also used : NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Group(org.onosproject.net.group.Group) GroupService(org.onosproject.net.group.GroupService)

Example 10 with GroupService

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

the class GroupsWebResource method updateGroupBuckets.

/**
 * Adds buckets to a group using the group service.
 *
 * @param deviceIdString device Id
 * @param appCookieString application cookie
 * @param stream JSON stream
 */
private void updateGroupBuckets(String deviceIdString, String appCookieString, InputStream stream) throws IOException {
    GroupService groupService = get(GroupService.class);
    DeviceId deviceId = DeviceId.deviceId(deviceIdString);
    final GroupKey groupKey = createKey(appCookieString);
    Group group = nullIsNotFound(groupService.getGroup(deviceId, groupKey), GROUP_NOT_FOUND);
    ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
    GroupBuckets buckets = null;
    List<GroupBucket> groupBucketList = new ArrayList<>();
    JsonNode bucketsJson = jsonTree.get("buckets");
    final JsonCodec<GroupBucket> groupBucketCodec = codec(GroupBucket.class);
    if (bucketsJson != null) {
        IntStream.range(0, bucketsJson.size()).forEach(i -> {
            ObjectNode bucketJson = (ObjectNode) bucketsJson.get(i);
            groupBucketList.add(groupBucketCodec.decode(bucketJson, this));
        });
        buckets = new GroupBuckets(groupBucketList);
    }
    groupService.addBucketsToGroup(deviceId, groupKey, buckets, groupKey, group.appId());
}
Also used : Group(org.onosproject.net.group.Group) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) GroupKey(org.onosproject.net.group.GroupKey) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) ArrayList(java.util.ArrayList) GroupBucket(org.onosproject.net.group.GroupBucket) JsonNode(com.fasterxml.jackson.databind.JsonNode) GroupBuckets(org.onosproject.net.group.GroupBuckets) GroupService(org.onosproject.net.group.GroupService)

Aggregations

GroupService (org.onosproject.net.group.GroupService)12 Group (org.onosproject.net.group.Group)9 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 Device (org.onosproject.net.Device)5 DeviceId (org.onosproject.net.DeviceId)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 GET (javax.ws.rs.GET)4 DeviceService (org.onosproject.net.device.DeviceService)4 DefaultGroupKey (org.onosproject.net.group.DefaultGroupKey)4 GroupKey (org.onosproject.net.group.GroupKey)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 List (java.util.List)3 GroupBucket (org.onosproject.net.group.GroupBucket)3 GroupDescription (org.onosproject.net.group.GroupDescription)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2