Merge "Add INFO.yaml for openflowplugin"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / CtClearCodecTest.java
1 /*
2  * Copyright (c) 2018 Redhat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.nx.codec.action;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionCtClear;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionCtClearBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.ct.clear.grouping.NxActionCtClear;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.ct.clear.grouping.NxActionCtClearBuilder;
26
27 public class CtClearCodecTest {
28     private final int length = 16;
29     private final byte nxCtClearSubType = 43;
30     private final int padding = 6;
31
32     private CtClearCodec ctClearCodec;
33     private ByteBuf buffer;
34     private Action action;
35
36     @Before
37     public void setUp() {
38         ctClearCodec = new CtClearCodec();
39         buffer = ByteBufAllocator.DEFAULT.buffer();
40     }
41
42     @Test
43     public void serializeTest() {
44         action = createAction();
45         ctClearCodec.serialize(action, buffer);
46         assertEquals(length, buffer.readableBytes());
47         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
48         assertEquals(length, buffer.readUnsignedShort());
49         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
50         assertEquals(nxCtClearSubType, buffer.readUnsignedShort());
51         buffer.skipBytes(padding);
52     }
53
54
55     @Test
56     public void deserializeTest() {
57         createBuffer(buffer);
58         action = ctClearCodec.deserialize(buffer);
59         assertEquals(action.getExperimenterId().getValue(), NiciraConstants.NX_VENDOR_ID);
60         ActionCtClear result = (ActionCtClear) action.getActionChoice();
61         NxActionCtClear nxActionCtClear = result.getNxActionCtClear();
62         assertNotNull(nxActionCtClear);
63
64     }
65
66     private static Action createAction() {
67
68         NxActionCtClearBuilder nxActionCtClearBuilder = new NxActionCtClearBuilder();
69         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
70         ActionBuilder actionBuilder = new ActionBuilder();
71         actionBuilder.setExperimenterId(experimenterId);
72         ActionCtClearBuilder actionCtClearBuilder = new ActionCtClearBuilder();
73         actionCtClearBuilder.setNxActionCtClear(nxActionCtClearBuilder.build());
74         actionBuilder.setActionChoice(actionCtClearBuilder.build());
75
76         return actionBuilder.build();
77     }
78
79     private void createBuffer(ByteBuf message) {
80         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
81         message.writeShort(length);
82         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
83         message.writeShort(nxCtClearSubType);
84         message.writeZero(6);
85     }
86 }
87
88