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