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