Bug 2756 - Match model update
[openflowjava.git] / 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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.math.BigInteger;
14
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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
24
25 /**
26  * Translates FlowRemoved messages (OpenFlow v1.0)
27  * @author michal.polkorab
28  */
29 public class OF10FlowRemovedMessageFactory implements OFDeserializer<FlowRemovedMessage>,
30         DeserializerRegistryInjector {
31
32     private static final byte PADDING_IN_FLOW_REMOVED_MESSAGE = 1;
33     private static final byte PADDING_IN_FLOW_REMOVED_MESSAGE_2 = 2;
34     private DeserializerRegistry registry;
35
36     @Override
37     public FlowRemovedMessage deserialize(ByteBuf rawMessage) {
38         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
39         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
40         builder.setXid(rawMessage.readUnsignedInt());
41         OFDeserializer<MatchV10> matchDeserializer = registry.getDeserializer(
42                 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE, MatchV10.class));
43         builder.setMatchV10(matchDeserializer.deserialize(rawMessage));
44         byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
45         rawMessage.readBytes(cookie);
46         builder.setCookie(new BigInteger(1, cookie));
47         builder.setPriority(rawMessage.readUnsignedShort());
48         builder.setReason(FlowRemovedReason.forValue(rawMessage.readUnsignedByte()));
49         rawMessage.skipBytes(PADDING_IN_FLOW_REMOVED_MESSAGE);
50         builder.setDurationSec(rawMessage.readUnsignedInt());
51         builder.setDurationNsec(rawMessage.readUnsignedInt());
52         builder.setIdleTimeout(rawMessage.readUnsignedShort());
53         rawMessage.skipBytes(PADDING_IN_FLOW_REMOVED_MESSAGE_2);
54         byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
55         rawMessage.readBytes(packetCount);
56         builder.setPacketCount(new BigInteger(1, packetCount));
57         byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
58         rawMessage.readBytes(byteCount);
59         builder.setByteCount(new BigInteger(1, byteCount));
60         return builder.build();
61     }
62
63     @Override
64     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
65         registry = deserializerRegistry;
66     }
67 }