Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / instructions / ApplyActionsInstructionSerializerTest.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.openflowplugin.impl.protocol.serialization.instructions;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import org.junit.Test;
14 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
15 import org.opendaylight.openflowjava.protocol.impl.util.ActionConstants;
16 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
27 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
28
29 public class ApplyActionsInstructionSerializerTest extends AbstractInstructionSerializerTest {
30     @Test
31     public void testSerialize() {
32         final int order = 0;
33         final Ipv4Prefix prefix = new Ipv4Prefix("192.168.76.0/32");
34
35         final Instruction instruction = new ApplyActionsCaseBuilder()
36                 .setApplyActions(new ApplyActionsBuilder()
37                         .setAction(BindingMap.of(new ActionBuilder()
38                                 .setOrder(order)
39                                 .withKey(new ActionKey(order))
40                                 .setAction(new SetNwSrcActionCaseBuilder()
41                                         .setSetNwSrcAction(new SetNwSrcActionBuilder()
42                                                 .setAddress(new Ipv4Builder()
43                                                         .setIpv4Address(prefix)
44                                                 .build())
45                                         .build())
46                                         .build())
47                                 .build()))
48                         .build())
49                 .build();
50
51         assertInstruction(instruction, out -> {
52             out.skipBytes(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION);
53             assertEquals(out.readUnsignedShort(), ActionConstants.SET_FIELD_CODE);
54             out.skipBytes(Short.BYTES); // Skip length of set field action
55             assertEquals(out.readUnsignedShort(), OxmMatchConstants.OPENFLOW_BASIC_CLASS);
56             assertEquals(out.readUnsignedByte(), OxmMatchConstants.IPV4_SRC << 1);
57             out.skipBytes(Byte.BYTES); // Skip match entry length
58
59             byte[] addressBytes = new byte[4];
60             out.readBytes(addressBytes);
61             assertArrayEquals(addressBytes, new byte[] { (byte) 192, (byte) 168, 76, 0 });
62
63             out.skipBytes(4); // Padding at end
64         });
65     }
66
67     @Override
68     protected Class<? extends Instruction> getClazz() {
69         return ApplyActionsCase.class;
70     }
71
72     @Override
73     protected int getType() {
74         return InstructionConstants.APPLY_ACTIONS_TYPE;
75     }
76
77     @Override
78     protected int getLength() {
79         return 24;
80     }
81 }