2 * Copyright (c) 2015 NetIDE Consortium 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
8 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10 import io.netty.buffer.ByteBuf;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice._goto.table._case.GotoTableBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.write.metadata._case.WriteMetadataBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
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.rev150225.InPhyPort;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
54 * @author giuseppex.petralia@intel.com
57 public class FlowModInputMessageFactoryTest {
58 private OFDeserializer<FlowModInput> flowFactory;
61 * Initializes deserializer registry and lookups correct deserializer
64 public void startUp() {
65 DeserializerRegistry registry = new DeserializerRegistryImpl();
67 flowFactory = registry
68 .getDeserializer(new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 14, FlowModInput.class));
72 public void test() throws Exception {
73 ByteBuf bb = BufferHelper
74 .buildBuffer("ff 01 04 01 06 00 07 01 ff 05 00 00 09 30 00 30 41 02 00 0c 00 00 00 7e 00 "
75 + "00 00 02 00 00 11 46 00 00 00 62 00 0b 00 00 00 01 00 11 80 00 02 04 00 00 00 2a 80 00 12 01 04 00 "
76 + "00 00 00 00 00 00 00 01 00 08 2b 00 00 00 00 02 00 18 00 00 00 00 ff 01 04 01 06 00 07 01 ff 05 00 00 "
77 + "09 30 00 30 00 04 00 18 00 00 00 00 00 00 00 10 00 00 00 2a 00 34 00 00 00 00 00 00");
78 FlowModInput deserializedMessage = BufferHelper.deserialize(flowFactory, bb);
79 BufferHelper.checkHeaderV13(deserializedMessage);
80 byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
81 Assert.assertEquals("Wrong cookie", new BigInteger(1, cookie), deserializedMessage.getCookie());
82 byte[] cookieMask = new byte[] { (byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30 };
83 Assert.assertEquals("Wrong cookie mask", new BigInteger(1, cookieMask), deserializedMessage.getCookieMask());
84 Assert.assertEquals("Wrong table id", new TableId(65L), deserializedMessage.getTableId());
85 Assert.assertEquals("Wrong command", FlowModCommand.forValue(2), deserializedMessage.getCommand());
86 Assert.assertEquals("Wrong idle timeout", 12, deserializedMessage.getIdleTimeout().intValue());
87 Assert.assertEquals("Wrong hard timeout", 0, deserializedMessage.getHardTimeout().intValue());
88 Assert.assertEquals("Wrong priority", 126, deserializedMessage.getPriority().intValue());
89 Assert.assertEquals("Wrong buffer id ", 2L, deserializedMessage.getBufferId().longValue());
90 Assert.assertEquals("Wrong out port", new PortNumber(4422L), deserializedMessage.getOutPort());
91 Assert.assertEquals("Wrong out group", 98L, deserializedMessage.getOutGroup().longValue());
92 Assert.assertEquals("Wrong flags", new FlowModFlags(true, false, true, false, true),
93 deserializedMessage.getFlags());
94 Assert.assertEquals("Wrong match", createMatch(), deserializedMessage.getMatch());
95 Assert.assertEquals("Wrong instructions", createInstructions(), deserializedMessage.getInstruction());
99 private List<Instruction> createInstructions() {
100 List<Instruction> instructions = new ArrayList<>();
101 InstructionBuilder insBuilder = new InstructionBuilder();
102 GotoTableCaseBuilder goToCaseBuilder = new GotoTableCaseBuilder();
103 GotoTableBuilder instructionBuilder = new GotoTableBuilder();
104 instructionBuilder.setTableId((short) 43);
105 goToCaseBuilder.setGotoTable(instructionBuilder.build());
106 insBuilder.setInstructionChoice(goToCaseBuilder.build());
107 instructions.add(insBuilder.build());
108 WriteMetadataCaseBuilder metadataCaseBuilder = new WriteMetadataCaseBuilder();
109 WriteMetadataBuilder metadataBuilder = new WriteMetadataBuilder();
110 byte[] metadata = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
111 metadataBuilder.setMetadata(metadata);
112 byte[] metadataMask = new byte[] { (byte) 0xFF, 0x05, 0x00, 0x00, 0x09, 0x30, 0x00, 0x30 };
113 metadataBuilder.setMetadataMask(metadataMask);
114 metadataCaseBuilder.setWriteMetadata(metadataBuilder.build());
115 insBuilder.setInstructionChoice(metadataCaseBuilder.build());
116 instructions.add(insBuilder.build());
117 insBuilder = new InstructionBuilder();
118 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
119 ApplyActionsBuilder actionsBuilder = new ApplyActionsBuilder();
120 List<Action> actions = new ArrayList<>();
121 ActionBuilder actionBuilder = new ActionBuilder();
122 OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
123 OutputActionBuilder outputBuilder = new OutputActionBuilder();
124 outputBuilder.setPort(new PortNumber(42L));
125 outputBuilder.setMaxLength(52);
126 caseBuilder.setOutputAction(outputBuilder.build());
127 actionBuilder.setActionChoice(caseBuilder.build());
128 actions.add(actionBuilder.build());
129 actionsBuilder.setAction(actions);
130 applyActionsCaseBuilder.setApplyActions(actionsBuilder.build());
131 insBuilder.setInstructionChoice(applyActionsCaseBuilder.build());
132 instructions.add(insBuilder.build());
136 private Match createMatch() {
137 MatchBuilder matchBuilder = new MatchBuilder();
138 matchBuilder.setType(OxmMatchType.class);
139 List<MatchEntry> entries = new ArrayList<>();
140 MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
141 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
142 entriesBuilder.setOxmMatchField(InPhyPort.class);
143 entriesBuilder.setHasMask(false);
144 InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
145 InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
146 inPhyPortBuilder.setPortNumber(new PortNumber(42L));
147 inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
148 entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
149 entries.add(entriesBuilder.build());
150 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
151 entriesBuilder.setOxmMatchField(IpEcn.class);
152 entriesBuilder.setHasMask(false);
153 IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
154 IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
155 ipEcnBuilder.setEcn((short) 4);
156 ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
157 entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
158 entries.add(entriesBuilder.build());
159 matchBuilder.setMatchEntry(entries);
160 return matchBuilder.build();