edf21a2c6f1cce1b05feb5103891fa4e27b58960
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactoryTest.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
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
24 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
25 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
26 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice._goto.table._case.GotoTableBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.write.metadata._case.WriteMetadataBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
56
57 /**
58  * @author timotej.kubas
59  * @author michal.polkorab
60  */
61 public class FlowModInputMessageFactoryTest {
62     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
63     private SerializerRegistry registry;
64     private OFSerializer<FlowModInput> flowModFactory;
65
66     /**
67      * Initializes serializer registry and stores correct factory in field
68      */
69     @Before
70     public void startUp() {
71         registry = new SerializerRegistryImpl();
72         registry.init();
73         flowModFactory = registry.getSerializer(new MessageTypeKey<>(
74                 EncodeConstants.OF13_VERSION_ID, FlowModInput.class));
75     }
76
77     /**
78      * @throws Exception
79      * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO
80      */
81     @Test
82     public void testFlowModInputMessageFactory() throws Exception {
83         FlowModInputBuilder builder = new FlowModInputBuilder();
84         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
85         byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
86         builder.setCookie(new BigInteger(1, cookie));
87         byte[] cookieMask = new byte[]{(byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30};
88         builder.setCookieMask(new BigInteger(1, cookieMask));
89         builder.setTableId(new TableId(65L));
90         builder.setCommand(FlowModCommand.forValue(2));
91         builder.setIdleTimeout(12);
92         builder.setHardTimeout(0);
93         builder.setPriority(126);
94         builder.setBufferId(2L);
95         builder.setOutPort(new PortNumber(4422L));
96         builder.setOutGroup(98L);
97         builder.setFlags(new FlowModFlags(true, false, true, false, true));
98         MatchBuilder matchBuilder = new MatchBuilder();
99         matchBuilder.setType(OxmMatchType.class);
100         List<MatchEntry> entries = new ArrayList<>();
101         MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
102         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
103         entriesBuilder.setOxmMatchField(InPhyPort.class);
104         entriesBuilder.setHasMask(false);
105         InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
106         InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
107         inPhyPortBuilder.setPortNumber(new PortNumber(42L));
108         inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
109         entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
110         entries.add(entriesBuilder.build());
111         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
112         entriesBuilder.setOxmMatchField(IpEcn.class);
113         entriesBuilder.setHasMask(false);
114         IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
115         IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
116         ipEcnBuilder.setEcn((short) 4);
117         ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
118         entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
119         entries.add(entriesBuilder.build());
120         matchBuilder.setMatchEntry(entries);
121         builder.setMatch(matchBuilder.build());
122         List<Instruction> instructions = new ArrayList<>();
123         InstructionBuilder insBuilder = new InstructionBuilder();
124         GotoTableCaseBuilder goToCaseBuilder = new GotoTableCaseBuilder();
125         GotoTableBuilder instructionBuilder = new GotoTableBuilder();
126         instructionBuilder.setTableId((short) 43);
127         goToCaseBuilder.setGotoTable(instructionBuilder.build());
128         insBuilder.setInstructionChoice(goToCaseBuilder.build());
129         instructions.add(insBuilder.build());
130         WriteMetadataCaseBuilder metadataCaseBuilder = new WriteMetadataCaseBuilder();
131         WriteMetadataBuilder metadataBuilder = new WriteMetadataBuilder();
132         metadataBuilder.setMetadata(cookie);
133         metadataBuilder.setMetadataMask(cookieMask);
134         metadataCaseBuilder.setWriteMetadata(metadataBuilder.build());
135         insBuilder.setInstructionChoice(metadataCaseBuilder.build());
136         instructions.add(insBuilder.build());
137         insBuilder = new InstructionBuilder();
138         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
139         ApplyActionsBuilder actionsBuilder = new ApplyActionsBuilder();
140         List<Action> actions = new ArrayList<>();
141         ActionBuilder actionBuilder = new ActionBuilder();
142         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
143         OutputActionBuilder outputBuilder = new OutputActionBuilder();
144         outputBuilder.setPort(new PortNumber(42L));
145         outputBuilder.setMaxLength(52);
146         caseBuilder.setOutputAction(outputBuilder.build());
147         actionBuilder.setActionChoice(caseBuilder.build());
148         actions.add(actionBuilder.build());
149         actionsBuilder.setAction(actions);
150         applyActionsCaseBuilder.setApplyActions(actionsBuilder.build());
151         insBuilder.setInstructionChoice(applyActionsCaseBuilder.build());
152         instructions.add(insBuilder.build());
153         builder.setInstruction(instructions);
154         FlowModInput message = builder.build();
155
156         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
157         flowModFactory.serialize(message, out);
158
159         BufferHelper.checkHeaderV13(out,(byte) 14, 128);
160         cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
161         out.readBytes(cookie);
162         Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
163         cookieMask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
164         out.readBytes(cookieMask);
165         Assert.assertEquals("Wrong cookieMask", message.getCookieMask(), new BigInteger(1,  cookieMask));
166         Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readUnsignedByte());
167         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedByte());
168         Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort());
169         Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort());
170         Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readUnsignedShort());
171         Assert.assertEquals("Wrong bufferId", message.getBufferId().intValue(), out.readUnsignedInt());
172         Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt());
173         Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt());
174         Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFlagsFromBitmap(out.readUnsignedShort()));
175         out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE);
176         Assert.assertEquals("Wrong match type", 1, out.readUnsignedShort());
177         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
178         Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort());
179         short fieldAndMask = out.readUnsignedByte();
180         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
181         Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
182         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
183         Assert.assertEquals("Wrong oxm value", 42, out.readUnsignedInt());
184         Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort());
185         fieldAndMask = out.readUnsignedByte();
186         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
187         Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
188         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
189         Assert.assertEquals("Wrong oxm value", 4, out.readUnsignedByte());
190         out.skipBytes(7);
191         Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());
192         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
193         Assert.assertEquals("Wrong instruction value", 43, out.readUnsignedByte());
194         out.skipBytes(3);
195         Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
196         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
197         out.skipBytes(EncodeConstants.SIZE_OF_INT_IN_BYTES);
198         byte[] cookieRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
199         out.readBytes(cookieRead);
200         byte[] cookieMaskRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
201         out.readBytes(cookieMaskRead);
202         Assert.assertArrayEquals("Wrong metadata", cookie, cookieRead);
203         Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead);
204         Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
205         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
206         out.skipBytes(4);
207         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
208         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
209         Assert.assertEquals("Wrong port", 42, out.readUnsignedInt());
210         Assert.assertEquals("Wrong max-length", 52, out.readUnsignedShort());
211         out.skipBytes(6);
212         Assert.assertTrue("Unread data", out.readableBytes() == 0);
213     }
214
215     private static FlowModFlags createFlowModFlagsFromBitmap(int input){
216         final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0;
217         final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0;
218         final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0;
219         final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0;
220         final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0;
221         return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS, _oFPFFSENDFLOWREM);
222     }
223
224 }