Merge "BUG-5888: moving the reconciliation process into a different thread to prevent...
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / RegLoadCodecTest.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
9 package org.opendaylight.openflowjava.nx.codec.action;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import java.math.BigInteger;
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.ActionRegLoad;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionRegLoadBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.reg.load.grouping.NxActionRegLoadBuilder;
26
27 public class RegLoadCodecTest {
28
29     RegLoadCodec regLoadCodec;
30     ByteBuf buffer;
31     Action action;
32
33     private final int LENGTH = 24;
34     private final byte SUBTYPE = 7;
35
36     @Before
37     public void setUp() {
38         regLoadCodec = new RegLoadCodec();
39         buffer = ByteBufAllocator.DEFAULT.buffer();
40     }
41
42     @Test
43     public void serializeTest() {
44         action = createAction();
45         regLoadCodec.serialize(action, buffer);
46
47         assertEquals(LENGTH, buffer.readableBytes());
48         //SerializeHeader part
49         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
50         assertEquals(LENGTH, buffer.readUnsignedShort());
51         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
52         assertEquals(SUBTYPE, buffer.readUnsignedShort());
53         //Serialize part
54         assertEquals(1, buffer.readUnsignedShort());
55         assertEquals(2, buffer.readUnsignedInt());
56         assertEquals(3, buffer.readLong());
57     }
58
59     @Test
60     public void deserializeTest() {
61         createBuffer(buffer);
62
63         action = regLoadCodec.deserialize(buffer);
64
65         ActionRegLoad result = (ActionRegLoad) action.getActionChoice();
66
67         assertEquals(1, result.getNxActionRegLoad().getOfsNbits().shortValue());
68         assertEquals(2, result.getNxActionRegLoad().getDst().longValue());
69         assertEquals(3, result.getNxActionRegLoad().getValue().longValue());
70         assertEquals(0, buffer.readableBytes());
71     }
72
73     private Action createAction() {
74         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
75         ActionBuilder actionBuilder = new ActionBuilder();
76         actionBuilder.setExperimenterId(experimenterId);
77         ActionRegLoadBuilder actionRegLoadBuilder = new ActionRegLoadBuilder();
78         NxActionRegLoadBuilder nxActionRegLoadBuilder = new NxActionRegLoadBuilder();
79
80         nxActionRegLoadBuilder.setOfsNbits(1);
81         nxActionRegLoadBuilder.setDst((long)2);
82         nxActionRegLoadBuilder.setValue(BigInteger.valueOf(3));
83
84         actionRegLoadBuilder.setNxActionRegLoad(nxActionRegLoadBuilder.build());
85         actionBuilder.setActionChoice(actionRegLoadBuilder.build());
86
87         return actionBuilder.build();
88     }
89
90     private void createBuffer(ByteBuf message) {
91         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
92         message.writeShort(LENGTH);
93         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
94         message.writeShort(SUBTYPE);
95
96         message.writeShort(1);
97         message.writeInt(2);
98         message.writeLong(3);
99     }
100
101 }