Add support for encap/decap nicira actions
[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
27 public class DecapCodecTest {
28
29     private static final int LENGTH = 16;
30     private static final int SUBTYPE = 47;
31     private static final long PACKET_TYPE = 0xFFFEL;
32
33     DecapCodec decapCodec;
34     ByteBuf buffer;
35     Action action;
36
37     @Before
38     public void setUp() {
39         decapCodec = new DecapCodec();
40         buffer = ByteBufAllocator.DEFAULT.buffer();
41     }
42
43     @Test
44     public void deserialize() {
45         createBuffer(buffer);
46
47         action = decapCodec.deserialize(buffer);
48
49         ActionDecap result = (ActionDecap) action.getActionChoice();
50
51         assertEquals(PACKET_TYPE, result.getNxActionDecap().getPacketType().longValue());
52         assertFalse(buffer.isReadable());
53     }
54
55     @Test
56     public void serialize() {
57         action = createAction();
58         decapCodec.serialize(action, buffer);
59
60         assertEquals(LENGTH, buffer.readableBytes());
61         //SerializeHeader part
62         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
63         assertEquals(LENGTH, buffer.readUnsignedShort());
64         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
65         assertEquals(SUBTYPE, buffer.readUnsignedShort());
66         //Serialize part
67         assertEquals(0, buffer.readUnsignedShort());
68         assertEquals(PACKET_TYPE, buffer.readUnsignedInt());
69         assertFalse(buffer.isReadable());
70     }
71
72     private void createBuffer(ByteBuf message) {
73         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
74         message.writeShort(LENGTH);
75         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
76         message.writeShort(SUBTYPE);
77         message.writeZero(2);
78         message.writeInt((int) PACKET_TYPE);
79     }
80
81     private Action createAction() {
82         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
83         ActionBuilder actionBuilder = new ActionBuilder();
84         actionBuilder.setExperimenterId(experimenterId);
85         final ActionDecapBuilder actionDecapBuilder = new ActionDecapBuilder();
86         NxActionDecapBuilder nxActionDecapBuilder = new NxActionDecapBuilder();
87
88         nxActionDecapBuilder.setPacketType(PACKET_TYPE);
89
90         actionDecapBuilder.setNxActionDecap(nxActionDecapBuilder.build());
91         actionBuilder.setActionChoice(actionDecapBuilder.build());
92
93         return actionBuilder.build();
94     }
95 }