Ensure P4Runtime byte strings are padded to their bit width
The P4Runtime server may send canonical byte strings (i.e.,
non-padded byte strings).
In ONOS we ensure, in the codecs, that all byte strings are
padded to match the model (P4Info) bit width. In this way,
we provide read-write symmetry inside ONOS.
ONOS always pads byte strings when sending messages to the
P4Runtime server.
This patch doesn't enforce read-write symmetry between
P4Runtime client and server on the wire.
N.B.: the current padding implementation works ONLY when
using non-negative integer.
Change-Id: I9f8e43de015bd0929dd543d7688c8e71bf5fe98d
diff --git a/protocols/p4runtime/utils/src/main/java/org/onosproject/p4runtime/ctl/codec/ActionCodec.java b/protocols/p4runtime/utils/src/main/java/org/onosproject/p4runtime/ctl/codec/ActionCodec.java
index d0a285e..daf91f5 100644
--- a/protocols/p4runtime/utils/src/main/java/org/onosproject/p4runtime/ctl/codec/ActionCodec.java
+++ b/protocols/p4runtime/utils/src/main/java/org/onosproject/p4runtime/ctl/codec/ActionCodec.java
@@ -28,6 +28,8 @@
import p4.v1.P4RuntimeOuterClass;
import static java.lang.String.format;
+import static org.onlab.util.ImmutableByteSequence.copyAndFit;
+import static org.onlab.util.ImmutableByteSequence.copyFrom;
import static org.onosproject.p4runtime.ctl.codec.Utils.assertSize;
/**
@@ -65,7 +67,7 @@
protected PiAction decode(
P4RuntimeOuterClass.Action message, Object ignored,
PiPipeconf pipeconf, P4InfoBrowser browser)
- throws P4InfoBrowser.NotFoundException {
+ throws P4InfoBrowser.NotFoundException, CodecException {
final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
browser.actionParams(message.getActionId());
final String actionName = browser.actions()
@@ -77,11 +79,17 @@
final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId());
final ImmutableByteSequence value;
if (browser.isTypeString(actionParam.getTypeName())) {
- value = ImmutableByteSequence.copyFrom(new String(p.getValue().toByteArray()));
+ value = copyFrom(new String(p.getValue().toByteArray()));
} else {
- value = ImmutableByteSequence.copyFrom(p.getValue().toByteArray());
+ try {
+ value = copyAndFit(p.getValue().asReadOnlyByteBuffer(),
+ actionParam.getBitwidth());
+ } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
+ throw new CodecException(e.getMessage());
+ }
}
- builder.withParameter(new PiActionParam(PiActionParamId.of(actionParam.getName()), value));
+ builder.withParameter(new PiActionParam(
+ PiActionParamId.of(actionParam.getName()), value));
}
return builder.build();
}