blob: f024fa329722e11adac839bc056451ce8a938319 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.util;
2
3import java.util.List;
4
5import org.jboss.netty.buffer.ChannelBuffer;
6import org.projectfloodlight.openflow.exceptions.OFParseError;
Andreas Wundsam70aa5492013-10-23 15:26:53 -07007import org.projectfloodlight.openflow.protocol.OFInstrumentationOptions;
Yotam Harcholf3f11152013-09-05 16:47:16 -07008import org.projectfloodlight.openflow.protocol.OFMessageReader;
9import org.projectfloodlight.openflow.protocol.Writeable;
Andreas Wundsam70aa5492013-10-23 15:26:53 -070010import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
Yotam Harcholf3f11152013-09-05 16:47:16 -070012
13import com.google.common.base.Charsets;
14import com.google.common.collect.ImmutableList;
15import com.google.common.collect.ImmutableList.Builder;
16
17/**
18 * Collection of helper functions for reading and writing into ChannelBuffers
19 *
20 * @author capveg
21 */
22
23public class ChannelUtils {
Andreas Wundsam70aa5492013-10-23 15:26:53 -070024 private static final Logger logger = LoggerFactory.getLogger(ChannelUtils.class);
Yotam Harcholf3f11152013-09-05 16:47:16 -070025 public static String readFixedLengthString(ChannelBuffer bb, int length) {
26 byte[] dst = new byte[length];
27 bb.readBytes(dst, 0, length);
Rob Vaterlaus64d06df2013-10-11 19:54:26 -070028 int validLength = 0;
29 for (validLength = 0; validLength < length; validLength++) {
30 if (dst[validLength] == 0)
31 break;
32 }
33 return new String(dst, 0, validLength, Charsets.US_ASCII);
Yotam Harcholf3f11152013-09-05 16:47:16 -070034 }
35
36 public static void writeFixedLengthString(ChannelBuffer bb, String string,
37 int length) {
38 int l = string.length();
39 if (l > length) {
40 throw new IllegalArgumentException("Error writing string: length="
41 + l + " > max Length=" + length);
42 }
43 bb.writeBytes(string.getBytes(Charsets.US_ASCII));
44 if (l < length) {
45 bb.writeZero(length - l);
46 }
47 }
48
49 static public byte[] readBytes(final ChannelBuffer bb, final int length) {
50 byte byteArray[] = new byte[length];
51 bb.readBytes(byteArray);
52 return byteArray;
53 }
54
55 static public void writeBytes(final ChannelBuffer bb,
56 final byte byteArray[]) {
57 bb.writeBytes(byteArray);
58 }
59
60 public static <T> List<T> readList(ChannelBuffer bb, int length, OFMessageReader<T> reader) throws OFParseError {
61 int end = bb.readerIndex() + length;
62 Builder<T> builder = ImmutableList.<T>builder();
Andreas Wundsam70aa5492013-10-23 15:26:53 -070063 if(OFInstrumentationOptions.TRACE_READS)
64 if(logger.isTraceEnabled())
65 logger.trace("readList(length={}, reader={})", length, reader.getClass());
Yotam Harcholf3f11152013-09-05 16:47:16 -070066 while(bb.readerIndex() < end) {
Andreas Wundsam70aa5492013-10-23 15:26:53 -070067 T read = reader.readFrom(bb);
68 if(OFInstrumentationOptions.TRACE_READS)
69 if(logger.isTraceEnabled())
70 logger.trace("readList: read={}, left={}", read, end - bb.readerIndex());
71 builder.add(read);
Yotam Harcholf3f11152013-09-05 16:47:16 -070072 }
73 if(bb.readerIndex() != end) {
74 throw new IllegalStateException("Overread length: length="+length + " overread by "+ (bb.readerIndex() - end) + " reader: "+reader);
75 }
76 return builder.build();
77 }
78
79 public static void writeList(ChannelBuffer bb, List<? extends Writeable> writeables) {
80 for(Writeable w: writeables)
81 w.writeTo(bb);
82 }
83}