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.ArrayList;
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.spi.MessageHandlerRegistry;
21 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.base.Preconditions;
28 import com.google.common.primitives.UnsignedBytes;
31 * A PCEP message parser which also does validation.
33 public final class PCEPMessageFactory implements ProtocolMessageFactory<Message> {
35 private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
37 private final static int TYPE_SIZE = 1; // bytes
39 private final static int LENGTH_SIZE = 2; // bytes
41 public final static int COMMON_HEADER_LENGTH = 4; // bytes
44 * Current supported version of PCEP.
46 public static final int PCEP_VERSION = 1;
48 private static final int VERSION_SF_LENGTH = 3;
50 private static final int VER_FLAGS_MF_LENGTH = 1;
51 private static final int TYPE_F_LENGTH = 1;
52 private static final int LENGTH_F_LENGTH = 2;
54 private static final int VER_FLAGS_MF_OFFSET = 0;
55 private static final int TYPE_F_OFFSET = VER_FLAGS_MF_LENGTH + VER_FLAGS_MF_OFFSET;
56 private static final int LENGTH_F_OFFSET = TYPE_F_LENGTH + TYPE_F_OFFSET;
58 private final MessageHandlerRegistry registry;
60 public PCEPMessageFactory(final MessageHandlerRegistry registry) {
61 this.registry = Preconditions.checkNotNull(registry);
65 public Message parse(final byte[] bytes) throws DeserializerException, DocumentedException {
66 Preconditions.checkArgument(bytes != null, "Bytes may not be null");
67 Preconditions.checkArgument(bytes.length != 0, "Bytes may not be empty");
69 logger.trace("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(bytes));
71 final int type = UnsignedBytes.toInt(bytes[1]);
73 final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE + 1, LENGTH_SIZE));
75 final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + 1 + LENGTH_SIZE);
77 if (msgBody.length != msgLength - COMMON_HEADER_LENGTH) {
78 throw new DeserializerException("Body size " + msgBody.length + " does not match header size "
79 + (msgLength - COMMON_HEADER_LENGTH));
82 final List<Message> errors = new ArrayList<>();
86 msg = this.registry.getMessageParser(type).parseMessage(msgBody, errors);
87 } catch (final PCEPDeserializerException e) {
88 logger.debug("Unexpected deserializer problem", e);
89 throw new DeserializerException(e.getMessage(), e);
92 if (!errors.isEmpty()) {
93 // FIXME: we have a bunch of error messages, how can we send them back?
96 logger.debug("Message was parsed. {}", msg);
101 public byte[] put(final Message msg) {
103 throw new IllegalArgumentException("PCEPMessage is mandatory.");
106 final ByteBuf buf = new UnpooledByteBufAllocator(false).buffer();
108 final MessageSerializer serializer = this.registry.getMessageSerializer(msg);
110 serializer.serializeMessage(msg, buf);
112 final byte[] msgBody = new byte[buf.readableBytes()];
114 buf.getBytes(0, msgBody);
116 final byte[] headerBytes = new byte[COMMON_HEADER_LENGTH];
119 headerBytes[VER_FLAGS_MF_OFFSET] = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
122 headerBytes[TYPE_F_OFFSET] = (byte) serializer.getMessageType();
125 System.arraycopy(ByteArray.intToBytes(msgBody.length + COMMON_HEADER_LENGTH), Integer.SIZE / Byte.SIZE - LENGTH_F_LENGTH,
126 headerBytes, LENGTH_F_OFFSET, LENGTH_F_LENGTH);
128 final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
130 ByteArray.copyWhole(headerBytes, retBytes, 0);
131 ByteArray.copyWhole(msgBody, retBytes, COMMON_HEADER_LENGTH);