Bump upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortModInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint32;
29
30 /**
31  * Unit tests for PortModInputMessageFactory.
32  *
33  * @author timotej.kubas
34  * @author michal.polkorab
35  */
36 public class PortModInputMessageFactoryTest {
37     private static final byte MESSAGE_TYPE = 16;
38     private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
39     private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
40     private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
41     private static final int MESSAGE_LENGTH = 40;
42     private SerializerRegistry registry;
43     private OFSerializer<PortModInput> portModFactory;
44
45     /**
46      * Initializes serializer registry and stores correct factory in field.
47      */
48     @Before
49     public void startUp() {
50         registry = new SerializerRegistryImpl();
51         registry.init();
52         portModFactory = registry.getSerializer(
53                 new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, PortModInput.class));
54     }
55
56     /**
57      * Testing of {@link PortModInputMessageFactory} for correct translation from POJO.
58      */
59     @Test
60     public void testPortModInput() throws Exception {
61         PortModInputBuilder builder = new PortModInputBuilder();
62         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
63         builder.setPortNo(new PortNumber(Uint32.valueOf(9)));
64         builder.setHwAddress(new MacAddress("08:00:27:00:b0:eb"));
65         builder.setConfig(new PortConfig(true, false, true, false));
66         builder.setMask(new PortConfig(false, true, false, true));
67         builder.setAdvertise(new PortFeatures(true, false, false, false,
68                                               false, false, false, true,
69                                               false, false, false, false,
70                                               false, false, false, false));
71         final PortModInput message = builder.build();
72
73         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
74
75         // simulate parent message
76         out.writeInt(1);
77         out.writeZero(2);
78         out.writeShort(3);
79
80         portModFactory.serialize(message, out);
81
82         // read parent message
83         out.readInt();
84         out.skipBytes(2);
85         out.readShort();
86
87         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
88         Assert.assertEquals("Wrong PortNo", message.getPortNo().getValue().longValue(), out.readUnsignedInt());
89         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_01);
90         byte[] address = new byte[6];
91         out.readBytes(address);
92         Assert.assertEquals("Wrong MacAddress", message.getHwAddress(), IetfYangUtil.macAddressFor(address));
93         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_02);
94         Assert.assertEquals("Wrong config", message.getConfig(), createPortConfig(out.readInt()));
95         Assert.assertEquals("Wrong mask", message.getMask(), createPortConfig(out.readInt()));
96         Assert.assertEquals("Wrong advertise", message.getAdvertise(), createPortFeatures(out.readInt()));
97         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_03);
98     }
99
100     private static PortConfig createPortConfig(final long input) {
101         final Boolean _portDown = (input & 1 << 0) > 0;
102         final Boolean _noRecv = (input & 1 << 2) > 0;
103         final Boolean _noFwd = (input & 1 << 5) > 0;
104         final Boolean _noPacketIn = (input & 1 << 6) > 0;
105         return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
106     }
107
108     private static PortFeatures createPortFeatures(final long input) {
109         final Boolean _10mbHd = (input & 1 << 0) > 0;
110         final Boolean _10mbFd = (input & 1 << 1) > 0;
111         final Boolean _100mbHd = (input & 1 << 2) > 0;
112         final Boolean _100mbFd = (input & 1 << 3) > 0;
113         final Boolean _1gbHd = (input & 1 << 4) > 0;
114         final Boolean _1gbFd = (input & 1 << 5) > 0;
115         final Boolean _10gbFd = (input & 1 << 6) > 0;
116         final Boolean _40gbFd = (input & 1 << 7) > 0;
117         final Boolean _100gbFd = (input & 1 << 8) > 0;
118         final Boolean _1tbFd = (input & 1 << 9) > 0;
119         final Boolean _other = (input & 1 << 10) > 0;
120         final Boolean _copper = (input & 1 << 11) > 0;
121         final Boolean _fiber = (input & 1 << 12) > 0;
122         final Boolean _autoneg = (input & 1 << 13) > 0;
123         final Boolean _pause = (input & 1 << 14) > 0;
124         final Boolean _pauseAsym = (input & 1 << 15) > 0;
125         return new PortFeatures(_100gbFd, _100mbFd,  _100mbHd, _10gbFd, _10mbFd, _10mbHd,
126                 _1gbFd, _1gbHd, _1tbFd, _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym);
127     }
128 }