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
9 package org.opendaylight.openflowplugin.impl.protocol.deserialization.multipart;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
23 import org.opendaylight.openflowplugin.impl.protocol.deserialization.key.MessageCodeMatchKey;
24 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.InstructionUtil;
25 import org.opendaylight.openflowplugin.impl.util.MatchUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStatsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
40 public class MultipartReplyFlowStatsDeserializer implements OFDeserializer<MultipartReplyBody>,
41 DeserializerRegistryInjector {
43 private static final MessageCodeKey MATCH_KEY = new MessageCodeMatchKey(EncodeConstants.OF13_VERSION_ID,
44 EncodeConstants.EMPTY_VALUE, Match.class,
45 MatchPath.FLOWS_STATISTICS_UPDATE_MATCH);
47 private static final byte PADDING_IN_FLOW_STATS_HEADER_01 = 1;
48 private static final byte PADDING_IN_FLOW_STATS_HEADER_02 = 4;
49 private DeserializerRegistry registry;
52 public MultipartReplyBody deserialize(ByteBuf message) {
53 final MultipartReplyFlowStatsBuilder builder = new MultipartReplyFlowStatsBuilder();
54 final List<FlowAndStatisticsMapList> items = new ArrayList<>();
56 while (message.readableBytes() > 0) {
57 final FlowAndStatisticsMapListBuilder itemBuilder = new FlowAndStatisticsMapListBuilder();
58 final int itemLength = message.readUnsignedShort();
59 final ByteBuf itemMessage = message.readSlice(itemLength - EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
61 itemBuilder.setTableId(itemMessage.readUnsignedByte());
62 itemMessage.skipBytes(PADDING_IN_FLOW_STATS_HEADER_01);
65 .setDuration(new DurationBuilder()
66 .setSecond(new Counter32(itemMessage.readUnsignedInt()))
67 .setNanosecond(new Counter32(itemMessage.readUnsignedInt()))
69 .setPriority(itemMessage.readUnsignedShort())
70 .setIdleTimeout(itemMessage.readUnsignedShort())
71 .setHardTimeout(itemMessage.readUnsignedShort())
72 .setFlags(createFlowModFlagsFromBitmap(itemMessage.readUnsignedShort()));
74 itemMessage.skipBytes(PADDING_IN_FLOW_STATS_HEADER_02);
76 final byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
77 itemMessage.readBytes(cookie);
78 final byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
79 itemMessage.readBytes(packetCount);
80 final byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
81 itemMessage.readBytes(byteCount);
84 .setCookie(new FlowCookie(new BigInteger(1, cookie)))
85 .setCookieMask(new FlowCookie(OFConstants.DEFAULT_COOKIE_MASK))
86 .setPacketCount(new Counter64(new BigInteger(1, packetCount)))
87 .setByteCount(new Counter64(new BigInteger(1, byteCount)));
89 final OFDeserializer<Match> matchDeserializer =
90 Preconditions.checkNotNull(registry).getDeserializer(MATCH_KEY);
91 itemBuilder.setMatch(MatchUtil.transformMatch(matchDeserializer.deserialize(itemMessage),
92 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match.class));
94 final int length = itemMessage.readableBytes();
97 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list
98 .Instruction> instructions = new ArrayList<>();
99 final int startIndex = itemMessage.readerIndex();
102 while (itemMessage.readerIndex() - startIndex < length) {
103 instructions.add(new InstructionBuilder()
104 .withKey(new InstructionKey(offset))
106 .setInstruction(InstructionUtil
107 .readInstruction(EncodeConstants.OF13_VERSION_ID, itemMessage, registry))
113 itemBuilder.setInstructions(new InstructionsBuilder()
114 .setInstruction(instructions)
118 items.add(itemBuilder.build());
122 .setFlowAndStatisticsMapList(items)
126 private static FlowModFlags createFlowModFlagsFromBitmap(int input) {
127 final Boolean ofp_FF_SendFlowRem = (input & 1) != 0;
128 final Boolean ofp_FF_CheckOverlap = (input & 1 << 1) != 0;
129 final Boolean ofp_FF_ResetCounts = (input & 1 << 2) != 0;
130 final Boolean ofp_FF_NoPktCounts = (input & 1 << 3) != 0;
131 final Boolean ofp_FF_NoBytCounts = (input & 1 << 4) != 0;
132 return new FlowModFlags(ofp_FF_CheckOverlap, ofp_FF_NoBytCounts, ofp_FF_NoPktCounts, ofp_FF_ResetCounts,
137 public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
138 registry = deserializerRegistry;