2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import java.util.List;
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;
23 * Translates Hello messages
24 * @author michal.polkorab
25 * @author timotej.kubas
27 public class HelloInputMessageFactory implements OFSerializer<HelloInput>{
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;
36 private HelloInputMessageFactory() {
37 // do nothing, just singleton
41 * @return singleton factory
43 public static synchronized HelloInputMessageFactory getInstance() {
44 if (instance == null) {
45 instance = new HelloInputMessageFactory();
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);
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;
72 length += bitmapLength;
80 public byte getMessageType() {
84 private static void encodeElementsList(HelloInput message, ByteBuf output) {
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]);
96 int padding = bitmapLength - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;
97 ByteBufUtils.padBuffer(padding , output);
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);
108 return elementlength;