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 java.util.HashMap;
11 import java.util.List;
13 import org.opendaylight.protocol.framework.DeserializerException;
14 import org.opendaylight.protocol.framework.DocumentedException;
15 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
16 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
18 import org.opendaylight.protocol.pcep.PCEPErrors;
19 import org.opendaylight.protocol.pcep.PCEPMessage;
20 import org.opendaylight.protocol.pcep.impl.message.PCCreateMessageParser;
21 import org.opendaylight.protocol.pcep.impl.message.PCEPCloseMessageParser;
22 import org.opendaylight.protocol.pcep.impl.message.PCEPErrorMessageParser;
23 import org.opendaylight.protocol.pcep.impl.message.PCEPKeepAliveMessageParser;
24 import org.opendaylight.protocol.pcep.impl.message.PCEPNotificationMessageParser;
25 import org.opendaylight.protocol.pcep.impl.message.PCEPOpenMessageParser;
26 import org.opendaylight.protocol.pcep.impl.message.PCEPRawMessage;
27 import org.opendaylight.protocol.pcep.impl.message.PCEPReplyMessageParser;
28 import org.opendaylight.protocol.pcep.impl.message.PCEPReportMessageParser;
29 import org.opendaylight.protocol.pcep.impl.message.PCEPRequestMessageParser;
30 import org.opendaylight.protocol.pcep.impl.message.PCEPUpdateRequestMessageParser;
31 import org.opendaylight.protocol.pcep.message.PCCreateMessage;
32 import org.opendaylight.protocol.pcep.message.PCEPCloseMessage;
33 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
34 import org.opendaylight.protocol.pcep.message.PCEPKeepAliveMessage;
35 import org.opendaylight.protocol.pcep.message.PCEPNotificationMessage;
36 import org.opendaylight.protocol.pcep.message.PCEPOpenMessage;
37 import org.opendaylight.protocol.pcep.message.PCEPReplyMessage;
38 import org.opendaylight.protocol.pcep.message.PCEPReportMessage;
39 import org.opendaylight.protocol.pcep.message.PCEPRequestMessage;
40 import org.opendaylight.protocol.pcep.message.PCEPUpdateRequestMessage;
41 import org.opendaylight.protocol.util.ByteArray;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.collect.Lists;
46 import com.google.common.primitives.UnsignedBytes;
49 * Factory for subclasses of {@link org.opendaylight.protocol.pcep.PCEPMessage PCEPMessage}
51 class RawPCEPMessageFactory implements ProtocolMessageFactory<PCEPMessage> {
53 private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
55 private final static int TYPE_SIZE = 1; // bytes
57 private final static int LENGTH_SIZE = 2; // bytes
59 public final static int COMMON_HEADER_LENGTH = 4; // bytes
61 private static class MapOfParsers extends HashMap<PCEPMessageType, PCEPMessageParser> {
63 private static final long serialVersionUID = -5715193806554448822L;
65 private final static MapOfParsers instance = new MapOfParsers();
67 private MapOfParsers() {
71 private void fillInMap() {
72 this.put(PCEPMessageType.OPEN, new PCEPOpenMessageParser());
73 this.put(PCEPMessageType.KEEPALIVE, new PCEPKeepAliveMessageParser());
74 this.put(PCEPMessageType.NOTIFICATION, new PCEPNotificationMessageParser());
75 this.put(PCEPMessageType.ERROR, new PCEPErrorMessageParser());
76 this.put(PCEPMessageType.RESPONSE, new PCEPReplyMessageParser());
77 this.put(PCEPMessageType.REQUEST, new PCEPRequestMessageParser());
78 this.put(PCEPMessageType.UPDATE_REQUEST, new PCEPUpdateRequestMessageParser());
79 this.put(PCEPMessageType.STATUS_REPORT, new PCEPReportMessageParser());
80 this.put(PCEPMessageType.CLOSE, new PCEPCloseMessageParser());
81 this.put(PCEPMessageType.PCCREATE, new PCCreateMessageParser());
84 public static MapOfParsers getInstance() {
91 * @param bytes assume array of bytes without common header
92 * @return Parsed specific PCEPMessage
93 * @throws PCEPDeserializerException
94 * @throws PCEPDocumentedException
98 public List<PCEPMessage> parse(final byte[] bytes) throws DeserializerException, DocumentedException {
99 if (bytes == null || bytes.length == 0) {
100 throw new IllegalArgumentException("Array of bytes is mandatory.");
103 logger.trace("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(bytes));
105 final int type = UnsignedBytes.toInt(bytes[1]);
107 final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE + 1, LENGTH_SIZE));
109 final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + 1 + LENGTH_SIZE);
111 if (msgBody.length != msgLength - COMMON_HEADER_LENGTH) {
112 throw new DeserializerException("Size don't match size specified in header. Passed: " + msgBody.length + "; Expected: "
113 + (msgLength - COMMON_HEADER_LENGTH) + ". " + msgLength);
117 * if PCEPObjectIdentifier.getObjectClassFromInt() dont't throws
118 * exception and if returned null we know the error type
120 PCEPMessageType msgType;
122 msgType = PCEPMessageType.getFromInt(type);
123 } catch (final PCEPDeserializerException e) {
124 throw new DeserializerException(e.getMessage(), e);
126 if (msgType == null) {
127 throw new DocumentedException("Unhandled message type " + type, new PCEPDocumentedException("Unhandled message type " + type, PCEPErrors.CAPABILITY_NOT_SUPPORTED));
132 msg = new PCEPRawMessage(PCEPObjectFactory.parseObjects(msgBody), msgType);
133 } catch (final PCEPDeserializerException e) {
134 throw new DeserializerException(e.getMessage(), e);
135 } catch (final PCEPDocumentedException e) {
136 throw new DocumentedException(e.getMessage(), e);
138 logger.debug("Message was parsed. {}", msg);
139 return Lists.newArrayList(msg);
143 public byte[] put(final PCEPMessage msg) {
145 throw new IllegalArgumentException("PCEPMessage is mandatory.");
148 final PCEPMessageType msgType;
150 if (msg instanceof PCEPOpenMessage) {
151 msgType = PCEPMessageType.OPEN;
152 } else if (msg instanceof PCEPKeepAliveMessage) {
153 msgType = PCEPMessageType.KEEPALIVE;
154 } else if (msg instanceof PCEPCloseMessage) {
155 msgType = PCEPMessageType.CLOSE;
156 } else if (msg instanceof PCEPReplyMessage) {
157 msgType = PCEPMessageType.RESPONSE;
158 } else if (msg instanceof PCEPRequestMessage) {
159 msgType = PCEPMessageType.REQUEST;
160 } else if (msg instanceof PCEPNotificationMessage) {
161 msgType = PCEPMessageType.NOTIFICATION;
162 } else if (msg instanceof PCEPErrorMessage) {
163 msgType = PCEPMessageType.ERROR;
164 } else if (msg instanceof PCEPReportMessage) {
165 msgType = PCEPMessageType.STATUS_REPORT;
166 } else if (msg instanceof PCEPUpdateRequestMessage) {
167 msgType = PCEPMessageType.UPDATE_REQUEST;
168 } else if (msg instanceof PCCreateMessage) {
169 msgType = PCEPMessageType.PCCREATE;
171 logger.error("Unknown instance of PCEPMessage. Message class: {}", msg.getClass());
172 throw new IllegalArgumentException("Unknown instance of PCEPMessage. Passed " + msg.getClass());
175 logger.trace("Serializing {}", msgType);
177 final byte[] msgBody = MapOfParsers.getInstance().get(msgType).put(msg);
179 final PCEPMessageHeader msgHeader = new PCEPMessageHeader(msgType.getIdentifier(), msgBody.length
180 + PCEPMessageHeader.COMMON_HEADER_LENGTH, PCEPMessage.PCEP_VERSION);
182 final byte[] headerBytes = msgHeader.toBytes();
183 final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
185 ByteArray.copyWhole(headerBytes, retBytes, 0);
186 ByteArray.copyWhole(msgBody, retBytes, PCEPMessageHeader.COMMON_HEADER_LENGTH);