2 * Copyright (c) 2018 SUSE LINUX GmbH. All rights reserved.
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
8 package org.opendaylight.openflowjava.nx.codec.action;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
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;
30 public class OutputReg2CodecTest {
32 private static final int LENGTH = 24;
33 private static final byte SUBTYPE = 32;
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)));
41 private OutputReg2Codec outReg2Codec;
42 private ByteBuf buffer;
46 outReg2Codec = new OutputReg2Codec();
47 buffer = ByteBufAllocator.DEFAULT.buffer();
51 public void deserializeTest() {
52 createBuffer(buffer, false);
54 Action action = outReg2Codec.deserialize(buffer);
56 ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
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());
65 public void deserializeWithExperimenterTest() {
66 createBuffer(buffer, true);
68 Action action = outReg2Codec.deserialize(buffer);
70 ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
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());
79 public void serializeTest() {
80 Action action = createAction(Uint64.valueOf(SRC));
81 outReg2Codec.serialize(action, buffer);
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());
90 assertEquals(OFS_N_BITS.shortValue(), buffer.readUnsignedShort());
91 assertEquals(MAX_LEN.shortValue(), buffer.readUnsignedShort());
92 assertEquals(SRC, buffer.readUnsignedInt());
95 assertEquals(0, buffer.readUnsignedInt());
96 assertEquals(0, buffer.readUnsignedShort());
98 assertFalse(buffer.isReadable());
102 public void serializeWithExperimenterTest() {
103 Action action = createAction(SRC_EXP_BIGINT);
104 outReg2Codec.serialize(action, buffer);
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());
113 assertEquals(OFS_N_BITS.shortValue(), buffer.readUnsignedShort());
114 assertEquals(MAX_LEN.shortValue(), buffer.readUnsignedShort());
115 assertEquals(SRC_EXP, buffer.readLong());
118 assertEquals(0, buffer.readUnsignedShort());
120 assertFalse(buffer.isReadable());
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();
130 nxActionOutputBuilder.setNBits(OFS_N_BITS);
131 nxActionOutputBuilder.setSrc(src);
132 nxActionOutputBuilder.setMaxLen(MAX_LEN);
134 actionOutputReg2Builder.setNxActionOutputReg2(nxActionOutputBuilder.build());
135 actionBuilder.setActionChoice(actionOutputReg2Builder.build());
137 return actionBuilder.build();
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());
148 message.writeLong(SRC_EXP);
149 message.writeZero(2);
151 message.writeInt(SRC);
152 message.writeZero(6);