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;
27 public class DecapCodecTest {
29 private static final int LENGTH = 16;
30 private static final int SUBTYPE = 47;
31 private static final long PACKET_TYPE = 0xFFFEL;
33 DecapCodec decapCodec;
39 decapCodec = new DecapCodec();
40 buffer = ByteBufAllocator.DEFAULT.buffer();
44 public void deserialize() {
47 action = decapCodec.deserialize(buffer);
49 ActionDecap result = (ActionDecap) action.getActionChoice();
51 assertEquals(PACKET_TYPE, result.getNxActionDecap().getPacketType().longValue());
52 assertFalse(buffer.isReadable());
56 public void serialize() {
57 action = createAction();
58 decapCodec.serialize(action, buffer);
60 assertEquals(LENGTH, buffer.readableBytes());
61 //SerializeHeader part
62 assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
63 assertEquals(LENGTH, buffer.readUnsignedShort());
64 assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
65 assertEquals(SUBTYPE, buffer.readUnsignedShort());
67 assertEquals(0, buffer.readUnsignedShort());
68 assertEquals(PACKET_TYPE, buffer.readUnsignedInt());
69 assertFalse(buffer.isReadable());
72 private void createBuffer(ByteBuf message) {
73 message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
74 message.writeShort(LENGTH);
75 message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
76 message.writeShort(SUBTYPE);
78 message.writeInt((int) PACKET_TYPE);
81 private Action createAction() {
82 ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
83 ActionBuilder actionBuilder = new ActionBuilder();
84 actionBuilder.setExperimenterId(experimenterId);
85 final ActionDecapBuilder actionDecapBuilder = new ActionDecapBuilder();
86 NxActionDecapBuilder nxActionDecapBuilder = new NxActionDecapBuilder();
88 nxActionDecapBuilder.setPacketType(PACKET_TYPE);
90 actionDecapBuilder.setNxActionDecap(nxActionDecapBuilder.build());
91 actionBuilder.setActionChoice(actionDecapBuilder.build());
93 return actionBuilder.build();