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