2 * Copyright (c) 2016 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.impl.protocol.serialization.messages;
11 import static org.junit.Assert.assertEquals;
13 import com.google.common.collect.ImmutableMap;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.UnpooledByteBufAllocator;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.BinContent;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.util.ByteBufUtils;
21 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumberValues;
31 public class PortMessageSerializerTest extends AbstractSerializerTest {
33 private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
34 private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
35 private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
36 private static final short LENGTH = 40;
37 private static final Long XID = 42L;
38 private static final short VERSION = EncodeConstants.OF13_VERSION_ID;
39 private static final String PORT_NUMBER = OutputPortValues.ALL.toString();
40 private static final Long PORT_NUMBER_VAL = BinContent.intToUnsignedLong(PortNumberValues.ALL.getIntValue());
41 private static final String MAC_ADDRESS = "E9:2A:55:BA:FA:4D";
44 private static final Boolean IS_NOFWD = false;
45 private static final Boolean IS_NOPACKETIN = false;
46 private static final Boolean IS_NORECV = true;
47 private static final Boolean IS_PORTDOWN = false;
50 private static final Boolean IS_AUTOENG = true;
51 private static final Boolean IS_COPPER = false;
52 private static final Boolean IS_FIBER = true;
53 private static final Boolean IS_40GBFD = true;
54 private static final Boolean IS_100GBFD = false;
55 private static final Boolean IS_100MBFD = false;
56 private static final Boolean IS_100MBHD = false;
57 private static final Boolean IS_1GBFD = false;
58 private static final Boolean IS_1GBHD = false;
59 private static final Boolean IS_1TBFD = false;
60 private static final Boolean IS_OTHER = false;
61 private static final Boolean IS_PAUSE = false;
62 private static final Boolean IS_PAUSE_ASYM = false;
63 private static final Boolean IS_10GBFD = false;
64 private static final Boolean IS_10MBFD = false;
65 private static final Boolean IS_10MBHD = false;
67 private static final PortMessage MESSAGE = new PortMessageBuilder()
70 .setPortNumber(new PortNumberUni(PORT_NUMBER))
71 .setConfiguration(new PortConfig(IS_NOFWD, IS_NOPACKETIN, IS_NORECV, IS_PORTDOWN))
72 .setMask(new PortConfig(true, true, true, true))
73 .setAdvertisedFeatures(new PortFeatures(
90 .setHardwareAddress(new MacAddress(MAC_ADDRESS))
93 private PortMessageSerializer serializer;
96 protected void init() {
97 serializer = getRegistry().getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PortMessage.class));
101 public void testSerialize() throws Exception {
102 final ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
103 serializer.serialize(MESSAGE, out);
106 assertEquals(out.readByte(), VERSION);
107 assertEquals(out.readByte(), serializer.getMessageType());
108 assertEquals(out.readShort(), LENGTH);
109 assertEquals(out.readInt(), XID.intValue());
112 assertEquals(out.readInt(), PORT_NUMBER_VAL.intValue());
113 out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_01);
114 byte[] address = new byte[6];
115 out.readBytes(address);
117 new MacAddress(ByteBufUtils.macAddressToString(address)).getValue(),
118 new MacAddress(MAC_ADDRESS).getValue());
119 out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_02);
122 final int config = ByteBufUtils.fillBitMaskFromMap(ImmutableMap
123 .<Integer, Boolean>builder()
127 .put(6, IS_NOPACKETIN)
130 final int mask = ByteBufUtils.fillBitMaskFromMap(ImmutableMap
131 .<Integer, Boolean>builder()
138 assertEquals(out.readInt(), config);
139 assertEquals(out.readInt(), mask);
142 assertEquals(out.readInt(), ByteBufUtils.fillBitMask(0,
160 out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_03);
162 assertEquals(out.readableBytes(), 0);