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