BUG-47 : PCEP migration to generated DTOs.
[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.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
17 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.open.message.Open;
23
24 /**
25  * Parser for {@link OpenMessage}
26  */
27 public class PCEPOpenMessageParser extends AbstractMessageParser {
28
29         private final int TYPE = 1;
30
31         public PCEPOpenMessageParser(final HandlerRegistry registry) {
32                 super(registry);
33         }
34
35         @Override
36         public void serializeMessage(final Message message, final ByteBuf buffer) {
37                 if (!(message instanceof OpenMessage))
38                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Nedded OpenMessage.");
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         public OpenMessage parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
50                 if (buffer == null || buffer.length == 0) {
51                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
52                 }
53                 final List<Object> objs = parseObjects(buffer);
54
55                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder().setOpenMessage(
56                                 validate(objs)).build();
57         }
58
59         private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage validate(
60                         final List<Object> objects) throws PCEPDeserializerException {
61                 if (objects == null)
62                         throw new IllegalArgumentException("Passed list can't be null.");
63
64                 if (objects.isEmpty() || !(objects.get(0) instanceof Open))
65                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
66
67                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
68                                 (Open) objects.get(0)).build();
69
70                 objects.remove(0);
71
72                 if (!objects.isEmpty())
73                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
74
75                 return msg;
76         }
77
78         @Override
79         public int getMessageType() {
80                 return this.TYPE;
81         }
82 }