675bc25bbc2da732659fbfb25a791f672c6d3f07
[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.ObjectHandlerRegistry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
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.object.Open;
23
24 /**
25  * Parser for {@link OpenMessage}
26  */
27 public class PCEPOpenMessageParser extends AbstractMessageParser {
28
29         public static final int TYPE = 1;
30
31         public PCEPOpenMessageParser(final ObjectHandlerRegistry 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                 }
40                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
41
42                 if (open.getOpen() == null) {
43                         throw new IllegalArgumentException("Open Object must be present in Open Message.");
44                 }
45
46                 buffer.writeBytes(serializeObject(open.getOpen()));
47         }
48
49         @Override
50         public OpenMessage parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
51                 if (buffer == null || buffer.length == 0) {
52                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
53                 }
54                 final List<Object> objs = parseObjects(buffer);
55
56                 return new OpenBuilder().setOpenMessage(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
65                 if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
66                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
67                 }
68
69                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
70                                 (Open) objects.get(0)).build();
71
72                 objects.remove(0);
73
74                 if (!objects.isEmpty()) {
75                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
76                 }
77
78                 return msg;
79         }
80
81         @Override
82         public int getMessageType() {
83                 return TYPE;
84         }
85 }