2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.protocol.deserialization.multipart;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
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.openflowplugin.impl.protocol.deserialization.AbstractDeserializerTest;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCase;
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.multipart.types.rev170112.MultipartReply;
30 public class MultipartReplyMessageDeserializerTest extends AbstractDeserializerTest {
31 private static final byte PADDING_IN_MULTIPART_REPLY_HEADER = 4;
32 private static final byte PADDING_IN_FLOW_STATS_HEADER_01 = 1;
33 private static final byte PADDING_IN_FLOW_STATS_HEADER_02 = 4;
35 private static final int XID = 42;
36 private static final short TYPE = 19;
37 private static final short MULTIPART_TYPE = 1;
38 private static final short REQ_MORE = 1;
39 private static final short ITEM_LENGTH = 80;
41 private static final byte TABLE_ID = 1;
42 private static final int SECOND = 1;
43 private static final int NANOSECOND = 2;
44 private static final short PRIORITY = 2;
45 private static final short IDLE_TIMEOUT = 3;
46 private static final short HARD_TIMEOUT = 4;
47 private static final boolean SEND_FLOWREM = true;
48 private static final boolean RESET_COUNTS = false;
49 private static final boolean NO_PKTCOUNTS = true;
50 private static final boolean NO_BYTCOUNTS = true;
51 private static final boolean CHECK_OVERLAP = false;
52 private static final FlowModFlags FLAGS = new FlowModFlags(
53 CHECK_OVERLAP, NO_BYTCOUNTS, NO_PKTCOUNTS, RESET_COUNTS, SEND_FLOWREM);
54 private static final long COOKIE = 2;
55 private static final long PACKET_COUNT = 4;
56 private static final long BYTE_COUNT = 5;
58 private static final int OXM_MATCH_TYPE_CODE = 1;
59 private static final int MPLS_LABEL = 135;
62 protected void init() {
67 public void deserialize() {
68 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
69 buffer.writeByte(TYPE);
70 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
72 buffer.writeShort(MULTIPART_TYPE);
73 buffer.writeShort(REQ_MORE);
74 buffer.writeZero(PADDING_IN_MULTIPART_REPLY_HEADER);
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.getSENDFLOWREM(),
86 FLAGS.getCHECKOVERLAP(),
87 FLAGS.getRESETCOUNTS(),
88 FLAGS.getNOPKTCOUNTS(),
89 FLAGS.getNOBYTCOUNTS()));
90 buffer.writeZero(PADDING_IN_FLOW_STATS_HEADER_02);
91 buffer.writeLong(COOKIE);
92 buffer.writeLong(PACKET_COUNT);
93 buffer.writeLong(BYTE_COUNT);
96 int matchStartIndex = buffer.writerIndex();
97 buffer.writeShort(OXM_MATCH_TYPE_CODE);
98 int matchLengthIndex = buffer.writerIndex();
99 buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
102 buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
103 buffer.writeByte(OxmMatchConstants.MPLS_LABEL << 1);
104 buffer.writeByte(Integer.BYTES);
105 buffer.writeInt(MPLS_LABEL);
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);
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);
123 buffer.writeShort(ActionConstants.POP_PBB_CODE);
124 buffer.writeShort(ActionConstants.GENERAL_ACTION_LENGTH);
125 buffer.writeZero(ActionConstants.PADDING_IN_ACTION_HEADER);
127 // Count total length of instruction
128 buffer.setShort(instructionLengthIndex, buffer.writerIndex() - instructionStartIndex);
130 // Deserialize and check everything
131 final MultipartReply message = (MultipartReply) getFactory()
132 .deserialize(buffer, EncodeConstants.OF_VERSION_1_3);
134 final MultipartReplyFlowStats reply = (MultipartReplyFlowStats) message.getMultipartReplyBody();
136 assertEquals(XID, message.getXid().intValue());
137 final FlowAndStatisticsMapList flowAndStatisticsMapList =
138 reply.nonnullFlowAndStatisticsMapList().iterator().next();
139 assertEquals(TABLE_ID, flowAndStatisticsMapList.getTableId().shortValue());
140 assertEquals(SECOND, flowAndStatisticsMapList.getDuration().getSecond().getValue().intValue());
141 assertEquals(NANOSECOND, flowAndStatisticsMapList.getDuration().getNanosecond().getValue().intValue());
142 assertEquals(PRIORITY, flowAndStatisticsMapList.getPriority().intValue());
143 assertEquals(IDLE_TIMEOUT, flowAndStatisticsMapList.getIdleTimeout().intValue());
144 assertEquals(HARD_TIMEOUT, flowAndStatisticsMapList.getHardTimeout().intValue());
145 assertTrue(flowAndStatisticsMapList.getFlags().equals(FLAGS));
146 assertEquals(COOKIE, flowAndStatisticsMapList.getCookie().getValue().longValue());
147 assertEquals(BYTE_COUNT, flowAndStatisticsMapList.getByteCount().getValue().longValue());
148 assertEquals(PACKET_COUNT, flowAndStatisticsMapList.getPacketCount().getValue().longValue());
150 assertEquals(1, flowAndStatisticsMapList.getInstructions().getInstruction().size());
152 final Instruction instruction =
153 flowAndStatisticsMapList.getInstructions().nonnullInstruction().values().iterator().next()
155 assertEquals(ApplyActionsCase.class, instruction.implementedInterface());
157 final ApplyActionsCase applyActions = (ApplyActionsCase) instruction;
158 assertEquals(1, applyActions.getApplyActions().getAction().size());
159 assertEquals(PopPbbActionCase.class, applyActions.getApplyActions().nonnullAction().values().iterator().next()
160 .getAction().implementedInterface());