8c25f962a4c0bcf0b9d3f458043eeac86971106d
[openflowplugin.git] / openflowjava / 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 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.openflowjava.protocol.impl.deserialization.action.AbstractActionDeserializer;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlOutCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecMplsTtlCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.DecNwTtlCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.GroupCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopMplsCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopPbbCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopVlanCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushMplsCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushPbbCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetMplsTtlCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwTtlCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetQueueCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCase;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Unit tests for ActionsDeserializer.
43  *
44  * @author michal.polkorab
45  */
46 public class ActionsDeserializerTest {
47
48     private static final Logger LOG = LoggerFactory
49             .getLogger(ActionsDeserializerTest.class);
50     private DeserializerRegistry registry;
51
52     /**
53      * Initializes deserializer registry and lookups correct deserializer.
54      */
55     @Before
56     public void startUp() {
57         registry = new DeserializerRegistryImpl();
58         registry.init();
59     }
60
61     /**
62      * Testing actions deserialization.
63      */
64     @Test
65     public void test() {
66         ByteBuf message = BufferHelper.buildBuffer("00 00 00 10 00 00 00 01 00 02 00 00 00 00 00 00 "
67                 + "00 0B 00 08 00 00 00 00 "
68                 + "00 0C 00 08 00 00 00 00 "
69                 + "00 0F 00 08 03 00 00 00 "
70                 + "00 10 00 08 00 00 00 00 "
71                 + "00 11 00 08 00 04 00 00 "
72                 + "00 12 00 08 00 00 00 00 "
73                 + "00 13 00 08 00 05 00 00 "
74                 + "00 14 00 08 00 06 00 00 "
75                 + "00 15 00 08 00 00 00 07 "
76                 + "00 16 00 08 00 00 00 08 "
77                 + "00 17 00 08 09 00 00 00 "
78                 + "00 18 00 08 00 00 00 00 "
79                 + "00 19 00 10 80 00 02 04 00 00 00 0B 00 00 00 00 "
80                 + "00 1A 00 08 00 0A 00 00 "
81                 + "00 1B 00 08 00 00 00 00");
82
83         message.skipBytes(4); // skip XID
84         LOG.info("bytes: {}", message.readableBytes());
85
86         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
87         List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
88                 message.readableBytes(), message, keyMaker, registry);
89         Assert.assertTrue("Wrong action type", actions.get(0).getActionChoice() instanceof OutputActionCase);
90         Assert.assertEquals("Wrong action port", 1,
91                 ((OutputActionCase) actions.get(0).getActionChoice()).getOutputAction()
92                 .getPort().getValue().intValue());
93         Assert.assertEquals("Wrong action max-length", 2,
94                 ((OutputActionCase) actions.get(0).getActionChoice()).getOutputAction()
95                 .getMaxLength().intValue());
96         Assert.assertTrue("Wrong action type", actions.get(1).getActionChoice() instanceof CopyTtlOutCase);
97         Assert.assertTrue("Wrong action type", actions.get(2).getActionChoice() instanceof CopyTtlInCase);
98         Assert.assertTrue("Wrong action type", actions.get(3).getActionChoice() instanceof SetMplsTtlCase);
99         Assert.assertEquals("Wrong action value", 3,
100                 ((SetMplsTtlCase) actions.get(3).getActionChoice()).getSetMplsTtlAction()
101                 .getMplsTtl().shortValue());
102         Assert.assertTrue("Wrong action type", actions.get(4).getActionChoice() instanceof DecMplsTtlCase);
103         Assert.assertTrue("Wrong action type", actions.get(5).getActionChoice() instanceof PushVlanCase);
104         Assert.assertEquals("Wrong action value", 4,
105                 ((PushVlanCase) actions.get(5).getActionChoice()).getPushVlanAction()
106                 .getEthertype().getValue().intValue());
107         Assert.assertTrue("Wrong action type", actions.get(6).getActionChoice() instanceof PopVlanCase);
108         Assert.assertTrue("Wrong action type", actions.get(7).getActionChoice() instanceof PushMplsCase);
109         Assert.assertEquals("Wrong action value", 5,
110                 ((PushMplsCase) actions.get(7).getActionChoice()).getPushMplsAction()
111                 .getEthertype().getValue().intValue());
112         Assert.assertTrue("Wrong action type", actions.get(8).getActionChoice() instanceof PopMplsCase);
113         Assert.assertEquals("Wrong action value", 6,
114                 ((PopMplsCase) actions.get(8).getActionChoice()).getPopMplsAction()
115                 .getEthertype().getValue().intValue());
116         Assert.assertTrue("Wrong action type", actions.get(9).getActionChoice() instanceof SetQueueCase);
117         Assert.assertEquals("Wrong action value", 7,
118                 ((SetQueueCase) actions.get(9).getActionChoice()).getSetQueueAction()
119                 .getQueueId().intValue());
120         Assert.assertTrue("Wrong action type", actions.get(10).getActionChoice() instanceof GroupCase);
121         Assert.assertEquals("Wrong action value", 8,
122                 ((GroupCase) actions.get(10).getActionChoice()).getGroupAction().getGroupId().intValue());
123         Assert.assertTrue("Wrong action type", actions.get(11).getActionChoice() instanceof SetNwTtlCase);
124         Assert.assertEquals("Wrong action value", 9,
125                 ((SetNwTtlCase) actions.get(11).getActionChoice()).getSetNwTtlAction().getNwTtl().intValue());
126         Assert.assertTrue("Wrong action type", actions.get(12).getActionChoice() instanceof DecNwTtlCase);
127         Assert.assertTrue("Wrong action type", actions.get(13).getActionChoice() instanceof SetFieldCase);
128         List<MatchEntry> entries = ((SetFieldCase) actions.get(13).getActionChoice())
129                 .getSetFieldAction().getMatchEntry();
130         Assert.assertEquals("Wrong number of fields", 1, entries.size());
131         Assert.assertEquals("Wrong match entry class", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow."
132                 + "oxm.rev150225.OpenflowBasicClass", entries.get(0).getOxmClass().getName());
133         Assert.assertEquals("Wrong match entry field", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow."
134                 + "oxm.rev150225.InPhyPort", entries.get(0).getOxmMatchField().getName());
135         Assert.assertEquals("Wrong match entry mask", false, entries.get(0).getHasMask());
136         Assert.assertEquals("Wrong match entry value", 11,
137                 ((InPhyPortCase) entries.get(0).getMatchEntryValue()).getInPhyPort().getPortNumber()
138                 .getValue().intValue());
139         Assert.assertTrue("Wrong action type", actions.get(14).getActionChoice() instanceof PushPbbCase);
140         Assert.assertEquals("Wrong action value", 10,
141                 ((PushPbbCase) actions.get(14).getActionChoice()).getPushPbbAction()
142                 .getEthertype().getValue().intValue());
143         Assert.assertTrue("Wrong action type", actions.get(15).getActionChoice() instanceof PopPbbCase);
144         Assert.assertTrue("Unread data in message", message.readableBytes() == 0);
145     }
146
147     /**
148      * Tests {@link AbstractActionDeserializer#deserializeHeader(ByteBuf)}.
149      */
150     @Test
151     public void testDeserializeHeader() {
152         ByteBuf message = BufferHelper.buildBuffer("00 00 00 04 00 19 00 04");
153
154         message.skipBytes(4); // skip XID
155         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
156         List<Action> actions = ListDeserializer.deserializeHeaders(EncodeConstants.OF13_VERSION_ID,
157                 message.readableBytes(), message, keyMaker, registry);
158
159         Assert.assertTrue("Wrong action type", actions.get(0).getActionChoice() instanceof OutputActionCase);
160         Assert.assertNull("Wrong action port", ((OutputActionCase) actions.get(0).getActionChoice()).getOutputAction());
161         Assert.assertNull("Wrong action max-length",
162                 ((OutputActionCase) actions.get(0).getActionChoice()).getOutputAction());
163         Assert.assertTrue("Wrong action type", actions.get(1).getActionChoice() instanceof SetFieldCase);
164         Assert.assertNull("Wrong action oxm field",
165                 ((SetFieldCase) actions.get(1).getActionChoice()).getSetFieldAction());
166     }
167 }