2 * Copyright (c) 2016 Cisco Systems, Inc. and others. 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
9 package org.opendaylight.openflowjava.nx.codec.action;
11 import static org.junit.Assert.assertEquals;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import java.math.BigInteger;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionRegLoad;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionRegLoadBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.load.grouping.NxActionRegLoadBuilder;
27 public class RegLoadCodecTest {
29 RegLoadCodec regLoadCodec;
33 private final int LENGTH = 24;
34 private final byte SUBTYPE = 7;
38 regLoadCodec = new RegLoadCodec();
39 buffer = ByteBufAllocator.DEFAULT.buffer();
43 public void serializeTest() {
44 action = createAction();
45 regLoadCodec.serialize(action, buffer);
47 assertEquals(LENGTH, buffer.readableBytes());
48 //SerializeHeader part
49 assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
50 assertEquals(LENGTH, buffer.readUnsignedShort());
51 assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
52 assertEquals(SUBTYPE, buffer.readUnsignedShort());
54 assertEquals(1, buffer.readUnsignedShort());
55 assertEquals(2, buffer.readUnsignedInt());
56 assertEquals(3, buffer.readLong());
60 public void deserializeTest() {
63 action = regLoadCodec.deserialize(buffer);
65 ActionRegLoad result = (ActionRegLoad) action.getActionChoice();
67 assertEquals(1, result.getNxActionRegLoad().getOfsNbits().shortValue());
68 assertEquals(2, result.getNxActionRegLoad().getDst().longValue());
69 assertEquals(3, result.getNxActionRegLoad().getValue().longValue());
70 assertEquals(0, buffer.readableBytes());
73 private Action createAction() {
74 ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
75 ActionBuilder actionBuilder = new ActionBuilder();
76 actionBuilder.setExperimenterId(experimenterId);
77 ActionRegLoadBuilder actionRegLoadBuilder = new ActionRegLoadBuilder();
78 NxActionRegLoadBuilder nxActionRegLoadBuilder = new NxActionRegLoadBuilder();
80 nxActionRegLoadBuilder.setOfsNbits(1);
81 nxActionRegLoadBuilder.setDst((long)2);
82 nxActionRegLoadBuilder.setValue(BigInteger.valueOf(3));
84 actionRegLoadBuilder.setNxActionRegLoad(nxActionRegLoadBuilder.build());
85 actionBuilder.setActionChoice(actionRegLoadBuilder.build());
87 return actionBuilder.build();
90 private void createBuffer(ByteBuf message) {
91 message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
92 message.writeShort(LENGTH);
93 message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
94 message.writeShort(SUBTYPE);
96 message.writeShort(1);