Jian Li
Committed by Ray Milkey

Support to encode and decode group id in InstructionCodec

Change-Id: Icd0947f494f572831e8b5c8d82d47c85eb074824
......@@ -22,6 +22,8 @@ import org.onlab.packet.MplsLabel;
import org.onlab.packet.TpPort;
import org.onlab.packet.VlanId;
import org.onlab.util.HexString;
import org.onosproject.core.DefaultGroupId;
import org.onosproject.core.GroupId;
import org.onosproject.net.ChannelSpacing;
import org.onosproject.net.GridType;
import org.onosproject.net.Lambda;
......@@ -261,6 +263,10 @@ public final class DecodeInstructionCodecHelper {
} else if (type.equals(Instruction.Type.TABLE.name())) {
return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID)
.asInt(), InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE));
} else if (type.equals(Instruction.Type.GROUP.name())) {
GroupId groupId = new DefaultGroupId(nullIsIllegal(json.get(InstructionCodec.GROUP_ID)
.asInt(), InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE));
return Instructions.createGroup(groupId);
} else if (type.equals(Instruction.Type.L0MODIFICATION.name())) {
return decodeL0();
} else if (type.equals(Instruction.Type.L1MODIFICATION.name())) {
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.util.HexString;
import org.onosproject.codec.CodecContext;
import org.onosproject.net.OchSignal;
......@@ -29,8 +30,6 @@ import org.onosproject.net.flow.instructions.L4ModificationInstruction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* JSON encoding of Instructions.
*/
......@@ -249,6 +248,12 @@ public final class EncodeInstructionCodecHelper {
case NOACTION:
break;
case GROUP:
final Instructions.GroupInstruction groupInstruction =
(Instructions.GroupInstruction) instruction;
result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
break;
case L0MODIFICATION:
encodeL0(result);
break;
......
......@@ -50,6 +50,7 @@ public final class InstructionCodec extends JsonCodec<Instruction> {
protected static final String TCP_PORT = "tcpPort";
protected static final String UDP_PORT = "udpPort";
protected static final String TABLE_ID = "tableId";
protected static final String GROUP_ID = "groupId";
protected static final String TRIBUTARY_PORT_NUMBER = "tributaryPortNumber";
protected static final String TRIBUTARY_SLOT_LEN = "tributarySlotLength";
protected static final String TRIBUTARY_SLOT_BITMAP = "tributarySlotBitmap";
......
......@@ -15,11 +15,13 @@
*/
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.JsonNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onlab.util.HexString;
import org.onosproject.net.OduSignalId;
import org.onosproject.net.flow.instructions.Instruction;
import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
......@@ -33,8 +35,6 @@ import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeade
import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
import com.fasterxml.jackson.databind.JsonNode;
/**
* Hamcrest matcher for instructions.
*/
......@@ -123,6 +123,33 @@ public final class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<Json
}
/**
* Matches the contents of a group instruction.
*
* @param instructionJson JSON instruction to match
* @param description Description object used for recording errors
* @return true if contents match, false otherwise
*/
private boolean matchGroupInstruction(JsonNode instructionJson,
Description description) {
final String jsonType = instructionJson.get("type").textValue();
GroupInstruction instructionToMatch = (GroupInstruction) instruction;
if (!instructionToMatch.type().name().equals(jsonType)) {
description.appendText("type was " + jsonType);
return false;
}
if (instructionJson.get("groupId").isInt()) {
final int jsonGroupId = instructionJson.get("groupId").asInt();
if (instructionToMatch.groupId().id() != jsonGroupId) {
description.appendText("groupId was " + jsonGroupId);
return false;
}
}
return true;
}
/**
* Matches the contents of a mod lambda instruction.
*
* @param instructionJson JSON instruction to match
......@@ -453,6 +480,8 @@ public final class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<Json
return matchPushHeaderInstruction(jsonInstruction, description);
} else if (instruction instanceof OutputInstruction) {
return matchOutputInstruction(jsonInstruction, description);
} else if (instruction instanceof GroupInstruction) {
return matchGroupInstruction(jsonInstruction, description);
} else if (instruction instanceof ModLambdaInstruction) {
return matchModLambdaInstruction(jsonInstruction, description);
} else if (instruction instanceof ModOchSignalInstruction) {
......