Fix checkstyle
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / RegMoveCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.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.ActionRegMove;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionRegMoveBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.move.grouping.NxActionRegMoveBuilder;
27
28 public class RegMoveCodecTest {
29     private static final byte SUBTYPE = 6;
30
31     private static final int SRC = 4;
32     private static final int DST = 5;
33     private static final long SRC_EXP = 0xFFFF000000000000L | SRC;
34     private static final long DST_EXP = 0xFFFF000000000000L | DST;
35     private static final BigInteger SRC_EXP_BIGINT = new BigInteger(1, Longs.toByteArray(SRC_EXP));
36     private static final BigInteger DST_EXP_BIGINT = new BigInteger(1, Longs.toByteArray(DST_EXP));
37
38     RegMoveCodec regMoveCodec;
39     ByteBuf buffer;
40     Action action;
41
42     @Before
43     public void setUp() {
44         regMoveCodec = new RegMoveCodec();
45         buffer = ByteBufAllocator.DEFAULT.buffer();
46     }
47
48     @Test
49     public void serializeTest() {
50         action = createAction(BigInteger.valueOf(SRC), BigInteger.valueOf(DST));
51
52         regMoveCodec.serialize(action, buffer);
53
54         //SerializeHeader part
55         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
56         assertEquals(24, buffer.readUnsignedShort());
57         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
58         assertEquals(SUBTYPE, buffer.readUnsignedShort());
59         //Serialize part
60         assertEquals(1, buffer.readUnsignedShort());
61         assertEquals(2, buffer.readUnsignedShort());
62         assertEquals(3, buffer.readUnsignedShort());
63         assertEquals(SRC, buffer.readUnsignedInt());
64         assertEquals(DST, buffer.readUnsignedInt());
65         assertFalse(buffer.isReadable());
66     }
67
68     @Test
69     public void serializeWithExperimenterSrcTest() {
70         action = createAction(SRC_EXP_BIGINT, BigInteger.valueOf(DST));
71
72         regMoveCodec.serialize(action, buffer);
73
74         //SerializeHeader part
75         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
76         assertEquals(32, buffer.readUnsignedShort());
77         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
78         assertEquals(SUBTYPE, buffer.readUnsignedShort());
79         //Serialize part
80         assertEquals(1, buffer.readUnsignedShort());
81         assertEquals(2, buffer.readUnsignedShort());
82         assertEquals(3, buffer.readUnsignedShort());
83         assertEquals(SRC_EXP, buffer.readLong());
84         assertEquals(5, buffer.readInt());
85         assertEquals(0, buffer.readInt()); // padding
86         assertFalse(buffer.isReadable());
87     }
88
89     @Test
90     public void serializeWithExperimenterBothTest() {
91         action = createAction(SRC_EXP_BIGINT, DST_EXP_BIGINT);
92
93         regMoveCodec.serialize(action, buffer);
94
95         //SerializeHeader part
96         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
97         assertEquals(32, buffer.readUnsignedShort());
98         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
99         assertEquals(SUBTYPE, buffer.readUnsignedShort());
100         //Serialize part
101         assertEquals(1, buffer.readUnsignedShort());
102         assertEquals(2, buffer.readUnsignedShort());
103         assertEquals(3, buffer.readUnsignedShort());
104         assertEquals(SRC_EXP, buffer.readLong());
105         assertEquals(DST_EXP, buffer.readLong());
106         assertFalse(buffer.isReadable());
107     }
108
109     @Test
110     public void deserializeTest() {
111         createBuffer(buffer, false, false);
112
113         action = regMoveCodec.deserialize(buffer);
114
115         ActionRegMove result = (ActionRegMove) action.getActionChoice();
116
117         assertEquals(1, result.getNxActionRegMove().getNBits().shortValue());
118         assertEquals(2, result.getNxActionRegMove().getSrcOfs().shortValue());
119         assertEquals(3, result.getNxActionRegMove().getDstOfs().shortValue());
120         assertEquals(4, result.getNxActionRegMove().getSrc().intValue());
121         assertEquals(5, result.getNxActionRegMove().getDst().intValue());
122         assertEquals(0, buffer.readableBytes());
123     }
124
125     @Test
126     public void deserializeWithExperimenterDstTest() {
127         createBuffer(buffer, false, true);
128
129         action = regMoveCodec.deserialize(buffer);
130
131         ActionRegMove result = (ActionRegMove) action.getActionChoice();
132
133         assertEquals(1, result.getNxActionRegMove().getNBits().shortValue());
134         assertEquals(2, result.getNxActionRegMove().getSrcOfs().shortValue());
135         assertEquals(3, result.getNxActionRegMove().getDstOfs().shortValue());
136         assertEquals(4, result.getNxActionRegMove().getSrc().longValue());
137         assertEquals(DST_EXP_BIGINT, result.getNxActionRegMove().getDst());
138         assertEquals(0, buffer.readableBytes());
139     }
140
141     @Test
142     public void deserializeWithExperimenterBothTest() {
143         createBuffer(buffer, true, true);
144
145         action = regMoveCodec.deserialize(buffer);
146
147         ActionRegMove result = (ActionRegMove) action.getActionChoice();
148
149         assertEquals(1, result.getNxActionRegMove().getNBits().shortValue());
150         assertEquals(2, result.getNxActionRegMove().getSrcOfs().shortValue());
151         assertEquals(3, result.getNxActionRegMove().getDstOfs().shortValue());
152         assertEquals(SRC_EXP_BIGINT, result.getNxActionRegMove().getSrc());
153         assertEquals(DST_EXP_BIGINT, result.getNxActionRegMove().getDst());
154         assertEquals(0, buffer.readableBytes());
155     }
156
157     private Action createAction(BigInteger src, BigInteger dst) {
158         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
159         ActionBuilder actionBuilder = new ActionBuilder();
160         actionBuilder.setExperimenterId(experimenterId);
161         final ActionRegMoveBuilder actionRegMoveBuilder = new ActionRegMoveBuilder();
162         NxActionRegMoveBuilder nxActionRegMoveBuilder = new NxActionRegMoveBuilder();
163
164         nxActionRegMoveBuilder.setNBits(1);
165         nxActionRegMoveBuilder.setSrcOfs(2);
166         nxActionRegMoveBuilder.setDstOfs(3);
167         nxActionRegMoveBuilder.setSrc(src);
168         nxActionRegMoveBuilder.setDst(dst);
169
170         actionRegMoveBuilder.setNxActionRegMove(nxActionRegMoveBuilder.build());
171         actionBuilder.setActionChoice(actionRegMoveBuilder.build());
172
173         return actionBuilder.build();
174     }
175
176     private void createBuffer(ByteBuf message, boolean withExpSrc, boolean withExpDst) {
177         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
178         int length = withExpSrc || withExpDst ? 32 : 24;
179         message.writeShort(length);
180         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
181         message.writeShort(SUBTYPE);
182
183         message.writeShort(1);
184         message.writeShort(2);
185         message.writeShort(3);
186         if (withExpSrc) {
187             message.writeLong(SRC_EXP);
188         } else {
189             message.writeInt(SRC);
190         }
191         if (withExpDst) {
192             message.writeLong(DST_EXP);
193         } else {
194             message.writeInt(DST);
195         }
196         if (message.writerIndex() < length) {
197             message.writeInt(0);
198         }
199     }
200 }