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
8 package org.opendaylight.openflowjava.protocol.impl.util;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlActionBuilder;
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.TableIdInstruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
57 * @author michal.polkorab
60 public class OF13InstructionsSerializerTest {
62 private SerializerRegistry registry;
65 * Initializes serializer table and stores correct factory in field
68 public void startUp() {
69 registry = new SerializerRegistryImpl();
74 * Testing instructions translation
78 List<Instruction> instructions = new ArrayList<>();
79 // Goto_table instruction
80 InstructionBuilder builder = new InstructionBuilder();
81 builder.setType(GotoTable.class);
82 TableIdInstructionBuilder tableIdBuilder = new TableIdInstructionBuilder();
83 tableIdBuilder.setTableId((short) 5);
84 builder.addAugmentation(TableIdInstruction.class, tableIdBuilder.build());
85 instructions.add(builder.build());
86 builder = new InstructionBuilder();
87 // Write_metadata instruction
88 builder.setType(WriteMetadata.class);
89 MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();
90 metaBuilder.setMetadata(ByteBufUtils.hexStringToBytes("00 01 02 03 04 05 06 07"));
91 metaBuilder.setMetadataMask(ByteBufUtils.hexStringToBytes("07 06 05 04 03 02 01 00"));
92 builder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
93 instructions.add(builder.build());
94 // Clear_actions instruction
95 builder = new InstructionBuilder();
96 builder.setType(ClearActions.class);
97 instructions.add(builder.build());
99 builder = new InstructionBuilder();
100 builder.setType(Meter.class);
101 MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();
102 meterBuilder.setMeterId(42L);
103 builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());
104 instructions.add(builder.build());
105 // Write_actions instruction
106 builder = new InstructionBuilder();
107 builder.setType(WriteActions.class);
108 ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();
109 List<Action> actions = new ArrayList<>();
110 ActionBuilder actionBuilder = new ActionBuilder();
111 actionBuilder.setType(Output.class);
112 PortActionBuilder portBuilder = new PortActionBuilder();
113 portBuilder.setPort(new PortNumber(45L));
114 actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
115 MaxLengthActionBuilder maxBuilder = new MaxLengthActionBuilder();
116 maxBuilder.setMaxLength(55);
117 actionBuilder.addAugmentation(MaxLengthAction.class, maxBuilder.build());
118 actions.add(actionBuilder.build());
119 actionBuilder = new ActionBuilder();
120 actionBuilder.setType(SetNwTtl.class);
121 NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();
122 nwTtl.setNwTtl((short) 64);
123 actionBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());
124 actions.add(actionBuilder.build());
125 actionsBuilder.setAction(actions);
126 builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());
127 instructions.add(builder.build());
128 // Apply_actions instruction
129 builder = new InstructionBuilder();
130 builder.setType(ApplyActions.class);
131 actionsBuilder = new ActionsInstructionBuilder();
132 actions = new ArrayList<>();
133 actionBuilder = new ActionBuilder();
134 actionBuilder.setType(PushVlan.class);
135 EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();
136 ethertypeBuilder.setEthertype(new EtherType(14));
137 actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());
138 actions.add(actionBuilder.build());
139 actionBuilder = new ActionBuilder();
140 actionBuilder.setType(PopPbb.class);
141 actions.add(actionBuilder.build());
142 actionsBuilder.setAction(actions);
143 builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());
144 instructions.add(builder.build());
146 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
147 ListSerializer.serializeList(instructions, TypeKeyMakerFactory
148 .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
150 Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());
151 Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
152 Assert.assertEquals("Wrong instruction table-id", 5, out.readUnsignedByte());
154 Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
155 Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
157 byte[] actual = new byte[8];
158 out.readBytes(actual);
159 Assert.assertEquals("Wrong instruction metadata", "00 01 02 03 04 05 06 07",
160 ByteBufUtils.bytesToHexString(actual));
161 actual = new byte[8];
162 out.readBytes(actual);
163 Assert.assertEquals("Wrong instruction metadata-mask", "07 06 05 04 03 02 01 00",
164 ByteBufUtils.bytesToHexString(actual));
165 Assert.assertEquals("Wrong instruction type", 5, out.readUnsignedShort());
166 Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
168 Assert.assertEquals("Wrong instruction type", 6, out.readUnsignedShort());
169 Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
170 Assert.assertEquals("Wrong instruction meter-id", 42, out.readUnsignedInt());
171 Assert.assertEquals("Wrong instruction type", 3, out.readUnsignedShort());
172 Assert.assertEquals("Wrong instruction length", 32, out.readUnsignedShort());
174 Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
175 Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
176 Assert.assertEquals("Wrong action type", 45, out.readUnsignedInt());
177 Assert.assertEquals("Wrong action type", 55, out.readUnsignedShort());
179 Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());
180 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
181 Assert.assertEquals("Wrong action type", 64, out.readUnsignedByte());
183 Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
184 Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
186 Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
187 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
188 Assert.assertEquals("Wrong action ethertype", 14, out.readUnsignedShort());
190 Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());
191 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
193 Assert.assertTrue("Not all data were read", out.readableBytes() == 0);