OFErrorMsg: add OFErrorCauseData wrapper for nicer toStrings BSC-5349
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index 471f1a8..1920bed 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -501,6 +501,8 @@
)
udf = JType("UDF") \
.op(version=ANY, read="UDF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="UDF.ZERO")
+error_cause_data = JType("OFErrorCauseData") \
+ .op(version=ANY, read="OFErrorCauseData.read(bb, $length, OFVersion.OF_$version)", write="$name.writeTo(bb)");
generic_t = JType("T")
@@ -716,6 +718,8 @@
elif field_name == "err_type":
return JType("OFErrorType", 'short') \
.op(read='bb.readShort()', write='bb.writeShort($name)')
+ elif loxi_utils.class_is(obj_name, "of_error_msg") and field_name == "data":
+ return error_cause_data
elif field_name == "stats_type":
return JType("OFStatsType", 'short') \
.op(read='bb.readShort()', write='bb.writeShort($name)')
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFErrorCauseData.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFErrorCauseData.java
new file mode 100644
index 0000000..b5c7af0
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFErrorCauseData.java
@@ -0,0 +1,121 @@
+package org.projectfloodlight.openflow.types;
+
+import java.util.Arrays;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.projectfloodlight.openflow.exceptions.OFParseError;
+import org.projectfloodlight.openflow.protocol.OFErrorMsg;
+import org.projectfloodlight.openflow.protocol.OFFactories;
+import org.projectfloodlight.openflow.protocol.OFFactory;
+import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFVersion;
+import org.projectfloodlight.openflow.protocol.Writeable;
+import org.projectfloodlight.openflow.util.ChannelUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Optional;
+import com.google.common.hash.PrimitiveSink;
+
+/** A special-purpose wrapper for the 'data' field in an {@link OFErrorMsg} message
+ * that contains a byte serialization of the offending message.
+ *
+ * This attempts to parse the offending message on demand, and if successful
+ * will present the parsed message.
+ *
+ * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
+ */
+public class OFErrorCauseData implements Writeable, PrimitiveSinkable {
+ private static final Logger logger =
+ LoggerFactory.getLogger(OFErrorCauseData.class);
+
+ private final byte[] data;
+ private final OFVersion version;
+
+ private OFErrorCauseData(byte[] data, OFVersion version) {
+ this.data = data;
+ this.version = version;
+ }
+
+ public static OFErrorCauseData of(byte[] data, OFVersion version) {
+ return new OFErrorCauseData(Arrays.copyOf(data, data.length), version);
+ }
+
+ public byte[] getData() {
+ return Arrays.copyOf(data, data.length);
+ }
+
+ public Optional<OFMessage> getParsedMessage() {
+ OFFactory factory = OFFactories.getFactory(version);
+ try {
+ OFMessage msg = factory.getReader().readFrom(ChannelBuffers.wrappedBuffer(data));
+ if(msg != null)
+ return Optional.of(msg);
+ else
+ return Optional.absent();
+ } catch (OFParseError e) {
+ logger.debug("Error parsing error cause data as OFMessage: {}", e.getMessage(), e);
+ return Optional.absent();
+ }
+ }
+
+ public static OFErrorCauseData read(ChannelBuffer bb, int length, OFVersion version) {
+ byte[] bytes = ChannelUtils.readBytes(bb, length);
+ return of(bytes, version);
+ }
+
+ @Override
+ public void putTo(PrimitiveSink sink) {
+ sink.putBytes(data);
+ }
+
+ @Override
+ public void writeTo(ChannelBuffer bb) {
+ bb.writeBytes(data);
+ }
+
+ @Override
+ public String toString() {
+ Optional<OFMessage> parsedMessage = getParsedMessage();
+ if(parsedMessage.isPresent()) {
+ return String.valueOf(parsedMessage.get());
+ } else {
+ StringBuilder b = new StringBuilder();
+ b.append("[unparsed: ");
+ for(int i=0; i<data.length; i++) {
+ if(i>0)
+ b.append(" ");
+ b.append(String.format("%02x", data[i]));
+ }
+ b.append("]");
+ return b.toString();
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Arrays.hashCode(data);
+ result = prime * result + ((version == null) ? 0 : version.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ OFErrorCauseData other = (OFErrorCauseData) obj;
+ if (!Arrays.equals(data, other.data))
+ return false;
+ if (version != other.version)
+ return false;
+ return true;
+ }
+
+}
\ No newline at end of file
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFErrorCauseDataTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFErrorCauseDataTest.java
new file mode 100644
index 0000000..9a5a4dc
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFErrorCauseDataTest.java
@@ -0,0 +1,74 @@
+package org.projectfloodlight.openflow.types;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.hamcrest.Matchers;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.Test;
+import org.projectfloodlight.openflow.protocol.OFFactories;
+import org.projectfloodlight.openflow.protocol.OFFlowAdd;
+import org.projectfloodlight.openflow.protocol.OFVersion;
+
+public class OFErrorCauseDataTest {
+ @Test
+ public void testEmpty() {
+ OFErrorCauseData emptyCause = OFErrorCauseData.of(new byte[] {}, OFVersion.OF_13);
+ assertThat(emptyCause.getData(), equalTo(new byte[] {}));
+ assertThat(emptyCause.getParsedMessage().isPresent(), equalTo(false));
+ assertThat(emptyCause.toString(), Matchers.containsString("unparsed"));
+ }
+
+ @Test
+ public void testTooShort() {
+ OFErrorCauseData emptyCause = OFErrorCauseData.of(new byte[] {0x1, 0x2}, OFVersion.OF_13);
+ assertThat(emptyCause.getData(), equalTo(new byte[] {0x1, 0x2}));
+ assertThat(emptyCause.getParsedMessage().isPresent(), equalTo(false));
+ assertThat(emptyCause.toString(), Matchers.containsString("unparsed"));
+ assertThat(emptyCause.toString(), Matchers.containsString("01 02"));
+ }
+
+ byte[] truncatedFlowAddd = new byte[] {
+ 0x04, 0x0e, // version, type
+ 0x00, (byte) 0x80, // length
+ 0x12, 0x34, 0x56, 0x78, // xid
+ (byte) 0xfe, (byte) 0xdc , (byte) 0xba, (byte) 0x98, 0x76, 0x54, 0x32, 0x10, // cookie
+ (byte) 0xff, 0x00, (byte) 0xff, 0x00, (byte) 0xff, 0x00, (byte) 0xff, 0x00, // cookie_mask
+ 0x03 // table_id
+ // rest truncated
+ };
+
+ @Test
+ public void testTruncated() {
+ OFErrorCauseData emptyCause = OFErrorCauseData.of(truncatedFlowAddd, OFVersion.OF_13);
+ assertThat(emptyCause.getData(), equalTo(truncatedFlowAddd));
+ assertThat(emptyCause.getParsedMessage().isPresent(), equalTo(false));
+ assertThat(emptyCause.toString(), Matchers.containsString("unparsed"));
+ assertThat(emptyCause.toString(), Matchers.containsString("04 0e 00 80"));
+ }
+
+ @Test
+ public void testFlowAdd() {
+ OFFlowAdd flowAdd = OFFactories.getFactory(OFVersion.OF_13).buildFlowAdd()
+ .setXid(0x12345678)
+ .setCookie(U64.parseHex("FEDCBA9876543210"))
+ .setCookieMask(U64.parseHex("FF00FF00FF00FF00"))
+ .setTableId(TableId.of(3))
+ .setIdleTimeout(5)
+ .setHardTimeout(10)
+ .setPriority(6000)
+ .build();
+
+ ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
+ flowAdd.writeTo(bb);
+ byte[] flowAddBytes = new byte[bb.readableBytes()];
+ bb.readBytes(flowAddBytes);
+
+ OFErrorCauseData emptyCause = OFErrorCauseData.of(flowAddBytes, OFVersion.OF_13);
+ assertThat(emptyCause.getData(), equalTo(flowAddBytes));
+ assertThat(emptyCause.getParsedMessage().isPresent(), equalTo(true));
+ assertThat(emptyCause.toString(), Matchers.containsString("OFFlowAdd"));
+ assertThat(emptyCause.toString(), Matchers.containsString("idleTimeout=5"));
+ }
+}
diff --git a/test_data/of13/bad_match_error_msg.data b/test_data/of13/bad_match_error_msg.data
index 80a925b..423ae94 100644
--- a/test_data/of13/bad_match_error_msg.data
+++ b/test_data/of13/bad_match_error_msg.data
@@ -19,4 +19,4 @@
-- java
builder.setXid(0x12345678)
.setCode(OFBadMatchCode.BAD_MASK)
- .setData(new byte[] { 0x61, 0x62, 0x63 });
+ .setData(OFErrorCauseData.of(new byte[] { 0x61, 0x62, 0x63 }, OFVersion.OF_13));
diff --git a/test_data/of13/bad_request_error_msg.data b/test_data/of13/bad_request_error_msg.data
index cc2c139..71adefa 100644
--- a/test_data/of13/bad_request_error_msg.data
+++ b/test_data/of13/bad_request_error_msg.data
@@ -19,4 +19,4 @@
-- java
builder.setXid(0x12345678)
.setCode(OFBadRequestCode.BUFFER_UNKNOWN)
- .setData(new byte[] { 0x61, 0x62, 0x63 });
+ .setData(OFErrorCauseData.of(new byte[] { 0x61, 0x62, 0x63 }, OFVersion.OF_13));