b48c265fde765d160cf6ed58268ca8af73714b6d
[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.EthertypeAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.EthertypeActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MaxLengthAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MaxLengthActionBuilder;
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.MeterIdInstruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MeterIdInstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NwTtlAction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NwTtlActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortAction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.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;
55
56 /**
57  * @author michal.polkorab
58  *
59  */
60 public class OF13InstructionsSerializerTest {
61
62     private SerializerRegistry registry;
63
64     /**
65      * Initializes serializer table and stores correct factory in field
66      */
67     @Before
68     public void startUp() {
69         registry = new SerializerRegistryImpl();
70         registry.init();
71     }
72
73     /**
74      * Testing instructions translation
75      */
76     @Test
77     public void test() {
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());
98         // Meter instruction
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());
145
146         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
147         ListSerializer.serializeList(instructions, TypeKeyMakerFactory
148                 .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, out);
149
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());
153         out.skipBytes(3);
154         Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());
155         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
156         out.skipBytes(4);
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());
167         out.skipBytes(4);
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());
173         out.skipBytes(4);
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());
178         out.skipBytes(6);
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());
182         out.skipBytes(3);
183         Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
184         Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());
185         out.skipBytes(4);
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());
189         out.skipBytes(2);
190         Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());
191         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
192         out.skipBytes(4);
193         Assert.assertTrue("Not all data were read", out.readableBytes() == 0);
194     }
195
196 }