Bug 2756 - Action model update
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF10ActionsSerializerTest.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 io.netty.buffer.UnpooledByteBufAllocator;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.EnqueueCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetDlDstCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetDlSrcCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwDstCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwSrcCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwTosCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpDstCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpSrcCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetVlanPcpCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetVlanVidCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.StripVlanCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.enqueue._case.EnqueueActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.dl.dst._case.SetDlDstActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.dl.src._case.SetDlSrcActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.dst._case.SetNwDstActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.src._case.SetNwSrcActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.tos._case.SetNwTosActionBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.dst._case.SetTpDstActionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.src._case.SetTpSrcActionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.vlan.pcp._case.SetVlanPcpActionBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.vlan.vid._case.SetVlanVidActionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueId;
52
53 /**
54  * @author michal.polkorab
55  *
56  */
57 public class OF10ActionsSerializerTest {
58
59     private SerializerRegistry registry;
60
61     /**
62      * Initializes serializer table and stores correct factory in field
63      */
64     @Before
65     public void startUp() {
66         registry = new SerializerRegistryImpl();
67         registry.init();
68     }
69
70     /**
71      * Testing correct serialization of actions (OF v1.0)
72      */
73     @Test
74     public void test() {
75         List<Action> actions = new ArrayList<>();
76         ActionBuilder actionBuilder = new ActionBuilder();
77         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
78         OutputActionBuilder outputBuilder = new OutputActionBuilder();
79         outputBuilder.setPort(new PortNumber(42L));
80         outputBuilder.setMaxLength(32);
81         caseBuilder.setOutputAction(outputBuilder.build());
82         actionBuilder.setActionChoice(caseBuilder.build());
83         actions.add(actionBuilder.build());
84         actionBuilder = new ActionBuilder();
85         SetVlanVidCaseBuilder vlanVidCaseBuilder = new SetVlanVidCaseBuilder();
86         SetVlanVidActionBuilder vlanVidBuilder = new SetVlanVidActionBuilder();
87         vlanVidBuilder.setVlanVid(15);
88         vlanVidCaseBuilder.setSetVlanVidAction(vlanVidBuilder.build());
89         actionBuilder.setActionChoice(vlanVidCaseBuilder.build());
90         actions.add(actionBuilder.build());
91         actionBuilder = new ActionBuilder();
92         SetVlanPcpCaseBuilder vlanPcpCaseBuilder = new SetVlanPcpCaseBuilder();
93         SetVlanPcpActionBuilder vlanPcpBuilder = new SetVlanPcpActionBuilder();
94         vlanPcpBuilder.setVlanPcp((short) 16);
95         vlanPcpCaseBuilder.setSetVlanPcpAction(vlanPcpBuilder.build());
96         actionBuilder.setActionChoice(vlanPcpCaseBuilder.build());
97         actions.add(actionBuilder.build());
98         actionBuilder = new ActionBuilder();
99         actionBuilder.setActionChoice(new StripVlanCaseBuilder().build());
100         actions.add(actionBuilder.build());
101         actionBuilder = new ActionBuilder();
102         SetDlSrcCaseBuilder dlSrcCaseBuilder = new SetDlSrcCaseBuilder();
103         SetDlSrcActionBuilder dlSrcBuilder = new SetDlSrcActionBuilder();
104         dlSrcBuilder.setDlSrcAddress(new MacAddress("00:00:00:02:03:04"));
105         dlSrcCaseBuilder.setSetDlSrcAction(dlSrcBuilder.build());
106         actionBuilder.setActionChoice(dlSrcCaseBuilder.build());
107         actions.add(actionBuilder.build());
108         actionBuilder = new ActionBuilder();
109         SetDlDstCaseBuilder dlDstCaseBuilder = new SetDlDstCaseBuilder();
110         SetDlDstActionBuilder dlDstBuilder = new SetDlDstActionBuilder();
111         dlDstBuilder.setDlDstAddress(new MacAddress("00:00:00:01:02:03"));
112         dlDstCaseBuilder.setSetDlDstAction(dlDstBuilder.build());
113         actionBuilder.setActionChoice(dlDstCaseBuilder.build());
114         actions.add(actionBuilder.build());
115         actionBuilder = new ActionBuilder();
116         SetNwSrcCaseBuilder nwSrcCaseBuilder = new SetNwSrcCaseBuilder();
117         SetNwSrcActionBuilder nwSrcBuilder = new SetNwSrcActionBuilder();
118         nwSrcBuilder.setIpAddress(new Ipv4Address("10.0.0.1"));
119         nwSrcCaseBuilder.setSetNwSrcAction(nwSrcBuilder.build());
120         actionBuilder.setActionChoice(nwSrcCaseBuilder.build());
121         actions.add(actionBuilder.build());
122         actionBuilder = new ActionBuilder();
123         SetNwDstCaseBuilder nwDstCaseBuilder = new SetNwDstCaseBuilder();
124         SetNwDstActionBuilder nwDstBuilder = new SetNwDstActionBuilder();
125         nwDstBuilder.setIpAddress(new Ipv4Address("10.0.0.3"));
126         nwDstCaseBuilder.setSetNwDstAction(nwDstBuilder.build());
127         actionBuilder.setActionChoice(nwDstCaseBuilder.build());
128         actions.add(actionBuilder.build());
129         actionBuilder = new ActionBuilder();
130         SetNwTosCaseBuilder tosCaseBuilder = new SetNwTosCaseBuilder();
131         SetNwTosActionBuilder tosBuilder = new SetNwTosActionBuilder();
132         tosBuilder.setNwTos((short) 204);
133         tosCaseBuilder.setSetNwTosAction(tosBuilder.build());
134         actionBuilder.setActionChoice(tosCaseBuilder.build());
135         actions.add(actionBuilder.build());
136         actionBuilder = new ActionBuilder();
137         SetTpSrcCaseBuilder tpSrcCaseBuilder = new SetTpSrcCaseBuilder();
138         SetTpSrcActionBuilder tpSrcBuilder = new SetTpSrcActionBuilder();
139         tpSrcBuilder.setPort(new PortNumber(6653L));
140         tpSrcCaseBuilder.setSetTpSrcAction(tpSrcBuilder.build());
141         actionBuilder.setActionChoice(tpSrcCaseBuilder.build());
142         actions.add(actionBuilder.build());
143         actionBuilder = new ActionBuilder();
144         SetTpDstCaseBuilder tpDstCaseBuilder = new SetTpDstCaseBuilder();
145         SetTpDstActionBuilder tpDstBuilder = new SetTpDstActionBuilder();
146         tpDstBuilder.setPort(new PortNumber(6633L));
147         tpDstCaseBuilder.setSetTpDstAction(tpDstBuilder.build());
148         actionBuilder.setActionChoice(tpDstCaseBuilder.build());
149         actions.add(actionBuilder.build());
150         actionBuilder = new ActionBuilder();
151         EnqueueCaseBuilder enqueueCaseBuilder = new EnqueueCaseBuilder();
152         EnqueueActionBuilder enqueueBuilder = new EnqueueActionBuilder();
153         enqueueBuilder.setPort(new PortNumber(6613L));
154         enqueueBuilder.setQueueId(new QueueId(400L));
155         enqueueCaseBuilder.setEnqueueAction(enqueueBuilder.build());
156         actionBuilder.setActionChoice(enqueueCaseBuilder.build());
157         actions.add(actionBuilder.build());
158
159         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
160         ListSerializer.serializeList(actions, TypeKeyMakerFactory
161                 .createActionKeyMaker(EncodeConstants.OF10_VERSION_ID), registry, out);
162
163         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
164         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
165         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
166         Assert.assertEquals("Wrong max-length", 32, out.readUnsignedShort());
167         Assert.assertEquals("Wrong action type", 1, out.readUnsignedShort());
168         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
169         Assert.assertEquals("Wrong vlan-vid", 15, out.readUnsignedShort());
170         out.skipBytes(2);
171         Assert.assertEquals("Wrong action type", 2, out.readUnsignedShort());
172         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
173         Assert.assertEquals("Wrong vlan-pcp", 16, out.readUnsignedByte());
174         out.skipBytes(3);
175         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
176         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
177         out.skipBytes(4);
178         Assert.assertEquals("Wrong action type", 4, out.readUnsignedShort());
179         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
180         byte[] data = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
181         out.readBytes(data);
182         Assert.assertArrayEquals("Wrong dl-address", ByteBufUtils.macAddressToBytes("00:00:00:02:03:04"), data);
183         out.skipBytes(6);
184         Assert.assertEquals("Wrong action type", 5, out.readUnsignedShort());
185         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
186         data = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
187         out.readBytes(data);
188         Assert.assertArrayEquals("Wrong dl-address", ByteBufUtils.macAddressToBytes("00:00:00:01:02:03"), data);
189         out.skipBytes(6);
190         Assert.assertEquals("Wrong action type", 6, out.readUnsignedShort());
191         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
192         Assert.assertEquals("Wrong ip-address(1)", 10, out.readUnsignedByte());
193         Assert.assertEquals("Wrong ip-address(2)", 0, out.readUnsignedByte());
194         Assert.assertEquals("Wrong ip-address(3)", 0, out.readUnsignedByte());
195         Assert.assertEquals("Wrong ip-address(4)", 1, out.readUnsignedByte());
196         Assert.assertEquals("Wrong action type", 7, out.readUnsignedShort());
197         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
198         Assert.assertEquals("Wrong ip-address(1)", 10, out.readUnsignedByte());
199         Assert.assertEquals("Wrong ip-address(2)", 0, out.readUnsignedByte());
200         Assert.assertEquals("Wrong ip-address(3)", 0, out.readUnsignedByte());
201         Assert.assertEquals("Wrong ip-address(4)", 3, out.readUnsignedByte());
202         Assert.assertEquals("Wrong action type", 8, out.readUnsignedShort());
203         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
204         Assert.assertEquals("Wrong nw-tos", 204, out.readUnsignedByte());
205         out.skipBytes(3);
206         Assert.assertEquals("Wrong action type", 9, out.readUnsignedShort());
207         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
208         Assert.assertEquals("Wrong port", 6653, out.readUnsignedShort());
209         out.skipBytes(2);
210         Assert.assertEquals("Wrong action type", 10, out.readUnsignedShort());
211         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
212         Assert.assertEquals("Wrong port", 6633, out.readUnsignedShort());
213         out.skipBytes(2);
214         Assert.assertEquals("Wrong action type", 11, out.readUnsignedShort());
215         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
216         Assert.assertEquals("Wrong port", 6613, out.readUnsignedShort());
217         out.skipBytes(6);
218         Assert.assertEquals("Wrong queue-id", 400, out.readUnsignedInt());
219         Assert.assertTrue("Written more bytes than needed", out.readableBytes() == 0);
220     }
221
222 }