Merge changes Ic58ee772,Id447e440
[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.spi.AbstractMessageParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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() + ". Needed 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         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 {
51                 if (objects == null) {
52                         throw new IllegalArgumentException("Passed list can't be null.");
53                 }
54
55                 if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
56                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
57                 }
58
59                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
60                                 (Open) objects.get(0)).build();
61
62                 objects.remove(0);
63
64                 if (!objects.isEmpty()) {
65                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
66                 }
67
68                 return new OpenBuilder().setOpenMessage(msg).build();
69         }
70
71         @Override
72         public int getMessageType() {
73                 return TYPE;
74         }
75 }