Merge "OPNFLWPLUG-983 Group and flow removal stats are not reported in order"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / FlowModInputMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import io.netty.buffer.ByteBuf;
12 import java.math.BigInteger;
13 import java.util.List;
14 import java.util.Objects;
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.openflowjava.protocol.impl.util.CodeKeyMaker;
21 import org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory;
22 import org.opendaylight.openflowjava.protocol.impl.util.ListDeserializer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
31
32 /**
33  * Translates FlowModInput messages.
34  */
35 public class FlowModInputMessageFactory implements OFDeserializer<FlowModInput>, DeserializerRegistryInjector {
36
37     private static final byte PADDING = 2;
38     private DeserializerRegistry registry;
39
40     @Override
41     @SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") // FB doesn't recognize Objects.requireNonNull
42     public FlowModInput deserialize(ByteBuf rawMessage) {
43         Objects.requireNonNull(registry);
44
45         FlowModInputBuilder builder = new FlowModInputBuilder();
46         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
47         builder.setXid(rawMessage.readUnsignedInt());
48         byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
49         rawMessage.readBytes(cookie);
50         builder.setCookie(new BigInteger(1, cookie));
51         final byte[] cookieMask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
52         rawMessage.readBytes(cookieMask);
53         builder.setCookieMask(new BigInteger(1, cookieMask));
54         builder.setTableId(new TableId((long) rawMessage.readUnsignedByte()));
55         builder.setCommand(FlowModCommand.forValue(rawMessage.readUnsignedByte()));
56         builder.setIdleTimeout(rawMessage.readUnsignedShort());
57         builder.setHardTimeout(rawMessage.readUnsignedShort());
58         builder.setPriority(rawMessage.readUnsignedShort());
59         builder.setBufferId(rawMessage.readUnsignedInt());
60         builder.setOutPort(new PortNumber(rawMessage.readUnsignedInt()));
61         builder.setOutGroup(rawMessage.readUnsignedInt());
62         builder.setFlags(createFlowModFlagsFromBitmap(rawMessage.readUnsignedShort()));
63         rawMessage.skipBytes(PADDING);
64         OFDeserializer<Match> matchDeserializer = registry.getDeserializer(
65                 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, EncodeConstants.EMPTY_VALUE, Match.class));
66         builder.setMatch(matchDeserializer.deserialize(rawMessage));
67         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createInstructionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
68         List<Instruction> instructions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
69                 rawMessage.readableBytes(), rawMessage, keyMaker, registry);
70         builder.setInstruction(instructions);
71         return builder.build();
72     }
73
74     @SuppressWarnings("checkstyle:AbbreviationAsWordInName")
75     private static FlowModFlags createFlowModFlagsFromBitmap(int input) {
76         final Boolean _oFPFFSENDFLOWREM = (input & 1 << 0) != 0;
77         final Boolean _oFPFFCHECKOVERLAP = (input & 1 << 1) != 0;
78         final Boolean _oFPFFRESETCOUNTS = (input & 1 << 2) != 0;
79         final Boolean _oFPFFNOPKTCOUNTS = (input & 1 << 3) != 0;
80         final Boolean _oFPFFNOBYTCOUNTS = (input & 1 << 4) != 0;
81         return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS,
82                 _oFPFFSENDFLOWREM);
83     }
84
85     @Override
86     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
87         registry = deserializerRegistry;
88     }
89 }