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