3ccb2589176ca4bb412a7ac496c9b2fa533184d2
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / RoleReplyMessageFactory.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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
19
20 /**
21  * Translates RoleReply messages
22  * @author timotej.kubas
23  * @author michal.polkorab
24  */
25 public class RoleReplyMessageFactory implements OFDeserializer<RoleRequestOutput>{
26     private static RoleReplyMessageFactory instance;
27     private static final byte PADDING_IN_ROLE_REPLY_HEADER = 4;
28     
29     private RoleReplyMessageFactory() {
30         // singleton
31     }
32     
33     /**
34      * 
35      * @return singleton factory
36      */
37     public static synchronized RoleReplyMessageFactory getInstance(){
38         if(instance == null){
39             instance = new RoleReplyMessageFactory();
40         }
41         return instance;
42     }
43
44     @Override
45     public RoleRequestOutput bufferToMessage(ByteBuf rawMessage, short version) {
46         RoleRequestOutputBuilder builder = new RoleRequestOutputBuilder();
47         builder.setVersion(version);
48         builder.setXid(rawMessage.readUnsignedInt());
49         builder.setRole(ControllerRole.forValue((int) rawMessage.readUnsignedInt()));
50         rawMessage.skipBytes(PADDING_IN_ROLE_REPLY_HEADER);
51         byte[] generationID = new byte[8];
52         rawMessage.readBytes(generationID);
53         builder.setGenerationId(new BigInteger(1, generationID));
54         return builder.build();
55     }
56 }