Merge "Remove redundant type specifiers"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PortStatusMessageFactoryTest.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.rev130715.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
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.PortStateV10;
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  * Unit tests for OF10PortStatusMessageFactory.
32  *
33  * @author giuseppex.petralia@intel.com
34  */
35 public class OF10PortStatusMessageFactoryTest {
36     private OFSerializer<PortStatusMessage> factory;
37     private static final byte MESSAGE_TYPE = 12;
38
39     @Before
40     public void startUp() {
41         SerializerRegistry registry = new SerializerRegistryImpl();
42         registry.init();
43         factory = registry
44                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PortStatusMessage.class));
45     }
46
47     @Test
48     public void testSerialize() throws Exception {
49         PortStatusMessageBuilder builder = new PortStatusMessageBuilder();
50         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
51         builder.setReason(PortReason.forValue(1));
52         builder.setPortNo(1L);
53         builder.setHwAddr(new MacAddress("94:de:80:a6:61:40"));
54         builder.setName("Port name");
55         builder.setConfigV10(new PortConfigV10(true, false, true, false, true, false, true));
56         builder.setStateV10(new PortStateV10(true, false, true, false, true, false, true, false));
57         builder.setCurrentFeaturesV10(
58                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
59         builder.setAdvertisedFeaturesV10(
60                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
61         builder.setSupportedFeaturesV10(
62                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
63         builder.setPeerFeaturesV10(
64                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
65         PortStatusMessage message = builder.build();
66
67         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
68         factory.serialize(message, serializedBuffer);
69         BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 64);
70         Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
71         serializedBuffer.skipBytes(7);
72         Assert.assertEquals("Wrong port No", message.getPortNo().intValue(), serializedBuffer.readShort());
73         byte[] address = new byte[6];
74         serializedBuffer.readBytes(address);
75         Assert.assertEquals("Wrong MacAddress", message.getHwAddr().getValue().toLowerCase(),
76                 new MacAddress(ByteBufUtils.macAddressToString(address)).getValue().toLowerCase());
77         byte[] name = new byte[16];
78         serializedBuffer.readBytes(name);
79         Assert.assertEquals("Wrong name", message.getName(), new String(name).trim());
80         Assert.assertEquals("Wrong config", message.getConfigV10(), createPortConfig(serializedBuffer.readInt()));
81         Assert.assertEquals("Wrong state", message.getStateV10(), createPortState(serializedBuffer.readInt()));
82         Assert.assertEquals("Wrong current", message.getCurrentFeaturesV10(),
83                 createPortFeatures(serializedBuffer.readInt()));
84         Assert.assertEquals("Wrong advertised", message.getAdvertisedFeaturesV10(),
85                 createPortFeatures(serializedBuffer.readInt()));
86         Assert.assertEquals("Wrong supported", message.getSupportedFeaturesV10(),
87                 createPortFeatures(serializedBuffer.readInt()));
88         Assert.assertEquals("Wrong peer", message.getPeerFeaturesV10(), createPortFeatures(serializedBuffer.readInt()));
89     }
90
91     private static PortConfigV10 createPortConfig(long input) {
92         final Boolean _portDown = (input & 1 << 0) > 0;
93         final Boolean _noStp = (input & 1 << 1) > 0;
94         final Boolean _noRecv = (input & 1 << 2) > 0;
95         final Boolean _noRecvStp = (input & 1 << 3) > 0;
96         final Boolean _noFlood = (input & 1 << 4) > 0;
97         final Boolean _noFwd = (input & 1 << 5) > 0;
98         final Boolean _noPacketIn = (input & 1 << 6) > 0;
99         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
100     }
101
102     private static PortFeaturesV10 createPortFeatures(long input) {
103         final Boolean _10mbHd = (input & 1 << 0) > 0;
104         final Boolean _10mbFd = (input & 1 << 1) > 0;
105         final Boolean _100mbHd = (input & 1 << 2) > 0;
106         final Boolean _100mbFd = (input & 1 << 3) > 0;
107         final Boolean _1gbHd = (input & 1 << 4) > 0;
108         final Boolean _1gbFd = (input & 1 << 5) > 0;
109         final Boolean _10gbFd = (input & 1 << 6) > 0;
110         final Boolean _copper = (input & 1 << 7) > 0;
111         final Boolean _fiber = (input & 1 << 8) > 0;
112         final Boolean _autoneg = (input & 1 << 9) > 0;
113         final Boolean _pause = (input & 1 << 10) > 0;
114         final Boolean _pauseAsym = (input & 1 << 11) > 0;
115         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd, _1gbHd, _autoneg, _copper,
116                 _fiber, _pause, _pauseAsym);
117     }
118
119     private static PortStateV10 createPortState(long input) {
120         final Boolean _linkDown = (input & 1 << 0) > 0;
121         final Boolean _blocked = (input & 1 << 1) > 0;
122         final Boolean _live = (input & 1 << 2) > 0;
123         final Boolean _stpListen = (input & 1 << 3) > 0;
124         final Boolean _stpLearn = (input & 1 << 4) > 0;
125         final Boolean _stpForward = (input & 1 << 5) > 0;
126         final Boolean _stpBlock = (input & 1 << 6) > 0;
127         final Boolean _stpMask = (input & 1 << 7) > 0;
128         return new PortStateV10(_blocked, _linkDown, _live, _stpBlock, _stpForward, _stpLearn, _stpListen, _stpMask);
129     }
130 }