Merge "Bug 7764 - Do no throw warning on explicit task cancellation"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / MultipathCodecTest.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 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.OfjNxHashFields;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.OfjNxMpAlgorithm;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionMultipath;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionMultipathBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.multipath.grouping.NxActionMultipathBuilder;
27
28 public class MultipathCodecTest {
29
30     MultipathCodec multipathCodec;
31     ByteBuf buffer;
32     Action action;
33
34     private final int LENGTH = 32;
35     private final byte NXAST_MULTIPATH_SUBTYPE = 10;
36
37
38     @Before
39     public void setUp() {
40         multipathCodec = new MultipathCodec();
41         buffer = ByteBufAllocator.DEFAULT.buffer();
42     }
43
44
45     @Test
46     public void serializeTest() {
47         action = createAction();
48         multipathCodec.serialize(action, buffer);
49
50         assertEquals(LENGTH, buffer.readableBytes());
51         //SerializeHeaders part
52         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
53         assertEquals(LENGTH, buffer.readUnsignedShort());
54         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
55         assertEquals(NXAST_MULTIPATH_SUBTYPE, buffer.readUnsignedShort());
56
57         //Serialize part
58         assertEquals(OfjNxHashFields.NXHASHFIELDSETHSRC.ordinal(), buffer.readUnsignedShort());
59         assertEquals(2, buffer.readUnsignedShort());
60         buffer.skipBytes(2);
61         assertEquals(OfjNxMpAlgorithm.NXMPALGMODULON.ordinal(), buffer.readUnsignedShort());
62         assertEquals(4, buffer.readUnsignedShort());
63         assertEquals(5, buffer.readUnsignedInt());
64     }
65
66     @Test
67     public void deserializeTest() {
68         createBuffer(buffer);
69
70         action = multipathCodec.deserialize(buffer);
71
72         ActionMultipath result = (ActionMultipath) action.getActionChoice();
73
74         assertEquals(OfjNxHashFields.NXHASHFIELDSETHSRC, result.getNxActionMultipath().getFields());
75         assertEquals(1, result.getNxActionMultipath().getBasis().intValue());
76         assertEquals(OfjNxMpAlgorithm.NXMPALGMODULON, result.getNxActionMultipath().getAlgorithm());
77         assertEquals(2, result.getNxActionMultipath().getMaxLink().shortValue());
78         assertEquals(3, result.getNxActionMultipath().getArg().intValue());
79         assertEquals(4, result.getNxActionMultipath().getOfsNbits().shortValue());
80         assertEquals(5, result.getNxActionMultipath().getDst().intValue());
81         assertEquals(0, buffer.readableBytes());
82     }
83
84
85     private Action createAction() {
86         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
87         ActionBuilder actionBuilder = new ActionBuilder();
88         actionBuilder.setExperimenterId(experimenterId);
89         ActionMultipathBuilder actionMultipathBuilder = new ActionMultipathBuilder();
90         NxActionMultipathBuilder nxActionMultipathBuilder = new NxActionMultipathBuilder();
91
92         nxActionMultipathBuilder.setFields(OfjNxHashFields.NXHASHFIELDSETHSRC);
93         nxActionMultipathBuilder.setBasis(2);
94
95         nxActionMultipathBuilder.setAlgorithm(OfjNxMpAlgorithm.NXMPALGMODULON);
96         nxActionMultipathBuilder.setMaxLink(4);
97         nxActionMultipathBuilder.setArg((long)5);
98
99
100         nxActionMultipathBuilder.setOfsNbits(6);
101         nxActionMultipathBuilder.setDst((long)7);
102
103
104         actionMultipathBuilder.setNxActionMultipath(nxActionMultipathBuilder.build());
105         actionBuilder.setActionChoice(actionMultipathBuilder.build());
106
107         return actionBuilder.build();
108     }
109
110     private void createBuffer(ByteBuf message) {
111         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
112         message.writeShort(LENGTH);
113         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
114         message.writeShort(NXAST_MULTIPATH_SUBTYPE);
115
116         //FIELDS = OfjNxHashFields.NXHASHFIELDSETHSRC
117         message.writeShort(OfjNxHashFields.NXHASHFIELDSETHSRC.getIntValue());
118         //BASIS = 1
119         message.writeShort(1);
120         //place 2 empty bytes
121         message.writeZero(2);
122         //Algorithm = OfjNxMpAlgorithm.NXMPALGMODULON
123         message.writeShort(OfjNxMpAlgorithm.NXMPALGMODULON.getIntValue());
124         //MaxLink = 2
125         message.writeShort(2);
126         //Arg = 3
127         message.writeInt(3);
128         //place 2 empty bytes
129         message.writeZero(2);
130         //OfsNbits = 4
131         message.writeShort(4);
132         //Dst = 5
133         message.writeInt(5);
134     }
135 }