Mass replace CRLF->LF
[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.augments.rev131002.DlAddressAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DlAddressActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTosAction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTosActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpAction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidAction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Enqueue;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetDlDst;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetDlSrc;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwDst;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwSrc;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTos;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetTpDst;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetTpSrc;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetVlanPcp;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetVlanVid;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.StripVlan;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
56
57 /**
58  * @author michal.polkorab
59  *
60  */
61 public class OF10ActionsSerializerTest {
62
63     private SerializerRegistry registry;
64
65     /**
66      * Initializes serializer table and stores correct factory in field
67      */
68     @Before
69     public void startUp() {
70         registry = new SerializerRegistryImpl();
71         registry.init();
72     }
73
74     /**
75      * Testing correct serialization of actions (OF v1.0) 
76      */
77     @Test
78     public void test() {
79         List<Action> actions = new ArrayList<>();
80         ActionBuilder actionBuilder = new ActionBuilder();
81         actionBuilder.setType(Output.class);
82         PortActionBuilder portBuilder = new PortActionBuilder();
83         portBuilder.setPort(new PortNumber(42L));
84         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
85         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
86         maxLen.setMaxLength(32);
87         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
88         actions.add(actionBuilder.build());
89         actionBuilder = new ActionBuilder();
90         actionBuilder.setType(SetVlanVid.class);
91         VlanVidActionBuilder vlanBuilder = new VlanVidActionBuilder();
92         vlanBuilder.setVlanVid(15);
93         actionBuilder.addAugmentation(VlanVidAction.class, vlanBuilder.build());
94         actions.add(actionBuilder.build());
95         actionBuilder = new ActionBuilder();
96         actionBuilder.setType(SetVlanPcp.class);
97         VlanPcpActionBuilder pcpBuilder = new VlanPcpActionBuilder();
98         pcpBuilder.setVlanPcp((short) 16);
99         actionBuilder.addAugmentation(VlanPcpAction.class, pcpBuilder.build());
100         actions.add(actionBuilder.build());
101         actionBuilder = new ActionBuilder();
102         actionBuilder.setType(StripVlan.class);
103         actions.add(actionBuilder.build());
104         actionBuilder = new ActionBuilder();
105         actionBuilder.setType(SetDlSrc.class);
106         DlAddressActionBuilder dlBuilder = new DlAddressActionBuilder();
107         dlBuilder.setDlAddress(new MacAddress("00:00:00:02:03:04"));
108         actionBuilder.addAugmentation(DlAddressAction.class, dlBuilder.build());
109         actions.add(actionBuilder.build());
110         actionBuilder = new ActionBuilder();
111         actionBuilder.setType(SetDlDst.class);
112         dlBuilder = new DlAddressActionBuilder();
113         dlBuilder.setDlAddress(new MacAddress("00:00:00:01:02:03"));
114         actionBuilder.addAugmentation(DlAddressAction.class, dlBuilder.build());
115         actions.add(actionBuilder.build());
116         actionBuilder = new ActionBuilder();
117         actionBuilder.setType(SetNwSrc.class);
118         IpAddressActionBuilder ipBuilder = new IpAddressActionBuilder();
119         ipBuilder.setIpAddress(new Ipv4Address("10.0.0.1"));
120         actionBuilder.addAugmentation(IpAddressAction.class, ipBuilder.build());
121         actions.add(actionBuilder.build());
122         actionBuilder = new ActionBuilder();
123         actionBuilder.setType(SetNwDst.class);
124         ipBuilder = new IpAddressActionBuilder();
125         ipBuilder.setIpAddress(new Ipv4Address("10.0.0.3"));
126         actionBuilder.addAugmentation(IpAddressAction.class, ipBuilder.build());
127         actions.add(actionBuilder.build());
128         actionBuilder = new ActionBuilder();
129         actionBuilder.setType(SetNwTos.class);
130         NwTosActionBuilder tosBuilder = new NwTosActionBuilder();
131         tosBuilder.setNwTos((short) 204);
132         actionBuilder.addAugmentation(NwTosAction.class, tosBuilder.build());
133         actions.add(actionBuilder.build());
134         actionBuilder = new ActionBuilder();
135         actionBuilder.setType(SetTpSrc.class);
136         portBuilder = new PortActionBuilder();
137         portBuilder.setPort(new PortNumber(6653L));
138         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
139         actions.add(actionBuilder.build());
140         actionBuilder = new ActionBuilder();
141         actionBuilder.setType(SetTpDst.class);
142         portBuilder = new PortActionBuilder();
143         portBuilder.setPort(new PortNumber(6633L));
144         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
145         actions.add(actionBuilder.build());
146         actionBuilder = new ActionBuilder();
147         actionBuilder.setType(Enqueue.class);
148         portBuilder = new PortActionBuilder();
149         portBuilder.setPort(new PortNumber(6613L));
150         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
151         QueueIdActionBuilder queueBuilder = new QueueIdActionBuilder();
152         queueBuilder.setQueueId(400L);
153         actionBuilder.addAugmentation(QueueIdAction.class, queueBuilder.build());
154         actions.add(actionBuilder.build());
155         
156         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
157         ListSerializer.serializeList(actions, TypeKeyMakerFactory
158                 .createActionKeyMaker(EncodeConstants.OF10_VERSION_ID), registry, out);
159         
160         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
161         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
162         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
163         Assert.assertEquals("Wrong max-length", 32, out.readUnsignedShort());
164         Assert.assertEquals("Wrong action type", 1, out.readUnsignedShort());
165         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
166         Assert.assertEquals("Wrong vlan-vid", 15, out.readUnsignedShort());
167         out.skipBytes(2);
168         Assert.assertEquals("Wrong action type", 2, out.readUnsignedShort());
169         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
170         Assert.assertEquals("Wrong vlan-pcp", 16, out.readUnsignedByte());
171         out.skipBytes(3);
172         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
173         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
174         out.skipBytes(4);
175         Assert.assertEquals("Wrong action type", 4, out.readUnsignedShort());
176         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
177         byte[] data = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
178         out.readBytes(data);
179         Assert.assertArrayEquals("Wrong dl-address", ByteBufUtils.macAddressToBytes("00:00:00:02:03:04"), data);
180         out.skipBytes(6);
181         Assert.assertEquals("Wrong action type", 5, out.readUnsignedShort());
182         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
183         data = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
184         out.readBytes(data);
185         Assert.assertArrayEquals("Wrong dl-address", ByteBufUtils.macAddressToBytes("00:00:00:01:02:03"), data);
186         out.skipBytes(6);
187         Assert.assertEquals("Wrong action type", 6, out.readUnsignedShort());
188         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
189         Assert.assertEquals("Wrong ip-address(1)", 10, out.readUnsignedByte());
190         Assert.assertEquals("Wrong ip-address(2)", 0, out.readUnsignedByte());
191         Assert.assertEquals("Wrong ip-address(3)", 0, out.readUnsignedByte());
192         Assert.assertEquals("Wrong ip-address(4)", 1, out.readUnsignedByte());
193         Assert.assertEquals("Wrong action type", 7, out.readUnsignedShort());
194         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
195         Assert.assertEquals("Wrong ip-address(1)", 10, out.readUnsignedByte());
196         Assert.assertEquals("Wrong ip-address(2)", 0, out.readUnsignedByte());
197         Assert.assertEquals("Wrong ip-address(3)", 0, out.readUnsignedByte());
198         Assert.assertEquals("Wrong ip-address(4)", 3, out.readUnsignedByte());
199         Assert.assertEquals("Wrong action type", 8, out.readUnsignedShort());
200         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
201         Assert.assertEquals("Wrong nw-tos", 204, out.readUnsignedByte());
202         out.skipBytes(3);
203         Assert.assertEquals("Wrong action type", 9, out.readUnsignedShort());
204         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
205         Assert.assertEquals("Wrong port", 6653, out.readUnsignedShort());
206         out.skipBytes(2);
207         Assert.assertEquals("Wrong action type", 10, out.readUnsignedShort());
208         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
209         Assert.assertEquals("Wrong port", 6633, out.readUnsignedShort());
210         out.skipBytes(2);
211         Assert.assertEquals("Wrong action type", 11, out.readUnsignedShort());
212         Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());
213         Assert.assertEquals("Wrong port", 6613, out.readUnsignedShort());
214         out.skipBytes(6);
215         Assert.assertEquals("Wrong queue-id", 400, out.readUnsignedInt());
216         Assert.assertTrue("Written more bytes than needed", out.readableBytes() == 0);
217     }
218     
219 }