Clean up instance checks and casts
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyFlowStatsDeserializerTest.java
1 /**
2  * Copyright (c) 2017 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.deserialization.multipart;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.UnpooledByteBufAllocator;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.openflowjava.protocol.impl.util.ActionConstants;
20 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStats;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
31
32 public class MultipartReplyFlowStatsDeserializerTest extends AbstractMultipartDeserializerTest {
33
34     private static final byte PADDING_IN_FLOW_STATS_HEADER_01 = 1;
35     private static final byte PADDING_IN_FLOW_STATS_HEADER_02 = 4;
36
37     private static  final short ITEM_LENGTH = 96;
38     private static  final byte TABLE_ID = 1;
39     private static  final int SECOND = 1;
40     private static  final int NANOSECOND = 2;
41     private static  final short PRIORITY = 2;
42     private static  final short IDLE_TIMEOUT = 3;
43     private static  final short HARD_TIMEOUT = 4;
44     private static final boolean SEND_FLOWREM = true;
45     private static final boolean RESET_COUNTS = false;
46     private static final boolean NO_PKTCOUNTS = true;
47     private static final boolean NO_BYTCOUNTS = true;
48     private static final boolean CHECK_OVERLAP = false;
49     private static final FlowModFlags FLAGS = new FlowModFlags(
50             CHECK_OVERLAP, NO_BYTCOUNTS, NO_PKTCOUNTS, RESET_COUNTS, SEND_FLOWREM);
51     private static  final long COOKIE = 2;
52     private static  final long PACKET_COUNT = 4;
53     private static  final long BYTE_COUNT = 5;
54
55     private static final int OXM_MATCH_TYPE_CODE = 1;
56     private static final int MPLS_LABEL = 135;
57
58     @Test
59     public void testDeserialize() throws Exception {
60
61         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
62
63         buffer.writeShort(ITEM_LENGTH);
64         buffer.writeByte(TABLE_ID);
65         buffer.writeZero(PADDING_IN_FLOW_STATS_HEADER_01);
66         buffer.writeInt(SECOND);
67         buffer.writeInt(NANOSECOND);
68         buffer.writeShort(PRIORITY);
69         buffer.writeShort(IDLE_TIMEOUT);
70         buffer.writeShort(HARD_TIMEOUT);
71         buffer.writeShort(ByteBufUtils.fillBitMask(0,
72                 FLAGS.isSENDFLOWREM(),
73                 FLAGS.isCHECKOVERLAP(),
74                 FLAGS.isRESETCOUNTS(),
75                 FLAGS.isNOPKTCOUNTS(),
76                 FLAGS.isNOBYTCOUNTS()));
77         buffer.writeZero(PADDING_IN_FLOW_STATS_HEADER_02);
78         buffer.writeLong(COOKIE);
79         buffer.writeLong(PACKET_COUNT);
80         buffer.writeLong(BYTE_COUNT);
81
82         // Match header
83         int matchStartIndex = buffer.writerIndex();
84         buffer.writeShort(OXM_MATCH_TYPE_CODE);
85         int matchLengthIndex = buffer.writerIndex();
86         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
87
88         // MplsLabel match
89         buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
90         buffer.writeByte(OxmMatchConstants.MPLS_LABEL << 1);
91         buffer.writeByte(EncodeConstants.SIZE_OF_INT_IN_BYTES);
92         buffer.writeInt(MPLS_LABEL);
93
94         // Match footer
95         int matchLength = buffer.writerIndex() - matchStartIndex;
96         buffer.setShort(matchLengthIndex, matchLength);
97         int paddingRemainder = matchLength % EncodeConstants.PADDING;
98         if (paddingRemainder != 0) {
99             buffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
100         }
101
102         // Instruction POP PBB header
103         int instructionStartIndex = buffer.writerIndex();
104         buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
105         int instructionLengthIndex = buffer.writerIndex();
106         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
107         buffer.writeZero(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION);
108
109         // POP PBB action
110         buffer.writeShort(ActionConstants.POP_PBB_CODE);
111         buffer.writeShort(ActionConstants.GENERAL_ACTION_LENGTH);
112         buffer.writeZero(ActionConstants.PADDING_IN_ACTION_HEADER);
113
114         // Count total length of instruction
115         buffer.setShort(instructionLengthIndex, buffer.writerIndex() - instructionStartIndex);
116
117         // Instruction POP Vlan header
118         instructionStartIndex = buffer.writerIndex();
119         buffer.writeShort(InstructionConstants.WRITE_ACTIONS_TYPE);
120         instructionLengthIndex = buffer.writerIndex();
121         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
122         buffer.writeZero(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION);
123
124         // POP Vlan action
125         buffer.writeShort(ActionConstants.POP_VLAN_CODE);
126         buffer.writeShort(ActionConstants.GENERAL_ACTION_LENGTH);
127         buffer.writeZero(ActionConstants.PADDING_IN_ACTION_HEADER);
128
129         // Count total length of instruction
130         buffer.setShort(instructionLengthIndex, buffer.writerIndex() - instructionStartIndex);
131
132         final MultipartReplyFlowStats reply = (MultipartReplyFlowStats) deserializeMultipart(buffer);
133         final FlowAndStatisticsMapList flowAndStatisticsMapList = reply.getFlowAndStatisticsMapList().get(0);
134         assertEquals(TABLE_ID, flowAndStatisticsMapList.getTableId().shortValue());
135         assertEquals(SECOND, flowAndStatisticsMapList.getDuration().getSecond().getValue().intValue());
136         assertEquals(NANOSECOND, flowAndStatisticsMapList.getDuration().getNanosecond().getValue().intValue());
137         assertEquals(PRIORITY, flowAndStatisticsMapList.getPriority().intValue());
138         assertEquals(IDLE_TIMEOUT, flowAndStatisticsMapList.getIdleTimeout().intValue());
139         assertEquals(HARD_TIMEOUT, flowAndStatisticsMapList.getHardTimeout().intValue());
140         assertTrue(flowAndStatisticsMapList.getFlags().equals(FLAGS));
141         assertEquals(COOKIE, flowAndStatisticsMapList.getCookie().getValue().longValue());
142         assertEquals(BYTE_COUNT, flowAndStatisticsMapList.getByteCount().getValue().longValue());
143         assertEquals(PACKET_COUNT, flowAndStatisticsMapList.getPacketCount().getValue().longValue());
144
145         assertEquals(2, flowAndStatisticsMapList.getInstructions().getInstruction().size());
146
147         final Instruction instruction =
148                 flowAndStatisticsMapList.getInstructions().getInstruction().get(0).getInstruction();
149         assertEquals(ApplyActionsCase.class, instruction.getImplementedInterface());
150
151         final ApplyActionsCase applyActions = (ApplyActionsCase) instruction;
152         assertEquals(1, applyActions.getApplyActions().getAction().size());
153         assertEquals(PopPbbActionCase.class, applyActions.getApplyActions().getAction().get(0)
154                 .getAction().getImplementedInterface());
155
156         final Instruction instruction1 =
157                 flowAndStatisticsMapList.getInstructions().getInstruction().get(1).getInstruction();
158         assertEquals(WriteActionsCase.class, instruction1.getImplementedInterface());
159
160         final WriteActionsCase writeActions = (WriteActionsCase) instruction1;
161         assertEquals(1, writeActions.getWriteActions().getAction().size());
162         assertEquals(PopVlanActionCase.class, writeActions.getWriteActions().getAction().get(0)
163                 .getAction().getImplementedInterface());
164     }
165
166     @Override
167     protected int getType() {
168         return MultipartType.OFPMPFLOW.getIntValue();
169     }
170 }