Bump upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FeaturesReplyMessageFactoryTest.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 java.util.ArrayList;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionTypeV10;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint32;
35 import org.opendaylight.yangtools.yang.common.Uint64;
36 import org.opendaylight.yangtools.yang.common.Uint8;
37
38 /**
39  * Unit tests for OF10FeaturesReplyMessageFactory.
40  *
41  * @author giuseppex.petralia@intel.com
42  */
43 public class OF10FeaturesReplyMessageFactoryTest {
44     private OFSerializer<GetFeaturesOutput> factory;
45     private static final byte MESSAGE_TYPE = 6;
46
47     @Before
48     public void startUp() {
49         SerializerRegistry registry = new SerializerRegistryImpl();
50         registry.init();
51         factory = registry
52                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_0, GetFeaturesOutput.class));
53     }
54
55     @Test
56     public void testSerialize() throws Exception {
57         GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();
58         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
59         builder.setDatapathId(Uint64.ONE);
60         builder.setBuffers(Uint32.ONE);
61         builder.setTables(Uint8.ONE);
62         builder.setCapabilitiesV10(new CapabilitiesV10(true, false, true, false, true, false, true, false));
63         builder.setActionsV10(
64                 new ActionTypeV10(true, false, true, false, true, false, true, false, true, false, true, false, true));
65         builder.setPhyPort(createPorts());
66         GetFeaturesOutput message = builder.build();
67
68         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
69         factory.serialize(message, serializedBuffer);
70         BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 80);
71         Assert.assertEquals("Wrong datapath id", message.getDatapathId().longValue(), serializedBuffer.readLong());
72         Assert.assertEquals("Wrong n buffers", message.getBuffers().longValue(), serializedBuffer.readUnsignedInt());
73         Assert.assertEquals("Wrong n tables", message.getTables().shortValue(), serializedBuffer.readUnsignedByte());
74         serializedBuffer.skipBytes(3);
75         Assert.assertEquals("Wrong capabilities", message.getCapabilitiesV10(),
76                 createCapabilities(serializedBuffer.readInt()));
77         Assert.assertEquals("Wrong actions", message.getActionsV10(), createActionsV10(serializedBuffer.readInt()));
78         PhyPort port = message.getPhyPort().get(0);
79         Assert.assertEquals("Wrong port No", port.getPortNo().intValue(), serializedBuffer.readShort());
80         byte[] address = new byte[6];
81         serializedBuffer.readBytes(address);
82         Assert.assertEquals("Wrong MacAddress", port.getHwAddr().getValue().toLowerCase(),
83                 IetfYangUtil.macAddressFor(address).getValue().toLowerCase());
84         byte[] name = new byte[16];
85         serializedBuffer.readBytes(name);
86         Assert.assertEquals("Wrong name", port.getName(), new String(name).trim());
87         Assert.assertEquals("Wrong config", port.getConfigV10(), createPortConfig(serializedBuffer.readInt()));
88         Assert.assertEquals("Wrong state", port.getStateV10(), createPortState(serializedBuffer.readInt()));
89         Assert.assertEquals("Wrong current", port.getCurrentFeaturesV10(),
90                 createPortFeatures(serializedBuffer.readInt()));
91         Assert.assertEquals("Wrong advertised", port.getAdvertisedFeaturesV10(),
92                 createPortFeatures(serializedBuffer.readInt()));
93         Assert.assertEquals("Wrong supported", port.getSupportedFeaturesV10(),
94                 createPortFeatures(serializedBuffer.readInt()));
95         Assert.assertEquals("Wrong peer", port.getPeerFeaturesV10(), createPortFeatures(serializedBuffer.readInt()));
96
97     }
98
99     private static List<PhyPort> createPorts() {
100         final List<PhyPort> ports = new ArrayList<>();
101         PhyPortBuilder builder = new PhyPortBuilder();
102         builder.setPortNo(Uint32.ONE);
103         builder.setHwAddr(new MacAddress("94:de:80:a6:61:40"));
104         builder.setName("Port name");
105         builder.setConfigV10(new PortConfigV10(true, false, true, false, true, false, true));
106         builder.setStateV10(new PortStateV10(true, false, true, false, true, false, true, false));
107         builder.setCurrentFeaturesV10(
108                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
109         builder.setAdvertisedFeaturesV10(
110                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
111         builder.setSupportedFeaturesV10(
112                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
113         builder.setPeerFeaturesV10(
114                 new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
115         ports.add(builder.build());
116         return ports;
117     }
118
119     private static PortConfigV10 createPortConfig(final long input) {
120         final Boolean _portDown = (input & 1 << 0) > 0;
121         final Boolean _noStp = (input & 1 << 1) > 0;
122         final Boolean _noRecv = (input & 1 << 2) > 0;
123         final Boolean _noRecvStp = (input & 1 << 3) > 0;
124         final Boolean _noFlood = (input & 1 << 4) > 0;
125         final Boolean _noFwd = (input & 1 << 5) > 0;
126         final Boolean _noPacketIn = (input & 1 << 6) > 0;
127         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
128     }
129
130     private static PortFeaturesV10 createPortFeatures(final long input) {
131         final Boolean _10mbHd = (input & 1 << 0) > 0;
132         final Boolean _10mbFd = (input & 1 << 1) > 0;
133         final Boolean _100mbHd = (input & 1 << 2) > 0;
134         final Boolean _100mbFd = (input & 1 << 3) > 0;
135         final Boolean _1gbHd = (input & 1 << 4) > 0;
136         final Boolean _1gbFd = (input & 1 << 5) > 0;
137         final Boolean _10gbFd = (input & 1 << 6) > 0;
138         final Boolean _copper = (input & 1 << 7) > 0;
139         final Boolean _fiber = (input & 1 << 8) > 0;
140         final Boolean _autoneg = (input & 1 << 9) > 0;
141         final Boolean _pause = (input & 1 << 10) > 0;
142         final Boolean _pauseAsym = (input & 1 << 11) > 0;
143         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd, _1gbHd, _autoneg, _copper,
144                 _fiber, _pause, _pauseAsym);
145     }
146
147     private static PortStateV10 createPortState(final long input) {
148         final Boolean _linkDown = (input & 1 << 0) > 0;
149         final Boolean _blocked = (input & 1 << 1) > 0;
150         final Boolean _live = (input & 1 << 2) > 0;
151         final Boolean _stpListen = (input & 1 << 3) > 0;
152         final Boolean _stpLearn = (input & 1 << 4) > 0;
153         final Boolean _stpForward = (input & 1 << 5) > 0;
154         final Boolean _stpBlock = (input & 1 << 6) > 0;
155         final Boolean _stpMask = (input & 1 << 7) > 0;
156         return new PortStateV10(_blocked, _linkDown, _live, _stpBlock, _stpForward, _stpLearn, _stpListen, _stpMask);
157     }
158
159     @SuppressWarnings("checkstyle:AbbreviationAsWordInName")
160     private static CapabilitiesV10 createCapabilities(final long input) {
161         final Boolean _oFPCFLOWSTATS = (input & 1 << 0) > 0;
162         final Boolean _oFPCTABLESTATS = (input & 1 << 1) > 0;
163         final Boolean _oFPCPORTSTATS = (input & 1 << 2) > 0;
164         final Boolean _oFPCSTP = (input & 1 << 3) > 0;
165         final Boolean _oFPCRESERVED = (input & 1 << 4) > 0;
166         final Boolean _oFPCIPREASM = (input & 1 << 5) > 0;
167         final Boolean _oFPCQUEUESTATS = (input & 1 << 6) > 0;
168         final Boolean _oFPCARPMATCHIP = (input & 1 << 7) > 0;
169         return new CapabilitiesV10(_oFPCARPMATCHIP, _oFPCFLOWSTATS, _oFPCIPREASM, _oFPCPORTSTATS, _oFPCQUEUESTATS,
170                 _oFPCRESERVED, _oFPCSTP, _oFPCTABLESTATS);
171     }
172
173     @SuppressWarnings("checkstyle:AbbreviationAsWordInName")
174     private static ActionTypeV10 createActionsV10(final long input) {
175         final Boolean _oFPATOUTPUT = (input & 1 << 0) > 0;
176         final Boolean _oFPATSETVLANVID = (input & 1 << 1) > 0;
177         final Boolean _oFPATSETVLANPCP = (input & 1 << 2) > 0;
178         final Boolean _oFPATSTRIPVLAN = (input & 1 << 3) > 0;
179         final Boolean _oFPATSETDLSRC = (input & 1 << 4) > 0;
180         final Boolean _oFPATSETDLDST = (input & 1 << 5) > 0;
181         final Boolean _oFPATSETNWSRC = (input & 1 << 6) > 0;
182         final Boolean _oFPATSETNWDST = (input & 1 << 7) > 0;
183         final Boolean _oFPATSETNWTOS = (input & 1 << 8) > 0;
184         final Boolean _oFPATSETTPSRC = (input & 1 << 9) > 0;
185         final Boolean _oFPATSETTPDST = (input & 1 << 10) > 0;
186         final Boolean _oFPATENQUEUE = (input & 1 << 11) > 0;
187         final Boolean _oFPATVENDOR = (input & 1 << 12) > 0;
188         return new ActionTypeV10(_oFPATENQUEUE, _oFPATOUTPUT, _oFPATSETDLDST, _oFPATSETDLSRC, _oFPATSETNWDST,
189                 _oFPATSETNWSRC, _oFPATSETNWTOS, _oFPATSETTPDST, _oFPATSETTPSRC, _oFPATSETVLANPCP, _oFPATSETVLANVID,
190                 _oFPATSTRIPVLAN, _oFPATVENDOR);
191     }
192 }