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 org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
21 * Translates Hello messages
22 * @author michal.polkorab
23 * @author timotej.kubas
25 public class HelloInputMessageFactory implements OFSerializer<HelloInput>{
27 /** Code type of Hello message */
28 private static final byte MESSAGE_TYPE = 0;
29 private static final byte HELLO_ELEMENT_HEADER_SIZE = 4;
31 private static void serializeElementsList(HelloInput message, ByteBuf output) {
33 if (message.getElements() != null) {
34 for (Elements currElement : message.getElements()) {
35 int elementStartIndex = output.writerIndex();
36 output.writeShort(currElement.getType().getIntValue());
37 if (currElement.getType().equals(HelloElementType.VERSIONBITMAP)) {
38 int elementLengthIndex = output.writerIndex();
39 output.writeShort(EncodeConstants.EMPTY_LENGTH);
40 versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap());
41 for (int i = 0; i < versionBitmap.length; i++) {
42 output.writeInt(versionBitmap[i]);
44 int length = output.writerIndex() - elementStartIndex;
45 int padding = length - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;
46 ByteBufUtils.padBuffer(padding , output);
47 output.setShort(elementLengthIndex, output.writerIndex() - elementStartIndex);
54 public void serialize(HelloInput message, ByteBuf outBuffer) {
55 int startWriterIndex = outBuffer.writerIndex();
56 ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
57 serializeElementsList(message, outBuffer);
58 int endWriterIndex = outBuffer.writerIndex();
59 int paddingRemainder = (endWriterIndex - startWriterIndex) % EncodeConstants.PADDING;
60 if (paddingRemainder != 0) {
61 ByteBufUtils.padBuffer(EncodeConstants.PADDING - paddingRemainder, outBuffer);
63 ByteBufUtils.updateOFHeaderLength(outBuffer);