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