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 java.util.List;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.GroupCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetMplsTtlCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetQueueCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
33 * Unit tests for InstructionsDeserializer.
35 * @author michal.polkorab
37 public class InstructionsDeserializerTest {
40 private DeserializerRegistry registry;
43 * Initializes deserializer registry and lookups correct deserializer.
46 public void startUp() {
47 registry = new DeserializerRegistryImpl();
52 * Testing instructions translation.
56 ByteBuf message = BufferHelper.buildBuffer("00 01 00 08 0A 00 00 00 00 02 00 18 00 00 00 00 "
57 + "00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 30 00 05 00 08 00 00 00 00 00 06 00 08 "
58 + "00 01 02 03 00 03 00 20 00 00 00 00 00 00 00 10 00 00 00 25 00 35 00 00 00 00 00 00 "
59 + "00 16 00 08 00 00 00 50 00 04 00 18 00 00 00 00 00 15 00 08 00 00 00 25 00 0F 00 08 05 00 00 00");
61 message.skipBytes(4); // skip XID
63 CodeKeyMaker keyMaker = CodeKeyMakerFactory.createInstructionsKeyMaker(EncodeConstants.OF_VERSION_1_3);
64 List<Instruction> instructions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
65 message.readableBytes(), message, keyMaker, registry);
66 Instruction i1 = instructions.get(0);
67 Assert.assertTrue("Wrong type - i1", i1.getInstructionChoice() instanceof GotoTableCase);
68 Assert.assertEquals("Wrong table-id - i1", 10, ((GotoTableCase) i1.getInstructionChoice())
69 .getGotoTable().getTableId().intValue());
70 Instruction i2 = instructions.get(1);
71 Assert.assertTrue("Wrong type - i2", i2.getInstructionChoice() instanceof WriteMetadataCase);
72 Assert.assertArrayEquals("Wrong metadata - i2", ByteBufUtils.hexStringToBytes("00 00 00 00 00 00 00 20"),
73 ((WriteMetadataCase) i2.getInstructionChoice()).getWriteMetadata().getMetadata());
74 Assert.assertArrayEquals("Wrong metadata-mask - i2", ByteBufUtils.hexStringToBytes("00 00 00 00 00 00 00 30"),
75 ((WriteMetadataCase) i2.getInstructionChoice()).getWriteMetadata().getMetadataMask());
76 Instruction i3 = instructions.get(2);
77 Assert.assertTrue("Wrong type - i3", i3.getInstructionChoice() instanceof ClearActionsCase);
78 Instruction i4 = instructions.get(3);
79 Assert.assertTrue("Wrong type - i4", i4.getInstructionChoice() instanceof MeterCase);
80 Assert.assertEquals("Wrong meterId - i4", 66051, ((MeterCase) i4.getInstructionChoice())
81 .getMeter().getMeterId().intValue());
82 Instruction i5 = instructions.get(4);
83 Assert.assertTrue("Wrong type - i5", i5.getInstructionChoice() instanceof WriteActionsCase);
84 Assert.assertEquals("Wrong instructions - i5", 2, ((WriteActionsCase) i5.getInstructionChoice())
85 .getWriteActions().getAction().size());
86 Action action1 = ((WriteActionsCase) i5.getInstructionChoice()).getWriteActions().getAction().get(0);
87 Assert.assertTrue("Wrong action", action1.getActionChoice() instanceof OutputActionCase);
88 Assert.assertEquals("Wrong action", 37, ((OutputActionCase) action1.getActionChoice()).getOutputAction()
89 .getPort().getValue().intValue());
90 Assert.assertEquals("Wrong action", 53, ((OutputActionCase) action1.getActionChoice()).getOutputAction()
91 .getMaxLength().intValue());
92 Action action2 = ((WriteActionsCase) i5.getInstructionChoice()).getWriteActions().getAction().get(1);
93 Assert.assertTrue("Wrong action", action2.getActionChoice() instanceof GroupCase);
94 Assert.assertEquals("Wrong action", 80,
95 ((GroupCase) action2.getActionChoice()).getGroupAction().getGroupId().intValue());
96 Instruction i6 = instructions.get(5);
97 Assert.assertTrue("Wrong type - i6", i6.getInstructionChoice() instanceof ApplyActionsCase);
98 Assert.assertEquals("Wrong instructions - i6", 2, ((ApplyActionsCase) i6.getInstructionChoice())
99 .getApplyActions().getAction().size());
100 action1 = ((ApplyActionsCase) i6.getInstructionChoice()).getApplyActions().getAction().get(0);
101 Assert.assertTrue("Wrong action", action1.getActionChoice() instanceof SetQueueCase);
102 Assert.assertEquals("Wrong action", 37, ((SetQueueCase) action1.getActionChoice()).getSetQueueAction()
103 .getQueueId().intValue());
104 action2 = ((ApplyActionsCase) i6.getInstructionChoice()).getApplyActions().getAction().get(1);
105 Assert.assertTrue("Wrong action", action2.getActionChoice() instanceof SetMplsTtlCase);
106 Assert.assertEquals("Wrong action", 5, ((SetMplsTtlCase) action2.getActionChoice()).getSetMplsTtlAction()
107 .getMplsTtl().shortValue());