Bump upstreams
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / messages / PortMessageSerializerTest.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.impl.protocol.serialization.messages;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.ImmutableMap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.BinContent;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.util.ByteBufUtils;
20 import org.opendaylight.openflowplugin.impl.protocol.serialization.AbstractSerializerTest;
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.flow.types.port.rev130925.PortConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumberValues;
30 import org.opendaylight.yangtools.yang.common.Uint32;
31 import org.opendaylight.yangtools.yang.common.Uint8;
32
33 public class PortMessageSerializerTest extends AbstractSerializerTest {
34
35     private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
36     private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
37     private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
38     private static final short LENGTH = 40;
39     private static final Uint32 XID = Uint32.valueOf(42);
40     private static final Uint8 VERSION = EncodeConstants.OF_VERSION_1_3;
41     private static final String PORT_NUMBER = OutputPortValues.ALL.toString();
42     private static final Long PORT_NUMBER_VAL = BinContent.intToUnsignedLong(PortNumberValues.ALL.getIntValue());
43     private static final String MAC_ADDRESS = "e9:2a:55:ba:fa:4d";
44
45     // Port config
46     private static final Boolean IS_NOFWD = false;
47     private static final Boolean IS_NOPACKETIN = false;
48     private static final Boolean IS_NORECV = true;
49     private static final Boolean IS_PORTDOWN = false;
50
51     // Port features
52     private static final Boolean IS_AUTOENG = true;
53     private static final Boolean IS_COPPER = false;
54     private static final Boolean IS_FIBER = true;
55     private static final Boolean IS_40GBFD = true;
56     private static final Boolean IS_100GBFD = false;
57     private static final Boolean IS_100MBFD = false;
58     private static final Boolean IS_100MBHD = false;
59     private static final Boolean IS_1GBFD = false;
60     private static final Boolean IS_1GBHD = false;
61     private static final Boolean IS_1TBFD = false;
62     private static final Boolean IS_OTHER = false;
63     private static final Boolean IS_PAUSE = false;
64     private static final Boolean IS_PAUSE_ASYM = false;
65     private static final Boolean IS_10GBFD = false;
66     private static final Boolean IS_10MBFD = false;
67     private static final Boolean IS_10MBHD = false;
68
69     private static final PortMessage MESSAGE = new PortMessageBuilder()
70             .setXid(XID)
71             .setVersion(VERSION)
72             .setPortNumber(new PortNumberUni(PORT_NUMBER))
73             .setConfiguration(new PortConfig(IS_NOFWD, IS_NOPACKETIN, IS_NORECV, IS_PORTDOWN))
74             .setMask(new PortConfig(true, true, true, true))
75             .setAdvertisedFeatures(new PortFeatures(
76                     IS_AUTOENG,
77                     IS_COPPER,
78                     IS_FIBER,
79                     IS_40GBFD,
80                     IS_100GBFD,
81                     IS_100MBFD,
82                     IS_100MBHD,
83                     IS_1GBFD,
84                     IS_1GBHD,
85                     IS_1TBFD,
86                     IS_OTHER,
87                     IS_PAUSE,
88                     IS_PAUSE_ASYM,
89                     IS_10GBFD,
90                     IS_10MBFD,
91                     IS_10MBHD))
92             .setHardwareAddress(new MacAddress(MAC_ADDRESS))
93             .build();
94
95     private PortMessageSerializer serializer;
96
97     @Override
98     protected void init() {
99         serializer = getRegistry()
100                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, PortMessage.class));
101     }
102
103     @Test
104     public void testSerialize() {
105         final ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
106         serializer.serialize(MESSAGE, out);
107
108         // Header
109         assertEquals(out.readByte(), VERSION.byteValue());
110         assertEquals(out.readByte(), serializer.getMessageType());
111         assertEquals(out.readShort(), LENGTH);
112         assertEquals(out.readInt(), XID.intValue());
113
114         // Body
115         assertEquals(out.readInt(), PORT_NUMBER_VAL.intValue());
116         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_01);
117         byte[] address = new byte[6];
118         out.readBytes(address);
119         assertEquals(new MacAddress(MAC_ADDRESS), IetfYangUtil.macAddressFor(address));
120         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_02);
121
122         // Port config
123         final int config = ByteBufUtils.fillBitMaskFromMap(ImmutableMap
124                 .<Integer, Boolean>builder()
125                 .put(0, IS_PORTDOWN)
126                 .put(2, IS_NORECV)
127                 .put(5, IS_NOFWD)
128                 .put(6, IS_NOPACKETIN)
129                 .build());
130
131         final int mask = ByteBufUtils.fillBitMaskFromMap(ImmutableMap
132                 .<Integer, Boolean>builder()
133                 .put(0, true)
134                 .put(2, true)
135                 .put(5, true)
136                 .put(6, true)
137                 .build());
138
139         assertEquals(out.readInt(), config);
140         assertEquals(out.readInt(), mask);
141
142         // Port features
143         assertEquals(out.readInt(), ByteBufUtils.fillBitMask(0,
144                 IS_10MBHD,
145                 IS_10MBFD,
146                 IS_100MBHD,
147                 IS_100MBFD,
148                 IS_1GBHD,
149                 IS_1GBFD,
150                 IS_10GBFD,
151                 IS_40GBFD,
152                 IS_100GBFD,
153                 IS_1TBFD,
154                 IS_OTHER,
155                 IS_COPPER,
156                 IS_FIBER,
157                 IS_AUTOENG,
158                 IS_PAUSE,
159                 IS_PAUSE_ASYM));
160
161         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_03);
162
163         assertEquals(out.readableBytes(), 0);
164
165     }
166
167 }