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