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 io.netty.buffer.ByteBuf;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowplugin.api.OFConstants;
21 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
22 import org.opendaylight.openflowplugin.impl.protocol.deserialization.key.MessageCodeMatchKey;
23 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.InstructionUtil;
24 import org.opendaylight.openflowplugin.impl.util.MatchUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStatsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
39 public class MultipartReplyFlowStatsDeserializer implements OFDeserializer<MultipartReplyBody>,
40 DeserializerRegistryInjector {
42 private static final MessageCodeKey MATCH_KEY = new MessageCodeMatchKey(EncodeConstants.OF13_VERSION_ID,
43 EncodeConstants.EMPTY_VALUE, Match.class,
44 MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
46 private static final byte PADDING_IN_FLOW_STATS_HEADER_01 = 1;
47 private static final byte PADDING_IN_FLOW_STATS_HEADER_02 = 4;
48 private DeserializerRegistry registry;
51 public MultipartReplyBody deserialize(ByteBuf message) {
52 final MultipartReplyFlowStatsBuilder builder = new MultipartReplyFlowStatsBuilder();
53 final List<FlowAndStatisticsMapList> items = new ArrayList<>();
55 while (message.readableBytes() > 0) {
56 final FlowAndStatisticsMapListBuilder itemBuilder = new FlowAndStatisticsMapListBuilder();
57 final int itemLength = message.readUnsignedShort();
58 final ByteBuf itemMessage = message.readSlice(itemLength - EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
60 itemBuilder.setTableId(itemMessage.readUnsignedByte());
61 itemMessage.skipBytes(PADDING_IN_FLOW_STATS_HEADER_01);
64 .setDuration(new DurationBuilder()
65 .setSecond(new Counter32(itemMessage.readUnsignedInt()))
66 .setNanosecond(new Counter32(itemMessage.readUnsignedInt()))
68 .setPriority(itemMessage.readUnsignedShort())
69 .setIdleTimeout(itemMessage.readUnsignedShort())
70 .setHardTimeout(itemMessage.readUnsignedShort())
71 .setFlags(createFlowModFlagsFromBitmap(itemMessage.readUnsignedShort()));
73 itemMessage.skipBytes(PADDING_IN_FLOW_STATS_HEADER_02);
75 final byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
76 itemMessage.readBytes(cookie);
77 final byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
78 itemMessage.readBytes(packetCount);
79 final byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
80 itemMessage.readBytes(byteCount);
83 .setCookie(new FlowCookie(new BigInteger(1, cookie)))
84 .setCookieMask(new FlowCookie(OFConstants.DEFAULT_COOKIE_MASK))
85 .setPacketCount(new Counter64(new BigInteger(1, packetCount)))
86 .setByteCount(new Counter64(new BigInteger(1, byteCount)));
88 final OFDeserializer<Match> matchDeserializer = registry.getDeserializer(MATCH_KEY);
89 itemBuilder.setMatch(MatchUtil.transformMatch(matchDeserializer.deserialize(itemMessage),
90 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match.class));
92 final int length = itemMessage.readableBytes();
95 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list
96 .Instruction> instructions = new ArrayList<>();
97 final int startIndex = itemMessage.readerIndex();
100 while ((itemMessage.readerIndex() - startIndex) < length) {
101 instructions.add(new InstructionBuilder()
102 .setKey(new InstructionKey(offset))
104 .setInstruction(InstructionUtil
105 .readInstruction(EncodeConstants.OF13_VERSION_ID, itemMessage, registry))
111 itemBuilder.setInstructions(new InstructionsBuilder()
112 .setInstruction(instructions)
116 items.add(itemBuilder.build());
120 .setFlowAndStatisticsMapList(items)
124 private static FlowModFlags createFlowModFlagsFromBitmap(int input) {
125 final Boolean ofp_FF_SendFlowRem = (input & (1)) > 0;
126 final Boolean ofp_FF_CheckOverlap = (input & (1 << 1)) > 0;
127 final Boolean ofp_FF_ResetCounts = (input & (1 << 2)) > 0;
128 final Boolean ofp_FF_NoPktCounts = (input & (1 << 3)) > 0;
129 final Boolean ofp_FF_NoBytCounts = (input & (1 << 4)) > 0;
130 return new FlowModFlags(ofp_FF_CheckOverlap, ofp_FF_NoBytCounts, ofp_FF_NoPktCounts, ofp_FF_ResetCounts,
135 public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
136 registry = deserializerRegistry;