2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.util;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.util.ArrayList;
14 import java.util.List;
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.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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;
54 * @author michal.polkorab
57 public class OF10ActionsSerializerTest {
59 private SerializerRegistry registry;
62 * Initializes serializer table and stores correct factory in field
65 public void startUp() {
66 registry = new SerializerRegistryImpl();
71 * Testing correct serialization of actions (OF v1.0)
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());
159 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
160 ListSerializer.serializeList(actions, TypeKeyMakerFactory
161 .createActionKeyMaker(EncodeConstants.OF10_VERSION_ID), registry, out);
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());
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());
175 Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
176 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
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];
182 Assert.assertArrayEquals("Wrong dl-address", ByteBufUtils.macAddressToBytes("00:00:00:02:03:04"), data);
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];
188 Assert.assertArrayEquals("Wrong dl-address", ByteBufUtils.macAddressToBytes("00:00:00:01:02:03"), data);
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());
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());
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());
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());
218 Assert.assertEquals("Wrong queue-id", 400, out.readUnsignedInt());
219 Assert.assertTrue("Written more bytes than needed", out.readableBytes() == 0);