Migrate openflowjava-extension-nicira tests to Uint types
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / OutputReg2CodecTest.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 package org.opendaylight.openflowjava.nx.codec.action;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12
13 import com.google.common.primitives.Longs;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufAllocator;
16 import java.math.BigInteger;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionOutputReg2;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionOutputReg2Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.output.reg2.grouping.NxActionOutputReg2Builder;
27 import org.opendaylight.yangtools.yang.common.Uint16;
28 import org.opendaylight.yangtools.yang.common.Uint64;
29
30 public class OutputReg2CodecTest {
31
32     private static final int LENGTH = 24;
33     private static final byte SUBTYPE = 32;
34
35     private static final Uint16 OFS_N_BITS = Uint16.ONE;
36     private static final int SRC = 2;
37     private static final Uint16 MAX_LEN = Uint16.TWO;
38     private static final long SRC_EXP = 0xFFFF000000000000L | SRC;
39     private static final Uint64 SRC_EXP_BIGINT = Uint64.valueOf(new BigInteger(1, Longs.toByteArray(SRC_EXP)));
40
41     private OutputReg2Codec outReg2Codec;
42     private ByteBuf buffer;
43
44     @Before
45     public void setUp() {
46         outReg2Codec = new OutputReg2Codec();
47         buffer = ByteBufAllocator.DEFAULT.buffer();
48     }
49
50     @Test
51     public void deserializeTest() {
52         createBuffer(buffer, false);
53
54         Action action = outReg2Codec.deserialize(buffer);
55
56         ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
57
58         assertEquals(OFS_N_BITS, result.getNxActionOutputReg2().getNBits());
59         assertEquals(MAX_LEN, result.getNxActionOutputReg2().getMaxLen());
60         assertEquals(Uint64.valueOf(SRC), result.getNxActionOutputReg2().getSrc());
61         assertFalse(buffer.isReadable());
62     }
63
64     @Test
65     public void deserializeWithExperimenterTest() {
66         createBuffer(buffer, true);
67
68         Action action = outReg2Codec.deserialize(buffer);
69
70         ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
71
72         assertEquals(OFS_N_BITS, result.getNxActionOutputReg2().getNBits());
73         assertEquals(MAX_LEN, result.getNxActionOutputReg2().getMaxLen());
74         assertEquals(SRC_EXP_BIGINT, result.getNxActionOutputReg2().getSrc());
75         assertFalse(buffer.isReadable());
76     }
77
78     @Test
79     public void serializeTest() {
80         Action action = createAction(Uint64.valueOf(SRC));
81         outReg2Codec.serialize(action, buffer);
82
83         //SerializeHeader part
84         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
85         assertEquals(LENGTH, buffer.readUnsignedShort());
86         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
87         assertEquals(SUBTYPE, buffer.readUnsignedShort());
88
89         //Serialize part
90         assertEquals(OFS_N_BITS.shortValue(), buffer.readUnsignedShort());
91         assertEquals(MAX_LEN.shortValue(), buffer.readUnsignedShort());
92         assertEquals(SRC, buffer.readUnsignedInt());
93
94         // padding
95         assertEquals(0, buffer.readUnsignedInt());
96         assertEquals(0, buffer.readUnsignedShort());
97
98         assertFalse(buffer.isReadable());
99     }
100
101     @Test
102     public void serializeWithExperimenterTest() {
103         Action action = createAction(SRC_EXP_BIGINT);
104         outReg2Codec.serialize(action, buffer);
105
106         //SerializeHeader part
107         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
108         assertEquals(LENGTH, buffer.readUnsignedShort());
109         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
110         assertEquals(SUBTYPE, buffer.readUnsignedShort());
111
112         //Serialize part
113         assertEquals(OFS_N_BITS.shortValue(), buffer.readUnsignedShort());
114         assertEquals(MAX_LEN.shortValue(), buffer.readUnsignedShort());
115         assertEquals(SRC_EXP, buffer.readLong());
116
117         // padding
118         assertEquals(0, buffer.readUnsignedShort());
119
120         assertFalse(buffer.isReadable());
121     }
122
123     private static Action createAction(final Uint64 src) {
124         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
125         ActionBuilder actionBuilder = new ActionBuilder();
126         actionBuilder.setExperimenterId(experimenterId);
127         final ActionOutputReg2Builder actionOutputReg2Builder = new ActionOutputReg2Builder();
128         NxActionOutputReg2Builder nxActionOutputBuilder = new NxActionOutputReg2Builder();
129
130         nxActionOutputBuilder.setNBits(OFS_N_BITS);
131         nxActionOutputBuilder.setSrc(src);
132         nxActionOutputBuilder.setMaxLen(MAX_LEN);
133
134         actionOutputReg2Builder.setNxActionOutputReg2(nxActionOutputBuilder.build());
135         actionBuilder.setActionChoice(actionOutputReg2Builder.build());
136
137         return actionBuilder.build();
138     }
139
140     private static void createBuffer(final ByteBuf message, final boolean withExpSrc) {
141         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
142         message.writeShort(LENGTH);
143         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
144         message.writeShort(SUBTYPE);
145         message.writeShort(OFS_N_BITS.shortValue());
146         message.writeShort(MAX_LEN.shortValue());
147         if (withExpSrc) {
148             message.writeLong(SRC_EXP);
149             message.writeZero(2);
150         } else {
151             message.writeInt(SRC);
152             message.writeZero(6);
153         }
154     }
155 }