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