7dbd6bf237328a8a9cc10e64f2ea483e94ba4f72
[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.Test;
20 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntryBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
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.rev130731.InPhyPort;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Nxm0Class;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
50
51 /**
52  * @author timotej.kubas
53  * @author michal.polkorab
54  */
55 public class FlowModInputMessageFactoryTest {
56     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
57     
58     /**
59      * @throws Exception 
60      * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO
61      */
62     @Test
63     public void testFlowModInputMessageFactory() throws Exception {
64         FlowModInputBuilder builder = new FlowModInputBuilder();
65         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
66         byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
67         builder.setCookie(new BigInteger(1, cookie));
68         byte[] cookieMask = new byte[]{(byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30};
69         builder.setCookieMask(new BigInteger(1, cookieMask));
70         builder.setTableId(new TableId(65L));
71         builder.setCommand(FlowModCommand.forValue(2));
72         builder.setIdleTimeout(12);
73         builder.setHardTimeout(0);
74         builder.setPriority(126);
75         builder.setBufferId(2L);
76         builder.setOutPort(new PortNumber(4422L));
77         builder.setOutGroup(98L);
78         builder.setFlags(new FlowModFlags(true, false, true, false, true));
79         MatchBuilder matchBuilder = new MatchBuilder();
80         matchBuilder.setType(OxmMatchType.class);
81         List<MatchEntries> entries = new ArrayList<>();
82         MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
83         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
84         entriesBuilder.setOxmMatchField(InPhyPort.class);
85         entriesBuilder.setHasMask(false);
86         PortNumberMatchEntryBuilder portNumberBuilder = new PortNumberMatchEntryBuilder();
87         portNumberBuilder.setPortNumber(new PortNumber(42L));
88         entriesBuilder.addAugmentation(PortNumberMatchEntry.class, portNumberBuilder.build());
89         entries.add(entriesBuilder.build());
90         entriesBuilder.setOxmClass(Nxm0Class.class);
91         entriesBuilder.setOxmMatchField(IpEcn.class);
92         entriesBuilder.setHasMask(false);
93         EcnMatchEntryBuilder ecnBuilder = new EcnMatchEntryBuilder();
94         ecnBuilder.setEcn((short) 4);
95         entriesBuilder.addAugmentation(EcnMatchEntry.class, ecnBuilder.build());
96         entries.add(entriesBuilder.build());
97         matchBuilder.setMatchEntries(entries);
98         builder.setMatch(matchBuilder.build());
99         List<Instruction> instructions = new ArrayList<>();
100         InstructionBuilder insBuilder = new InstructionBuilder();
101         insBuilder.setType(GotoTable.class);
102         TableIdInstructionBuilder idBuilder = new TableIdInstructionBuilder();
103         idBuilder.setTableId((short) 43);
104         insBuilder.addAugmentation(TableIdInstruction.class, idBuilder.build());
105         instructions.add(insBuilder.build());
106         insBuilder.setType(WriteMetadata.class);
107         MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();
108         metaBuilder.setMetadata(cookie);
109         metaBuilder.setMetadataMask(cookieMask);
110         insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
111         instructions.add(insBuilder.build());
112         insBuilder = new InstructionBuilder();
113         insBuilder.setType(ApplyActions.class);
114         insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
115         instructions.add(insBuilder.build());
116         builder.setInstruction(instructions);
117         FlowModInput message = builder.build();
118         
119         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
120         FlowModInputMessageFactory factory = FlowModInputMessageFactory.getInstance();
121         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);
122         
123         BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message));
124         cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
125         out.readBytes(cookie);
126         Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
127         cookieMask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
128         out.readBytes(cookieMask);
129         Assert.assertEquals("Wrong cookieMask", message.getCookieMask(), new BigInteger(1,  cookieMask));
130         Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readUnsignedByte());
131         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedByte());
132         Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort());
133         Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort());
134         Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readUnsignedShort());
135         Assert.assertEquals("Wrong bufferId", message.getBufferId().intValue(), out.readUnsignedInt());
136         Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt());
137         Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt());
138         Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFlagsFromBitmap(out.readUnsignedShort()));
139         out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE);
140         Assert.assertEquals("Wrong match type", 1, out.readUnsignedShort());
141         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
142         Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort());
143         short fieldAndMask = out.readUnsignedByte();
144         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
145         Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
146         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
147         Assert.assertEquals("Wrong oxm value", 42, out.readUnsignedInt());
148         Assert.assertEquals("Wrong oxm class", 0, out.readUnsignedShort());
149         fieldAndMask = out.readUnsignedByte();
150         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
151         Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
152         out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
153         Assert.assertEquals("Wrong oxm value", 4, out.readUnsignedByte());
154         out.skipBytes(7);
155         Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());
156         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
157         Assert.assertEquals("Wrong instruction value", 43, out.readUnsignedByte());
158         out.skipBytes(3);
159         Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
160         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
161         out.skipBytes(EncodeConstants.SIZE_OF_INT_IN_BYTES);
162         byte[] cookieRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
163         out.readBytes(cookieRead);
164         byte[] cookieMaskRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
165         out.readBytes(cookieMaskRead);
166         Assert.assertArrayEquals("Wrong metadata", cookie, cookieRead);
167         Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead);
168         Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
169         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
170         out.skipBytes(4);
171         Assert.assertTrue("Unread data", out.readableBytes() == 0);
172     }
173     
174     private static FlowModFlags createFlowModFlagsFromBitmap(int input){
175         final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0;
176         final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0;
177         final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0; 
178         final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0;
179         final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0;
180         return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS, _oFPFFSENDFLOWREM);
181     }
182
183 }