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.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
24 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
25 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
26 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionsInstruction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionsInstructionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstructionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
61 * @author timotej.kubas
62 * @author michal.polkorab
64 public class FlowModInputMessageFactoryTest {
65 private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
66 private SerializerRegistry registry;
67 private OFSerializer<FlowModInput> flowModFactory;
70 * Initializes serializer registry and stores correct factory in field
73 public void startUp() {
74 registry = new SerializerRegistryImpl();
76 flowModFactory = registry.getSerializer(new MessageTypeKey<>(
77 EncodeConstants.OF13_VERSION_ID, FlowModInput.class));
82 * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO
85 public void testFlowModInputMessageFactory() throws Exception {
86 FlowModInputBuilder builder = new FlowModInputBuilder();
87 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
88 byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
89 builder.setCookie(new BigInteger(1, cookie));
90 byte[] cookieMask = new byte[]{(byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30};
91 builder.setCookieMask(new BigInteger(1, cookieMask));
92 builder.setTableId(new TableId(65L));
93 builder.setCommand(FlowModCommand.forValue(2));
94 builder.setIdleTimeout(12);
95 builder.setHardTimeout(0);
96 builder.setPriority(126);
97 builder.setBufferId(2L);
98 builder.setOutPort(new PortNumber(4422L));
99 builder.setOutGroup(98L);
100 builder.setFlags(new FlowModFlags(true, false, true, false, true));
101 MatchBuilder matchBuilder = new MatchBuilder();
102 matchBuilder.setType(OxmMatchType.class);
103 List<MatchEntry> entries = new ArrayList<>();
104 MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
105 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
106 entriesBuilder.setOxmMatchField(InPhyPort.class);
107 entriesBuilder.setHasMask(false);
108 InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
109 InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
110 inPhyPortBuilder.setPortNumber(new PortNumber(42L));
111 inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
112 entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
113 entries.add(entriesBuilder.build());
114 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
115 entriesBuilder.setOxmMatchField(IpEcn.class);
116 entriesBuilder.setHasMask(false);
117 IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
118 IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
119 ipEcnBuilder.setEcn((short) 4);
120 ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
121 entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
122 entries.add(entriesBuilder.build());
123 matchBuilder.setMatchEntry(entries);
124 builder.setMatch(matchBuilder.build());
125 List<Instruction> instructions = new ArrayList<>();
126 InstructionBuilder insBuilder = new InstructionBuilder();
127 insBuilder.setType(GotoTable.class);
128 TableIdInstructionBuilder idBuilder = new TableIdInstructionBuilder();
129 idBuilder.setTableId((short) 43);
130 insBuilder.addAugmentation(TableIdInstruction.class, idBuilder.build());
131 instructions.add(insBuilder.build());
132 insBuilder.setType(WriteMetadata.class);
133 MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();
134 metaBuilder.setMetadata(cookie);
135 metaBuilder.setMetadataMask(cookieMask);
136 insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
137 instructions.add(insBuilder.build());
138 insBuilder = new InstructionBuilder();
139 insBuilder.setType(ApplyActions.class);
140 List<Action> actions = new ArrayList<>();
141 ActionBuilder actionBuilder = new ActionBuilder();
142 OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
143 OutputActionBuilder outputBuilder = new OutputActionBuilder();
144 outputBuilder.setPort(new PortNumber(42L));
145 outputBuilder.setMaxLength(52);
146 caseBuilder.setOutputAction(outputBuilder.build());
147 actionBuilder.setActionChoice(caseBuilder.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);