Bump upstreams
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / OutputRegCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufAllocator;
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.ofj.aug.nx.action.ActionOutputReg;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.aug.nx.action.ActionOutputRegBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.output.reg.grouping.NxActionOutputRegBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint16;
25 import org.opendaylight.yangtools.yang.common.Uint32;
26
27 public class OutputRegCodecTest {
28     private static final int LENGTH = 24;
29     private static final byte SUBTYPE = 15;
30     private static final byte PADDING = 6;
31
32     OutputRegCodec outRegCodec;
33     ByteBuf buffer;
34     Action action;
35
36     @Before
37     public void setUp() {
38         outRegCodec = new OutputRegCodec();
39         buffer = ByteBufAllocator.DEFAULT.buffer();
40     }
41
42     @Test
43     public void serializeTest() {
44         action = createAction();
45         outRegCodec.serialize(action, buffer);
46
47         assertEquals(LENGTH, buffer.readableBytes());
48
49         //SerializeHeader part
50         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
51         assertEquals(LENGTH, buffer.readUnsignedShort());
52         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
53         assertEquals(SUBTYPE, buffer.readUnsignedShort());
54
55         //Serialize part
56         assertEquals(1, buffer.readUnsignedShort());
57         assertEquals(2, buffer.readUnsignedInt());
58         assertEquals(3, buffer.readUnsignedShort());
59     }
60
61     @Test
62     public void deserializeTest() {
63         createBuffer(buffer);
64
65         action = outRegCodec.deserialize(buffer);
66
67         ActionOutputReg result = (ActionOutputReg) action.getActionChoice();
68
69         assertEquals(1, result.getNxActionOutputReg().getNBits().shortValue());
70         assertEquals(2, result.getNxActionOutputReg().getSrc().intValue());
71         assertEquals(3, result.getNxActionOutputReg().getMaxLen().shortValue());
72         assertEquals(0, buffer.readableBytes());
73     }
74
75
76     private static Action createAction() {
77         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
78         ActionBuilder actionBuilder = new ActionBuilder();
79         actionBuilder.setExperimenterId(experimenterId);
80         final ActionOutputRegBuilder actionOutputRegBuilder = new ActionOutputRegBuilder();
81         NxActionOutputRegBuilder nxActionOutputBuilder = new NxActionOutputRegBuilder();
82
83         nxActionOutputBuilder.setNBits(Uint16.ONE);
84         nxActionOutputBuilder.setSrc(Uint32.TWO);
85         nxActionOutputBuilder.setMaxLen(Uint16.valueOf(3));
86
87         actionOutputRegBuilder.setNxActionOutputReg(nxActionOutputBuilder.build());
88         actionBuilder.setActionChoice(actionOutputRegBuilder.build());
89
90         return actionBuilder.build();
91     }
92
93     private static void createBuffer(final ByteBuf message) {
94         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
95         message.writeShort(LENGTH);
96         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
97         message.writeShort(SUBTYPE);
98
99         message.writeShort(1);
100         message.writeInt(2);
101         message.writeShort(3);
102         message.writeZero(PADDING);
103     }
104 }