Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPOpenMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.message;
9
10 import io.netty.buffer.ByteBuf;
11
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
22
23 /**
24  * Parser for {@link OpenMessage}
25  */
26 public class PCEPOpenMessageParser extends AbstractMessageParser {
27
28         public static final int TYPE = 1;
29
30         public PCEPOpenMessageParser(final ObjectHandlerRegistry registry) {
31                 super(registry);
32         }
33
34         @Override
35         public void serializeMessage(final Message message, final ByteBuf buffer) {
36                 if (!(message instanceof OpenMessage)) {
37                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Needed OpenMessage.");
38                 }
39                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
40
41                 if (open.getOpen() == null) {
42                         throw new IllegalArgumentException("Open Object must be present in Open Message.");
43                 }
44
45                 buffer.writeBytes(serializeObject(open.getOpen()));
46         }
47
48         @Override
49         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 {
50                 if (objects == null) {
51                         throw new IllegalArgumentException("Passed list can't be null.");
52                 }
53
54                 if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
55                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
56                 }
57
58                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
59                                 (Open) objects.get(0)).build();
60
61                 objects.remove(0);
62
63                 if (!objects.isEmpty()) {
64                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
65                 }
66
67                 return new OpenBuilder().setOpenMessage(msg).build();
68         }
69
70         @Override
71         public int getMessageType() {
72                 return TYPE;
73         }
74 }