Migrate uint/ByteBuf interactions
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10FlowRemovedMessageFactory.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
9
10 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint16;
11 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint32;
12 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint64;
13
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import io.netty.buffer.ByteBuf;
16 import java.util.Objects;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
20 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
26
27 /**
28  * Translates FlowRemoved messages (OpenFlow v1.0).
29  *
30  * @author michal.polkorab
31  */
32 public class OF10FlowRemovedMessageFactory implements OFDeserializer<FlowRemovedMessage>,
33         DeserializerRegistryInjector {
34
35     private static final byte PADDING_IN_FLOW_REMOVED_MESSAGE = 1;
36     private static final byte PADDING_IN_FLOW_REMOVED_MESSAGE_2 = 2;
37     private DeserializerRegistry registry;
38
39     @Override
40     @SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") // FB doesn't recognize Objects.requireNonNull
41     public FlowRemovedMessage deserialize(ByteBuf rawMessage) {
42         Objects.requireNonNull(registry);
43
44         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
45         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
46         builder.setXid(readUint32(rawMessage));
47         OFDeserializer<MatchV10> matchDeserializer = registry.getDeserializer(
48                 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE, MatchV10.class));
49         builder.setMatchV10(matchDeserializer.deserialize(rawMessage));
50         builder.setCookie(readUint64(rawMessage));
51         builder.setPriority(readUint16(rawMessage));
52         builder.setReason(FlowRemovedReason.forValue(rawMessage.readUnsignedByte()));
53         rawMessage.skipBytes(PADDING_IN_FLOW_REMOVED_MESSAGE);
54         builder.setDurationSec(readUint32(rawMessage));
55         builder.setDurationNsec(readUint32(rawMessage));
56         builder.setIdleTimeout(readUint16(rawMessage));
57         rawMessage.skipBytes(PADDING_IN_FLOW_REMOVED_MESSAGE_2);
58         builder.setPacketCount(readUint64(rawMessage));
59         builder.setByteCount(readUint64(rawMessage));
60         return builder.build();
61     }
62
63     @Override
64     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
65         registry = deserializerRegistry;
66     }
67 }