Merge "Added JSON and XML payloads tabs with RFC 8040 URL"
[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.Uint64;
28
29 public class OutputReg2CodecTest {
30
31     private static final int LENGTH = 24;
32     private static final byte SUBTYPE = 32;
33
34     private static final int OFS_N_BITS = 1;
35     private static final int SRC = 2;
36     private static final int MAX_LEN = 2;
37     private static final long SRC_EXP = 0xFFFF000000000000L | SRC;
38     private static final Uint64 SRC_EXP_BIGINT = Uint64.valueOf(new BigInteger(1, Longs.toByteArray(SRC_EXP)));
39
40     private OutputReg2Codec outReg2Codec;
41     private ByteBuf buffer;
42
43     @Before
44     public void setUp() {
45         outReg2Codec = new OutputReg2Codec();
46         buffer = ByteBufAllocator.DEFAULT.buffer();
47     }
48
49     @Test
50     public void deserializeTest() {
51         createBuffer(buffer, false);
52
53         Action action = outReg2Codec.deserialize(buffer);
54
55         ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
56
57         assertEquals(OFS_N_BITS, result.getNxActionOutputReg2().getNBits().shortValue());
58         assertEquals(MAX_LEN, result.getNxActionOutputReg2().getMaxLen().shortValue());
59         assertEquals(Uint64.valueOf(SRC), result.getNxActionOutputReg2().getSrc());
60         assertFalse(buffer.isReadable());
61     }
62
63     @Test
64     public void deserializeWithExperimenterTest() {
65         createBuffer(buffer, true);
66
67         Action action = outReg2Codec.deserialize(buffer);
68
69         ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
70
71         assertEquals(OFS_N_BITS, result.getNxActionOutputReg2().getNBits().shortValue());
72         assertEquals(MAX_LEN, result.getNxActionOutputReg2().getMaxLen().shortValue());
73         assertEquals(SRC_EXP_BIGINT, result.getNxActionOutputReg2().getSrc());
74         assertFalse(buffer.isReadable());
75     }
76
77     @Test
78     public void serializeTest() {
79         Action action = createAction(Uint64.valueOf(SRC));
80         outReg2Codec.serialize(action, buffer);
81
82         //SerializeHeader part
83         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
84         assertEquals(LENGTH, buffer.readUnsignedShort());
85         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
86         assertEquals(SUBTYPE, buffer.readUnsignedShort());
87
88         //Serialize part
89         assertEquals(OFS_N_BITS, buffer.readUnsignedShort());
90         assertEquals(MAX_LEN, buffer.readUnsignedShort());
91         assertEquals(SRC, buffer.readUnsignedInt());
92
93         // padding
94         assertEquals(0, buffer.readUnsignedInt());
95         assertEquals(0, buffer.readUnsignedShort());
96
97         assertFalse(buffer.isReadable());
98     }
99
100     @Test
101     public void serializeWithExperimenterTest() {
102         Action action = createAction(SRC_EXP_BIGINT);
103         outReg2Codec.serialize(action, buffer);
104
105         //SerializeHeader part
106         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
107         assertEquals(LENGTH, buffer.readUnsignedShort());
108         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
109         assertEquals(SUBTYPE, buffer.readUnsignedShort());
110
111         //Serialize part
112         assertEquals(OFS_N_BITS, buffer.readUnsignedShort());
113         assertEquals(MAX_LEN, buffer.readUnsignedShort());
114         assertEquals(SRC_EXP, buffer.readLong());
115
116         // padding
117         assertEquals(0, buffer.readUnsignedShort());
118
119         assertFalse(buffer.isReadable());
120     }
121
122     private static Action createAction(final Uint64 src) {
123         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
124         ActionBuilder actionBuilder = new ActionBuilder();
125         actionBuilder.setExperimenterId(experimenterId);
126         final ActionOutputReg2Builder actionOutputReg2Builder = new ActionOutputReg2Builder();
127         NxActionOutputReg2Builder nxActionOutputBuilder = new NxActionOutputReg2Builder();
128
129         nxActionOutputBuilder.setNBits(OFS_N_BITS);
130         nxActionOutputBuilder.setSrc(src);
131         nxActionOutputBuilder.setMaxLen(MAX_LEN);
132
133         actionOutputReg2Builder.setNxActionOutputReg2(nxActionOutputBuilder.build());
134         actionBuilder.setActionChoice(actionOutputReg2Builder.build());
135
136         return actionBuilder.build();
137     }
138
139     private static void createBuffer(final ByteBuf message, final boolean withExpSrc) {
140         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
141         message.writeShort(LENGTH);
142         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
143         message.writeShort(SUBTYPE);
144         message.writeShort(OFS_N_BITS);
145         message.writeShort(MAX_LEN);
146         if (withExpSrc) {
147             message.writeLong(SRC_EXP);
148             message.writeZero(2);
149         } else {
150             message.writeInt(SRC);
151             message.writeZero(6);
152         }
153     }
154 }