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