import java.util.HashMap;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import org.opendaylight.protocol.framework.DeserializerException;
import org.opendaylight.protocol.framework.DocumentedException;
import org.opendaylight.protocol.framework.ProtocolMessage;
import org.opendaylight.protocol.framework.ProtocolMessageFactory;
-import org.opendaylight.protocol.framework.ProtocolMessageHeader;
-import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.protocol.pcep.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.PCEPDocumentedException;
import org.opendaylight.protocol.pcep.PCEPErrors;
import org.opendaylight.protocol.pcep.message.PCEPUpdateRequestMessage;
import org.opendaylight.protocol.pcep.message.PCEPXRAddTunnelMessage;
import org.opendaylight.protocol.pcep.message.PCEPXRDeleteTunnelMessage;
+import org.opendaylight.protocol.util.ByteArray;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.primitives.UnsignedBytes;
/**
* Factory for subclasses of {@link org.opendaylight.protocol.pcep.PCEPMessage PCEPMessage}
*/
public class PCEPMessageFactory implements ProtocolMessageFactory {
- private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
+ private final static Logger logger = LoggerFactory.getLogger(PCEPMessageFactory.class);
+
+ private final static int TYPE_SIZE = 1; // bytes
- /**
- * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPMessage PCEPMessage}
- */
- public enum PCEPMessageType {
+ private final static int LENGTH_SIZE = 2; // bytes
+
+ public final static int COMMON_HEADER_LENGTH = 4; // bytes
+
+ /**
+ * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPMessage PCEPMessage}
+ */
+ public enum PCEPMessageType {
OPEN(1), NOTIFICATION(5), KEEPALIVE(2), RESPONSE(4), REQUEST(3), ERROR(6), CLOSE(7), UPDATE_REQUEST(11), STATUS_REPORT(10),
// TODO: replace with actual values by IANA
- XR_ADD_TUNNEL(8),
- XR_DELETE_TUNNEL(9),
- PCCREATE(12);
+ XR_ADD_TUNNEL(8), XR_DELETE_TUNNEL(9), PCCREATE(12);
private final int identifier;
PCEPMessageType(final int identifier) {
- this.identifier = identifier;
+ this.identifier = identifier;
}
public int getIdentifier() {
- return this.identifier;
+ return this.identifier;
}
public static PCEPMessageType getFromInt(final int type) throws PCEPDeserializerException {
- for (final PCEPMessageType type_e : PCEPMessageType.values()) {
- if (type_e.getIdentifier() == type)
- return type_e;
- }
+ for (final PCEPMessageType type_e : PCEPMessageType.values()) {
+ if (type_e.getIdentifier() == type)
+ return type_e;
+ }
- throw new PCEPDeserializerException("Unknown PCEPMessage Class identifier. Passed: " + type + "; Known: " + PCEPMessageType.values() + ".");
+ throw new PCEPDeserializerException("Unknown PCEPMessage Class identifier. Passed: " + type + "; Known: "
+ + PCEPMessageType.values() + ".");
}
}
- private static class MapOfParsers extends HashMap<PCEPMessageType, PCEPMessageParser> {
+ private static class MapOfParsers extends HashMap<PCEPMessageType, PCEPMessageParser> {
- private static final long serialVersionUID = -5715193806554448822L;
+ private static final long serialVersionUID = -5715193806554448822L;
- private final static MapOfParsers instance = new MapOfParsers();
+ private final static MapOfParsers instance = new MapOfParsers();
- private MapOfParsers() {
- this.fillInMap();
- }
+ private MapOfParsers() {
+ this.fillInMap();
+ }
- private void fillInMap() {
- this.put(PCEPMessageType.OPEN, new PCEPOpenMessageParser());
- this.put(PCEPMessageType.KEEPALIVE, new PCEPKeepAliveMessageParser());
- this.put(PCEPMessageType.NOTIFICATION, new PCEPNotificationMessageParser());
- this.put(PCEPMessageType.ERROR, new PCEPErrorMessageParser());
- this.put(PCEPMessageType.RESPONSE, new PCEPReplyMessageParser());
- this.put(PCEPMessageType.REQUEST, new PCEPRequestMessageParser());
- this.put(PCEPMessageType.UPDATE_REQUEST, new PCEPUpdateRequestMessageParser());
- this.put(PCEPMessageType.STATUS_REPORT, new PCEPReportMessageParser());
- this.put(PCEPMessageType.CLOSE, new PCEPCloseMessageParser());
- this.put(PCEPMessageType.XR_ADD_TUNNEL, new PCEPXRAddTunnelMessageParser());
- this.put(PCEPMessageType.XR_DELETE_TUNNEL, new PCEPXRDeleteTunnelMessageParser());
- this.put(PCEPMessageType.PCCREATE, new PCCreateMessageParser());
- }
+ private void fillInMap() {
+ this.put(PCEPMessageType.OPEN, new PCEPOpenMessageParser());
+ this.put(PCEPMessageType.KEEPALIVE, new PCEPKeepAliveMessageParser());
+ this.put(PCEPMessageType.NOTIFICATION, new PCEPNotificationMessageParser());
+ this.put(PCEPMessageType.ERROR, new PCEPErrorMessageParser());
+ this.put(PCEPMessageType.RESPONSE, new PCEPReplyMessageParser());
+ this.put(PCEPMessageType.REQUEST, new PCEPRequestMessageParser());
+ this.put(PCEPMessageType.UPDATE_REQUEST, new PCEPUpdateRequestMessageParser());
+ this.put(PCEPMessageType.STATUS_REPORT, new PCEPReportMessageParser());
+ this.put(PCEPMessageType.CLOSE, new PCEPCloseMessageParser());
+ this.put(PCEPMessageType.XR_ADD_TUNNEL, new PCEPXRAddTunnelMessageParser());
+ this.put(PCEPMessageType.XR_DELETE_TUNNEL, new PCEPXRDeleteTunnelMessageParser());
+ this.put(PCEPMessageType.PCCREATE, new PCCreateMessageParser());
+ }
- public static MapOfParsers getInstance() {
- return instance;
+ public static MapOfParsers getInstance() {
+ return instance;
}
- }
-
- /**
- *
- * @param bytes
- * assume array of bytes without common header
- * @param msgHeader
- * @return Parsed specific PCEPMessage
- * @throws PCEPDeserializerException
- * @throws PCEPDocumentedException
- */
-
- @Override
- public ProtocolMessage parse(final byte[] bytes, final ProtocolMessageHeader msgH) throws DeserializerException, DocumentedException {
- final PCEPMessageHeader msgHeader = (PCEPMessageHeader) msgH;
+ }
+
+ /**
+ *
+ * @param bytes assume array of bytes without common header
+ * @param msgHeader
+ * @return Parsed specific PCEPMessage
+ * @throws PCEPDeserializerException
+ * @throws PCEPDocumentedException
+ */
+
+ @Override
+ public ProtocolMessage parse(final byte[] bytes) throws DeserializerException, DocumentedException {
if (bytes == null)
- throw new IllegalArgumentException("Array of bytes is mandatory.");
- if (msgHeader == null)
- throw new IllegalArgumentException("PCEPMessageHeader is mandatory.");
+ throw new IllegalArgumentException("Array of bytes is mandatory.");
logger.trace("Attempt to parse message from bytes: {}", ByteArray.bytesToHexString(bytes));
- if (bytes.length != (msgHeader.getLength() - PCEPMessageHeader.COMMON_HEADER_LENGTH))
- throw new DeserializerException("Size don't match size specified in header. Passed: " + bytes.length + "; Expected: "
- + (msgHeader.getLength() - PCEPMessageHeader.COMMON_HEADER_LENGTH) + ". " + msgHeader.getLength());
+ final int type = UnsignedBytes.toInt(bytes[0]);
+
+ final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE, LENGTH_SIZE));
+
+ final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + LENGTH_SIZE);
+
+ if (msgBody.length != (msgLength - COMMON_HEADER_LENGTH))
+ throw new DeserializerException("Size don't match size specified in header. Passed: " + msgBody.length + "; Expected: "
+ + (msgLength - COMMON_HEADER_LENGTH) + ". " + msgLength);
/*
* if PCEPObjectIdentifier.getObjectClassFromInt() dont't throws
*/
PCEPMessageType msgType;
try {
- msgType = PCEPMessageType.getFromInt(msgHeader.getType());
+ msgType = PCEPMessageType.getFromInt(type);
} catch (final PCEPDeserializerException e) {
throw new DeserializerException(e.getMessage(), e);
}
if (msgType == null)
- throw new DocumentedException("Unhandled message type " + msgHeader.getType(), new PCEPDocumentedException("Unhandled message type " + msgHeader.getType(), PCEPErrors.CAPABILITY_NOT_SUPPORTED));
+ throw new DocumentedException("Unhandled message type " + type, new PCEPDocumentedException("Unhandled message type " + type, PCEPErrors.CAPABILITY_NOT_SUPPORTED));
PCEPMessage msg;
try {
- msg = new PCEPRawMessage(PCEPObjectFactory.parseObjects(bytes), msgType);
+ msg = new PCEPRawMessage(PCEPObjectFactory.parseObjects(msgBody), msgType);
} catch (final PCEPDeserializerException e) {
throw new DeserializerException(e.getMessage(), e);
} catch (final PCEPDocumentedException e) {
}
logger.debug("Message was parsed. {}", msg);
return msg;
- }
+ }
- @Override
+ @Override
public byte[] put(final ProtocolMessage msg) {
- final PCEPMessage pcepMsg = (PCEPMessage) msg;
+ final PCEPMessage pcepMsg = (PCEPMessage) msg;
if (pcepMsg == null)
- throw new IllegalArgumentException("PCEPMessage is mandatory.");
+ throw new IllegalArgumentException("PCEPMessage is mandatory.");
final PCEPMessageType msgType;
if (pcepMsg instanceof PCEPOpenMessage) {
- msgType = PCEPMessageType.OPEN;
+ msgType = PCEPMessageType.OPEN;
} else if (pcepMsg instanceof PCEPKeepAliveMessage) {
- msgType = PCEPMessageType.KEEPALIVE;
+ msgType = PCEPMessageType.KEEPALIVE;
} else if (pcepMsg instanceof PCEPCloseMessage) {
- msgType = PCEPMessageType.CLOSE;
+ msgType = PCEPMessageType.CLOSE;
} else if (pcepMsg instanceof PCEPReplyMessage) {
- msgType = PCEPMessageType.RESPONSE;
+ msgType = PCEPMessageType.RESPONSE;
} else if (pcepMsg instanceof PCEPRequestMessage) {
- msgType = PCEPMessageType.REQUEST;
+ msgType = PCEPMessageType.REQUEST;
} else if (pcepMsg instanceof PCEPNotificationMessage) {
- msgType = PCEPMessageType.NOTIFICATION;
+ msgType = PCEPMessageType.NOTIFICATION;
} else if (pcepMsg instanceof PCEPErrorMessage) {
- msgType = PCEPMessageType.ERROR;
+ msgType = PCEPMessageType.ERROR;
} else if (pcepMsg instanceof PCEPReportMessage) {
- msgType = PCEPMessageType.STATUS_REPORT;
+ msgType = PCEPMessageType.STATUS_REPORT;
} else if (pcepMsg instanceof PCEPUpdateRequestMessage) {
- msgType = PCEPMessageType.UPDATE_REQUEST;
+ msgType = PCEPMessageType.UPDATE_REQUEST;
} else if (pcepMsg instanceof PCEPXRAddTunnelMessage) {
- msgType = PCEPMessageType.XR_ADD_TUNNEL;
+ msgType = PCEPMessageType.XR_ADD_TUNNEL;
} else if (pcepMsg instanceof PCEPXRDeleteTunnelMessage) {
- msgType = PCEPMessageType.XR_DELETE_TUNNEL;
+ msgType = PCEPMessageType.XR_DELETE_TUNNEL;
} else if (pcepMsg instanceof PCCreateMessage) {
- msgType = PCEPMessageType.PCCREATE;
+ msgType = PCEPMessageType.PCCREATE;
} else {
- logger.error("Unknown instance of PCEPMessage. Message class: {}", pcepMsg.getClass());
- throw new IllegalArgumentException("Unknown instance of PCEPMessage. Passed " + pcepMsg.getClass());
+ logger.error("Unknown instance of PCEPMessage. Message class: {}", pcepMsg.getClass());
+ throw new IllegalArgumentException("Unknown instance of PCEPMessage. Passed " + pcepMsg.getClass());
}
logger.trace("Serializing {}", msgType);
final byte[] msgBody = MapOfParsers.getInstance().get(msgType).put(pcepMsg);
- final PCEPMessageHeader msgHeader = new PCEPMessageHeader(msgType.getIdentifier(), msgBody.length + PCEPMessageHeader.COMMON_HEADER_LENGTH,
- PCEPMessage.PCEP_VERSION);
+ final PCEPMessageHeader msgHeader = new PCEPMessageHeader(msgType.getIdentifier(), msgBody.length
+ + PCEPMessageHeader.COMMON_HEADER_LENGTH, PCEPMessage.PCEP_VERSION);
final byte[] headerBytes = msgHeader.toBytes();
final byte[] retBytes = new byte[headerBytes.length + msgBody.length];
ByteArray.copyWhole(msgBody, retBytes, PCEPMessageHeader.COMMON_HEADER_LENGTH);
return retBytes;
- }
+ }
}
-