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