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 io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufAllocator;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionConntrack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionConntrackBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.conntrack.grouping.NxActionConntrackBuilder;
25 public class ConntrackCodecTest {
27 private ConntrackCodec conntrackCodec;
29 private ByteBuf buffer;
30 private Action action;
32 private final int LENGTH = 24;
33 private final byte NXAST_CONNTRACK_SUBTYPE = 35;
37 conntrackCodec = new ConntrackCodec();
38 buffer = ByteBufAllocator.DEFAULT.buffer();
42 public void serializeTest() {
43 action = createAction();
44 conntrackCodec.serialize(action, buffer);
46 Assert.assertEquals(24, buffer.readableBytes());
47 Assert.assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
48 Assert.assertEquals(LENGTH, buffer.readUnsignedShort());
49 Assert.assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
50 Assert.assertEquals(NXAST_CONNTRACK_SUBTYPE, buffer.readUnsignedShort());
51 Assert.assertEquals(1, buffer.readUnsignedShort());
52 Assert.assertEquals(2, buffer.readUnsignedInt());
53 Assert.assertEquals(3, buffer.readUnsignedShort());
54 Assert.assertEquals(4, buffer.readByte());
59 public void deserializeTest() {
61 action = conntrackCodec.deserialize(buffer);
63 ActionConntrack result = ((ActionConntrack) action.getActionChoice());
65 Assert.assertEquals(1, result.getNxActionConntrack().getFlags().shortValue());
66 Assert.assertEquals(2, result.getNxActionConntrack().getZoneSrc().intValue());
67 Assert.assertEquals(3, result.getNxActionConntrack().getConntrackZone().shortValue());
68 Assert.assertEquals(4, result.getNxActionConntrack().getRecircTable().byteValue());
69 Assert.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 ActionConntrackBuilder actionConntrackBuilder = new ActionConntrackBuilder();
79 NxActionConntrackBuilder nxActionConntrackBuilder = new NxActionConntrackBuilder();
80 nxActionConntrackBuilder.setFlags(1);
81 nxActionConntrackBuilder.setZoneSrc((long) 2);
82 nxActionConntrackBuilder.setConntrackZone(3);
83 nxActionConntrackBuilder.setRecircTable((short) 4);
84 actionConntrackBuilder.setNxActionConntrack(nxActionConntrackBuilder.build());
85 actionBuilder.setActionChoice(actionConntrackBuilder.build());
87 return actionBuilder.build();
90 private void createBufer(ByteBuf message) {
91 message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
92 message.writeShort(LENGTH);
93 message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
94 message.writeShort(NXAST_CONNTRACK_SUBTYPE);
96 message.writeShort(1);
100 message.writeShort(3);
102 message.writeByte(4);
104 message.writeZero(5);