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;
12 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.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
20 import org.opendaylight.openflowjava.util.ByteBufUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionsInstruction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstruction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MeterIdInstruction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstruction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.GroupCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetMplsTtlCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetQueueCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
33 * @author michal.polkorab
36 public class InstructionsDeserializerTest {
39 private DeserializerRegistry registry;
42 * Initializes deserializer registry and lookups correct deserializer
45 public void startUp() {
46 registry = new DeserializerRegistryImpl();
51 * Testing instructions translation
55 ByteBuf message = BufferHelper.buildBuffer("00 01 00 08 0A 00 00 00 00 02 00 18 00 00 00 00 "
56 + "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 "
57 + "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 "
58 + "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");
60 message.skipBytes(4); // skip XID
62 CodeKeyMaker keyMaker = CodeKeyMakerFactory.createInstructionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
63 List<Instruction> instructions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
64 message.readableBytes(), message, keyMaker, registry);
65 Instruction i1 = instructions.get(0);
66 Assert.assertEquals("Wrong type - i1", "org.opendaylight.yang.gen.v1.urn."
67 + "opendaylight.openflow.common.instruction.rev130731.GotoTable", i1.getType().getName());
68 Assert.assertEquals("Wrong table-id - i1", 10, i1.getAugmentation(TableIdInstruction.class).getTableId().intValue());
69 Instruction i2 = instructions.get(1);
70 Assert.assertEquals("Wrong type - i2", "org.opendaylight.yang.gen.v1.urn."
71 + "opendaylight.openflow.common.instruction.rev130731.WriteMetadata", i2.getType().getName());
72 Assert.assertArrayEquals("Wrong metadata - i2", ByteBufUtils.hexStringToBytes("00 00 00 00 00 00 00 20"),
73 i2.getAugmentation(MetadataInstruction.class).getMetadata());
74 Assert.assertArrayEquals("Wrong metadata-mask - i2", ByteBufUtils.hexStringToBytes("00 00 00 00 00 00 00 30"),
75 i2.getAugmentation(MetadataInstruction.class).getMetadataMask());
76 Instruction i3 = instructions.get(2);
77 Assert.assertEquals("Wrong type - i3", "org.opendaylight.yang.gen.v1.urn."
78 + "opendaylight.openflow.common.instruction.rev130731.ClearActions", i3.getType().getName());
79 Assert.assertEquals("Wrong instructions - i3", 0, i3.getAugmentation(ActionsInstruction.class).getAction().size());
80 Instruction i4 = instructions.get(3);
81 Assert.assertEquals("Wrong type - i4", "org.opendaylight.yang.gen.v1.urn."
82 + "opendaylight.openflow.common.instruction.rev130731.Meter", i4.getType().getName());
83 Assert.assertEquals("Wrong meterId - i4", 66051, i4.getAugmentation(MeterIdInstruction.class).getMeterId().intValue());
84 Instruction i5 = instructions.get(4);
85 Assert.assertEquals("Wrong type - i5", "org.opendaylight.yang.gen.v1.urn."
86 + "opendaylight.openflow.common.instruction.rev130731.WriteActions", i5.getType().getName());
87 Assert.assertEquals("Wrong instructions - i5", 2, i5.getAugmentation(ActionsInstruction.class).getAction().size());
88 Action action1 = i5.getAugmentation(ActionsInstruction.class).getAction().get(0);
89 Assert.assertTrue("Wrong action", action1.getActionChoice() instanceof OutputActionCase);
90 Assert.assertEquals("Wrong action", 37, ((OutputActionCase) action1.getActionChoice()).getOutputAction()
91 .getPort().getValue().intValue());
92 Assert.assertEquals("Wrong action", 53, ((OutputActionCase) action1.getActionChoice()).getOutputAction()
93 .getMaxLength().intValue());
94 Action action2 = i5.getAugmentation(ActionsInstruction.class).getAction().get(1);
95 Assert.assertTrue("Wrong action", action2.getActionChoice() instanceof GroupCase);
96 Assert.assertEquals("Wrong action", 80, ((GroupCase) action2.getActionChoice()).getGroupAction().getGroupId().intValue());
97 Instruction i6 = instructions.get(5);
98 Assert.assertEquals("Wrong type - i6", "org.opendaylight.yang.gen.v1.urn."
99 + "opendaylight.openflow.common.instruction.rev130731.ApplyActions", i6.getType().getName());
100 Assert.assertEquals("Wrong instructions - i6", 2, i6.getAugmentation(ActionsInstruction.class).getAction().size());
101 action1 = i6.getAugmentation(ActionsInstruction.class).getAction().get(0);
102 Assert.assertTrue("Wrong action", action1.getActionChoice() instanceof SetQueueCase);
103 Assert.assertEquals("Wrong action", 37, ((SetQueueCase) action1.getActionChoice()).getSetQueueAction()
104 .getQueueId().intValue());
105 action2 = i6.getAugmentation(ActionsInstruction.class).getAction().get(1);
106 Assert.assertTrue("Wrong action", action2.getActionChoice() instanceof SetMplsTtlCase);
107 Assert.assertEquals("Wrong action", 5, ((SetMplsTtlCase) action2.getActionChoice()).getSetMplsTtlAction()
108 .getMplsTtl().shortValue());