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