Search in sources :

Example 16 with TrafficSelector

use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.

the class FlowRuleCodec method decode.

@Override
public FlowRule decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
    CoreService coreService = context.getService(CoreService.class);
    JsonNode appIdJson = json.get(APP_ID);
    String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
    resultBuilder.fromApp(coreService.registerApplication(appId));
    int priority = nullIsIllegal(json.get(PRIORITY), PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
    resultBuilder.withPriority(priority);
    boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT), IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
    if (isPermanent) {
        resultBuilder.makePermanent();
    } else {
        resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT), TIMEOUT + MISSING_MEMBER_MESSAGE + " if the flow is temporary").asInt());
    }
    JsonNode tableIdJson = json.get(TABLE_ID);
    if (tableIdJson != null) {
        String tableId = tableIdJson.asText();
        try {
            int tid = Integer.parseInt(tableId);
            resultBuilder.forTable(IndexTableId.of(tid));
        } catch (NumberFormatException e) {
            resultBuilder.forTable(PiTableId.of(tableId));
        }
    }
    DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
    resultBuilder.forDevice(deviceId);
    ObjectNode treatmentJson = get(json, TREATMENT);
    if (treatmentJson != null) {
        JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
        resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
    }
    ObjectNode selectorJson = get(json, SELECTOR);
    if (selectorJson != null) {
        JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class);
        resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
    }
    return resultBuilder.build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule)

Example 17 with TrafficSelector

use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.

the class ForwardingObjectiveCodec method encode.

@Override
public ObjectNode encode(ForwardingObjective forwardingObjective, CodecContext context) {
    checkNotNull(forwardingObjective, NOT_NULL_MESSAGE);
    final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
    final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
    // encode common properties
    ObjectiveCodecHelper och = new ObjectiveCodecHelper();
    ObjectNode result = och.encode(forwardingObjective, context);
    // encode id
    result.put(ID, forwardingObjective.id());
    // encode flag
    result.put(FLAG, forwardingObjective.flag().toString());
    // encode op
    result.put(OPERATION, forwardingObjective.op().toString());
    // encode selector
    ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(forwardingObjective.selector(), context);
    result.set(SELECTOR, trafficSelectorNode);
    // encode nextId
    if (forwardingObjective.nextId() != null) {
        result.put(NEXT_ID, forwardingObjective.nextId());
    }
    // encode treatment
    if (forwardingObjective.treatment() != null) {
        ObjectNode trafficTreatmentNode = trafficTreatmentCodec.encode(forwardingObjective.treatment(), context);
        result.set(TREATMENT, trafficTreatmentNode);
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TrafficSelector(org.onosproject.net.flow.TrafficSelector) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment)

Example 18 with TrafficSelector

use of org.onosproject.net.flow.TrafficSelector 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 19 with TrafficSelector

use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.

the class PacketRequestCodec method encode.

@Override
public ObjectNode encode(PacketRequest packetRequest, CodecContext context) {
    checkNotNull(packetRequest, NULL_OBJECT_MSG);
    final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
    final ObjectNode result = context.mapper().createObjectNode().put(NODE_ID, packetRequest.nodeId().toString()).put(PRIORITY, packetRequest.priority().name()).put(APP_ID, packetRequest.appId().toString());
    if (packetRequest.deviceId().isPresent()) {
        result.put(DEVICE_ID, packetRequest.deviceId().get().toString());
    }
    result.set(TRAFFIC_SELECTOR, trafficSelectorCodec.encode(packetRequest.selector(), context));
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TrafficSelector(org.onosproject.net.flow.TrafficSelector)

Example 20 with TrafficSelector

use of org.onosproject.net.flow.TrafficSelector 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

TrafficSelector (org.onosproject.net.flow.TrafficSelector)396 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)354 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)249 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)235 FlowRule (org.onosproject.net.flow.FlowRule)94 Test (org.junit.Test)85 DefaultFlowRule (org.onosproject.net.flow.DefaultFlowRule)84 PiAction (org.onosproject.net.pi.runtime.PiAction)54 ConnectPoint (org.onosproject.net.ConnectPoint)51 ForwardingObjective (org.onosproject.net.flowobjective.ForwardingObjective)48 DeviceId (org.onosproject.net.DeviceId)43 PortNumber (org.onosproject.net.PortNumber)43 List (java.util.List)42 NextObjective (org.onosproject.net.flowobjective.NextObjective)41 FilteredConnectPoint (org.onosproject.net.FilteredConnectPoint)39 PiActionParam (org.onosproject.net.pi.runtime.PiActionParam)38 Instruction (org.onosproject.net.flow.instructions.Instruction)37 Criterion (org.onosproject.net.flow.criteria.Criterion)36 PiCriterion (org.onosproject.net.flow.criteria.PiCriterion)36 DefaultForwardingObjective (org.onosproject.net.flowobjective.DefaultForwardingObjective)35