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.Uint64;
29 public class OutputReg2CodecTest {
31 private static final int LENGTH = 24;
32 private static final byte SUBTYPE = 32;
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)));
40 private OutputReg2Codec outReg2Codec;
41 private ByteBuf buffer;
45 outReg2Codec = new OutputReg2Codec();
46 buffer = ByteBufAllocator.DEFAULT.buffer();
50 public void deserializeTest() {
51 createBuffer(buffer, false);
53 Action action = outReg2Codec.deserialize(buffer);
55 ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
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());
64 public void deserializeWithExperimenterTest() {
65 createBuffer(buffer, true);
67 Action action = outReg2Codec.deserialize(buffer);
69 ActionOutputReg2 result = (ActionOutputReg2) action.getActionChoice();
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());
78 public void serializeTest() {
79 Action action = createAction(Uint64.valueOf(SRC));
80 outReg2Codec.serialize(action, buffer);
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());
89 assertEquals(OFS_N_BITS, buffer.readUnsignedShort());
90 assertEquals(MAX_LEN, buffer.readUnsignedShort());
91 assertEquals(SRC, buffer.readUnsignedInt());
94 assertEquals(0, buffer.readUnsignedInt());
95 assertEquals(0, buffer.readUnsignedShort());
97 assertFalse(buffer.isReadable());
101 public void serializeWithExperimenterTest() {
102 Action action = createAction(SRC_EXP_BIGINT);
103 outReg2Codec.serialize(action, buffer);
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());
112 assertEquals(OFS_N_BITS, buffer.readUnsignedShort());
113 assertEquals(MAX_LEN, buffer.readUnsignedShort());
114 assertEquals(SRC_EXP, buffer.readLong());
117 assertEquals(0, buffer.readUnsignedShort());
119 assertFalse(buffer.isReadable());
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();
129 nxActionOutputBuilder.setNBits(OFS_N_BITS);
130 nxActionOutputBuilder.setSrc(src);
131 nxActionOutputBuilder.setMaxLen(MAX_LEN);
133 actionOutputReg2Builder.setNxActionOutputReg2(nxActionOutputBuilder.build());
134 actionBuilder.setActionChoice(actionOutputReg2Builder.build());
136 return actionBuilder.build();
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);
147 message.writeLong(SRC_EXP);
148 message.writeZero(2);
150 message.writeInt(SRC);
151 message.writeZero(6);