*/
package org.opendaylight.protocol.pcep.impl.message;
-import org.opendaylight.protocol.pcep.PCEPMessage;
-import org.opendaylight.protocol.pcep.impl.PCEPMessageParser;
-import org.opendaylight.protocol.pcep.impl.PCEPObjectFactory;
-import org.opendaylight.protocol.pcep.message.PCEPOpenMessage;
+import io.netty.buffer.ByteBuf;
+
+import java.util.List;
+
+import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
/**
- * Parser for {@link org.opendaylight.protocol.pcep.message.PCEPOpenMessage PCEPOpenMessage}
+ * Parser for {@link OpenMessage}
*/
-public class PCEPOpenMessageParser implements PCEPMessageParser {
+public class PCEPOpenMessageParser extends AbstractMessageParser {
+
+ public static final int TYPE = 1;
+
+ public PCEPOpenMessageParser(final ObjectHandlerRegistry registry) {
+ super(registry);
+ }
+
+ @Override
+ public void serializeMessage(final Message message, final ByteBuf buffer) {
+ if (!(message instanceof OpenMessage)) {
+ throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Needed OpenMessage.");
+ }
+ final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
+
+ if (open.getOpen() == null) {
+ throw new IllegalArgumentException("Open Object must be present in Open Message.");
+ }
+
+ buffer.writeBytes(serializeObject(open.getOpen()));
+ }
+
+ @Override
+ protected org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
+ if (objects == null) {
+ throw new IllegalArgumentException("Passed list can't be null.");
+ }
+
+ if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
+ throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
+ }
+
+ final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
+ (Open) objects.get(0)).build();
+
+ objects.remove(0);
- @Override
- public byte[] put(PCEPMessage msg) {
- if (!(msg instanceof PCEPOpenMessage))
- throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance " + msg.getClass() + ". Nedded PCEPOpenMessage.");
+ if (!objects.isEmpty()) {
+ throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
+ }
- return PCEPObjectFactory.put(((PCEPOpenMessage) msg).getAllObjects());
- }
+ return new OpenBuilder().setOpenMessage(msg).build();
+ }
+ @Override
+ public int getMessageType() {
+ return TYPE;
+ }
}