Bug 752 - removed model ambiguities
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / HelloInputMessageFactory.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.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.List;
14
15 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
17 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
21
22 /**
23  * Translates Hello messages
24  * @author michal.polkorab
25  * @author timotej.kubas
26  */
27 public class HelloInputMessageFactory implements OFSerializer<HelloInput>{
28
29     /** Code type of Hello message */
30     private static final byte MESSAGE_TYPE = 0;
31     private static int MESSAGE_LENGTH = 8;
32     /** Size of hello element header (in bytes) */
33     public static final byte HELLO_ELEMENT_HEADER_SIZE = 4;
34     private static HelloInputMessageFactory instance;
35
36     private HelloInputMessageFactory() {
37         // do nothing, just singleton
38     }
39     
40     /**
41      * @return singleton factory
42      */
43     public static synchronized HelloInputMessageFactory getInstance() {
44         if (instance == null) {
45             instance = new HelloInputMessageFactory();
46         }
47         return instance;
48     }
49
50     @Override
51     public void messageToBuffer(short version, ByteBuf out, HelloInput message) {
52         int startWriterIndex = out.writerIndex();
53         ByteBufUtils.writeOFHeader(instance, message, out);
54         encodeElementsList(message, out);
55         int endWriterIndex = out.writerIndex();
56         int writtenBytesDiff = computeLength(message) - (endWriterIndex - startWriterIndex);
57         ByteBufUtils.padBuffer(writtenBytesDiff, out);
58     }
59
60     @Override
61     public int computeLength(HelloInput message) {
62         int length = MESSAGE_LENGTH;
63         List<Elements> elements = message.getElements();
64         if (elements != null) {
65             for (Elements element : elements) {
66                 if (HelloElementType.VERSIONBITMAP.equals(element.getType())) {
67                     int bitmapLength = computeVersionBitmapLength(element);
68                     int paddingRemainder = bitmapLength % EncodeConstants.PADDING;
69                     if (paddingRemainder != 0) {
70                         bitmapLength += EncodeConstants.PADDING - paddingRemainder;
71                     }
72                     length += bitmapLength;
73                 }
74             }
75         }
76         return length;
77     }
78
79     @Override
80     public byte getMessageType() {
81         return MESSAGE_TYPE;
82     }
83     
84     private static void encodeElementsList(HelloInput message, ByteBuf output) {
85         int[] versionBitmap;
86         if (message.getElements() != null) {
87             for (Elements currElement : message.getElements()) {
88                 output.writeShort(currElement.getType().getIntValue());
89                 if (currElement.getType().equals(HelloElementType.VERSIONBITMAP)) {
90                     short bitmapLength = computeVersionBitmapLength(currElement);
91                     output.writeShort(bitmapLength);
92                     versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap());
93                     for (int i = 0; i < versionBitmap.length; i++) {
94                         output.writeInt(versionBitmap[i]);
95                     }
96                     int padding = bitmapLength - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;
97                     ByteBufUtils.padBuffer(padding , output);
98                 }
99             } 
100         }
101     }
102     
103     private static short computeVersionBitmapLength(Elements element) {
104         short elementlength = HELLO_ELEMENT_HEADER_SIZE;
105         if (!element.getVersionBitmap().isEmpty()) {
106             elementlength += ((element.getVersionBitmap().size() - 1) / Integer.SIZE + 1) * (EncodeConstants.SIZE_OF_INT_IN_BYTES);
107         }
108         return elementlength;
109     }
110 }