2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.protocol.pcep.spi;
10 import io.netty.buffer.ByteBuf;
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedBytes;
15 public final class MessageUtil {
17 private static final int VERSION_SF_LENGTH = 3;
19 private MessageUtil() {
20 throw new UnsupportedOperationException("Utility class should not be instantiated");
23 public static void formatMessage(final int messageType, final ByteBuf body, final ByteBuf out) {
24 final int msgLength = body.readableBytes();
25 final byte[] header = new byte[] {
26 UnsignedBytes.checkedCast(PCEPMessageConstants.PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)),
27 UnsignedBytes.checkedCast(messageType),
28 UnsignedBytes.checkedCast((msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH) / 256),
29 UnsignedBytes.checkedCast((msgLength + PCEPMessageConstants.COMMON_HEADER_LENGTH) % 256)
31 Preconditions.checkState(header.length == PCEPMessageConstants.COMMON_HEADER_LENGTH);
32 out.writeBytes(header);