2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.List;
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;
51 * @author timotej.kubas
52 * @author michal.polkorab
54 public class FlowModInputMessageFactoryTest {
55 private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
59 * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO
62 public void testFlowModInputMessageFactory() throws Exception {
63 FlowModInputBuilder builder = new FlowModInputBuilder();
64 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
65 byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
66 builder.setCookie(new BigInteger(1, cookie));
67 byte[] cookieMask = new byte[]{(byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30};
68 builder.setCookieMask(new BigInteger(1, 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();
114 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
115 FlowModInputMessageFactory factory = FlowModInputMessageFactory.getInstance();
116 factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);
118 BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message));
119 cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
120 out.readBytes(cookie);
121 Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
122 cookieMask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
123 out.readBytes(cookieMask);
124 Assert.assertEquals("Wrong cookieMask", message.getCookieMask(), new BigInteger(1, cookieMask));
125 Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readUnsignedByte());
126 Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedByte());
127 Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort());
128 Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort());
129 Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readUnsignedShort());
130 Assert.assertEquals("Wrong bufferId", message.getBufferId().intValue(), out.readUnsignedInt());
131 Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt());
132 Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt());
133 Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFlagsFromBitmap(out.readUnsignedShort()));
134 out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE);
135 Assert.assertEquals("Wrong match type", 1, out.readUnsignedShort());
136 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
137 Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort());
138 short fieldAndMask = out.readUnsignedByte();
139 Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
140 Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
141 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
142 Assert.assertEquals("Wrong oxm value", 42, out.readUnsignedInt());
143 Assert.assertEquals("Wrong oxm class", 0, out.readUnsignedShort());
144 fieldAndMask = out.readUnsignedByte();
145 Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
146 Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
147 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
148 Assert.assertEquals("Wrong oxm value", 4, out.readUnsignedByte());
150 Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());
151 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
152 Assert.assertEquals("Wrong instruction value", 43, out.readUnsignedByte());
154 Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
155 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
156 out.skipBytes(EncodeConstants.SIZE_OF_INT_IN_BYTES);
157 byte[] cookieRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
158 out.readBytes(cookieRead);
159 byte[] cookieMaskRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
160 out.readBytes(cookieMaskRead);
161 Assert.assertArrayEquals("Wrong metadata", cookie, cookieRead);
162 Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead);
165 private static FlowModFlags createFlowModFlagsFromBitmap(int input){
166 final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0;
167 final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0;
168 final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0;
169 final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0;
170 final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0;
171 return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS, _oFPFFSENDFLOWREM);