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