Bug 2756 - Action model update
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF13InstructionsSerializerTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.util;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
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.rev150225.ActionsInstruction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionsInstructionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstruction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstructionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MeterIdInstruction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MeterIdInstructionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstructionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopPbbCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwTtlCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.vlan._case.PushVlanActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.ttl._case.SetNwTtlActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
50
51 /**
52  * @author michal.polkorab
53  *
54  */
55 public class OF13InstructionsSerializerTest {
56
57     private SerializerRegistry registry;
58
59     /**
60      * Initializes serializer table and stores correct factory in field
61      */
62     @Before
63     public void startUp() {
64         registry = new SerializerRegistryImpl();
65         registry.init();
66     }
67
68     /**
69      * Testing instructions translation
70      */
71     @Test
72     public void test() {
73         List<Instruction> instructions = new ArrayList<>();
74         // Goto_table instruction
75         InstructionBuilder builder = new InstructionBuilder();
76         builder.setType(GotoTable.class);
77         TableIdInstructionBuilder tableIdBuilder = new TableIdInstructionBuilder();
78         tableIdBuilder.setTableId((short) 5);
79         builder.addAugmentation(TableIdInstruction.class, tableIdBuilder.build());
80         instructions.add(builder.build());
81         builder = new InstructionBuilder();
82         // Write_metadata instruction
83         builder.setType(WriteMetadata.class);
84         MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();
85         metaBuilder.setMetadata(ByteBufUtils.hexStringToBytes("00 01 02 03 04 05 06 07"));
86         metaBuilder.setMetadataMask(ByteBufUtils.hexStringToBytes("07 06 05 04 03 02 01 00"));
87         builder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
88         instructions.add(builder.build());
89         // Clear_actions instruction
90         builder = new InstructionBuilder();
91         builder.setType(ClearActions.class);
92         instructions.add(builder.build());
93         // Meter instruction
94         builder = new InstructionBuilder();
95         builder.setType(Meter.class);
96         MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();
97         meterBuilder.setMeterId(42L);
98         builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());
99         instructions.add(builder.build());
100         // Write_actions instruction
101         builder = new InstructionBuilder();
102         builder.setType(WriteActions.class);
103         ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();
104         List<Action> actions = new ArrayList<>();
105         ActionBuilder actionBuilder = new ActionBuilder();
106         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
107         OutputActionBuilder outputBuilder = new OutputActionBuilder();
108         outputBuilder.setPort(new PortNumber(45L));
109         outputBuilder.setMaxLength(55);
110         caseBuilder.setOutputAction(outputBuilder.build());
111         actionBuilder.setActionChoice(caseBuilder.build());
112         actions.add(actionBuilder.build());
113         actionBuilder = new ActionBuilder();
114         SetNwTtlCaseBuilder ttlCaseBuilder = new SetNwTtlCaseBuilder();
115         SetNwTtlActionBuilder ttlActionBuilder = new SetNwTtlActionBuilder();
116         ttlActionBuilder.setNwTtl((short) 64);
117         ttlCaseBuilder.setSetNwTtlAction(ttlActionBuilder.build());
118         actionBuilder.setActionChoice(ttlCaseBuilder.build());
119         actions.add(actionBuilder.build());
120         actionsBuilder.setAction(actions);
121         builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());
122         instructions.add(builder.build());
123         // Apply_actions instruction
124         builder = new InstructionBuilder();
125         builder.setType(ApplyActions.class);
126         actionsBuilder = new ActionsInstructionBuilder();
127         actions = new ArrayList<>();
128         actionBuilder = new ActionBuilder();
129         PushVlanCaseBuilder vlanCaseBuilder = new PushVlanCaseBuilder();
130         PushVlanActionBuilder vlanBuilder = new PushVlanActionBuilder();
131         vlanBuilder.setEthertype(new EtherType(new EtherType(14)));
132         vlanCaseBuilder.setPushVlanAction(vlanBuilder.build());
133         actionBuilder.setActionChoice(vlanCaseBuilder.build());
134         actions.add(actionBuilder.build());
135         actionBuilder = new ActionBuilder();
136         actionBuilder.setActionChoice(new PopPbbCaseBuilder().build());
137         actions.add(actionBuilder.build());
138         actionsBuilder.setAction(actions);
139         builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());
140         instructions.add(builder.build());
141
142         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
143         ListSerializer.serializeList(instructions, TypeKeyMakerFactory
144                 .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
145
146         Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());
147         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
148         Assert.assertEquals("Wrong instruction table-id", 5, out.readUnsignedByte());
149         out.skipBytes(3);
150         Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
151         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
152         out.skipBytes(4);
153         byte[] actual = new byte[8];
154         out.readBytes(actual);
155         Assert.assertEquals("Wrong instruction metadata", "00 01 02 03 04 05 06 07",
156                 ByteBufUtils.bytesToHexString(actual));
157         actual = new byte[8];
158         out.readBytes(actual);
159         Assert.assertEquals("Wrong instruction metadata-mask", "07 06 05 04 03 02 01 00",
160                 ByteBufUtils.bytesToHexString(actual));
161         Assert.assertEquals("Wrong instruction type", 5, out.readUnsignedShort());
162         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
163         out.skipBytes(4);
164         Assert.assertEquals("Wrong instruction type", 6, out.readUnsignedShort());
165         Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
166         Assert.assertEquals("Wrong instruction meter-id", 42, out.readUnsignedInt());
167         Assert.assertEquals("Wrong instruction type", 3, out.readUnsignedShort());
168         Assert.assertEquals("Wrong instruction length", 32, out.readUnsignedShort());
169         out.skipBytes(4);
170         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
171         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
172         Assert.assertEquals("Wrong action type", 45, out.readUnsignedInt());
173         Assert.assertEquals("Wrong action type", 55, out.readUnsignedShort());
174         out.skipBytes(6);
175         Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());
176         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
177         Assert.assertEquals("Wrong action type", 64, out.readUnsignedByte());
178         out.skipBytes(3);
179         Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
180         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
181         out.skipBytes(4);
182         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
183         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
184         Assert.assertEquals("Wrong action ethertype", 14, out.readUnsignedShort());
185         out.skipBytes(2);
186         Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());
187         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
188         out.skipBytes(4);
189         Assert.assertTrue("Not all data were read", out.readableBytes() == 0);
190     }
191
192 }