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