BUG-612 : switched PCEP RRO subobject serializers to ByteBuf
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / MessageUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.protocol.pcep.spi;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13
14 public final class MessageUtil {
15
16     private static final int VERSION_SF_LENGTH = 3;
17
18     private MessageUtil() {
19         throw new UnsupportedOperationException("Utility class should not be instantiated");
20     }
21
22     public static void formatMessage(final int messageType, final ByteBuf body, final ByteBuf out) {
23         final int msgLength = body.writerIndex();
24         out.writeByte(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
25         out.writeByte(messageType);
26         out.writeShort(msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH);
27         Preconditions.checkState(out.writerIndex() == PCEPMessageConstants.COMMON_HEADER_LENGTH);
28         out.writeBytes(body);
29     }
30 }