2dd21e82b12b05220eb692515c9ca3653e27f5ef
[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.augments.rev131002.DlAddressAction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressAction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTosAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpAction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidAction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
31
32 /**
33  * @author michal.polkorab
34  *
35  */
36 public class OF10ActionsDeserializerTest {
37
38     private DeserializerRegistry registry;
39
40     /**
41      * Initializes deserializer registry and lookups correct deserializer
42      */
43     @Before
44     public void startUp() {
45         registry = new DeserializerRegistryImpl();
46         registry.init();
47     }
48
49     /**
50      * Testing correct deserialization of actions (OF v1.0)
51      */
52     @Test
53     public void test() {
54         ByteBuf message = BufferHelper.buildBuffer("00 00 00 08 00 10 20 00 "
55                 + "00 01 00 08 10 10 00 00 "
56                 + "00 02 00 08 25 00 00 00 "
57                 + "00 03 00 08 00 00 00 00 "
58                 + "00 04 00 10 01 02 03 04 05 06 00 00 00 00 00 00 "
59                 + "00 05 00 10 02 03 04 05 06 07 00 00 00 00 00 00 "
60                 + "00 06 00 08 0A 00 00 01 "
61                 + "00 07 00 08 0B 00 00 02 "
62                 + "00 08 00 08 01 00 00 00 "
63                 + "00 09 00 08 00 02 00 00 "
64                 + "00 0A 00 08 00 03 00 00 "
65                 + "00 0B 00 10 00 04 00 00 00 00 00 00 00 00 00 30");
66
67         message.skipBytes(4); // skip XID
68         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF10_VERSION_ID);
69         List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF10_VERSION_ID,
70                 message.readableBytes(), message, keyMaker, registry);
71         Assert.assertEquals("Wrong number of actions", 12, actions.size());
72         Action action1 = actions.get(0);
73         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
74                 + ".openflow.common.action.rev130731.Output", action1.getType().getName());
75         Assert.assertEquals("Wrong port", 16,
76                 action1.getAugmentation(PortAction.class).getPort().getValue().intValue());
77         Assert.assertEquals("Wrong max-length", 8192,
78                 action1.getAugmentation(MaxLengthAction.class).getMaxLength().intValue());
79         Action action2 = actions.get(1);
80         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
81                 + ".openflow.common.action.rev130731.SetVlanVid", action2.getType().getName());
82         Assert.assertEquals("Wrong vlan-vid", 4112,
83                 action2.getAugmentation(VlanVidAction.class).getVlanVid().intValue());
84         Action action3 = actions.get(2);
85         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
86                 + ".openflow.common.action.rev130731.SetVlanPcp", action3.getType().getName());
87         Assert.assertEquals("Wrong vlan-pcp", 37,
88                 action3.getAugmentation(VlanPcpAction.class).getVlanPcp().intValue());
89         Action action4 = actions.get(3);
90         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
91                 + ".openflow.common.action.rev130731.StripVlan", action4.getType().getName());
92         Action action5 = actions.get(4);
93         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
94                 + ".openflow.common.action.rev130731.SetDlSrc", action5.getType().getName());
95         Assert.assertArrayEquals("Wrong dl-src", ByteBufUtils.macAddressToBytes("01:02:03:04:05:06"),
96                 ByteBufUtils.macAddressToBytes(action5.getAugmentation(DlAddressAction.class).getDlAddress().getValue()));
97         Action action6 = actions.get(5);
98         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
99                 + ".openflow.common.action.rev130731.SetDlDst", action6.getType().getName());
100         Assert.assertArrayEquals("Wrong dl-dst", ByteBufUtils.macAddressToBytes("02:03:04:05:06:07"),
101                 ByteBufUtils.macAddressToBytes(action6.getAugmentation(DlAddressAction.class).getDlAddress().getValue()));
102         Action action7 = actions.get(6);
103         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
104                 + ".openflow.common.action.rev130731.SetNwSrc", action7.getType().getName());
105         Assert.assertEquals("Wrong nw-src", new Ipv4Address("10.0.0.1"),
106                 action7.getAugmentation(IpAddressAction.class).getIpAddress());
107         Action action8 = actions.get(7);
108         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
109                 + ".openflow.common.action.rev130731.SetNwDst", action8.getType().getName());
110         Assert.assertEquals("Wrong nw-dst", new Ipv4Address("11.0.0.2"),
111                 action8.getAugmentation(IpAddressAction.class).getIpAddress());
112         Action action9 = actions.get(8);
113         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
114                 + ".openflow.common.action.rev130731.SetNwTos", action9.getType().getName());
115         Assert.assertEquals("Wrong nw-tos", 1, action9.getAugmentation(NwTosAction.class).getNwTos().intValue());
116         Action action10 = actions.get(9);
117         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
118                 + ".openflow.common.action.rev130731.SetTpSrc", action10.getType().getName());
119         Assert.assertEquals("Wrong port", 2, action10.getAugmentation(PortAction.class)
120                 .getPort().getValue().intValue());
121         Action action11 = actions.get(10);
122         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
123                 + ".openflow.common.action.rev130731.SetTpDst", action11.getType().getName());
124         Assert.assertEquals("Wrong port", 3, action11.getAugmentation(PortAction.class)
125                 .getPort().getValue().intValue());
126         Action action12 = actions.get(11);
127         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight"
128                 + ".openflow.common.action.rev130731.Enqueue", action12.getType().getName());
129         Assert.assertEquals("Wrong port", 4, action12.getAugmentation(PortAction.class)
130                 .getPort().getValue().intValue());
131         Assert.assertEquals("Wrong queue-id", 48,
132                 action12.getAugmentation(QueueIdAction.class).getQueueId().intValue());
133     }
134
135 }