e0dfb8f70d1261ede3a73b9da076a12da6b5c01c
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / ActionsDeserializerTest.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.openflowjava.protocol.impl.util;\r
9 \r
10 import io.netty.buffer.ByteBuf;\r
11 \r
12 import java.util.List;\r
13 \r
14 import org.junit.Assert;\r
15 import org.junit.Before;\r
16 import org.junit.Test;\r
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;\r
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;\r
20 import org.opendaylight.openflowjava.protocol.impl.deserialization.action.AbstractActionDeserializer;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdAction;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlAction;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntry;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\r
34 \r
35 /**\r
36  * @author michal.polkorab\r
37  *\r
38  */\r
39 public class ActionsDeserializerTest {\r
40 \r
41     private static final Logger LOGGER = LoggerFactory\r
42             .getLogger(ActionsDeserializerTest.class);\r
43     private DeserializerRegistry registry;\r
44 \r
45     /**\r
46      * Initializes deserializer registry and lookups correct deserializer\r
47      */\r
48     @Before\r
49     public void startUp() {\r
50         registry = new DeserializerRegistryImpl();\r
51         registry.init();\r
52     }\r
53 \r
54     /**\r
55      * Testing actions deserialization\r
56      */\r
57     @Test\r
58     public void test() {\r
59         ByteBuf message = BufferHelper.buildBuffer("00 00 00 10 00 00 00 01 00 02 00 00 00 00 00 00 "\r
60                 + "00 0B 00 08 00 00 00 00 "\r
61                 + "00 0C 00 08 00 00 00 00 "\r
62                 + "00 0F 00 08 03 00 00 00 "\r
63                 + "00 10 00 08 00 00 00 00 "\r
64                 + "00 11 00 08 00 04 00 00 "\r
65                 + "00 12 00 08 00 00 00 00 "\r
66                 + "00 13 00 08 00 05 00 00 "\r
67                 + "00 14 00 08 00 06 00 00 "\r
68                 + "00 15 00 08 00 00 00 07 "\r
69                 + "00 16 00 08 00 00 00 08 "\r
70                 + "00 17 00 08 09 00 00 00 "\r
71                 + "00 18 00 08 00 00 00 00 "\r
72                 + "00 19 00 10 80 00 02 04 00 00 00 0B 00 00 00 00 "\r
73                 + "00 1A 00 08 00 0A 00 00 "\r
74                 + "00 1B 00 08 00 00 00 00");\r
75         \r
76         message.skipBytes(4); // skip XID\r
77         LOGGER.info("bytes: " + message.readableBytes());\r
78         \r
79         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);\r
80         List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,\r
81                 message.readableBytes(), message, keyMaker, registry);\r
82         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
83                 + "openflow.common.action.rev130731.Output", actions.get(0).getType().getName());\r
84         Assert.assertEquals("Wrong action port", 1,\r
85                 actions.get(0).getAugmentation(PortAction.class).getPort().getValue().intValue());\r
86         Assert.assertEquals("Wrong action max-length", 2,\r
87                 actions.get(0).getAugmentation(MaxLengthAction.class).getMaxLength().intValue());\r
88         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
89                 + "openflow.common.action.rev130731.CopyTtlOut", actions.get(1).getType().getName());\r
90         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
91                 + "openflow.common.action.rev130731.CopyTtlIn", actions.get(2).getType().getName());\r
92         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
93                 + "openflow.common.action.rev130731.SetMplsTtl", actions.get(3).getType().getName());\r
94         Assert.assertEquals("Wrong action value", 3,\r
95                 actions.get(3).getAugmentation(MplsTtlAction.class).getMplsTtl().shortValue());\r
96         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
97                 + "openflow.common.action.rev130731.DecMplsTtl", actions.get(4).getType().getName());\r
98         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
99                 + "openflow.common.action.rev130731.PushVlan", actions.get(5).getType().getName());\r
100         Assert.assertEquals("Wrong action value", 4,\r
101                 actions.get(5).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());\r
102         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
103                 + "openflow.common.action.rev130731.PopVlan", actions.get(6).getType().getName());\r
104         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
105                 + "openflow.common.action.rev130731.PushMpls", actions.get(7).getType().getName());\r
106         Assert.assertEquals("Wrong action value", 5,\r
107                 actions.get(7).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());\r
108         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
109                 + "openflow.common.action.rev130731.PopMpls", actions.get(8).getType().getName());\r
110         Assert.assertEquals("Wrong action value", 6,\r
111                 actions.get(8).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());\r
112         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
113                 + "openflow.common.action.rev130731.SetQueue", actions.get(9).getType().getName());\r
114         Assert.assertEquals("Wrong action value", 7,\r
115                 actions.get(9).getAugmentation(QueueIdAction.class).getQueueId().intValue());\r
116         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
117                 + "openflow.common.action.rev130731.Group", actions.get(10).getType().getName());\r
118         Assert.assertEquals("Wrong action value", 8,\r
119                 actions.get(10).getAugmentation(GroupIdAction.class).getGroupId().intValue());\r
120         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
121                 + "openflow.common.action.rev130731.SetNwTtl", actions.get(11).getType().getName());\r
122         Assert.assertEquals("Wrong action value", 9,\r
123                 actions.get(11).getAugmentation(NwTtlAction.class).getNwTtl().intValue());\r
124         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
125                 + "openflow.common.action.rev130731.DecNwTtl", actions.get(12).getType().getName());\r
126         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
127                 + "openflow.common.action.rev130731.SetField", actions.get(13).getType().getName());\r
128         List<MatchEntries> entries = actions.get(13).getAugmentation(OxmFieldsAction.class).getMatchEntries();\r
129         Assert.assertEquals("Wrong number of fields", 1, entries.size());\r
130         Assert.assertEquals("Wrong match entry class", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow."\r
131                 + "oxm.rev130731.OpenflowBasicClass", entries.get(0).getOxmClass().getName());\r
132         Assert.assertEquals("Wrong match entry field", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow."\r
133                 + "oxm.rev130731.InPhyPort", entries.get(0).getOxmMatchField().getName());\r
134         Assert.assertEquals("Wrong match entry mask", false, entries.get(0).isHasMask());\r
135         Assert.assertEquals("Wrong match entry value", 11, \r
136                 entries.get(0).getAugmentation(PortNumberMatchEntry.class).getPortNumber().getValue().intValue());\r
137         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
138                 + "openflow.common.action.rev130731.PushPbb", actions.get(14).getType().getName());\r
139         Assert.assertEquals("Wrong action value", 10,\r
140                 actions.get(14).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());\r
141         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
142                 + "openflow.common.action.rev130731.PopPbb", actions.get(15).getType().getName());\r
143         Assert.assertTrue("Unread data in message", message.readableBytes() == 0);\r
144     }\r
145 \r
146     /**\r
147      * Tests {@link AbstractActionDeserializer#deserializeHeader(ByteBuf)}\r
148      */\r
149     @Test\r
150     public void testDeserializeHeader() {\r
151         ByteBuf message = BufferHelper.buildBuffer("00 00 00 04 00 19 00 04");\r
152 \r
153         message.skipBytes(4); // skip XID\r
154         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);\r
155         List<Action> actions = ListDeserializer.deserializeHeaders(EncodeConstants.OF13_VERSION_ID,\r
156                 message.readableBytes(), message, keyMaker, registry);\r
157 \r
158         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
159                 + "openflow.common.action.rev130731.Output", actions.get(0).getType().getName());\r
160         Assert.assertEquals("Wrong action port", null, actions.get(0).getAugmentation(PortAction.class));\r
161         Assert.assertEquals("Wrong action max-length", null, actions.get(0).getAugmentation(MaxLengthAction.class));\r
162         Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."\r
163                 + "openflow.common.action.rev130731.SetField", actions.get(1).getType().getName());\r
164         Assert.assertEquals("Wrong action oxm field", null, actions.get(1).getAugmentation(OxmFieldsAction.class));\r
165     }\r
166 }\r