3d95646bd92921f36550babb9b23c7b846f72eb2
[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 org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
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;
19
20 /**
21  * Translates Hello messages
22  * @author michal.polkorab
23  * @author timotej.kubas
24  */
25 public class HelloInputMessageFactory implements OFSerializer<HelloInput>{
26
27     /** Code type of Hello message */
28     private static final byte MESSAGE_TYPE = 0;
29     private static final byte HELLO_ELEMENT_HEADER_SIZE = 4;
30
31     private static void serializeElementsList(HelloInput message, ByteBuf output) {
32         int[] versionBitmap;
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]);
43                     }
44                     int length = output.writerIndex() - elementStartIndex;
45                     int padding = length - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;
46                     output.writeZero(padding);
47                     output.setShort(elementLengthIndex, output.writerIndex() - elementStartIndex);
48                 }
49             } 
50         }
51     }
52
53     @Override
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             outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
62         }
63         ByteBufUtils.updateOFHeaderLength(outBuffer);
64     }
65
66 }