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
9 package org.opendaylight.openflowjava.nx.codec.action;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufAllocator;
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.ActionDecap;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionDecapBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.decap.grouping.NxActionDecapBuilder;
26 import org.opendaylight.yangtools.yang.common.Uint32;
28 public class DecapCodecTest {
30 private static final int LENGTH = 16;
31 private static final int SUBTYPE = 47;
32 private static final Uint32 PACKET_TYPE = Uint32.valueOf(0xFFFEL);
34 DecapCodec decapCodec;
40 decapCodec = new DecapCodec();
41 buffer = ByteBufAllocator.DEFAULT.buffer();
45 public void deserialize() {
48 action = decapCodec.deserialize(buffer);
50 ActionDecap result = (ActionDecap) action.getActionChoice();
52 assertEquals(PACKET_TYPE, result.getNxActionDecap().getPacketType());
53 assertFalse(buffer.isReadable());
57 public void serialize() {
58 action = createAction();
59 decapCodec.serialize(action, buffer);
61 assertEquals(LENGTH, buffer.readableBytes());
62 //SerializeHeader part
63 assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
64 assertEquals(LENGTH, buffer.readUnsignedShort());
65 assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
66 assertEquals(SUBTYPE, buffer.readUnsignedShort());
68 assertEquals(0, buffer.readUnsignedShort());
69 assertEquals(PACKET_TYPE.longValue(), buffer.readUnsignedInt());
70 assertFalse(buffer.isReadable());
73 private static void createBuffer(ByteBuf message) {
74 message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
75 message.writeShort(LENGTH);
76 message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
77 message.writeShort(SUBTYPE);
79 message.writeInt(PACKET_TYPE.intValue());
82 private static Action createAction() {
83 ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
84 ActionBuilder actionBuilder = new ActionBuilder();
85 actionBuilder.setExperimenterId(experimenterId);
86 final ActionDecapBuilder actionDecapBuilder = new ActionDecapBuilder();
87 NxActionDecapBuilder nxActionDecapBuilder = new NxActionDecapBuilder();
89 nxActionDecapBuilder.setPacketType(PACKET_TYPE);
91 actionDecapBuilder.setNxActionDecap(nxActionDecapBuilder.build());
92 actionBuilder.setActionChoice(actionDecapBuilder.build());
94 return actionBuilder.build();