70d4672ff9d22063266b89b695573d240bb0d62b
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyMessageDeserializerTest.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.openflowplugin.impl.protocol.deserialization.AbstractDeserializerTest;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCase;
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.multipart.types.rev170112.MultipartReply;
30
31 public class MultipartReplyMessageDeserializerTest extends AbstractDeserializerTest {
32     private static final byte PADDING_IN_MULTIPART_REPLY_HEADER = 4;
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 int XID = 42;
37     private static final short TYPE = 19;
38     private static final short MULTIPART_TYPE = 1;
39     private static final short REQ_MORE = 1;
40     private static final short ITEM_LENGTH = 80;
41
42     private static  final byte TABLE_ID = 1;
43     private static  final int SECOND = 1;
44     private static  final int NANOSECOND = 2;
45     private static  final short PRIORITY = 2;
46     private static  final short IDLE_TIMEOUT = 3;
47     private static  final short HARD_TIMEOUT = 4;
48     private static final boolean SEND_FLOWREM = true;
49     private static final boolean RESET_COUNTS = false;
50     private static final boolean NO_PKTCOUNTS = true;
51     private static final boolean NO_BYTCOUNTS = true;
52     private static final boolean CHECK_OVERLAP = false;
53     private static final FlowModFlags FLAGS = new FlowModFlags(
54             CHECK_OVERLAP, NO_BYTCOUNTS, NO_PKTCOUNTS, RESET_COUNTS, SEND_FLOWREM);
55     private static  final long COOKIE = 2;
56     private static  final long PACKET_COUNT = 4;
57     private static  final long BYTE_COUNT = 5;
58
59     private static final int OXM_MATCH_TYPE_CODE = 1;
60     private static final int MPLS_LABEL = 135;
61
62     @Override
63     protected void init() {
64     }
65
66     @Test
67     public void deserialize() {
68         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
69         buffer.writeByte(TYPE);
70         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
71         buffer.writeInt(XID);
72         buffer.writeShort(MULTIPART_TYPE);
73         buffer.writeShort(REQ_MORE);
74         buffer.writeZero(PADDING_IN_MULTIPART_REPLY_HEADER);
75
76         buffer.writeShort(ITEM_LENGTH);
77         buffer.writeByte(TABLE_ID);
78         buffer.writeZero(PADDING_IN_FLOW_STATS_HEADER_01);
79         buffer.writeInt(SECOND);
80         buffer.writeInt(NANOSECOND);
81         buffer.writeShort(PRIORITY);
82         buffer.writeShort(IDLE_TIMEOUT);
83         buffer.writeShort(HARD_TIMEOUT);
84         buffer.writeShort(ByteBufUtils.fillBitMask(0,
85                 FLAGS.isSENDFLOWREM(),
86                 FLAGS.isCHECKOVERLAP(),
87                 FLAGS.isRESETCOUNTS(),
88                 FLAGS.isNOPKTCOUNTS(),
89                 FLAGS.isNOBYTCOUNTS()));
90         buffer.writeZero(PADDING_IN_FLOW_STATS_HEADER_02);
91         buffer.writeLong(COOKIE);
92         buffer.writeLong(PACKET_COUNT);
93         buffer.writeLong(BYTE_COUNT);
94
95         // Match header
96         int matchStartIndex = buffer.writerIndex();
97         buffer.writeShort(OXM_MATCH_TYPE_CODE);
98         int matchLengthIndex = buffer.writerIndex();
99         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
100
101         // MplsLabel match
102         buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
103         buffer.writeByte(OxmMatchConstants.MPLS_LABEL << 1);
104         buffer.writeByte(EncodeConstants.SIZE_OF_INT_IN_BYTES);
105         buffer.writeInt(MPLS_LABEL);
106
107         // Match footer
108         int matchLength = buffer.writerIndex() - matchStartIndex;
109         buffer.setShort(matchLengthIndex, matchLength);
110         int paddingRemainder = matchLength % EncodeConstants.PADDING;
111         if (paddingRemainder != 0) {
112             buffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
113         }
114
115         // Instruction POP PBB header
116         int instructionStartIndex = buffer.writerIndex();
117         buffer.writeShort(InstructionConstants.APPLY_ACTIONS_TYPE);
118         int instructionLengthIndex = buffer.writerIndex();
119         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
120         buffer.writeZero(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION);
121
122         // POP PBB action
123         buffer.writeShort(ActionConstants.POP_PBB_CODE);
124         buffer.writeShort(ActionConstants.GENERAL_ACTION_LENGTH);
125         buffer.writeZero(ActionConstants.PADDING_IN_ACTION_HEADER);
126
127         // Count total length of instruction
128         buffer.setShort(instructionLengthIndex, buffer.writerIndex() - instructionStartIndex);
129
130         // Deserialize and check everything
131         final MultipartReply message = (MultipartReply) getFactory()
132                 .deserialize(buffer, EncodeConstants.OF13_VERSION_ID);
133
134         final MultipartReplyFlowStats reply = (MultipartReplyFlowStats) message.getMultipartReplyBody();
135
136         assertEquals(XID, message.getXid().intValue());
137         final FlowAndStatisticsMapList flowAndStatisticsMapList = reply.getFlowAndStatisticsMapList().get(0);
138         assertEquals(TABLE_ID, flowAndStatisticsMapList.getTableId().shortValue());
139         assertEquals(SECOND, flowAndStatisticsMapList.getDuration().getSecond().getValue().intValue());
140         assertEquals(NANOSECOND, flowAndStatisticsMapList.getDuration().getNanosecond().getValue().intValue());
141         assertEquals(PRIORITY, flowAndStatisticsMapList.getPriority().intValue());
142         assertEquals(IDLE_TIMEOUT, flowAndStatisticsMapList.getIdleTimeout().intValue());
143         assertEquals(HARD_TIMEOUT, flowAndStatisticsMapList.getHardTimeout().intValue());
144         assertTrue(flowAndStatisticsMapList.getFlags().equals(FLAGS));
145         assertEquals(COOKIE, flowAndStatisticsMapList.getCookie().getValue().longValue());
146         assertEquals(BYTE_COUNT, flowAndStatisticsMapList.getByteCount().getValue().longValue());
147         assertEquals(PACKET_COUNT, flowAndStatisticsMapList.getPacketCount().getValue().longValue());
148
149         assertEquals(1, flowAndStatisticsMapList.getInstructions().getInstruction().size());
150
151         final Instruction instruction =
152                 flowAndStatisticsMapList.getInstructions().getInstruction().get(0).getInstruction();
153         assertEquals(ApplyActionsCase.class, instruction.implementedInterface());
154
155         final ApplyActionsCase applyActions = (ApplyActionsCase) instruction;
156         assertEquals(1, applyActions.getApplyActions().getAction().size());
157         assertEquals(PopPbbActionCase.class, applyActions.getApplyActions().getAction().get(0)
158                 .getAction().implementedInterface());
159     }
160 }