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