Merge "Fix checkstyle warnings for impl/protocol package"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyFlowStatsDeserializer.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 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;
38
39 public class MultipartReplyFlowStatsDeserializer implements OFDeserializer<MultipartReplyBody>,
40         DeserializerRegistryInjector {
41
42     private static final MessageCodeKey MATCH_KEY = new MessageCodeMatchKey(EncodeConstants.OF13_VERSION_ID,
43             EncodeConstants.EMPTY_VALUE, Match.class,
44             MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
45
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;
49
50     @Override
51     public MultipartReplyBody deserialize(ByteBuf message) {
52         final MultipartReplyFlowStatsBuilder builder = new MultipartReplyFlowStatsBuilder();
53         final List<FlowAndStatisticsMapList> items = new ArrayList<>();
54
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);
59
60             itemBuilder.setTableId(itemMessage.readUnsignedByte());
61             itemMessage.skipBytes(PADDING_IN_FLOW_STATS_HEADER_01);
62
63             itemBuilder
64                     .setDuration(new DurationBuilder()
65                             .setSecond(new Counter32(itemMessage.readUnsignedInt()))
66                             .setNanosecond(new Counter32(itemMessage.readUnsignedInt()))
67                             .build())
68                     .setPriority(itemMessage.readUnsignedShort())
69                     .setIdleTimeout(itemMessage.readUnsignedShort())
70                     .setHardTimeout(itemMessage.readUnsignedShort())
71                     .setFlags(createFlowModFlagsFromBitmap(itemMessage.readUnsignedShort()));
72
73             itemMessage.skipBytes(PADDING_IN_FLOW_STATS_HEADER_02);
74
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);
81
82             itemBuilder
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)));
87
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));
91
92             final int length = itemMessage.readableBytes();
93
94             if (length > 0) {
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();
98                 int offset = 0;
99
100                 while ((itemMessage.readerIndex() - startIndex) < length) {
101                     instructions.add(new InstructionBuilder()
102                             .setKey(new InstructionKey(offset))
103                             .setOrder(offset)
104                             .setInstruction(InstructionUtil
105                                     .readInstruction(EncodeConstants.OF13_VERSION_ID, itemMessage, registry))
106                             .build());
107
108                     offset++;
109                 }
110
111                 itemBuilder.setInstructions(new InstructionsBuilder()
112                         .setInstruction(instructions)
113                         .build());
114             }
115
116             items.add(itemBuilder.build());
117         }
118
119         return builder
120                 .setFlowAndStatisticsMapList(items)
121                 .build();
122     }
123
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,
131                 ofp_FF_SendFlowRem);
132     }
133
134     @Override
135     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
136         registry = deserializerRegistry;
137     }
138
139 }