import io.netty.buffer.UnpooledByteBufAllocator;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
*/
public static String byteBufToHexString(ByteBuf bb) {
StringBuffer sb = new StringBuffer();
- for (int i = 0; i < bb.readableBytes(); i++) {
+ for (int i = bb.readerIndex(); i < (bb.readerIndex() + bb.readableBytes()); i++) {
sb.append(String.format(" %02x", bb.getUnsignedByte(i)));
}
return sb.toString().trim();
}
}
+
/**
* Create standard OF header
- * @param factory serialization factory
+ * @param msgType message code
* @param message POJO
* @param out writing buffer
+ * @param length ofheader length
*/
- public static <E extends OfHeader> void writeOFHeader(OFSerializer<E> factory, E message, ByteBuf out) {
+ public static <E extends OfHeader> void writeOFHeader(byte msgType, E message, ByteBuf out, int length) {
out.writeByte(message.getVersion());
- out.writeByte(factory.getMessageType());
- out.writeShort(factory.computeLength(message));
+ out.writeByte(msgType);
+ out.writeShort(length);
out.writeInt(message.getXid().intValue());
-
+ }
+
+ /**
+ * Write length standard OF header
+ * @param out writing buffer
+ */
+ public static void updateOFHeaderLength(ByteBuf out) {
+ out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes());
}
/**
* @return byte representation of mac address
* @see {@link MacAddress}
*/
+ @SuppressWarnings("javadoc")
public static byte[] macAddressToBytes(String macAddress) {
String[] sequences = macAddress.split(":");
byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
* @return String representation of mac address
* @see {@link MacAddress}
*/
+ @SuppressWarnings("javadoc")
public static String macAddressToString(byte[] address) {
List<String> groups = new ArrayList<>();
for(int i=0; i < EncodeConstants.MAC_ADDRESS_LENGTH; i++){
}
/**
- * Reads and parses port name from ByteBuf
+ * Reads and parses null-terminated string from ByteBuf
* @param rawMessage
* @param length maximal length of String
* @return String with name of port
*/
public static String decodeNullTerminatedString(ByteBuf rawMessage, int length) {
- byte[] name = new byte[EncodeConstants.MAX_PORT_NAME_LENGTH];
+ byte[] name = new byte[length];
rawMessage.readBytes(name);
- int index = 0;
- for (int i = 0; i < name.length; i++) {
- if (name[i] != 0) {
- index++;
- } else {
- break;
- }
- }
- byte[] correctName = Arrays.copyOf(name, index);
- return new String(correctName);
+ return new String(name).trim();
}
+
}