2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.extension.onf;
11 import com.google.common.base.Splitter;
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
21 * Abstract class for common ByteBuf util methods.
23 public abstract class ByteBufUtils {
25 private static final Splitter HEXSTRING_SPLITTER = Splitter.onPattern("\\s+").omitEmptyStrings();
26 private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 };
27 public static final Long DEFAULT_XID = 0x01020304L;
29 public static ByteBuf hexStringToByteBuf(final String hexSrc) {
30 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
31 out.writeBytes(hexStringToBytes(hexSrc));
35 public static ByteBuf buildBuf(final String hexSrc) {
36 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
38 out.writeBytes(hexStringToBytes(hexSrc));
42 public static void checkHeaderV13(OfHeader ofHeader) {
43 final Short version = EncodeConstants.OF13_VERSION_ID;
44 Assert.assertEquals("Wrong version", version, ofHeader.getVersion());
45 Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid());
48 private static byte[] hexStringToBytes(final String hexSrc) {
49 List<String> byteChips = Lists.newArrayList(HEXSTRING_SPLITTER.split(hexSrc));
50 byte[] result = new byte[byteChips.size()];
52 for (String chip : byteChips) {
53 result[i] = (byte) Short.parseShort(chip, 16);