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