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