2 * Copyright (c) 2015 NetIDE Consortium 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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
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.openflow.common.types.rev130731.PortConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessageBuilder;
31 * Unit tests for PortStatusMessageFactory.
33 * @author giuseppex.petralia@intel.com
35 public class PortStatusMessageFactoryTest {
36 private OFSerializer<PortStatusMessage> factory;
37 private static final byte MESSAGE_TYPE = 12;
38 private static final byte PADDING = 7;
39 private static final byte PORT_PADDING_1 = 4;
40 private static final byte PORT_PADDING_2 = 2;
43 public void startUp() {
44 SerializerRegistry registry = new SerializerRegistryImpl();
47 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PortStatusMessage.class));
51 public void testSerialize() throws Exception {
52 PortStatusMessageBuilder builder = new PortStatusMessageBuilder();
53 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
54 builder.setReason(PortReason.forValue(1));
55 builder.setPortNo(1L);
56 builder.setHwAddr(new MacAddress("94:de:80:a6:61:40"));
57 builder.setName("Port name");
58 builder.setConfig(new PortConfig(true, false, true, false));
59 builder.setState(new PortState(true, false, true));
60 builder.setCurrentFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true, false,
61 true, false, true, false, true, false));
62 builder.setAdvertisedFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true, false,
63 true, false, true, false, true, false));
64 builder.setSupportedFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true, false,
65 true, false, true, false, true, false));
66 builder.setPeerFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true, false, true,
67 false, true, false, true, false));
68 builder.setCurrSpeed(1234L);
69 builder.setMaxSpeed(1234L);
70 PortStatusMessage message = builder.build();
72 ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
73 factory.serialize(message, serializedBuffer);
74 BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 80);
75 Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
76 serializedBuffer.skipBytes(PADDING);
77 Assert.assertEquals("Wrong PortNo", message.getPortNo().intValue(), serializedBuffer.readUnsignedInt());
78 serializedBuffer.skipBytes(PORT_PADDING_1);
79 byte[] address = new byte[6];
80 serializedBuffer.readBytes(address);
81 Assert.assertEquals("Wrong MacAddress", message.getHwAddr().getValue().toLowerCase(),
82 new MacAddress(ByteBufUtils.macAddressToString(address)).getValue().toLowerCase());
83 serializedBuffer.skipBytes(PORT_PADDING_2);
84 byte[] name = new byte[16];
85 serializedBuffer.readBytes(name);
86 Assert.assertEquals("Wrong name", message.getName(), new String(name).trim());
87 Assert.assertEquals("Wrong config", message.getConfig(), createPortConfig(serializedBuffer.readInt()));
88 Assert.assertEquals("Wrong state", message.getState(), createPortState(serializedBuffer.readInt()));
89 Assert.assertEquals("Wrong current", message.getCurrentFeatures(),
90 createPortFeatures(serializedBuffer.readInt()));
91 Assert.assertEquals("Wrong advertised", message.getAdvertisedFeatures(),
92 createPortFeatures(serializedBuffer.readInt()));
93 Assert.assertEquals("Wrong supported", message.getSupportedFeatures(),
94 createPortFeatures(serializedBuffer.readInt()));
95 Assert.assertEquals("Wrong peer", message.getPeerFeatures(), createPortFeatures(serializedBuffer.readInt()));
96 Assert.assertEquals("Wrong Current speed", message.getCurrSpeed().longValue(), serializedBuffer.readInt());
97 Assert.assertEquals("Wrong Max speed", message.getMaxSpeed().longValue(), serializedBuffer.readInt());
100 private static PortConfig createPortConfig(long input) {
101 final Boolean _portDown = (input & 1 << 0) > 0;
102 final Boolean _noRecv = (input & 1 << 2) > 0;
103 final Boolean _noFwd = (input & 1 << 5) > 0;
104 final Boolean _noPacketIn = (input & 1 << 6) > 0;
105 return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
108 private static PortFeatures createPortFeatures(long input) {
109 final Boolean _10mbHd = (input & 1 << 0) > 0;
110 final Boolean _10mbFd = (input & 1 << 1) > 0;
111 final Boolean _100mbHd = (input & 1 << 2) > 0;
112 final Boolean _100mbFd = (input & 1 << 3) > 0;
113 final Boolean _1gbHd = (input & 1 << 4) > 0;
114 final Boolean _1gbFd = (input & 1 << 5) > 0;
115 final Boolean _10gbFd = (input & 1 << 6) > 0;
116 final Boolean _40gbFd = (input & 1 << 7) > 0;
117 final Boolean _100gbFd = (input & 1 << 8) > 0;
118 final Boolean _1tbFd = (input & 1 << 9) > 0;
119 final Boolean _other = (input & 1 << 10) > 0;
120 final Boolean _copper = (input & 1 << 11) > 0;
121 final Boolean _fiber = (input & 1 << 12) > 0;
122 final Boolean _autoneg = (input & 1 << 13) > 0;
123 final Boolean _pause = (input & 1 << 14) > 0;
124 final Boolean _pauseAsym = (input & 1 << 15) > 0;
125 return new PortFeatures(_100gbFd, _100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd, _1gbHd, _1tbFd,
126 _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym);
129 private static PortState createPortState(long input) {
130 final Boolean one = (input & 1 << 0) > 0;
131 final Boolean two = (input & 1 << 1) > 0;
132 final Boolean three = (input & 1 << 2) > 0;
133 return new PortState(two, one, three);