Search in sources :

Example 6 with CoreService

use of org.onosproject.core.CoreService in project onos by opennetworkinglab.

the class VirtualFlowsListCommand method doExecute.

@Override
protected void doExecute() {
    CoreService coreService = get(CoreService.class);
    VirtualNetworkService vnetservice = get(VirtualNetworkService.class);
    DeviceService deviceService = vnetservice.get(NetworkId.networkId(networkId), DeviceService.class);
    FlowRuleService service = vnetservice.get(NetworkId.networkId(networkId), FlowRuleService.class);
    contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
    compilePredicate();
    SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
    if (outputJson()) {
        print("%s", json(flows.keySet(), flows));
    } else {
        flows.forEach((device, flow) -> printFlows(device, flow, coreService));
    }
}
Also used : VirtualNetworkService(org.onosproject.incubator.net.virtual.VirtualNetworkService) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) CoreService(org.onosproject.core.CoreService) StringFilter(org.onlab.util.StringFilter) ArrayList(java.util.ArrayList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) FlowRuleService(org.onosproject.net.flow.FlowRuleService)

Example 7 with CoreService

use of org.onosproject.core.CoreService in project onos by opennetworkinglab.

the class FlowsListCommand method doExecute.

@Override
protected void doExecute() {
    CoreService coreService = get(CoreService.class);
    DeviceService deviceService = get(DeviceService.class);
    FlowRuleService service = get(FlowRuleService.class);
    contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
    compilePredicate();
    if (countOnly && !suppressCoreOutput && filter.isEmpty() && remove == null) {
        if (state == null && uri == null) {
            deviceService.getDevices().forEach(device -> printCount(device, service));
        } else if (uri == null) {
            deviceService.getDevices().forEach(device -> printCount(device, FlowEntryState.valueOf(state.toUpperCase()), service));
        } else {
            Device device = deviceService.getDevice(DeviceId.deviceId(uri));
            if (device != null) {
                printCount(device, FlowEntryState.valueOf(state.toUpperCase()), service);
            }
        }
        return;
    }
    SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service, coreService);
    // Remove flows
    if (remove != null) {
        flows.values().forEach(flowList -> {
            if (!remove.isEmpty()) {
                filter.add(remove);
                contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
            }
            if (!filter.isEmpty() || (remove != null && !remove.isEmpty())) {
                flowList = filterFlows(flowList);
                this.removeFlowsInteractive(flowList, service, coreService);
            }
        });
        return;
    }
    // Show flows
    if (outputJson()) {
        print("%s", json(flows.keySet(), flows));
    } else {
        flows.forEach((device, flow) -> printFlows(device, flow, coreService));
    }
}
Also used : StringFilter(org.onlab.util.StringFilter) Comparators(org.onosproject.utils.Comparators) FlowEntryState(org.onosproject.net.flow.FlowEntry.FlowEntryState) CoreService(org.onosproject.core.CoreService) DeviceService(org.onosproject.net.device.DeviceService) FlowEntry(org.onosproject.net.flow.FlowEntry) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) ArrayList(java.util.ArrayList) FlowRuleService(org.onosproject.net.flow.FlowRuleService) Map(java.util.Map) ApplicationId(org.onosproject.core.ApplicationId) JsonNode(com.fasterxml.jackson.databind.JsonNode) PlaceholderCompleter(org.onosproject.cli.PlaceholderCompleter) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Device(org.onosproject.net.Device) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Argument(org.apache.karaf.shell.api.action.Argument) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) TreeMap(java.util.TreeMap) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Completion(org.apache.karaf.shell.api.action.Completion) BufferedReader(java.io.BufferedReader) Option(org.apache.karaf.shell.api.action.Option) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) SortedMap(java.util.SortedMap) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) CoreService(org.onosproject.core.CoreService) StringFilter(org.onlab.util.StringFilter) ArrayList(java.util.ArrayList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) FlowRuleService(org.onosproject.net.flow.FlowRuleService)

Example 8 with CoreService

use of org.onosproject.core.CoreService in project onos by opennetworkinglab.

the class GroupCodec method decode.

@Override
public Group decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    final JsonCodec<GroupBucket> groupBucketCodec = context.codec(GroupBucket.class);
    CoreService coreService = context.getService(CoreService.class);
    // parse uInt Group id from the ID field
    JsonNode idNode = json.get(ID);
    Long id = (null == idNode) ? // use GROUP_ID for the corresponding Group id if ID is not supplied
    nullIsIllegal(json.get(GROUP_ID), ID + MISSING_MEMBER_MESSAGE).asLong() : idNode.asLong();
    GroupId groupId = GroupId.valueOf(id.intValue());
    // parse uInt Group id given by caller. see the GroupDescription javadoc
    JsonNode givenGroupIdNode = json.get(GIVEN_GROUP_ID);
    // if GIVEN_GROUP_ID is not supplied, set null so the group subsystem
    // to choose the Group id (which is the value of ID indeed).
    // else, must be same with ID to show both originate from the same source
    Integer givenGroupIdInt = (null == givenGroupIdNode) ? null : new Long(json.get(GIVEN_GROUP_ID).asLong()).intValue();
    if (givenGroupIdInt != null && !givenGroupIdInt.equals(groupId.id())) {
        throw new IllegalArgumentException(GIVEN_GROUP_ID + " must be same with " + ID);
    }
    // parse group key (appCookie)
    String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE), APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
    if (!groupKeyStr.startsWith("0x")) {
        throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
    }
    GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString(groupKeyStr.split("0x")[1], ""));
    // parse device id
    DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
    // application id
    ApplicationId appId = coreService.registerApplication(REST_APP_ID);
    // parse group type
    String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
    GroupDescription.Type groupType = null;
    switch(type) {
        case "SELECT":
            groupType = Group.Type.SELECT;
            break;
        case "INDIRECT":
            groupType = Group.Type.INDIRECT;
            break;
        case "ALL":
            groupType = Group.Type.ALL;
            break;
        case "CLONE":
            groupType = Group.Type.CLONE;
            break;
        case "FAILOVER":
            groupType = Group.Type.FAILOVER;
            break;
        default:
            nullIsIllegal(groupType, "The requested group type " + type + " is not valid");
    }
    // parse group buckets
    GroupBuckets buckets = null;
    List<GroupBucket> groupBucketList = new ArrayList<>();
    JsonNode bucketsJson = json.get(BUCKETS);
    checkNotNull(bucketsJson);
    if (bucketsJson != null) {
        IntStream.range(0, bucketsJson.size()).forEach(i -> {
            ObjectNode bucketJson = get(bucketsJson, i);
            bucketJson.put("type", type);
            groupBucketList.add(groupBucketCodec.decode(bucketJson, context));
        });
        buckets = new GroupBuckets(groupBucketList);
    }
    GroupDescription groupDescription = new DefaultGroupDescription(deviceId, groupType, buckets, groupKey, givenGroupIdInt, appId);
    return new DefaultGroup(groupId, groupDescription);
}
Also used : 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) DefaultGroup(org.onosproject.net.group.DefaultGroup) CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode) HexString(org.onlab.util.HexString) GroupBuckets(org.onosproject.net.group.GroupBuckets) GroupId(org.onosproject.core.GroupId) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription) GroupDescription(org.onosproject.net.group.GroupDescription) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) GroupBucket(org.onosproject.net.group.GroupBucket) ApplicationId(org.onosproject.core.ApplicationId) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription)

Example 9 with CoreService

use of org.onosproject.core.CoreService in project onos by opennetworkinglab.

the class NextObjectiveCodec method decode.

@Override
public NextObjective decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    CoreService coreService = context.getService(CoreService.class);
    final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
    final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
    ObjectiveCodecHelper och = new ObjectiveCodecHelper();
    DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder();
    final DefaultNextObjective.Builder builder = (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context);
    // decode id
    JsonNode idJson = json.get(ID);
    checkNotNull(idJson);
    builder.withId(idJson.asInt());
    // decode application id
    JsonNode appIdJson = json.get(APP_ID);
    String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
    builder.fromApp(coreService.registerApplication(appId));
    // decode type
    String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
    switch(typeStr) {
        case "HASHED":
            builder.withType(NextObjective.Type.HASHED);
            break;
        case "BROADCAST":
            builder.withType(NextObjective.Type.BROADCAST);
            break;
        case "FAILOVER":
            builder.withType(NextObjective.Type.FAILOVER);
            break;
        case "SIMPLE":
            builder.withType(NextObjective.Type.SIMPLE);
            break;
        default:
            throw new IllegalArgumentException("The requested type " + typeStr + " is not defined for NextObjective.");
    }
    // decode treatments
    JsonNode treatmentsJson = json.get(TREATMENTS);
    checkNotNull(treatmentsJson);
    if (treatmentsJson != null) {
        IntStream.range(0, treatmentsJson.size()).forEach(i -> {
            ObjectNode treatmentJson = get(treatmentsJson, i);
            JsonNode weightJson = treatmentJson.get(WEIGHT);
            int weight = (weightJson != null) ? weightJson.asInt() : NextTreatment.DEFAULT_WEIGHT;
            builder.addTreatment(DefaultNextTreatment.of(trafficTreatmentCodec.decode(treatmentJson, context), weight));
        });
    }
    // decode meta
    JsonNode metaJson = json.get(META);
    if (metaJson != null) {
        TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context);
        builder.withMeta(trafficSelector);
    }
    // decode operation
    String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
    NextObjective nextObjective;
    switch(opStr) {
        case "ADD":
            nextObjective = builder.add();
            break;
        case "REMOVE":
            nextObjective = builder.remove();
            break;
        default:
            throw new IllegalArgumentException("The requested operation " + opStr + " is not defined for NextObjective.");
    }
    return nextObjective;
}
Also used : DefaultNextObjective(org.onosproject.net.flowobjective.DefaultNextObjective) NextObjective(org.onosproject.net.flowobjective.NextObjective) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) DefaultNextObjective(org.onosproject.net.flowobjective.DefaultNextObjective) TrafficSelector(org.onosproject.net.flow.TrafficSelector)

Example 10 with CoreService

use of org.onosproject.core.CoreService in project onos by opennetworkinglab.

the class PacketRequestCodec method decode.

@Override
public PacketRequest decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
    TrafficSelector trafficSelector = trafficSelectorCodec.decode(get(json, TRAFFIC_SELECTOR), context);
    NodeId nodeId = NodeId.nodeId(extractMember(NODE_ID, json));
    PacketPriority priority = PacketPriority.valueOf(extractMember(PRIORITY, json));
    CoreService coreService = context.getService(CoreService.class);
    // TODO check appId (currently hardcoded - should it be read from json node?)
    ApplicationId appId = coreService.registerApplication(REST_APP_ID);
    DeviceId deviceId = null;
    JsonNode node = json.get(DEVICE_ID);
    if (node != null) {
        deviceId = DeviceId.deviceId(node.asText());
    }
    return new DefaultPacketRequest(trafficSelector, priority, appId, nodeId, Optional.ofNullable(deviceId));
}
Also used : PacketPriority(org.onosproject.net.packet.PacketPriority) DefaultPacketRequest(org.onosproject.net.packet.DefaultPacketRequest) DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) TrafficSelector(org.onosproject.net.flow.TrafficSelector) CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApplicationId(org.onosproject.core.ApplicationId)

Aggregations

CoreService (org.onosproject.core.CoreService)71 Before (org.junit.Before)31 ApplicationId (org.onosproject.core.ApplicationId)30 FlowRuleService (org.onosproject.net.flow.FlowRuleService)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 ComponentConfigAdapter (org.onosproject.cfg.ComponentConfigAdapter)12 NetworkConfigService (org.onosproject.net.config.NetworkConfigService)12 TrafficSelector (org.onosproject.net.flow.TrafficSelector)11 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 DeviceId (org.onosproject.net.DeviceId)10 IntentExtensionService (org.onosproject.net.intent.IntentExtensionService)10 TestServiceDirectory (org.onlab.osgi.TestServiceDirectory)9 MockResourceService (org.onosproject.net.resource.MockResourceService)9 TestEventDispatcher (org.onosproject.common.event.impl.TestEventDispatcher)8 DistributedVirtualNetworkStore (org.onosproject.incubator.net.virtual.store.impl.DistributedVirtualNetworkStore)8 DeviceService (org.onosproject.net.device.DeviceService)8 TestStorageService (org.onosproject.store.service.TestStorageService)8 ArrayList (java.util.ArrayList)7 DomainService (org.onosproject.net.domain.DomainService)6