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