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.Before;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
23 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
24 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
25 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
26 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EcnMatchEntryBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpEcn;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
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;
64 * @author timotej.kubas
65 * @author michal.polkorab
67 public class FlowModInputMessageFactoryTest {
68 private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
69 private SerializerRegistry registry;
70 private OFSerializer<FlowModInput> flowModFactory;
73 * Initializes serializer registry and stores correct factory in field
76 public void startUp() {
77 registry = new SerializerRegistryImpl();
79 flowModFactory = registry.getSerializer(
80 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, FlowModInput.class));
85 * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO
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<MatchEntries> entries = new ArrayList<>();
107 MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder();
108 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
109 entriesBuilder.setOxmMatchField(InPhyPort.class);
110 entriesBuilder.setHasMask(false);
111 PortNumberMatchEntryBuilder portNumberBuilder = new PortNumberMatchEntryBuilder();
112 portNumberBuilder.setPortNumber(new PortNumber(42L));
113 entriesBuilder.addAugmentation(PortNumberMatchEntry.class, portNumberBuilder.build());
114 entries.add(entriesBuilder.build());
115 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
116 entriesBuilder.setOxmMatchField(IpEcn.class);
117 entriesBuilder.setHasMask(false);
118 EcnMatchEntryBuilder ecnBuilder = new EcnMatchEntryBuilder();
119 ecnBuilder.setEcn((short) 4);
120 entriesBuilder.addAugmentation(EcnMatchEntry.class, ecnBuilder.build());
121 entries.add(entriesBuilder.build());
122 matchBuilder.setMatchEntries(entries);
123 builder.setMatch(matchBuilder.build());
124 List<Instruction> instructions = new ArrayList<>();
125 InstructionBuilder insBuilder = new InstructionBuilder();
126 insBuilder.setType(GotoTable.class);
127 TableIdInstructionBuilder idBuilder = new TableIdInstructionBuilder();
128 idBuilder.setTableId((short) 43);
129 insBuilder.addAugmentation(TableIdInstruction.class, idBuilder.build());
130 instructions.add(insBuilder.build());
131 insBuilder.setType(WriteMetadata.class);
132 MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();
133 metaBuilder.setMetadata(cookie);
134 metaBuilder.setMetadataMask(cookieMask);
135 insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
136 instructions.add(insBuilder.build());
137 insBuilder = new InstructionBuilder();
138 insBuilder.setType(ApplyActions.class);
139 List<Action> actions = new ArrayList<>();
140 ActionBuilder actionBuilder = new ActionBuilder();
141 actionBuilder.setType(Output.class);
142 PortActionBuilder port = new PortActionBuilder();
143 port.setPort(new PortNumber(42L));
144 actionBuilder.addAugmentation(PortAction.class, port.build());
145 MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
146 maxLen.setMaxLength(52);
147 actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
148 actions.add(actionBuilder.build());
149 ActionsInstructionBuilder actionInstructionBuilder = new ActionsInstructionBuilder();
150 actionInstructionBuilder.setAction(actions);
151 insBuilder.addAugmentation(ActionsInstruction.class, actionInstructionBuilder.build());
152 instructions.add(insBuilder.build());
153 builder.setInstruction(instructions);
154 FlowModInput message = builder.build();
156 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
157 flowModFactory.serialize(message, out);
159 BufferHelper.checkHeaderV13(out,(byte) 14, 128);
160 cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
161 out.readBytes(cookie);
162 Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
163 cookieMask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
164 out.readBytes(cookieMask);
165 Assert.assertEquals("Wrong cookieMask", message.getCookieMask(), new BigInteger(1, cookieMask));
166 Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readUnsignedByte());
167 Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedByte());
168 Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort());
169 Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort());
170 Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readUnsignedShort());
171 Assert.assertEquals("Wrong bufferId", message.getBufferId().intValue(), out.readUnsignedInt());
172 Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt());
173 Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt());
174 Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFlagsFromBitmap(out.readUnsignedShort()));
175 out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE);
176 Assert.assertEquals("Wrong match type", 1, out.readUnsignedShort());
177 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
178 Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort());
179 short fieldAndMask = out.readUnsignedByte();
180 Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
181 Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
182 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
183 Assert.assertEquals("Wrong oxm value", 42, out.readUnsignedInt());
184 Assert.assertEquals("Wrong oxm class", 0x8000, out.readUnsignedShort());
185 fieldAndMask = out.readUnsignedByte();
186 Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
187 Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
188 out.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
189 Assert.assertEquals("Wrong oxm value", 4, out.readUnsignedByte());
191 Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());
192 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
193 Assert.assertEquals("Wrong instruction value", 43, out.readUnsignedByte());
195 Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
196 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
197 out.skipBytes(EncodeConstants.SIZE_OF_INT_IN_BYTES);
198 byte[] cookieRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
199 out.readBytes(cookieRead);
200 byte[] cookieMaskRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
201 out.readBytes(cookieMaskRead);
202 Assert.assertArrayEquals("Wrong metadata", cookie, cookieRead);
203 Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead);
204 Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
205 Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
207 Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
208 Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
209 Assert.assertEquals("Wrong port", 42, out.readUnsignedInt());
210 Assert.assertEquals("Wrong max-length", 52, out.readUnsignedShort());
212 Assert.assertTrue("Unread data", out.readableBytes() == 0);
215 private static FlowModFlags createFlowModFlagsFromBitmap(int input){
216 final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0;
217 final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0;
218 final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0;
219 final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0;
220 final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0;
221 return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS, _oFPFFSENDFLOWREM);