Bug-2230: Revision of module RSVP
[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 import io.netty.buffer.ByteBuf;
12
13 public final class MessageUtil {
14
15     private static final int VERSION_SF_LENGTH = 3;
16
17     private MessageUtil() {
18         throw new UnsupportedOperationException();
19     }
20
21     public static void formatMessage(final int messageType, final ByteBuf body, final ByteBuf out) {
22         final int msgLength = body.writerIndex();
23         out.writeByte(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
24         out.writeByte(messageType);
25         out.writeShort(msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH);
26         Preconditions.checkState(out.writerIndex() == PCEPMessageConstants.COMMON_HEADER_LENGTH);
27         out.writeBytes(body);
28     }
29 }