388a6e772c609d0b8616f14c88d1d306241efeec
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / FeaturesReplyMessageFactory.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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
20
21 /**
22  * Translates FeaturesReply messages
23  * @author michal.polkorab
24  * @author timotej.kubas
25  */
26 public class FeaturesReplyMessageFactory implements OFDeserializer<GetFeaturesOutput>{
27     
28     private static final byte PADDING_IN_FEATURES_REPLY_HEADER = 2;
29     
30     private static FeaturesReplyMessageFactory instance;
31
32     private FeaturesReplyMessageFactory() {
33         // do nothing, just singleton
34     }
35     
36     /**
37      * @return singleton factory
38      */
39     public static synchronized FeaturesReplyMessageFactory getInstance() {
40         if (instance == null) {
41             instance = new FeaturesReplyMessageFactory();
42         }
43         return instance;
44     }
45     
46     @Override
47     public GetFeaturesOutput bufferToMessage(ByteBuf rawMessage, short version) {
48         GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();
49         builder.setVersion(version);
50         builder.setXid(rawMessage.readUnsignedInt());
51         byte[] datapathId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
52         rawMessage.readBytes(datapathId);
53         builder.setDatapathId(new BigInteger(1, datapathId));
54         builder.setBuffers(rawMessage.readUnsignedInt());
55         builder.setTables(rawMessage.readUnsignedByte());
56         builder.setAuxiliaryId(rawMessage.readUnsignedByte());
57         rawMessage.skipBytes(PADDING_IN_FEATURES_REPLY_HEADER);
58         builder.setCapabilities(createCapabilities(rawMessage.readUnsignedInt()));
59         builder.setReserved(rawMessage.readUnsignedInt());
60         return builder.build();
61     }
62
63     private static Capabilities createCapabilities(long input) {
64         final Boolean FLOW_STATS = (input & (1 << 0)) != 0;
65         final Boolean TABLE_STATS = (input & (1 << 1)) != 0;
66         final Boolean PORT_STATS = (input & (1 << 2)) != 0;
67         final Boolean GROUP_STATS = (input & (1 << 3)) != 0;
68         final Boolean IP_REASM = (input & (1 << 5)) != 0;
69         final Boolean QUEUE_STATS = (input & (1 << 6)) != 0;
70         final Boolean PORT_BLOCKED = (input & (1 << 8)) != 0;
71         return new Capabilities(FLOW_STATS, GROUP_STATS, IP_REASM,
72                 PORT_BLOCKED, PORT_STATS, QUEUE_STATS, TABLE_STATS);
73     }
74
75 }