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