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