Remove EncodeConstants.SIZE_OF_{BYTE,SHORT,INT,LONG}_IN_BYTES
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF10ActionsDeserializerTest.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 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.EnqueueCase;
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.SetDlDstCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetDlSrcCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwDstCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwSrcCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwTosCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpDstCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpSrcCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetVlanPcpCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetVlanVidCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.StripVlanCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
32
33 /**
34  * Unit tests for OF10ActionsDeserializer.
35  *
36  * @author michal.polkorab
37  */
38 public class OF10ActionsDeserializerTest {
39
40     private DeserializerRegistry registry;
41
42     /**
43      * Initializes deserializer registry and lookups correct deserializer.
44      */
45     @Before
46     public void startUp() {
47         registry = new DeserializerRegistryImpl();
48         registry.init();
49     }
50
51     /**
52      * Testing correct deserialization of actions (OF v1.0).
53      */
54     @Test
55     public void test() {
56         ByteBuf message = BufferHelper.buildBuffer("00 00 00 08 00 10 20 00 "
57                 + "00 01 00 08 10 10 00 00 "
58                 + "00 02 00 08 25 00 00 00 "
59                 + "00 03 00 08 00 00 00 00 "
60                 + "00 04 00 10 01 02 03 04 05 06 00 00 00 00 00 00 "
61                 + "00 05 00 10 02 03 04 05 06 07 00 00 00 00 00 00 "
62                 + "00 06 00 08 0A 00 00 01 "
63                 + "00 07 00 08 0B 00 00 02 "
64                 + "00 08 00 08 01 00 00 00 "
65                 + "00 09 00 08 00 02 00 00 "
66                 + "00 0A 00 08 00 03 00 00 "
67                 + "00 0B 00 10 00 04 00 00 00 00 00 00 00 00 00 30");
68
69         message.skipBytes(4); // skip XID
70         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF10_VERSION_ID);
71         List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF10_VERSION_ID,
72                 message.readableBytes(), message, keyMaker, registry);
73         Assert.assertEquals("Wrong number of actions", 12, actions.size());
74         Action action1 = actions.get(0);
75         Assert.assertTrue("Wrong action type", action1.getActionChoice() instanceof OutputActionCase);
76         Assert.assertEquals("Wrong port", 16,
77                 ((OutputActionCase) action1.getActionChoice()).getOutputAction().getPort().getValue().intValue());
78         Assert.assertEquals("Wrong max-length", 8192,
79                 ((OutputActionCase) action1.getActionChoice()).getOutputAction().getMaxLength().intValue());
80         Action action2 = actions.get(1);
81         Assert.assertTrue("Wrong action type", action2.getActionChoice() instanceof SetVlanVidCase);
82         Assert.assertEquals("Wrong vlan-vid", 4112,
83                 ((SetVlanVidCase) action2.getActionChoice()).getSetVlanVidAction().getVlanVid().intValue());
84         Action action3 = actions.get(2);
85         Assert.assertTrue("Wrong action type", action3.getActionChoice() instanceof SetVlanPcpCase);
86         Assert.assertEquals("Wrong vlan-pcp", 37,
87                 ((SetVlanPcpCase) action3.getActionChoice()).getSetVlanPcpAction().getVlanPcp().intValue());
88         Action action4 = actions.get(3);
89         Assert.assertTrue("Wrong action type", action4.getActionChoice() instanceof StripVlanCase);
90         Action action5 = actions.get(4);
91         Assert.assertTrue("Wrong action type", action5.getActionChoice() instanceof SetDlSrcCase);
92         Assert.assertEquals("Wrong dl-src", "01:02:03:04:05:06",
93             ((SetDlSrcCase) action5.getActionChoice()).getSetDlSrcAction().getDlSrcAddress().getValue());
94         Action action6 = actions.get(5);
95         Assert.assertTrue("Wrong action type", action6.getActionChoice() instanceof SetDlDstCase);
96         Assert.assertEquals("Wrong dl-dst", "02:03:04:05:06:07",
97             ((SetDlDstCase) action6.getActionChoice()).getSetDlDstAction().getDlDstAddress().getValue());
98         Action action7 = actions.get(6);
99         Assert.assertTrue("Wrong action type", action7.getActionChoice() instanceof SetNwSrcCase);
100         Assert.assertEquals("Wrong nw-src", new Ipv4Address("10.0.0.1"),
101                 ((SetNwSrcCase) action7.getActionChoice()).getSetNwSrcAction().getIpAddress());
102         Action action8 = actions.get(7);
103         Assert.assertTrue("Wrong action type", action8.getActionChoice() instanceof SetNwDstCase);
104         Assert.assertEquals("Wrong nw-dst", new Ipv4Address("11.0.0.2"),
105                 ((SetNwDstCase) action8.getActionChoice()).getSetNwDstAction().getIpAddress());
106         Action action9 = actions.get(8);
107         Assert.assertTrue("Wrong action type", action9.getActionChoice() instanceof SetNwTosCase);
108         Assert.assertEquals("Wrong nw-tos", 1, ((SetNwTosCase) action9.getActionChoice())
109                 .getSetNwTosAction().getNwTos().intValue());
110         Action action10 = actions.get(9);
111         Assert.assertTrue("Wrong action type", action10.getActionChoice() instanceof SetTpSrcCase);
112         Assert.assertEquals("Wrong port", 2, ((SetTpSrcCase) action10.getActionChoice())
113                 .getSetTpSrcAction().getPort().getValue().intValue());
114         Action action11 = actions.get(10);
115         Assert.assertTrue("Wrong action type", action11.getActionChoice() instanceof SetTpDstCase);
116         Assert.assertEquals("Wrong port", 3, ((SetTpDstCase) action11.getActionChoice())
117                 .getSetTpDstAction().getPort().getValue().intValue());
118         Action action12 = actions.get(11);
119         Assert.assertTrue("Wrong action type", action12.getActionChoice() instanceof EnqueueCase);
120         Assert.assertEquals("Wrong port", 4, ((EnqueueCase) action12.getActionChoice())
121                 .getEnqueueAction().getPort().getValue().intValue());
122         Assert.assertEquals("Wrong queue-id", 48, ((EnqueueCase) action12.getActionChoice())
123                 .getEnqueueAction().getQueueId().getValue().intValue());
124     }
125
126 }