Merge "Fix a modernizer warning"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / DecapCodecTest.java
1 /*
2  * Copyright (c) 2018 SUSE LINUX GmbH.  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
9 package org.opendaylight.openflowjava.nx.codec.action;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufAllocator;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionDecap;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionDecapBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.decap.grouping.NxActionDecapBuilder;
26 import org.opendaylight.yangtools.yang.common.Uint32;
27
28 public class DecapCodecTest {
29
30     private static final int LENGTH = 16;
31     private static final int SUBTYPE = 47;
32     private static final Uint32 PACKET_TYPE = Uint32.valueOf(0xFFFEL);
33
34     DecapCodec decapCodec;
35     ByteBuf buffer;
36     Action action;
37
38     @Before
39     public void setUp() {
40         decapCodec = new DecapCodec();
41         buffer = ByteBufAllocator.DEFAULT.buffer();
42     }
43
44     @Test
45     public void deserialize() {
46         createBuffer(buffer);
47
48         action = decapCodec.deserialize(buffer);
49
50         ActionDecap result = (ActionDecap) action.getActionChoice();
51
52         assertEquals(PACKET_TYPE, result.getNxActionDecap().getPacketType());
53         assertFalse(buffer.isReadable());
54     }
55
56     @Test
57     public void serialize() {
58         action = createAction();
59         decapCodec.serialize(action, buffer);
60
61         assertEquals(LENGTH, buffer.readableBytes());
62         //SerializeHeader part
63         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
64         assertEquals(LENGTH, buffer.readUnsignedShort());
65         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
66         assertEquals(SUBTYPE, buffer.readUnsignedShort());
67         //Serialize part
68         assertEquals(0, buffer.readUnsignedShort());
69         assertEquals(PACKET_TYPE.longValue(), buffer.readUnsignedInt());
70         assertFalse(buffer.isReadable());
71     }
72
73     private static void createBuffer(ByteBuf message) {
74         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
75         message.writeShort(LENGTH);
76         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
77         message.writeShort(SUBTYPE);
78         message.writeZero(2);
79         message.writeInt(PACKET_TYPE.intValue());
80     }
81
82     private static Action createAction() {
83         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
84         ActionBuilder actionBuilder = new ActionBuilder();
85         actionBuilder.setExperimenterId(experimenterId);
86         final ActionDecapBuilder actionDecapBuilder = new ActionDecapBuilder();
87         NxActionDecapBuilder nxActionDecapBuilder = new NxActionDecapBuilder();
88
89         nxActionDecapBuilder.setPacketType(PACKET_TYPE);
90
91         actionDecapBuilder.setNxActionDecap(nxActionDecapBuilder.build());
92         actionBuilder.setActionChoice(actionDecapBuilder.build());
93
94         return actionBuilder.build();
95     }
96 }