69d71d8d58e954ed7713ad5d4dbd5f29ef9e50c4
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / FlowRemovedMessageFactory.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.impl.deserialization.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.MatchDeserializer;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
22
23 /**
24  * Translates FlowRemoved messages
25  * @author michal.polkorab
26  * @author timotej.kubas
27  */
28 public class FlowRemovedMessageFactory implements OFDeserializer<FlowRemovedMessage> {
29     
30     private static FlowRemovedMessageFactory instance;
31     
32     private FlowRemovedMessageFactory() {
33         // singleton
34     }
35     
36     /**
37      * @return singleton factory
38      */
39     public static synchronized FlowRemovedMessageFactory getInstance(){
40         if(instance == null){
41             instance = new FlowRemovedMessageFactory();
42         }
43         return instance;
44     }
45
46     @Override
47     public FlowRemovedMessage bufferToMessage(ByteBuf rawMessage, short version) {
48         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
49         builder.setVersion(version);
50         builder.setXid(rawMessage.readUnsignedInt());
51         byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
52         rawMessage.readBytes(cookie);
53         builder.setCookie(new BigInteger(1, cookie));
54         builder.setPriority(rawMessage.readUnsignedShort());
55         builder.setReason(FlowRemovedReason.forValue(rawMessage.readUnsignedByte()));
56         builder.setTableId(new TableId((long)rawMessage.readUnsignedByte()));
57         builder.setDurationSec(rawMessage.readUnsignedInt());
58         builder.setDurationNsec(rawMessage.readUnsignedInt());
59         builder.setIdleTimeout(rawMessage.readUnsignedShort());
60         builder.setHardTimeout(rawMessage.readUnsignedShort());
61         byte[] packet_count = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
62         rawMessage.readBytes(packet_count);
63         builder.setPacketCount(new BigInteger(1, packet_count));
64         byte[] byte_count = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
65         rawMessage.readBytes(byte_count);
66         builder.setByteCount(new BigInteger(1, byte_count));
67         builder.setMatch(MatchDeserializer.createMatch(rawMessage));
68         return builder.build();
69     }
70 }