2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.nx.codec.action;
11 import static org.junit.Assert.assertEquals;
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;
28 public class MultipathCodecTest {
30 MultipathCodec multipathCodec;
34 private final int LENGTH = 32;
35 private final byte NXAST_MULTIPATH_SUBTYPE = 10;
40 multipathCodec = new MultipathCodec();
41 buffer = ByteBufAllocator.DEFAULT.buffer();
46 public void serializeTest() {
47 action = createAction();
48 multipathCodec.serialize(action, buffer);
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());
58 assertEquals(OfjNxHashFields.NXHASHFIELDSETHSRC.ordinal(), buffer.readUnsignedShort());
59 assertEquals(2, buffer.readUnsignedShort());
61 assertEquals(OfjNxMpAlgorithm.NXMPALGMODULON.ordinal(), buffer.readUnsignedShort());
62 assertEquals(4, buffer.readUnsignedShort());
63 assertEquals(5, buffer.readUnsignedInt());
67 public void deserializeTest() {
70 action = multipathCodec.deserialize(buffer);
72 ActionMultipath result = (ActionMultipath) action.getActionChoice();
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());
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();
92 nxActionMultipathBuilder.setFields(OfjNxHashFields.NXHASHFIELDSETHSRC);
93 nxActionMultipathBuilder.setBasis(2);
95 nxActionMultipathBuilder.setAlgorithm(OfjNxMpAlgorithm.NXMPALGMODULON);
96 nxActionMultipathBuilder.setMaxLink(4);
97 nxActionMultipathBuilder.setArg((long)5);
100 nxActionMultipathBuilder.setOfsNbits(6);
101 nxActionMultipathBuilder.setDst((long)7);
104 actionMultipathBuilder.setNxActionMultipath(nxActionMultipathBuilder.build());
105 actionBuilder.setActionChoice(actionMultipathBuilder.build());
107 return actionBuilder.build();
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);
116 //FIELDS = OfjNxHashFields.NXHASHFIELDSETHSRC
117 message.writeShort(OfjNxHashFields.NXHASHFIELDSETHSRC.getIntValue());
119 message.writeShort(1);
120 //place 2 empty bytes
121 message.writeZero(2);
122 //Algorithm = OfjNxMpAlgorithm.NXMPALGMODULON
123 message.writeShort(OfjNxMpAlgorithm.NXMPALGMODULON.getIntValue());
125 message.writeShort(2);
128 //place 2 empty bytes
129 message.writeZero(2);
131 message.writeShort(4);