Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPCloseMessageParser.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.Close;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.CCloseMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.CCloseMessageBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CClose;
24
25 /**
26  * Parser for {@link CloseMessage}
27  */
28 public class PCEPCloseMessageParser extends AbstractMessageParser {
29
30         public static final int TYPE = 7;
31
32         public PCEPCloseMessageParser(final ObjectHandlerRegistry registry) {
33                 super(registry);
34         }
35
36         @Override
37         public void serializeMessage(final Message message, final ByteBuf buffer) {
38                 if (!(message instanceof CloseMessage)) {
39                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
40                                         + ". Nedded CloseMessage.");
41                 }
42                 final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
43
44                 if (close.getCClose() == null) {
45                         throw new IllegalArgumentException("Close Object must be present in Close Message.");
46                 }
47                 buffer.writeBytes(serializeObject(close.getCClose()));
48         }
49
50         @Override
51         protected Close validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
52                 if (objects == null) {
53                         throw new IllegalArgumentException("Passed list can't be null.");
54                 }
55                 if (objects.isEmpty() || !(objects.get(0) instanceof CClose)) {
56                         throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
57                 }
58                 final Object o = objects.get(0);
59                 final CCloseMessage msg = new CCloseMessageBuilder().setCClose((CClose) o).build();
60                 objects.remove(0);
61                 if (!objects.isEmpty()) {
62                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
63                 }
64                 return new CloseBuilder().setCCloseMessage(msg).build();
65         }
66
67         @Override
68         public int getMessageType() {
69                 return TYPE;
70         }
71 }