2 * Copyright (c) 2013 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.impl;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.util.Arrays;
14 import java.util.List;
16 import org.opendaylight.protocol.framework.DeserializerException;
17 import org.opendaylight.protocol.framework.DocumentedException;
18 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
19 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
21 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
22 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Preconditions;
29 import com.google.common.primitives.UnsignedBytes;
32 * A PCEP message parser which also does validation.
34 public final class PCEPMessageFactory implements ProtocolMessageFactory<Message> {
36 private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
38 private final static int TYPE_SIZE = 1; // bytes
40 private final static int LENGTH_SIZE = 2; // bytes
42 public final static int COMMON_HEADER_LENGTH = 4; // bytes
45 * Current supported version of PCEP.
47 public static final int PCEP_VERSION = 1;
49 private static final int VERSION_SF_LENGTH = 3;
51 private final HandlerRegistry registry;
53 private static final int VER_FLAGS_MF_LENGTH = 1;
54 private static final int TYPE_F_LENGTH = 1;
55 private static final int LENGTH_F_LENGTH = 2;
57 private static final int VER_FLAGS_MF_OFFSET = 0;
58 private static final int TYPE_F_OFFSET = VER_FLAGS_MF_LENGTH + VER_FLAGS_MF_OFFSET;
59 private static final int LENGTH_F_OFFSET = TYPE_F_LENGTH + TYPE_F_OFFSET;
61 public PCEPMessageFactory() {
62 this.registry = HandlerRegistryImpl.INSTANCE;
66 public List<Message> parse(final byte[] bytes) throws DeserializerException, DocumentedException {
67 Preconditions.checkArgument(bytes != null, "Bytes may not be null");
68 Preconditions.checkArgument(bytes.length != 0, "Bytes may not be empty");
70 logger.trace("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(bytes));
72 final int type = UnsignedBytes.toInt(bytes[1]);
74 final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE + 1, LENGTH_SIZE));
76 final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + 1 + LENGTH_SIZE);
78 if (msgBody.length != msgLength - COMMON_HEADER_LENGTH) {
79 throw new DeserializerException("Body size " + msgBody.length + " does not match header size "
80 + (msgLength - COMMON_HEADER_LENGTH));
86 msg = this.registry.getMessageParser(type).parseMessage(msgBody);
87 } catch (final PCEPDeserializerException e) {
88 logger.debug("Unexpected deserializer problem", e);
89 throw new DeserializerException(e.getMessage(), e);
90 } catch (final PCEPDocumentedException e) {
91 logger.debug("Documented deserializer problem", e);
92 throw new DocumentedException(e.getMessage(), e);
94 logger.debug("Message was parsed. {}", msg);
95 return Arrays.asList(msg);
99 public byte[] put(final Message msg) {
101 throw new IllegalArgumentException("PCEPMessage is mandatory.");
104 final ByteBuf buf = new UnpooledByteBufAllocator(false).buffer();
106 final MessageSerializer serializer = this.registry.getMessageSerializer(msg);
108 serializer.serializeMessage(msg, buf);
110 final byte[] msgBody = buf.array();
112 final byte[] headerBytes = new byte[COMMON_HEADER_LENGTH];
115 headerBytes[VER_FLAGS_MF_OFFSET] = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
118 headerBytes[TYPE_F_OFFSET] = (byte) serializer.getMessageType();
121 System.arraycopy(ByteArray.intToBytes(msgBody.length), Integer.SIZE / Byte.SIZE - LENGTH_F_LENGTH, headerBytes, LENGTH_F_OFFSET,
124 final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
126 ByteArray.copyWhole(headerBytes, retBytes, 0);
127 ByteArray.copyWhole(msgBody, retBytes, PCEPMessageHeader.COMMON_HEADER_LENGTH);