Merge "Added JSON and XML payloads tabs with RFC 8040 URL"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / PacketOutInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.deserialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopVlanCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.vlan._case.PushVlanActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
31
32 /**
33  * Unit tests for PacketOutInputMessageFactory.
34  *
35  * @author giuseppex.petralia@intel.com
36  */
37 public class PacketOutInputMessageFactoryTest {
38     private OFDeserializer<PacketOutInput> factory;
39
40     @Before
41     public void startUp() {
42         DeserializerRegistry registry = new DeserializerRegistryImpl();
43         registry.init();
44         factory = registry
45                 .getDeserializer(new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 13, PacketOutInput.class));
46
47     }
48
49     @Test
50     public void test() {
51         ByteBuf bb = BufferHelper.buildBuffer(
52             "00 00 01 00 00 00 01 00 00 28 00 00 00 00 00 00 00 11 00 08 00 19 00 00 00 12 00 08 00 00 00 00 00 12 "
53             + "00 08 00 00 00 00 00 12 00 08 00 00 00 00 00 12 00 08 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 10 "
54             + "11 12 13 14");
55         PacketOutInput deserializedMessage = BufferHelper.deserialize(factory, bb);
56         BufferHelper.checkHeaderV13(deserializedMessage);
57
58         Assert.assertEquals("Wrong buffer Id", 256L, deserializedMessage.getBufferId().longValue());
59         Assert.assertEquals("Wrong In Port", new PortNumber(256L), deserializedMessage.getInPort());
60         Assert.assertEquals("Wrong Numbers of actions", createAction(), deserializedMessage.getAction());
61         byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
62         Assert.assertArrayEquals("Wrong data", data, deserializedMessage.getData());
63     }
64
65     private static List<Action> createAction() {
66         final List<Action> actions = new ArrayList<>();
67         ActionBuilder actionBuilder = new ActionBuilder();
68         PushVlanCaseBuilder pushVlanCaseBuilder = new PushVlanCaseBuilder();
69         PushVlanActionBuilder pushVlanBuilder = new PushVlanActionBuilder();
70         pushVlanBuilder.setEthertype(new EtherType(new EtherType(25)));
71         pushVlanCaseBuilder.setPushVlanAction(pushVlanBuilder.build());
72         actionBuilder.setActionChoice(pushVlanCaseBuilder.build());
73         actions.add(actionBuilder.build());
74         actionBuilder = new ActionBuilder();
75         actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
76         actions.add(actionBuilder.build());
77         actionBuilder = new ActionBuilder();
78         actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
79         actions.add(actionBuilder.build());
80         actionBuilder = new ActionBuilder();
81         actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
82         actions.add(actionBuilder.build());
83         actionBuilder = new ActionBuilder();
84         actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
85         actions.add(actionBuilder.build());
86         return actions;
87     }
88 }