Search in sources :

Example 1 with PiActionProfileMemberId

use of org.onosproject.net.pi.runtime.PiActionProfileMemberId in project onos by opennetworkinglab.

the class DecodeInstructionCodecHelper method decodePi.

/**
 * Decodes a protocol-independent instruction.
 *
 * @return instruction object decoded from the JSON
 * @throws IllegalArgumentException if the JSON is invalid
 */
private Instruction decodePi() {
    String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
    if (subType.equals(PiTableAction.Type.ACTION.name())) {
        PiActionId piActionId = PiActionId.of(nullIsIllegal(json.get(InstructionCodec.PI_ACTION_ID), InstructionCodec.PI_ACTION_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
        JsonNode params = json.get(InstructionCodec.PI_ACTION_PARAMS);
        PiAction.Builder builder = PiAction.builder();
        PiActionParam piActionParam;
        PiActionParamId piActionParamId;
        if (params != null) {
            for (Map.Entry<String, String> param : ((Map<String, String>) (context.mapper().convertValue(params, Map.class))).entrySet()) {
                piActionParamId = PiActionParamId.of(param.getKey());
                piActionParam = new PiActionParam(piActionParamId, ImmutableByteSequence.copyFrom(HexString.fromHexString(param.getValue(), null)));
                builder.withParameter(piActionParam);
            }
        }
        return Instructions.piTableAction(builder.withId(piActionId).build());
    } else if (subType.equals(PiTableAction.Type.ACTION_PROFILE_GROUP_ID.name())) {
        PiActionProfileGroupId piActionGroupId = PiActionProfileGroupId.of(nullIsIllegal(json.get(InstructionCodec.PI_ACTION_PROFILE_GROUP_ID), InstructionCodec.PI_ACTION_PROFILE_GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
        return Instructions.piTableAction(piActionGroupId);
    } else if (subType.equals(PiTableAction.Type.ACTION_PROFILE_MEMBER_ID.name())) {
        PiActionProfileMemberId piActionProfileMemberId = PiActionProfileMemberId.of(nullIsIllegal(json.get(InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID), InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
        return Instructions.piTableAction(piActionProfileMemberId);
    }
    // TODO: implement JSON decoder for ACTION_SET
    throw new IllegalArgumentException("Protocol-independent Instruction subtype " + subType + " is not supported");
}
Also used : PiActionProfileMemberId(org.onosproject.net.pi.runtime.PiActionProfileMemberId) PiActionId(org.onosproject.net.pi.model.PiActionId) PiActionParamId(org.onosproject.net.pi.model.PiActionParamId) PiActionProfileGroupId(org.onosproject.net.pi.runtime.PiActionProfileGroupId) JsonNode(com.fasterxml.jackson.databind.JsonNode) HexString(org.onlab.util.HexString) PiActionParam(org.onosproject.net.pi.runtime.PiActionParam) Map(java.util.Map) PiAction(org.onosproject.net.pi.runtime.PiAction)

Example 2 with PiActionProfileMemberId

use of org.onosproject.net.pi.runtime.PiActionProfileMemberId in project onos by opennetworkinglab.

the class EncodeInstructionCodecHelper method encodePi.

/**
 * Encode a protocol-independent instruction.
 *
 * @param result json node that the instruction attributes are added to
 */
private void encodePi(ObjectNode result) {
    PiInstruction piInstruction = (PiInstruction) instruction;
    result.put(InstructionCodec.SUBTYPE, piInstruction.action().type().name());
    switch(piInstruction.action().type()) {
        case ACTION:
            final PiAction piAction = (PiAction) piInstruction.action();
            result.put(InstructionCodec.PI_ACTION_ID, piAction.id().id());
            final ObjectNode jsonActionParams = context.mapper().createObjectNode();
            for (PiActionParam actionParam : piAction.parameters()) {
                jsonActionParams.put(actionParam.id().id(), HexString.toHexString(actionParam.value().asArray(), null));
            }
            result.set(InstructionCodec.PI_ACTION_PARAMS, jsonActionParams);
            break;
        case ACTION_PROFILE_GROUP_ID:
            final PiActionProfileGroupId groupId = (PiActionProfileGroupId) piInstruction.action();
            result.put(InstructionCodec.PI_ACTION_PROFILE_GROUP_ID, groupId.id());
            break;
        case ACTION_PROFILE_MEMBER_ID:
            final PiActionProfileMemberId memberId = (PiActionProfileMemberId) piInstruction.action();
            result.put(InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID, memberId.id());
            break;
        // TODO: implement JSON encoder for ACTION_SET
        default:
            throw new IllegalArgumentException("Cannot convert protocol-independent subtype of" + piInstruction.action().type().name());
    }
}
Also used : PiActionProfileMemberId(org.onosproject.net.pi.runtime.PiActionProfileMemberId) PiInstruction(org.onosproject.net.flow.instructions.PiInstruction) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PiActionProfileGroupId(org.onosproject.net.pi.runtime.PiActionProfileGroupId) PiActionParam(org.onosproject.net.pi.runtime.PiActionParam) PiAction(org.onosproject.net.pi.runtime.PiAction)

Example 3 with PiActionProfileMemberId

use of org.onosproject.net.pi.runtime.PiActionProfileMemberId in project onos by opennetworkinglab.

the class InstructionJsonMatcher method matchPiInstruction.

/**
 * Matches the contents of a protocol-independent instruction.
 *
 * @param instructionJson JSON instruction to match
 * @param description Description object used for recording errors
 * @return true if contents match, false otherwise
 */
private boolean matchPiInstruction(JsonNode instructionJson, Description description) {
    PiInstruction instructionToMatch = (PiInstruction) instruction;
    final String jsonSubtype = instructionJson.get("subtype").textValue();
    if (!instructionToMatch.action().type().name().equals(jsonSubtype)) {
        description.appendText("subtype was " + jsonSubtype);
        return false;
    }
    final String jsonType = instructionJson.get("type").textValue();
    if (!instructionToMatch.type().name().equals(jsonType)) {
        description.appendText("type was " + jsonType);
        return false;
    }
    switch(instructionToMatch.action().type()) {
        case ACTION:
            if (!Objects.equals(instructionJson.get("actionId").textValue(), ((PiAction) instructionToMatch.action()).id().id())) {
                description.appendText("action was " + ((PiAction) instructionToMatch.action()).id().id());
                return false;
            }
            Collection<PiActionParam> piActionParams = ((PiAction) instructionToMatch.action()).parameters();
            JsonNode jsonParams = instructionJson.get("actionParams");
            for (PiActionParam actionParam : piActionParams) {
                if (!Objects.equals(copyFrom(HexString.fromHexString(jsonParams.get(actionParam.id().id()).textValue(), null)), actionParam.value())) {
                    description.appendText("action param value was " + actionParam.value());
                    return false;
                }
            }
            break;
        case ACTION_PROFILE_GROUP_ID:
            if (!Objects.equals(instructionJson.get("groupId").asInt(), ((PiActionProfileGroupId) instructionToMatch.action()).id())) {
                description.appendText("action profile group id was " + ((PiActionProfileGroupId) instructionToMatch.action()).id());
                return false;
            }
            break;
        case ACTION_PROFILE_MEMBER_ID:
            if (!Objects.equals(instructionJson.get("memberId").asInt(), ((PiActionProfileMemberId) instructionToMatch.action()).id())) {
                description.appendText("action profile member id was " + ((PiActionProfileMemberId) instructionToMatch.action()).id());
                return false;
            }
            break;
        default:
            description.appendText("type was " + jsonType);
            return false;
    }
    return true;
}
Also used : PiActionProfileMemberId(org.onosproject.net.pi.runtime.PiActionProfileMemberId) PiInstruction(org.onosproject.net.flow.instructions.PiInstruction) PiActionProfileGroupId(org.onosproject.net.pi.runtime.PiActionProfileGroupId) JsonNode(com.fasterxml.jackson.databind.JsonNode) HexString(org.onlab.util.HexString) PiActionParam(org.onosproject.net.pi.runtime.PiActionParam) PiAction(org.onosproject.net.pi.runtime.PiAction)

Aggregations

PiAction (org.onosproject.net.pi.runtime.PiAction)3 PiActionParam (org.onosproject.net.pi.runtime.PiActionParam)3 PiActionProfileGroupId (org.onosproject.net.pi.runtime.PiActionProfileGroupId)3 PiActionProfileMemberId (org.onosproject.net.pi.runtime.PiActionProfileMemberId)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 HexString (org.onlab.util.HexString)2 PiInstruction (org.onosproject.net.flow.instructions.PiInstruction)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Map (java.util.Map)1 PiActionId (org.onosproject.net.pi.model.PiActionId)1 PiActionParamId (org.onosproject.net.pi.model.PiActionParamId)1